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

adding release script #25

Merged
merged 1 commit into from
Nov 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 26 additions & 13 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
65 changes: 65 additions & 0 deletions release.py
Original file line number Diff line number Diff line change
@@ -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)
Loading