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
67 changes: 64 additions & 3 deletions scripts/release-notes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,23 @@ def num_commits_since_prev_tag(token, base_url):
print(f"There are {num_commits} new commits since {prev_release_tag}")
return num_commits

UNCATTEGORIZED = 'Uncategorized'
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': None, 'label': 'changelog:test'},
{'title': None, 'label': 'changelog:skip'}
]

def categorize_pull_request(label):
for category, prefix in categories.items():
if label.startswith(prefix):
return category
return UNCATTEGORIZED # 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 +83,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 +109,57 @@ 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}))')

result = f'* {msg} ([@{author}]({author_url}) in [{short_sha}]({commit_url}))'
other_results.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}))')

# 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 = UNCATTEGORIZED
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 == UNCATTEGORIZED:
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 'UNCATTEGORIZED' category
if other_results:
print(f'#### {UNCATTEGORIZED}:')
for result in other_results:
print(result)

if skipped_dependabot:
print(f"\n(Skipped {skipped_dependabot} dependabot commit{'' if skipped_dependabot == 1 else 's'})")
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