-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_youtrack_hook.py
280 lines (207 loc) · 10.3 KB
/
github_youtrack_hook.py
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
# -*- coding: utf-8 -*-
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import re
import time
import logging
import traceback
from flask import Flask
from github_webhook import Webhook
import common
import config
from common import *
from gitobjects import *
from youtrack.connection import Connection
from logging.handlers import TimedRotatingFileHandler
logger = logging.getLogger(__name__)
logger.setLevel(logging._levelNames[config.LOG_LEVEL])
logfile_handler = TimedRotatingFileHandler(config.LOG_FILE, config.LOG_ROTATE_PERIOD, config.LOG_ROTATE_INTERVAL, config.LOG_ROTATE_BACKUP_COUNT)
logfile_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
logger.addHandler(logfile_handler)
console_handler = logging.StreamHandler(sys.stdout)
console_handler.setFormatter(logging.Formatter('%(asctime)s %(levelname)s %(message)s'))
logger.addHandler(console_handler)
if (config.WEBHOOK_SECRET == '<REQUIRED>') or \
(config.LDAP_EMAIL_DOMAIN == '<REQUIRED>') or \
(config.DEFAULT_YOUTRACK_URL == '<REQUIRED>') or \
(config.DEFAULT_YOUTRACK_USER == '<REQUIRED>') or \
(config.DEFAULT_YOUTRACK_PASS == '<REQUIRED>'):
sys.exit(logger.error("Your config.py looks unmodified! Fill the actual data to the file and retry. Exit."))
sys.path.append(os.path.join(os.path.dirname(os.path.abspath(__file__)), 'api'))
app = Flask(__name__) # Standard Flask app
web_hook = Webhook(app, config.WEBHOOK_ENDPOINT, config.WEBHOOK_SECRET)
logger.info("Program started.")
def process_push_event(data, conn):
branch = data["ref"].replace('refs/heads/', '')
is_release_branch = False
for regex in config.DEFAULT_GIT_RELEASE_BRANCH_MASKS:
pattern = re.compile(regex)
if pattern.match(branch):
is_release_branch = True
break
if is_release_branch == False:
logger.info('Found commit to non-release branch "%s" of "%s" by user %s. Ignoring push event.' % (branch, data["repository"]["full_name"], data["pusher"]["email"]))
return
git_repo_full_name = data["repository"]["full_name"]
commits = data["commits"]
logger.debug('All commits: "%s"' % commits)
issue_map = {}
for commit in commits:
issues = regexp_list_filter([commit["message"]], config.DEFAULT_GIT_ISSUE_NUMBER_MASKS)
for issue in issues:
if issue not in issue_map:
issue_map[issue] = []
author_issues_list = issue_map[issue]
author_issues_list.append(CommitInfo(commit["author"]["email"], commit["id"], branch, commit["message"],
issue))
for issue in issue_map:
project_id = issue[0:issue.rfind('-')]
if project_id not in config.YOUTRACK_PROJECTS:
logger.warning(
'Project from committer\'s comment not allowed: "%s". Skipping issue, mentioned in revisions %s.' % (
project_id, issue_map[issue]))
continue
if git_repo_full_name not in config.YOUTRACK_PROJECTS[project_id]["GITHUB_PROJECTS"]:
logger.warning(
'Full name not allowed: "%s". Skipping issue, mentioned in revisions %s.' % (
project_id, issue_map[issue]))
continue
if "COMMITTED_TO" in config.YOUTRACK_PROJECTS[project_id]:
if not config.YOUTRACK_PROJECTS[project_id]["COMMITTED_TO"]:
committed_branches_field = None
else:
committed_branches_field = config.YOUTRACK_PROJECTS[project_id]["COMMITTED_TO"]
else:
committed_branches_field = None
if "FIX_VERSIONS" in config.YOUTRACK_PROJECTS[project_id]:
if not config.YOUTRACK_PROJECTS[project_id]["FIX_VERSIONS"]:
fix_versions_field = None
else:
fix_versions_field = config.YOUTRACK_PROJECTS[project_id]["FIX_VERSIONS"]
else:
fix_versions_field = None
issue_commits = issue_map[issue]
commit_map = {}
for commit_info in issue_commits:
author = commit_info.author
if author not in commit_map:
commit_map[author] = []
author_commits_list = commit_map[author]
author_commits_list.append(commit_info)
try:
for author in commit_map:
logger.info('Found commits by "%s" for issue "%s" :\n %s' % (author, issue, commit_map[author]))
post_push_comment(issue, author, commit_map[author], conn, git_repo_full_name)
post_push_info(issue, project_id, author, commit_map[author], conn, committed_branches_field,
fix_versions_field, config.DEFAULT_FIX_VERSIONS_REGEX)
except Exception, ex:
if '404: Not Found: Issue not found' in ex.message:
commits_sha1_short = []
for commit_info in issue_commits:
commit_sha1 = commit_info.revision[0:8]
commits_sha1_short.append(commit_sha1)
logger.warning('Issue Not Found: %s, mentioned in commit %s. Skipping issue.' % (
issue, ', '.join(commits_sha1_short)))
else:
raise
def post_push_comment(issue, author_login, author_commits_list, conn, git_repo_fullname):
if author_login.find(config.LDAP_EMAIL_DOMAIN) >= 0:
author_nick = author_login.replace(config.LDAP_EMAIL_DOMAIN, '')
issue_comment = "Git changesets by +%s+ in *%s*:\n" % (author_nick, git_repo_fullname)
else:
author_nick = author_login.split('@', 1)[0]
issue_comment = "Git changesets by external contributor +%s+ in *%s*:\n" % (author_nick, git_repo_fullname)
for commit in author_commits_list:
changeset_link = config.DEFAULT_GITHUB_URL % (git_repo_fullname, commit.revision)
issue_comment += "{monospace}[%s %s]{monospace} in *%s*: {noformat}%s{noformat}\n" % (
changeset_link,
commit.revision[0:8],
commit.branch,
commit.comment
)
# prevent duplicate comments
comments = common.yt_get_issue_comments(conn, issue, logger)
comment_found = False
for comment in comments:
if comment['text'] == issue_comment:
comment_found = True
logger.info('Duplicated comment found for issue %s. Skipping comment.' % issue)
break
if not comment_found:
run_as = author_login
if not config.LDAP_LOGIN_INCLUDES_EMAIL:
if author_login.find(config.LDAP_EMAIL_DOMAIN) >= 0:
run_as = author_login.replace(config.LDAP_EMAIL_DOMAIN, '')
if (author_login.find('@') >= 0) and (author_login.find(config.LDAP_EMAIL_DOMAIN) < 0):
run_as = None
common.yt_add_simple_comment_to_issue(conn, issue, issue_comment, run_as, logger)
def post_push_info(issue, project_id, author_login, author_commits_list, conn, committed_branches_field,
fix_versions_field, fix_versions_regex):
run_as = author_login
if not config.LDAP_LOGIN_INCLUDES_EMAIL:
if author_login.find(config.LDAP_EMAIL_DOMAIN) >= 0:
run_as = author_login.replace(config.LDAP_EMAIL_DOMAIN, '')
if (author_login.find('@') >= 0) and (author_login.find(config.LDAP_EMAIL_DOMAIN) < 0):
run_as = None
branches = set()
for commit in author_commits_list:
branches.add(commit.branch)
for branch in branches:
if committed_branches_field:
if common.yt_get_project_has_custom_field(conn, project_id, committed_branches_field):
logger.info('Adding "%s" for issue: "%s", value: "%s"' % (
committed_branches_field,
issue,
branch))
committed_field_bundle = common.yt_get_field_bundle(conn, project_id, committed_branches_field)
common.yt_add_field_value(
conn,
issue,
committed_branches_field,
committed_field_bundle,
branch,
run_as)
else:
logger.warning(
'not found in YouTrack! Failed to set "%s" for issue: "%s", value: "%s"' % (
committed_branches_field,
issue,
branch))
if fix_versions_field:
if common.yt_get_project_has_custom_field(conn, project_id, fix_versions_field):
logger.info('Adding "%s" for issue: "%s"' % (fix_versions_field, issue))
minor_version = common.find_minor_version(branch, fix_versions_regex)
logger.debug('Minor version: %s' % minor_version)
if minor_version or branch == 'master':
bundle_vals = common.yt_get_versions(conn, project_id)
latest_version = common.find_latest_version(bundle_vals, minor_version)
logger.debug('Latest version: %s' % latest_version)
if latest_version:
logger.info('Add fix version [Issue: "%s" Version: "%s"]' % (issue, latest_version))
common.yt_add_value_to_issue_field(conn, issue, fix_versions_field, latest_version, run_as)
else:
logger.warning(
'Version not found in YouTrack! Failed to set "%s" for issue: "%s", value: "%s"' % (
fix_versions_field,
issue,
branch))
@web_hook.hook()
def on_push(data):
"""
Defines a handler for the 'push' event
"""
logger.info("Received push event, before:" + data["before"][0:8] + ", after:" + data["after"][0:8])
logger.debug("Got push event with: {0}".format(data))
try:
conn = Connection(config.DEFAULT_YOUTRACK_URL, config.DEFAULT_YOUTRACK_USER, config.DEFAULT_YOUTRACK_PASS)
except Exception:
logger.error("Unable to connect to YouTrack!")
raise
try:
process_push_event(data, conn)
except Exception:
logger.exception("Exception during push event processing", exc_info=True)
raise
if __name__ == "__main__":
app.run(host=config.IP_ADDRESS, port=config.PORT)