diff --git a/.github/workflows/assemble_changelog.yml b/.github/workflows/assemble_changelog.yml index 352de2221b9..8d94b800c54 100644 --- a/.github/workflows/assemble_changelog.yml +++ b/.github/workflows/assemble_changelog.yml @@ -1,9 +1,7 @@ name: Assemble changelog on: - push: - branches: - - main - - release-v** + pull_request: + types: [ opened, synchronize ] jobs: process: permissions: @@ -11,6 +9,7 @@ jobs: contents: write runs-on: ubuntu-20.04 env: + PR_NUMBER: ${{ github.event.pull_request.number }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} steps: - uses: actions/checkout@v3 diff --git a/changelog/README.md b/changelog/README.md index 1b621760894..fd0952cf56b 100644 --- a/changelog/README.md +++ b/changelog/README.md @@ -26,10 +26,8 @@ If you have implemented several features or bugfixes you should describe all of You can choose any name for your changelog files because the GitHub action will rename files in `changelog/unreleased/features` and `changelog/unreleased/bugfixes` directories to `${PR_NUMBER}.md` when you open a PR. -Every push to the main or release branch Assemble changelog GitHub action will be executed: - -* collect all files from `changelog/unreleased` -* assemble the changelog like: +To view the changelog for the current branch, run the command `python3 scripts/changelog/assemble_changelog.py`. +This script will collect all the files in the `changelog/unreleased` directory and generate a changelog in the following format: ``` #### Features @@ -47,11 +45,11 @@ Every push to the main or release branch Assemble changelog GitHub action will b Some other changes ``` -* write the changelog to the `changelog/unreleased/CHANGELOG.md` file +To view the changelog for the android auto project, run the command `python3 scripts/changelog/assemble_changelog.py --auto`. Every release the release train app will: -* get changelog from `changelog/unreleased/CHANGELOG.md` file +* assemble the changelog by the script `python3 scripts/changelog/assemble_changelog.py` * add information about dependencies and compile changelog like: ``` ## Mapbox Navigation SDK 1.1.1 - 13 December, 2022 diff --git a/scripts/changelog/assemble_changelog.py b/scripts/changelog/assemble_changelog.py index 587c1014050..794d8fe10c7 100644 --- a/scripts/changelog/assemble_changelog.py +++ b/scripts/changelog/assemble_changelog.py @@ -1,6 +1,9 @@ import os -import git +import requests + +pr_number = os.environ['PR_NUMBER'] +token = os.environ['GITHUB_TOKEN'] def get_changes(path): @@ -30,31 +33,37 @@ def get_changes(path): issues = get_changes('changelog/unreleased/issues/') other = get_changes('changelog/unreleased/other/') -changelog = '#### Features\n' + features + '\n\n' + \ +changelog = '# Changelog\n' + \ + '#### Features\n' + features + '\n\n' + \ '#### Bug fixes and improvements\n' + bugfixes + '\n\n' + \ '#### Known issues :warning:\n' + issues + '\n\n' + \ '#### Other changes\n' + other -old_changelog = open('changelog/unreleased/CHANGELOG.md', 'r').read() - -if changelog != old_changelog: - open('changelog/unreleased/CHANGELOG.md', 'w').write(changelog) - repository = git.Repo('.') - repository.git.add('changelog/unreleased') - repository.index.commit('Assemble changelog file [skip ci]') - repository.remotes.origin.push().raise_if_error() - auto_bugfixes = get_changes('libnavui-androidauto/changelog/unreleased/bugfixes/') auto_features = get_changes('libnavui-androidauto/changelog/unreleased/features/') -auto_changelog = '#### Features\n' + auto_features + '\n\n' + \ - '#### Bug fixes and improvements\n' + auto_bugfixes +auto_changelog = '# Android Auto Changelog\n' + \ + '#### Features\n' + auto_features + '\n\n' + \ + '#### Bug fixes and improvements\n' + auto_bugfixes + +pr_comments_url = 'https://api.github.com/repos/mapbox/mapbox-navigation-android/issues/' + pr_number + '/comments' +headers = {"Authorization": "Bearer " + token} +comments = requests.get(pr_comments_url, headers=headers).json() + + +def update_comment(title, content): + comment_with_changelog_id = None + for comment in comments: + if comment['body'].startswith(title): + comment_with_changelog_id = comment['id'] + + if comment_with_changelog_id: + comments_url = 'https://api.github.com/repos/mapbox/mapbox-navigation-android/issues/comments/' + comment_url = comments_url + str(comment_with_changelog_id) + requests.patch(comment_url, json={'body': content}, headers=headers) + else: + requests.post(pr_comments_url, json={'body': content}, headers=headers) -auto_old_changelog = open('libnavui-androidauto/changelog/unreleased/CHANGELOG.md', 'r').read() -if auto_changelog != auto_old_changelog: - open('libnavui-androidauto/changelog/unreleased/CHANGELOG.md', 'w').write(auto_changelog) - repository = git.Repo('.') - repository.git.add('libnavui-androidauto/changelog/unreleased') - repository.index.commit('Assemble auto changelog file [skip ci]') - repository.remotes.origin.push().raise_if_error() +update_comment('# Changelog', changelog) +update_comment('# Android Auto Changelog', auto_changelog)