forked from readthedocs/readthedocs.org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
101 lines (88 loc) · 3.24 KB
/
tasks.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
# -*- coding: utf-8 -*-
"""Read the Docs invoke tasks."""
from __future__ import division, print_function, unicode_literals
import os
import textwrap
from dateutil.parser import parse
from future.moves.configparser import RawConfigParser
from invoke import task, Exit
@task
def prepare(ctx, version):
"""
Prepare the next release version by updating files.
This will stage a few updates for manual review and commit:
* Prepend the most recent PRs and issues that were closed to CHANGELOG.rst.
* Update the setup.cfg version
Changelog uses the file modification date to track the last time it was
updated. New entries will end up at the top of the file, under a heading
for the new version.
"""
# Ensure we're on the master branch first
git_rev_parse = ctx.run('git rev-parse --abbrev-ref HEAD', hide=True)
current_branch = git_rev_parse.stdout.strip()
if current_branch != 'master':
print('You must be on master branch!')
raise Exit(1)
print('Updating release version in setup.cfg')
setupcfg_path = os.path.join(os.path.dirname(__file__), 'setup.cfg')
config = RawConfigParser()
config.read(setupcfg_path)
config.set('metadata', 'version', version)
with open(setupcfg_path, 'wb') as configfile:
config.write(configfile)
print('Installing github-changelog')
# Get last modified date from Git instead of assuming the file metadata is
# correct. This can change depending on git reset, etc.
git_log = ctx.run('git log -1 --format="%ad" -- CHANGELOG.rst')
last_modified = parse(git_log.stdout.strip()).strftime('%Y-%m-%d')
# Install and run
ctx.run('npm install git+https://github.com/agjohnson/github-changelog.git')
changelog_path = os.path.join(os.path.dirname(__file__), 'CHANGELOG.rst')
template_path = os.path.join(
os.path.dirname(__file__),
'contrib',
'changelog.hbs',
)
bin_path = os.path.join(
os.path.dirname(__file__),
'node_modules',
'.bin',
)
cmd = (
'{bin_path}/gh-changelog '
'-o rtfd -r readthedocs.org '
'--file {changelog_path} '
'--since {last_modified} '
'--template {template_path} '
'--header "Version {version}"'
).format(
bin_path=bin_path,
version=version,
template_path=template_path,
changelog_path=changelog_path,
last_modified=last_modified,
) # yapf: disable
try:
token = os.environ['GITHUB_TOKEN']
cmd += '--token ' + token + ' '
except KeyError:
print('')
print(
'\n'.join(
textwrap.wrap(
'In order to avoid rate limiting on the GitHub API, you can specify '
'an environment variable `GITHUB_TOKEN` with a personal access token. '
'There is no need for the token to have any permissions unless the '
'repoistory is private.')))
print('')
print('Updating changelog')
ctx.run(cmd)
@task
def release(ctx, version):
"""
Tag release of Read the Docs.
Do this after prepare task and manual cleanup/commit
"""
ctx.run(
('git tag {version} && '
'git push --tags').format(version=version))