1
+ # mypy:disallow-untyped-defs
1
2
"""
2
3
Script used to publish GitHub release notes extracted from CHANGELOG.rst.
3
4
19
20
20
21
Requires Python3.6+.
21
22
"""
22
- import os
23
23
import re
24
24
import sys
25
25
from pathlib import Path
26
26
27
- import github3
28
27
import pypandoc
29
28
30
29
31
- def publish_github_release (slug , token , tag_name , body ):
32
- github = github3 .login (token = token )
33
- owner , repo = slug .split ("/" )
34
- repo = github .repository (owner , repo )
35
- return repo .create_release (tag_name = tag_name , body = body )
36
-
37
-
38
30
def parse_changelog (tag_name ):
39
31
p = Path (__file__ ).parent .parent / "doc/en/changelog.rst"
40
32
changelog_lines = p .read_text (encoding = "UTF-8" ).splitlines ()
41
33
42
- title_regex = re .compile (r"pytest (\d\.\d+\.\d+) \(\d{4}-\d{2}-\d{2}\)" )
34
+ title_regex = re .compile (r"pytest (\d\.\d+\.\d+\w* ) \(\d{4}-\d{2}-\d{2}\)" )
43
35
consuming_version = False
44
36
version_lines = []
45
37
for line in changelog_lines :
@@ -64,36 +56,17 @@ def convert_rst_to_md(text):
64
56
65
57
66
58
def main (argv ):
67
- if len (argv ) > 1 :
68
- tag_name = argv [1 ]
69
- else :
70
- tag_name = os .environ .get ("GITHUB_REF" )
71
- if not tag_name :
72
- print ("tag_name not given and $GITHUB_REF not set" , file = sys .stderr )
73
- return 1
74
- if tag_name .startswith ("refs/tags/" ):
75
- tag_name = tag_name [len ("refs/tags/" ) :]
76
-
77
- token = os .environ .get ("GH_RELEASE_NOTES_TOKEN" )
78
- if not token :
79
- print ("GH_RELEASE_NOTES_TOKEN not set" , file = sys .stderr )
80
- return 1
81
-
82
- slug = os .environ .get ("GITHUB_REPOSITORY" )
83
- if not slug :
84
- print ("GITHUB_REPOSITORY not set" , file = sys .stderr )
85
- return 1
86
-
87
- rst_body = parse_changelog (tag_name )
88
- md_body = convert_rst_to_md (rst_body )
89
- if not publish_github_release (slug , token , tag_name , md_body ):
90
- print ("Could not publish release notes:" , file = sys .stderr )
91
- print (md_body , file = sys .stderr )
92
- return 5
59
+ if len (argv ) != 3 :
60
+ print ("Usage: generate-gh-release-notes VERSION FILE" )
61
+ return 2
93
62
63
+ version , filename = argv [1 :3 ]
64
+ print (f"Generating GitHub release notes for version { version } " )
65
+ rst_body = parse_changelog (version )
66
+ md_body = convert_rst_to_md (rst_body )
67
+ Path (filename ).write_text (md_body , encoding = "UTF-8" )
94
68
print ()
95
- print (f"Release notes for { tag_name } published successfully:" )
96
- print (f"https://github.com/{ slug } /releases/tag/{ tag_name } " )
69
+ print (f"Done: { filename } " )
97
70
print ()
98
71
return 0
99
72
0 commit comments