From f99e5741ed7248a869ca78727855fa5cee0ef860 Mon Sep 17 00:00:00 2001 From: Nikola Milosavljevic <73236646+NikolaMilosa@users.noreply.github.com> Date: Tue, 21 Nov 2023 14:02:42 +0100 Subject: [PATCH] adding release script (#25) --- .github/workflows/release.yml | 39 ++++++++++++++------- release.py | 65 +++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+), 13 deletions(-) create mode 100644 release.py diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 2527b4b..52a862c 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -1,7 +1,9 @@ on: - push: - tags: - - "v[0-9]+.[0-9]+.[0-9]+" + workflow_dispatch: + inputs: + tag: + description: "Tag of the release - should be in v[0-9]+.[0-9]+.[0.9]+" + required: True jobs: create-release: @@ -11,26 +13,37 @@ jobs: - name: Create artifacts directory run: mkdir artifacts - - name: Get the release version from the tag - if: env.VERSION == '' - run: | - echo "VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV - echo "version is: ${{ env.VERSION }}" + - name: Set up Python + uses: actions/setup-python@v2 + with: + python-version: '3.x' + - name: Install dependancies + run: | + pip install requests + pip install GitPython + + - name: Generate body + env: + TAG: ${{ github.event.inputs.tag }} + run: | + python release.py + - name: Create GitHub release id: release uses: actions/create-release@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: - tag_name: ${{ env.VERSION }} - release_name: ${{ env.VERSION }} + tag_name: ${{ github.event.inputs.tag }} + release_name: ${{ github.event.inputs.tag }} + body_path: ./body - name: Save release upload URL to artifact run: echo "${{ steps.release.outputs.upload_url }}" > artifacts/release-upload-url - name: Save version number to artifact - run: echo "${{ env.VERSION }}" > artifacts/release-version + run: echo "${{ github.event.inputs.tag }}" > artifacts/release-version - name: Upload artifacts uses: actions/upload-artifact@v1 @@ -115,10 +128,10 @@ jobs: run: | if [ "${{ matrix.build }}" = "windows" ]; then echo "ASSET=target/${{ matrix.target }}/release/ntfs-undelete.exe" >> $GITHUB_ENV - echo "ASSET_NAME=ntfs-undelete-${{ env.RELEASE_VERSION }}.exe" >> $GITHUB_ENV + echo "ASSET_NAME=ntfs-undelete-${{ github.event.inputs.tag }}.exe" >> $GITHUB_ENV else echo "ASSET=target/${{ matrix.target }}/release/ntfs-undelete" >> $GITHUB_ENV - echo "ASSET_NAME=ntfs-undelete-${{ env.RELEASE_VERSION }}-${{ matrix.target }}" >> $GITHUB_ENV + echo "ASSET_NAME=ntfs-undelete-${{ github.event.inputs.tag }}-${{ matrix.target }}" >> $GITHUB_ENV fi - name: Upload release archive uses: actions/upload-release-asset@v1.0.1 diff --git a/release.py b/release.py new file mode 100644 index 0000000..a6f4dfd --- /dev/null +++ b/release.py @@ -0,0 +1,65 @@ +import git +import requests +import os + +def get_github_releases(repo_owner='NikolaMilosa', repo_name='ntfs-undelete', github_token=None): + # GitHub API endpoint for releases + url = f'https://api.github.com/repos/{repo_owner}/{repo_name}/releases' + + headers = {} + if github_token: + headers['Authorization'] = f'Bearer {github_token}' + + # Make a request to the GitHub API + response = requests.get(url, headers=headers) + + # Check if the request was successful (status code 200) + if response.status_code == 200: + releases = response.json() + return releases + else: + # Print an error message if the request was not successful + return None + +def get_commits_since_tag(repo_path='.', tag_name=None): + repo = git.Repo(repo_path) + + if tag_name: + # If tag_name is provided, get the commit associated with the tag + try: + commit = repo.commit(tag_name) + except git.exc.GitCommandError: + return None + else: + # If tag_name is None, get the latest commit + commit = repo.head.commit + + # Get all commits from the specified commit until the latest commit + commits = list(repo.iter_commits(commit, max_count=repo.git.rev_list('HEAD', count=True), reverse=True)) + + return commits + +commit_prefix = 'https://github.com/NikolaMilosa/ntfs-undelete/commit/' +author_prefix = 'https://github.com/' + +github_token = os.environ['GITHUB_TOKEN'] +current_tag = os.environ['TAG'] + +releases = get_github_releases() +tag_name = None +if len(releases) != 0: + tag_name = sorted(releases, lambda x: x['created_at'])[-1]['tag_name'] + +release_text = f""" +# Release {current_tag} + +### Changelog: +""" + +commits = get_commits_since_tag(tag_name=tag_name) +for commit in reversed(commits): + username = commit.author.email.split('+')[1].split('@')[0] + release_text += f"* [{commit.hexsha[:7]}]({commit_prefix}{commit.hexsha}) [Author: [{username}]({author_prefix}{username})] - {commit.message.splitlines()[0]}\n" + +with open('body', 'w') as f: + f.write(release_text)