aboutsummaryrefslogtreecommitdiff
blob: 6c85aacfdd8e902e2809b2fc7abca64341cebf91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# Copyright 2022 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

import re

from twisted.internet import defer

from buildbot.process.buildstep import BuildStep
from buildbot.process.results import SUCCESS
from buildbot.process.results import FAILURE
from buildbot.process.results import SKIPPED

from bugz.cli import check_bugz_token, login, list_bugs
from bugz.cli_argparser import make_arg_parser
from bugz.configfile import load_config
from bugz.settings import Settings
from bugz.exceptions import BugzError
from bugz.log import log_error, log_info

from portage.versions import cpv_getversion, pkgsplit, catpkgsplit

# Origins: bugz.cli
# Modifyed by Gentoo Authors.
# main
def main_bugz(args):
    ArgParser = make_arg_parser()
    opt = ArgParser.parse_args(args)

    ConfigParser = load_config(getattr(opt, 'config_file', None))

    check_bugz_token()
    settings = Settings(opt, ConfigParser)
    return settings

# search
def search_bugz(args):
    settings = main_bugz(args)
    valid_keys = ['alias', 'assigned_to', 'component', 'creator',
                'limit', 'offset', 'op_sys', 'platform',
                'priority', 'product', 'resolution', 'severity',
                'version', 'whiteboard', 'cc']

    params = {}
    d = vars(settings)
    for key in d:
        if key in valid_keys:
            params[key] = d[key]
    if 'search_statuses' in d:
        if 'all' not in d['search_statuses']:
            params['status'] = d['search_statuses']
    if 'terms' in d:
        params['summary'] = d['terms']

    if not params:
        raise BugzError('Please give search terms or options.')

    log_info('Searching for bugs meeting the following criteria:')
    for key in params:
        log_info('   {0:<20} = {1}'.format(key, params[key]))

    login(settings)

    result = settings.call_bz(settings.bz.Bug.search, params)['bugs']

    if not len(result):
        log_info('No bugs found.')
        return []
    else:
        list_bugs(result, settings)
        return result

class GetBugs(BuildStep):
    
    name = 'GetBugs'
    description = 'Running'
    descriptionDone = 'Ran'
    descriptionSuffix = None
    haltOnFailure = True
    flunkOnFailure = True

    def __init__(self, **kwargs):
        super().__init__(**kwargs)

    @defer.inlineCallbacks
    def find_match(self, buglist):
        log = yield self.addLog('Bugs')
        yield log.addStdout('Open Bugs\n')
        match = False
        for bug in buglist:
            yield log.addStdout(f"Bug: {str(bug['id'])} Summary: {bug['summary']}\n")
            # we splite the lines to lists and try to match the words
            matches = 0
            match_search_text = list(self.getProperty('error_dict')['title_issue'].split())
            match_bug_text = list(bug['summary'].split())
            #FIXME: add check for cp
            for match_word in match_search_text:
                if match_word in match_bug_text:
                    matches = matches + 1
            print(f"Bug: {str(bug['id'])} Matched words: {str(matches)} Summary: {bug['summary']}")
            if matches >= 5:
                match = {}
                match['match'] = True
                match['id'] = bug['id']
                match['summary'] = bug['summary']
        yield log.addStdout(f"Line to match: {self.getProperty('error_dict')['title_issue']}\n")
        if match:
            yield log.addStdout('Match bug: YES\n')
            yield log.addStdout(f"Bug: {str(match['id'])} Summary: {match['summary']}\n")
            self.setProperty("bgo", match, 'bgo')
            return
        yield log.addStdout('Match bug: NO\n')

    @defer.inlineCallbacks
    def run(self):
        # self.gentooci = self.master.namedServices['services'].namedServices['gentooci']
        cpv = self.getProperty('error_dict')['cpv']
        c = yield catpkgsplit(cpv)[0]
        p = yield catpkgsplit(cpv)[1]
        cp = c + '/' + p
        # search for open bugs
        args = []
        args.append('--skip-auth')
        args.append('search')
        # set limit
        # set date last 30 days
        # search for cp
        args.append(cp)
        print(args)
        buglist = search_bugz(args)
        print(buglist)
        self.find_match(buglist)
        return SUCCESS