Merge lp:~3v1n0/ubuntu-bots/github-support into lp:ubuntu-bots

Proposed by Marco Trevisan (Treviño)
Status: Needs review
Proposed branch: lp:~3v1n0/ubuntu-bots/github-support
Merge into: lp:ubuntu-bots
Diff against target: 168 lines (+90/-4)
3 files modified
Bugtracker/README.txt (+2/-0)
Bugtracker/plugin.py (+87/-4)
README.txt (+1/-0)
To merge this branch: bzr merge lp:~3v1n0/ubuntu-bots/github-support
Reviewer Review Type Date Requested Status
Ubuntu IRC Bots Pending
Review via email: mp+342155@code.launchpad.net

Commit message

Bugtracker: Add github support

To post a comment you must log in.
lp:~3v1n0/ubuntu-bots/github-support updated
322. By Marco Trevisan (Treviño)

Bugtracker: add support for trackers with multiple sub-trackers

Properly log pull requests in Github

323. By Marco Trevisan (Treviño)

Bugtracker: add ext info for Github

Unmerged revisions

323. By Marco Trevisan (Treviño)

Bugtracker: add ext info for Github

322. By Marco Trevisan (Treviño)

Bugtracker: add support for trackers with multiple sub-trackers

Properly log pull requests in Github

321. By Marco Trevisan (Treviño)

