Skip to content

Commit

Permalink
update validation scripts for running changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
Łukasz Paczos committed Dec 21, 2021
1 parent cfa4807 commit b03b6ad
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 39 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ commands:
name: Install python dependencies
command: python3 -m pip install requests
- run:
name: Verify that a changelog entry is present in the PR description
name: Verify that a changelog entry is present in CHANGELOG.md
command: |
MBX_CI_GITHUB_TOKEN=$(./mbx-ci github reader token)
if [[ -n "$CIRCLE_PULL_REQUEST" ]]; then
Expand Down
19 changes: 5 additions & 14 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,16 @@ Include issue references (e.g., fixes [#issue](link))
Include necessary implementation details (e.g. I opted to use this algorithm because ... and test it in this way ...).
-->

### Changelog
<!--
Include changelog entry (e.g. Fixed an unexpected change in recenter button when resuming the app.).
See https://github.com/mapbox/navigation-sdks/blob/main/documentation/android-changelog-guidelines.md.
You can remove the changelog block and add a `skip changelog` label, when applicable.
-->
```
<changelog></changelog>
```

### Screenshots or Gifs
<!-- Include media files to provide additional context. It's REALLY useful for UI related PRs (e.g. ![screenshot gif](link)) -->


<!--
---------- CHECKLIST ----------
1. Add related labels (`bug`, `feature`, `new API(s)`, `SEMVER-MAJOR`, `needs-backporting`, etc.).
2. Update progress status on the project board.
3. Request a review from the team, if not a draft.
4. Add targeted milestone, when applicable.
5. Create ticket tracking addition of public documentation pages entry, when applicable.
1. Adda a changelog entry under `Unreleased` tag or a `skip changelog` label if not applicable.
1. Update progress status on the project board.
1. Request a review from the team, if not a draft.
1. Add targeted milestone, when applicable.
1. Create ticket tracking addition of public documentation pages entry, when applicable.
-->
60 changes: 36 additions & 24 deletions scripts/validate-changelog.py
Original file line number Diff line number Diff line change
@@ -1,34 +1,46 @@
#!/usr/bin/python

import sys
import requests
import re
import requests
import sys

print("Validating that changelog entry is provided in the PR description...")
print("Validating that changelog entry is provided in the CHANGELOG.md...")

pr_number = sys.argv[1]
github_token = sys.argv[2]
url = "https://api.github.com/repos/mapbox/mapbox-navigation-android/pulls/" + pr_number
headers = { "accept": "application/vnd.github.v3+json", "authorization": "token " + github_token }
with requests.get(url, headers = headers) as r:
response_json = r.json()
pr_description = response_json["body"]
pr_labels = response_json["labels"]
api_url = "https://api.github.com/repos/mapbox/mapbox-navigation-android/pulls/" + str(pr_number)
pr_link_regex = "\[#\d+]\(https:\/\/github\.com\/mapbox\/mapbox-navigation-android\/pull\/\d+\)"
headers = {"accept": "application/vnd.github.v3+json", "authorization": "token " + github_token}
changelog_diff_substring = "diff --git a/CHANGELOG.md b/CHANGELOG.md"
any_diff_substring = "diff --git"

skip_changelog = False
if pr_labels is not None:
for label in pr_labels:
if label["name"] == "skip changelog":
skip_changelog = True
break
with requests.get(api_url, headers=headers) as pr_response:
response_json = pr_response.json()
pr_labels = response_json["labels"]

matches = re.search(r'<changelog>(.+)</changelog>', pr_description, flags=re.S) is not None
skip_changelog = False
if pr_labels is not None:
for label in pr_labels:
if label["name"] == "skip changelog":
skip_changelog = True
break

if skip_changelog and matches:
raise Exception("Both `skip changelog` label and `<changelog></changelog>` closure present.")
elif skip_changelog:
print("`skip changelog` label present, exiting.")
elif matches:
print("Changelog entry validation successful.")
else:
raise Exception("Add a non-empty changelog entry in a `<changelog></changelog>` closure in the PR description or add a `skip changelog` label if not applicable.")
if not skip_changelog:
pr_diff_url = response_json["diff_url"]
with requests.get(pr_diff_url, headers) as diff_response:
diff = diff_response.text
changelog_entry_index = diff.find(changelog_diff_substring)
if changelog_entry_index == -1:
raise Exception("Add a non-empty changelog entry in a CHANGELOG.md or add a `skip changelog` label if not applicable.")
else:
changelog_entry_last_index = changelog_entry_index + len(changelog_diff_substring)
last_reachable_index = diff.find(any_diff_substring, changelog_entry_last_index)
if last_reachable_index == -1:
last_reachable_index = len(diff)
diff_searchable = diff[changelog_entry_last_index:last_reachable_index]
pr_link_matches = re.search(pr_link_regex, diff_searchable)
if not pr_link_matches:
raise Exception("The changelog entry should contain a link to the original PR that matches `" + pr_link_regex + "`")
print("Changelog entry validation successful.")
else:
print("`skip changelog` label present, exiting.")

0 comments on commit b03b6ad

Please sign in to comment.