Skip to content

Commit d64ce47

Browse files
committed
feature: Check if PR exist in latest release.
1 parent fa404b0 commit d64ce47

File tree

1 file changed

+39
-23
lines changed

1 file changed

+39
-23
lines changed

script/custom-release.py

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
SER_VER_ID_PROPERTY_PTN = SER_VER_ID_PROPERTY.replace('.', r'\.')
1818
SER_VER_ID_PATTERN = re.compile(
1919
'<' + SER_VER_ID_PROPERTY_PTN + r'>\s*(.*)\s*</' + SER_VER_ID_PROPERTY_PTN + '>')
20+
OTP_GITHUB_PULLREQUEST_URL = "https://github.com/opentripplanner/OpenTripPlanner/pull/"
2021
STATE_FILE = '.custom_release_resume_state.json'
2122
SUMMARY_FILE = '.custom_release_summary.md'
2223

@@ -100,8 +101,7 @@ def description_w_labels(self):
100101
return f'{self.description()} {self.labels}'
101102

102103
def description_link(self):
103-
url = f"https://github.com/opentripplanner/OpenTripPlanner/pull/{self.number}"
104-
return f"[{self.description()}]({url}) {self.labels}".replace("'", "`")
104+
return f"[{self.description()}]({OTP_GITHUB_PULLREQUEST_URL}{self.number}) {self.labels}".replace("'", "`")
105105

106106
# The execution state of the script + the CLI arguments
107107
class ScriptState:
@@ -116,15 +116,15 @@ def __init__(self):
116116
self.gotoStep = False
117117
self.step = None
118118

119-
def next_version_tag(self):
120-
return f'v{self.next_version}'
121-
122119
def latest_version_tag(self):
123120
return f'v{self.latest_version}'
124121

125122
def latest_version_git_hash(self):
126123
return git_commit_hash(self.latest_version_tag())
127124

125+
def next_version_tag(self):
126+
return f'v{self.next_version}'
127+
128128
def next_version_description(self):
129129
return f'Version {self.next_version} ({self.next_ser_ver_id})'
130130

@@ -207,6 +207,7 @@ def setup_and_verify():
207207
resolve_version_number()
208208
resolve_next_version()
209209
read_pull_request_info_from_github()
210+
check_if_prs_exist_in_latest_release()
210211
resolve_next_ser_ver_id()
211212
print_setup()
212213

@@ -328,7 +329,11 @@ def print_summary():
328329
print(f"These PRs are tagged with {config.include_prs_label}.\n", file=f)
329330
for pr in pullRequests:
330331
print(f" - {pr.link()}", file=f)
331-
p = execute("./script/changelog-diff.py", state.latest_version_tag(), state.next_version_tag())
332+
p = execute(
333+
"./script/changelog-diff.py",
334+
state.latest_version_tag(),
335+
state.next_version_tag()
336+
)
332337
print(p.stdout, file=f)
333338

334339

@@ -506,25 +511,35 @@ def read_pull_request_info_from_github():
506511
# Example response
507512
# {"data":{"repository":{"pullRequests":{"nodes":[{"number":2222,"labels":{"nodes":[{"name":"bump serialization id"},{"name":"Entur Test"}]}}]}}}}
508513
json_doc = json.loads(result.stdout)
514+
defined_labels = [LBL_BUMP_SER_VER_ID.lower(), config.include_prs_label.lower()]
515+
509516
for node in json_doc['data']['repository']['pullRequests']['nodes']:
510517
pr = PullRequest()
511518
pr.number = node['number']
512519
pr.title = node['title']
513520
pr.commitHash = node['headRefOid']
514521
labels = node['labels']['nodes']
515522
# GitHub labels are not case-sensitive, hence using 'lower()'
516-
include_prs_label_lc = config.include_prs_label.lower()
517523
for label in labels:
518524
lbl_name = label['name']
519525
lbl_name_lc= lbl_name.lower()
520-
if include_prs_label_lc == lbl_name_lc:
521-
pr.labels.append(lbl_name)
522-
if LBL_BUMP_SER_VER_ID == lbl_name_lc:
526+
if lbl_name_lc in defined_labels:
523527
pr.labels.append(lbl_name)
524-
state.prs_bump_ser_ver_id = True
525528
pullRequests.append(pr)
526529

527530

531+
def check_if_prs_exist_in_latest_release():
532+
info(f'Check if one of the PRs labeled with {LBL_BUMP_SER_VER_ID} does not exist in the '
533+
f'latest release. If so, bump the ser.ver.id ...')
534+
latest_release_hash = state.latest_version_git_hash()
535+
536+
for pr in pullRequests:
537+
if pr.serLabelSet and not git_is_commit_ancestor(pr.commitHash, latest_release_hash):
538+
info(f' - The top commit does not exist in the latest release. Bumping ser.ver.id. ({pr.description()})')
539+
state.prs_bump_ser_ver_id = True
540+
return
541+
542+
528543
# The script will resolve what the next serialization version id (SID) should be. This is a complex task.
529544
# Here is an overview:
530545
# 1. If the --serVerId option exist, then the latest SID is bumped and used.
@@ -658,18 +673,6 @@ def delete_script_state():
658673
## Utility functions ##
659674
## ------------------------------------------------------------------------------------ ##
660675

661-
# Get the full git hash for a qualified branch name, tag or hash
662-
def git_commit_hash(ref):
663-
if re.compile(r'[0-9a-f]{40}').match(ref):
664-
return ref
665-
output = execute('git', 'show-ref', ref).stdout
666-
return output.split()[0]
667-
668-
669-
# Create a tag name used in git for a given version
670-
def git_tag(version):
671-
return f'v{version}'
672-
673676

674677
def bump_release_ser_ver_id(latest_id):
675678
# The id format can be either 'A-00053' or 'AA-0053'
@@ -711,6 +714,19 @@ def run_maven_test():
711714
mvn('clean', '-PprettierSkip', 'test')
712715

713716

717+
# Get the full git hash for a qualified branch name, tag or hash
718+
def git_commit_hash(ref):
719+
if re.compile(r'[0-9a-f]{40}').match(ref):
720+
return ref
721+
output = execute('git', 'show-ref', ref).stdout
722+
return output.split()[0]
723+
724+
725+
def git_is_commit_ancestor(childCommit: str, parentCommit : str):
726+
p = subprocess.run("git", "merge-base" "--is-ancestor", childCommit, parentCommit, timeout=5)
727+
return p.returncode == 0
728+
729+
714730
def git(*cmd, error_msg=None):
715731
return execute('git', *cmd, error_msg=error_msg)
716732

0 commit comments

Comments
 (0)