Bugtracker: Add github support

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'Bugtracker/README.txt'
2--- Bugtracker/README.txt 2011-05-28 06:33:21 +0000
3+++ Bugtracker/README.txt 2018-03-26 23:08:26 +0000
4@@ -7,6 +7,7 @@
5 @bugtracker add openoffice issuezilla http://www.openoffice.org/issues OpenOffice
6 @bugtracker add django trac http://code.djangoproject.com/ticket Django
7 @bugtracker add gaim sourceforge http://sourceforge.net/tracker/?group_id=235&atid=100235 Gaim
8+@bugtracker add snapcore github https://github.com/snapcore Snapcraft
9
10 In general: @bugtracker add <name> <type> <baseurl> [description]
11 Bugtracker dialects (types) this plugin understands:
12@@ -18,6 +19,7 @@
13 * WikiForms (see bugs.gnewsense.org for an example)
14 * str.php from the CUPS project
15 * Mantis (http://www.mantisbt.org)
16+* Github
17
18 A notable exception is Sourceforge. Unfortunatly, it has no API or data export
19 feature to output bug information in a well-formed way.
20
21=== modified file 'Bugtracker/plugin.py'
22--- Bugtracker/plugin.py 2014-08-12 16:47:38 +0000
23+++ Bugtracker/plugin.py 2018-03-26 23:08:26 +0000
24@@ -419,7 +419,7 @@
25 irc.reply(makeClean(r), prefixNick=False)
26
27 def turlSnarfer(self, irc, msg, match):
28- r"(?P<tracker>https?://\S*?)/(?:Bugs/0*|str.php\?L|show_bug.cgi\?id=|bugreport.cgi\?bug=|(?:bugs|\+bug)/|ticket/|tracker/|\S*aid=|bug=)?(?P<bug>\d+)(?P<sfurl>&group_id=\d+&at_id=\d+)?"
29+ r"(?P<tracker>https?://\S*?)/(?:Bugs/0*|str.php\?L|show_bug.cgi\?id=|bugreport.cgi\?bug=|(?:bugs|\+bug)/|ticket/|tracker/|\S*aid=|bug=|issues/|pull/)?(?P<bug>\d+)(?P<sfurl>&group_id=\d+&at_id=\d+)?"
30 channel = ircutils.isChannel(msg.args[0]) and msg.args[0] or None
31 if not self.registryValue('bugSnarfer', channel):
32 return
33@@ -515,6 +515,8 @@
34 if '/' in url:
35 url = url[:url.index('/')]
36 if url in snarfhost:
37+ if tracker.has_multiple_trackers():
38+ return tracker.get_tracker(snarfurl)
39 return tracker
40
41 if snarfhost == 'pad.lv': # Launchpad URL shortening
42@@ -541,7 +543,7 @@
43 else:
44 (bid, product, title, severity, status, assignee, url) = r
45
46- severity = severity[0].upper() + severity[1:].lower()
47+ severity = severity[0].upper() + severity[1:].lower() if severity else ''
48 status = status[0].upper() + status[1:].lower()
49 tracker_name = tracker.description + ' '
50 if not do_url:
51@@ -575,8 +577,12 @@
52 def get_bug(self, id):
53 raise BugTrackerError("Bugtracker class does not implement get_bug")
54
55+ def has_multiple_trackers(self):
56+ return False
57+
58 def get_tracker(self, url):
59- raise BugTrackerError("Bugtracker class does not implement get_tracker")
60+ if self.has_multiple_trackers():
61+ raise BugTrackerError("Bugtracker class does not implement get_tracker")
62
63 def __str__(self):
64 return '%s(%s)' % (self.__class__.__name__, self.url)
65@@ -1057,6 +1063,82 @@
66 except:
67 raise BugNotFoundError
68
69+
70+class Github(IBugtracker):
71+ _gh = None
72+
73+ def __init__(self, *args, **kwargs):
74+ IBugtracker.__init__(self, *args, **kwargs)
75+
76+ if not Github._gh:
77+ try: # We need python-github
78+ from github import Github as GH
79+ Github._gh = GH(user_agent='PyGithub/ubuntu-bot')
80+ except ImportError:
81+ self.log.error("Please install python-github")
82+
83+ self.gh = Github._gh
84+ self.repo = None
85+
86+ def has_multiple_trackers(self):
87+ return True
88+
89+ def get_tracker(self, url):
90+ if not self.gh:
91+ return
92+ if not url:
93+ self.log.info("Impossible to get tracker for an empty url")
94+ return
95+
96+ try:
97+ url = url.replace('http://', 'https://', 1)
98+ if url.startswith(self.url):
99+ projectmatch = re.search("([^/]+/[^/]+)/(issues|pull)/\d+", url)
100+
101+ if projectmatch:
102+ repo_name = projectmatch.group(1)
103+ repo = self.gh.get_repo(repo_name)
104+ if not repo.has_issues:
105+ s = u'Repository {} has no issues'.format(repo_name)
106+ self.log.exception(s)
107+
108+ gh = Github(repo.full_name.replace('/', '-'), repo.html_url, repo.owner.login)
109+ gh.repo = repo
110+ return gh
111+
112+ except Exception as e:
113+ s = u'{}: Impossible to get tracker for {}: {}'.format(
114+ self.description, url, e)
115+ self.log.exception(s)
116+
117+ def get_bug(self, id):
118+ if not self.gh or not self.repo:
119+ self.log.info("Github or repository not initialized")
120+ return []
121+
122+ try:
123+ issue = self.repo.get_issue(id)
124+
125+ assignee = None
126+ labels = u', '.join([l.name for l in issue.labels])
127+ is_pull_request = issue.pull_request != None
128+
129+ if issue.assignee:
130+ assignee = u'{} ({})'.format(issue.assignee.login, issue.assignee.name)
131+ if is_pull_request:
132+ id = u'(Pull request) {}'.format(id)
133+
134+ # extinfo = "(comments: {}, reactions: {})" % (issue.comments, len(issue.get_reactions()))
135+ extinfo = "(comments: {})".format(issue.comments)
136+
137+ return [(id, self.repo.name, issue.title, labels, issue.state, assignee, issue.html_url, extinfo)]
138+ except Exception as e:
139+ s = u'{}: Impossible to get infos for {} issue {}: {}'.format(
140+ self.description, self.repo.full_name, id, e)
141+ self.log.exception(s)
142+ raise BugtrackerError, s
143+
144+
145 # Introspection is quite cool
146 defined_bugtrackers = {}
147 v = vars()
148@@ -1084,6 +1166,7 @@
149 registerBugtracker('supybot', 'http://sourceforge.net/tracker/?group_id=58965&atid=489447', 'Supybot', 'sourceforge')
150 registerBugtracker('mantis', "http://www.mantisbt.org/bugs", "Mantis", 'mantis')
151 registerBugtracker('ubottu', 'https://launchpad.net', 'Ubottu', 'launchpad')
152+registerBugtracker('sourceforge', 'http://sourceforge.net/tracker/', 'Sourceforge', 'sourceforge')
153+registerBugtracker('github', 'https://github.com', 'Github', 'github')
154 # Don't delete this one
155-registerBugtracker('sourceforge', 'http://sourceforge.net/tracker/', 'Sourceforge', 'sourceforge')
156 Class = Bugtracker
157
158=== modified file 'README.txt'
159--- README.txt 2011-05-28 06:33:21 +0000
160+++ README.txt 2018-03-26 23:08:26 +0000
161@@ -18,6 +18,7 @@
162
163 (Optional)
164 Launchpadlib python-launchpadlib https://launchpad.net/launchpadlib
165+PyGithub python-github https://github.com/PyGithub/PyGithub
166 apt-file apt-file N/A Debian (and derivatives) only
167
168 Launchpadlib will become a required module for Bugtracker (and possibly others)

Subscribers

People subscribed via source and target branches