Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CI: modify release-notes.py to auto-categorize pull requests based on labels #4850

Merged
merged 15 commits into from
Oct 17, 2023
81 changes: 77 additions & 4 deletions scripts/release-notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,22 @@ def num_commits_since_prev_tag(token, base_url):
print(f"There are {num_commits} new commits since {prev_release_tag}")
return num_commits

categories = [
{'title': '#### ⛔ Breaking Changes', 'label': 'changelog:breaking-change'},
{'title': '#### New Features', 'label': 'changelog:new-feature'},
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
{'title': '#### Bug fixes, Minor Improvements', 'label': 'changelog:bugfix-or-minor-feature'},
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
{'title': '#### 🚧 Experimental Features', 'label': 'changelog:exprimental'},
{'title': '#### CI Improvements', 'label': 'changelog:ci'},
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved
{'title': '', 'label': 'changelog:test'},
{'title': '', 'label': 'changelog:skip'}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'title': None would be more idiomatic Python.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense. I am writing Python code for the first time :)

]

def categorize_pull_request(label):
for category, prefix in categories.items():
if label.startswith(prefix):
return category
return 'Other' # Default category if no matching prefix is found
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return 'Other' # Default category if no matching prefix is found
return 'Uncategorized' # Default category if no matching prefix is found



def main(token, repo, num_commits, exclude_dependabot):
accept_header = "application/vnd.github.groot-preview+json"
Expand All @@ -66,6 +82,10 @@ def main(token, repo, num_commits, exclude_dependabot):
print('Retrieved', len(commits), 'commits')

# Load PR for each commit and print summary
print("Processing...")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed

category_results = {category['title']: [] for category in categories}
other_results = []

for commit in commits:
sha = commit['sha']
author = commit['author']['login']
Expand All @@ -88,17 +108,70 @@ def main(token, repo, num_commits, exclude_dependabot):
if not pulls:
short_sha = sha[:7]
commit_url = commit['html_url']
print(f'* {msg} ([@{author}]({author_url}) in [{short_sha}]({commit_url}))')
# Check if the commit has changelog label
commit_labels = get_pull_request_labels(token, args.repo, pull_id)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if there are not pulls where are you getting pull_id?

Copy link
Member Author

@anshgoyalevil anshgoyalevil Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Correct actually. Now, I just threw all commits without pull_ids to the UNCATEGORISED group.

changelog_labels = [label for label in commit_labels if label.startswith('changelog:')]

category = 'Other'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe define a constant at the top

UNCATTEGORIZED = 'uncategorized'

if changelog_labels:
for cat in categories:
if changelog_labels[0].startswith(cat['label']):
category = cat['title']
break

result = f'* {msg} ([@{author}]({author_url}) in [{short_sha}]({commit_url}))'
if category == 'Other':
other_results.append(result)
else:
category_results[category].append(result)
continue

pull = pulls[0]
pull_id = pull['number']
pull_url = pull['html_url']
msg = msg.replace(f'(#{pull_id})', '').strip()
print(f'* {msg} ([@{author}]({author_url}) in [#{pull_id}]({pull_url}))')

if skipped_dependabot:
print(f"\n(Skipped {skipped_dependabot} dependabot commit{'' if skipped_dependabot == 1 else 's'})")
# Check if the pull request has changelog label
pull_labels = get_pull_request_labels(token, args.repo, pull_id)
changelog_labels = [label for label in pull_labels if label.startswith('changelog:')]
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved

category = 'Other'
if changelog_labels:
for cat in categories:
if changelog_labels[0].startswith(cat['label']):
category = cat['title']
break

result = f'* {msg} ([@{author}]({author_url}) in [#{pull_id}]({pull_url}))'
if category == 'Other':
other_results.append(result)
else:
category_results[category].append(result)

# Print categorized pull requests
for category, results in category_results.items():
if results and category:
print(f'{category}:')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

new line after header

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need an empty line after the header? Currently it is like:

Header 1:
PRs Table

Header 2:
PRs Table

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we do, look at the existing changelog format. In plain text markdown it's much easier to see headers when they are spaced, not jammed with text

for result in results:
print(result)
print()

# Print pull requests in the 'Other' category
if other_results:
print('#### Other:')
for result in other_results:
print(result)

if skipped_dependabot:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be indented left?

Copy link
Member Author

@anshgoyalevil anshgoyalevil Oct 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missed that. Thanks

print(f"\n(Skipped {skipped_dependabot} dependabot commit{'' if skipped_dependabot == 1 else 's'})")
anshgoyalevil marked this conversation as resolved.
Show resolved Hide resolved


def get_pull_request_labels(token, repo, pull_number):
labels_url = f"https://api.github.com/repos/jaegertracing/{repo}/issues/{pull_number}/labels"
req = Request(labels_url)
req.add_header('Authorization', f'token {token}')
labels = json.loads(urlopen(req).read())
return [label['name'] for label in labels]


if __name__ == "__main__":
Expand Down
Loading