-
Notifications
You must be signed in to change notification settings - Fork 24
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
Générer le changelog automatiquement depuis GitHub [GEN-1526] #3952
Merged
+171
−1
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
name: 📃 Changelog | ||
|
||
on: | ||
schedule: | ||
- cron: 0 0 * * 1 | ||
|
||
jobs: | ||
generate-changelog: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/[email protected] | ||
- name: 💂 Install Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.11" | ||
- name: 📥 Generate changelog | ||
run: scripts/mkchangelog | ||
env: | ||
GH_TOKEN: ${{ secrets.ITOU_TECH_GH_TOKEN }} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
name: Label Checks | ||
|
||
# Add merge_group and make the check required when | ||
# https:/mheap/github-action-required-labels/issues/66 is solved. | ||
on: [pull_request] | ||
|
||
jobs: | ||
require-label: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Verify changelog label | ||
uses: mheap/[email protected] | ||
with: | ||
mode: exactly | ||
count: 1 | ||
labels: "ajouté, modifié, supprimé, dependencies, no-changelog" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
#!/usr/bin/env python3 | ||
|
||
|
||
import datetime | ||
import json | ||
import logging | ||
import os | ||
import pathlib | ||
import re | ||
import subprocess | ||
import sys | ||
import tempfile | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
def gh(*args, **kwargs): | ||
env = ( | ||
{ | ||
"GH_TOKEN": os.getenv("GH_TOKEN", ""), | ||
"PATH": os.environ["PATH"], | ||
} | ||
if os.getenv("CI", False) | ||
else None | ||
) | ||
return subprocess.run(["gh", *args], check=True, env=env, **kwargs) | ||
|
||
|
||
def list_pull_requests(start, end, labels): | ||
limit = 1000 | ||
result = gh( | ||
"search", | ||
"prs", | ||
"--merged-at", | ||
f"{start}..{end}", | ||
"--repo", | ||
"gip-inclusion/les-emplois", | ||
"--limit", | ||
f"{limit}", | ||
"--json", | ||
"title,url,labels,body", | ||
"--", | ||
labels, | ||
capture_output=True, | ||
) | ||
pull_requests = json.loads(result.stdout) | ||
if len(pull_requests) == limit: | ||
sys.exit("Limit has been exceeded when fetching pull requests, keep up the good work!") | ||
return pull_requests | ||
|
||
|
||
def list_missing_pull_requests(start, end): | ||
return list_pull_requests(start, end, "-label:ajouté,modifié,supprimé,dependencies,no-changelog") | ||
|
||
|
||
def list_merged_pull_requests(start, end): | ||
return list_pull_requests(start, end, "label:ajouté,modifié,supprimé") | ||
|
||
|
||
def open_pull_request(branch, title): | ||
gh("pr", "create", "--label", "no-changelog", "--title", title, "--body", "") | ||
|
||
|
||
def main(): | ||
changelog_path = pathlib.Path("CHANGELOG.md") | ||
with ( | ||
open(changelog_path, "r+") as changelog, | ||
tempfile.NamedTemporaryFile(dir=os.getcwd(), mode="w+", suffix="~", delete=False) as new_changelog, | ||
): | ||
try: | ||
for line in changelog: | ||
if line.strip() in ["# Journal des modifications", ""]: | ||
new_changelog.write(line) | ||
continue | ||
break | ||
last_entry_header = line | ||
|
||
_h2, sprint_start_text = last_entry_header.split() | ||
sprint_start = datetime.date.fromisoformat(sprint_start_text) | ||
sprint_end = sprint_start + datetime.timedelta(days=7) | ||
|
||
if missing := list_missing_pull_requests(sprint_start, sprint_end): | ||
missing_pull_requests = "\n- ".join(pr["url"] for pr in missing) | ||
sys.exit(f"The following pull requests should have a label:\n- {missing_pull_requests}\n") | ||
|
||
added, changed, removed = [], [], [] | ||
for pull_request in list_merged_pull_requests(sprint_start, sprint_end): | ||
title = re.sub(r"\[(GEN|ITOU)-[0-9]+\]", "", pull_request["title"]).strip() | ||
title = f"[{title}]({pull_request['url']})" | ||
if re.search(r"\!\[[^]]+]", pull_request["body"]): | ||
title += " 🖼" | ||
labels = [label["name"] for label in pull_request["labels"]] | ||
if len({"ajouté", "modifié", "supprimé"}.intersection(set(labels))) != 1: | ||
label_names = " ".join(labels) | ||
raise ValueError( | ||
f"Expected only one of “ajouté”, “modifié” or “supprimé” in labels, got {label_names}." | ||
) | ||
if "ajouté" in labels: | ||
added.append(title) | ||
elif "modifié" in labels: | ||
changed.append(title) | ||
else: | ||
removed.append(title) | ||
|
||
if added or changed or removed: | ||
new_changelog.write(f"## {sprint_end}\n\n") | ||
if added: | ||
new_changelog.write("### Ajouté\n\n") | ||
for title in sorted(added): | ||
new_changelog.write(f"- {title}\n") | ||
new_changelog.write("\n") | ||
if changed: | ||
new_changelog.write("### Modifié\n\n") | ||
for title in sorted(changed): | ||
new_changelog.write(f"- {title}\n") | ||
new_changelog.write("\n") | ||
if removed: | ||
new_changelog.write("### Supprimé\n\n") | ||
for title in sorted(removed): | ||
new_changelog.write(f"- {title}\n") | ||
new_changelog.write("\n") | ||
|
||
# Don’t forget the previous header. | ||
new_changelog.write(last_entry_header) | ||
for line in changelog: | ||
new_changelog.write(line) | ||
|
||
# Atomically replace file. | ||
pathlib.Path(new_changelog.name).rename(changelog_path) | ||
finally: | ||
pathlib.Path(new_changelog.name).unlink(missing_ok=True) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
1000 PR en une semaine, ça laisse un peu de marge ! 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Y'a eu débat sur le nombre de zéro, il faut savoir que François est ambitieux 🥲