forked from nerdvegas/gitch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgitch.py
372 lines (291 loc) · 10.2 KB
/
gitch.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
#!/usr/bin/env python
from __future__ import print_function
import argparse
import logging
import os
import re
import sys
import subprocess
import requests
logger = logging.getLogger("gitch")
debugging = (os.getenv("GITCH_DEBUG", "").lower() in ("1", "t", "true"))
class GitchError(Exception):
pass
class SectionNotExistsError(GitchError):
"""A changelog section does not exist."""
pass
class TagNotExistsError(GitchError):
"""A tag does not exist at the remote."""
pass
class ReleaseExistsError(GitchError):
"""A Github release already exists."""
pass
class ChangelogSyncer(object):
"""Sync changelog to github release notes.
https://developer.github.com/v3/repos/releases/
"""
def __init__(self, token, path=None, overwrite=False, dry_run=False):
"""Create a syncer.
Args:
token (str): Github personal access token.
path (str): Path on disk within target git repo.
overwrite (bool): If True, overwrite existing gh releases.
dry_run (bool): Dry mode (no writes to github) if True.
"""
self.path = path or os.getcwd()
self.token = token
self.overwrite = overwrite
self.dry_run = dry_run
self.changelog_filepath = None
self.github_user = None
self.github_repo_name = None
self.changelog_sections = None
self.gh_releases = None
self.branch = None
# find root dir of local git repo checkout
try:
out = self._git("rev-parse", "--show-toplevel")
root_path = out.strip()
except subprocess.CalledProcessError:
raise GitchError("Not in a git repository")
# check that there is a remote
try:
out = self._git("remote", "get-url", "origin")
remote_url = out.strip()
except subprocess.CalledProcessError:
raise GitchError("There is no git remote")
# find changelog
self.changelog_filepath = os.path.join(root_path, "CHANGELOG.md")
if not os.path.isfile(self.changelog_filepath):
raise GitchError("Expected changelog at %s", self.changelog_filepath)
# get checked out branch
try:
out = self._git("rev-parse", "--abbrev-ref", "HEAD")
branch = out.strip()
if branch != "HEAD": # detached HEAD mode
self.branch = branch
except:
pass
# Check remote is a github repo, and parse out user and repo name.
# Expecting remote_url like 'git@github.com:nerdvegas/gitch.git'
#
regex = re.compile("^git@(.*):(.*)/(.*)\\.git$")
m = regex.match(remote_url)
if not m or m.groups()[0] != "github.com":
raise GitchError("Not a github repository")
self.github_user = m.groups()[1]
self.github_repo_name = m.groups()[2]
@property
def github_url(self):
return (
"https://api.github.com/repos/%s/%s"
% (self.github_user, self.github_repo_name)
)
def get_changelog_tags(self):
"""Get tags in changelog, in order they appear.
"""
sections = self._get_changelog_sections()
return [x["tag"] for x in sections]
def sync(self, tag):
"""Sync a changelog section to github release notes.
Args:
tag (str): changelog section to sync.
"""
section = None
# get changelog section for this tag
sections = self._get_changelog_sections()
for sec in sections:
if sec["tag"] == tag:
section = sec
break
if not section:
raise SectionNotExistsError(
"No such tag %r in %s"
% (tag, self.changelog_filepath)
)
# Ensure tag exists at remote. You can create a release for a tag that
# doesn't exist, and github will create the tag for you. However it's
# too easy to bugger things up by creating a tag unintentionally (due to
# a typo in your changelog), and we don't want that.
#
out = self._git("ls-remote", "origin", "refs/tags/" + tag)
if not out.strip():
raise TagNotExistsError(
"Tag %r does not exist at the remote" % tag
)
# determine if release already exists
existing_release = None
for release in self._get_gh_releases():
if release["tag_name"] == tag:
existing_release = release
# avoid overwrite if gh release already exists
if not self.overwrite and existing_release:
raise ReleaseExistsError(
"Github release %r already exists" % tag
)
# create the gh release
if self.dry_run:
return "https://github.com/jbloggs/i-dont-exist/releases/v1.0.0"
data = {
"tag_name": tag,
"name": section["header"],
"body": section["content"],
"target_commitish": self.branch
}
if existing_release:
endpoint = "releases/" + str(existing_release["id"])
resp = self._send_github_request("PATCH", endpoint, json=data)
else:
resp = self._send_github_request("POST", "releases", json=data)
resp.raise_for_status()
return resp.json()["html_url"]
def _git(self, *nargs):
return subprocess.check_output(
["git"] + list(nargs),
stderr=subprocess.PIPE,
cwd=self.path
)
def _get_changelog_sections(self):
"""
Note that this does very dumb parsing. There don't seem to be good
solutions to getting AST from markdown in python out there, but we don't
really need that anyway.
"""
if self.changelog_sections is not None:
return self.changelog_sections
with open(self.changelog_filepath) as f:
lines = f.read().split('\n')
sections = []
curr_tag = None
curr_header = None
curr_lines = []
def consume_section():
if curr_tag:
sections.append({
"tag": curr_tag,
"header": curr_header,
"content": '\n'.join(curr_lines).rstrip()
})
for line in lines:
parts = line.split()
if len(parts) > 1 and parts[0] == "##": # H2
consume_section()
curr_tag = parts[1]
curr_header = ' '.join(parts[1:])
curr_lines = []
elif curr_tag:
curr_lines.append(line)
consume_section()
self.changelog_sections = sections
return self.changelog_sections
def _get_gh_releases(self):
if self.gh_releases is not None:
return self.gh_releases
resp = self._send_github_request("GET", "releases")
resp.raise_for_status()
self.gh_releases = resp.json()
return self.gh_releases
def _send_github_request(self, method, endpoint, **kwargs):
url = self.github_url + '/' + endpoint
headers = {
"Content-Type": "application/json",
"Authorization": "token " + self.token
}
return requests.request(method, url, headers=headers, **kwargs)
def parse_args():
parser = argparse.ArgumentParser(
"Sync github release notes with your project's changelog"
)
parser.add_argument(
"-a", "--all", action="store_true",
help="Sync all tags; TAG is ignored"
)
parser.add_argument(
"-o", "--overwrite", action="store_true",
help="Overwrite github release if it exists"
)
parser.add_argument(
"-l", "--list", action="store_true",
help="List tags present in changelog, and exit"
)
parser.add_argument(
"--dry-run", action="store_true",
help="Dry run mode"
)
parser.add_argument(
"TAG", nargs='?',
help="Tag to sync github release to. If not provided, the latest tag "
"is used"
)
opts = parser.parse_args()
if opts.TAG and opts.all:
parser.error("Do not provide TAG with --all option")
return opts
def error(msg, *nargs):
print(msg % nargs, file=sys.stderr)
sys.exit(1)
def init_logging():
global logger
formatter = logging.Formatter("%(name)s %(levelname)s %(message)s")
handler = logging.StreamHandler()
handler.setFormatter(formatter)
if debugging:
# enable debug-level logging in all packages
root_logger = logging.getLogger()
root_logger.addHandler(handler)
root_logger.setLevel(logging.DEBUG)
else:
logger.addHandler(handler)
logger.setLevel(logging.INFO)
def list_changelog_tags(syncer):
tags = syncer.get_changelog_tags()
if tags:
for tag in tags:
print(tag)
else:
error("No tags in changelog")
def sync_to_github(opts, syncer):
tags_to_sync = []
changelog_tags = syncer.get_changelog_tags()
# get list of changelog entries to push to github
if opts.TAG:
tags_to_sync = [opts.TAG]
elif opts.all:
tags_to_sync = changelog_tags
elif changelog_tags: # latest tag
tags_to_sync = [changelog_tags[0]]
if not tags_to_sync:
error("No changelog entries")
num_synced = 0
for tag in tags_to_sync:
logger.info("Syncing %r to github...", tag)
try:
url = syncer.sync(tag)
logger.info("%r synced, see %s", tag, url)
num_synced += 1
except GitchError as e:
logger.warning(str(e))
print("\n%d changelog entries pushed to github" % num_synced)
def _main():
init_logging()
opts = parse_args()
token = os.getenv("GITCH_GITHUB_TOKEN")
if not token:
error("Expected $GITCH_GITHUB_TOKEN")
syncer = ChangelogSyncer(
token=token,
overwrite=opts.overwrite,
dry_run=opts.dry_run
)
if opts.list:
list_changelog_tags(syncer)
else:
sync_to_github(opts, syncer)
if __name__ == "__main__":
try:
_main()
except GitchError as e:
if debugging:
raise
else:
error(str(e))