-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction.py
More file actions
executable file
·218 lines (157 loc) · 4.74 KB
/
Copy pathaction.py
File metadata and controls
executable file
·218 lines (157 loc) · 4.74 KB
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
#!/usr/bin/env python3
from git import Repo
import github
import os
import re
import signal
import sys
import tomllib
###
# GLOBALS
###
PYPROJECT_TOML = "pyproject.toml"
###
# MAIN METHOD
###
def main():
# Only execute on pull requests
if get_env_var("GITHUB_REF") == "master":
print("[INFO] master branch detected")
sys.exit()
# Read version from pyproject.toml
project_version = read_project_version()
# Read expected "next" version
next_version = read_next_version()
# Compare versions
if not project_version == next_version:
die(
[
f"Version value in {PYPROJECT_TOML} does not match expected version",
f"Project version : {project_version}",
f"Next expected version : {next_version}",
"Aborting.",
]
)
else:
print(f"[INFO] NEXT VERSION MATCHES {PYPROJECT_TOML} VERSION:", next_version)
# Use "git describe" to retrieve most recent tag
def read_next_version():
# Get latest tag
git_describe = git_describe_version()
# Get PR title
pr_title = read_pr_title()
# Parse PR header and increment tag
next_version = determine_next_version(git_describe, pr_title)
print("[DEBUG] ")
print("[DEBUG] pr title : '%s'" % pr_title)
print("[DEBUG] previous tag version : %s" % git_describe)
print("[DEBUG] next tag version : %s" % next_version)
print("[DEBUG] ")
return next_version
# Use PR title to determine next incremented version
def determine_next_version(git_describe, pr_title):
# Work out which version part to increment
if "[major]" in pr_title.lower():
return increment_tag(git_describe, 0)
elif "[minor]" in pr_title.lower():
return increment_tag(git_describe, 1)
elif "[patch]" in pr_title.lower():
return increment_tag(git_describe, 2)
elif "[notag]" in pr_title.lower():
return git_describe
else:
die(
[
"One of the following - [major], [minor], [patch], [notag] - not found in PR title"
]
)
# Increment tag based on PR title
def increment_tag(git_describe, position):
# Split tag
semver = git_describe.split(".")
# Increment component
semver[position] = str(int(semver[position]) + 1)
# Reset version subcomponents to zero as required
while position < 2:
position += 1
semver[position] = "0"
# Return tag
return ".".join(semver)
# Call git describe
def git_describe_version():
# Read tag using git describe
repo = Repo()
git_tag = repo.git.describe(
"--abbrev=0",
"--tags",
"--match=v*.*.*",
"--exclude=*/*",
"--exclude=*-rc[0-9]*",
)
# String 'v' character
return git_tag.replace("v", "")
# Read PR title using github client
def read_pr_title():
# Load github params
token = get_env_var("GITHUB_TOKEN")
repo_name = get_env_var("GITHUB_REPOSITORY")
pr_number = get_pr_number()
# Fetch PR details
gh = github.Github(token)
repo = gh.get_repo(repo_name)
pr = repo.get_pull(pr_number)
# Return title
return pr.title
# Extract PR number from GITHUB_REF
def get_pr_number():
# Look up from env var (eg.: "refs/pull/123/merge")
pr_ref = get_env_var("GITHUB_REF")
# Extract number
match = re.match("refs/pull/(.*?)/.*", pr_ref)
if match:
return int(match.group(1))
else:
die(["could not extract PR number from GITHUB_REF"])
# Extract version value from pyproject.toml
def read_project_version():
with open(PYPROJECT_TOML, "rb") as config:
data = tomllib.load(config)
return data["project"]["version"]
# Look up env var
def get_env_var(env_var_name, strict=True):
# Check env var
value = os.getenv(env_var_name)
# Handle missing value
if not value:
if strict:
if env_var_name == "GITHUB_TOKEN":
print(f"error: env var not found: {env_var_name}")
print("""please ensure your workflow step includes
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}""")
sys.exit(1)
else:
print(f"error: env var not found: {env_var_name}")
sys.exit(1)
return value
# Error message util
def die(messages):
# Display message
print("[ERROR]")
for msg in messages:
print(f"[ERROR] {msg}")
print("[ERROR]")
# Exit non-zero
sys.exit(1)
# Handle interrupt
def signal_handler(_, __):
print(" ")
sys.exit(0)
####
# MAIN
####
# Set up Ctrl-C handler
signal.signal(signal.SIGINT, signal_handler)
# Invoke main method
if __name__ == "__main__":
main()