Skip to content

Commit

Permalink
feat: refine changelog creation for releases (#51) (#52)
Browse files Browse the repository at this point in the history
This change refines the way changelogs are calculated. The resulting
behavior is such that a release includes the commits that were made
on main since the previous release was minted and any commits made
on the specific release branch (such as targeted fixes or backports).
Prerelease is a running log of changes made to main since the most
recent release was minted.
  • Loading branch information
Nostracodus committed Mar 4, 2022
1 parent 0c4c4b4 commit 6cd6d70
Show file tree
Hide file tree
Showing 8 changed files with 210 additions and 101 deletions.
77 changes: 77 additions & 0 deletions .github/workflows/compile-changelog.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

# This script identifies the commits that have been made to the current
# branch since the release before this branch. It formats them according
# to a format specifier.
#
# Usage:
# compile-changelog outfile
#
# outfile: the file location to write the changelog to

module Git
def self.one_line_format(repository_url)
"[<code>%h</code>](#{repository_url}/commit/%h) %s (%an)"
end

def self.current_branch
`git rev-parse --abbrev-ref HEAD`.strip
end

def self.default_branch
`git rev-parse --abbrev-ref origin/HEAD`.strip
end

def self.default_branch_head
`git rev-parse origin/HEAD`.strip
end

def self.initial_commit
`git rev-list --max-parents=0 #{default_branch}`.strip
end

def self.release_branches
`git branch --list -r origin/release/*`.split("\n").map(&:strip)
end

def self.commit_date(commit)
`git show -s --format=%ct #{commit}`.strip
end

def self.merge_base_from(branch1, branch2)
`git merge-base #{branch1} #{branch2}`.strip
end

# Returns the commit where the given branch forked from the default git branch.
# If the given branch is the default git branch, the branch commit is the initial
# commit in the repo.
def self.merge_base(branch)
if branch == Git::default_branch_head
Git::initial_commit()
else
Git::merge_base_from(Git::default_branch, branch)
end
end

def self.formatted_commits_between(low, high, format)
`git log --pretty="format:#{format}" --no-merges #{low}..#{high}`
end
end

outfile = ARGV[0]
repository_url = "https://github.com/#{ENV['GITHUB_REPOSITORY']}"
commit_upper_bound = Git::current_branch
commit_lower_bound = Git::release_branches
.map { |branch| Git::merge_base(branch) }
.reject { |commit| Git::commit_date(commit) >= Git::commit_date(commit_upper_bound) }
.sort_by { |commit| Git::commit_date(commit) }
.reverse
.first

changelog = Git::formatted_commits_between(
commit_lower_bound || Git::initial_commit,
commit_upper_bound,
Git::one_line_format(repository_url))

File.open(outfile, 'a') do |file|
file.puts changelog
end
46 changes: 46 additions & 0 deletions .github/workflows/on-main-branch-change.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Creates or updates a 'prerelease' release whenever main is
# changed. Attaches a built PDF to that release.
name: On Main Branch Change
on:
push:
branches:
- main

jobs:
build_draft_pdf:
runs-on: ubuntu-latest
steps:
- name: Set up Git repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set origin HEAD
run: git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

- name: Compile document
uses: xu-cheng/latex-action@v2
with:
root_file: ifc.tex
working_directory: ltx

- name: Compile changelog
run: |
echo 'CHANGELOG<<EOF' >> $GITHUB_ENV
ruby ./.github/workflows/compile-changelog.rb $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Create prerelease
uses: ncipollo/release-action@v1
with:
token: "${{ secrets.GITHUB_TOKEN }}"
allowUpdates: true
name: Prerelease
prerelease: true
tag: prerelease
body: |
This release is a prerelease. The contents of this release are likely to change.
# Changelog
${{ env.CHANGELOG }}
artifacts: |
ltx/ifc.pdf
50 changes: 50 additions & 0 deletions .github/workflows/on-release-branch-change.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Creates or updates a release's generated PDF when a change occurs
# to a release branch
name: On Release Branch Change
on:
push:
branches:
- "release/*"

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Set up Git repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Set origin HEAD
run: git symbolic-ref refs/remotes/origin/HEAD refs/remotes/origin/main

- name: Extract branch name
shell: bash
run: echo "RELEASE_NAME=$(echo ${GITHUB_REF#refs/heads/release/})" >> $GITHUB_ENV

- name: Compile document
uses: xu-cheng/latex-action@v2
with:
root_file: ifc.tex
working_directory: ltx

- name: Compile changelog
run: |
echo 'CHANGELOG<<EOF' >> $GITHUB_ENV
ruby ./.github/workflows/compile-changelog.rb $GITHUB_ENV
echo 'EOF' >> $GITHUB_ENV
- name: Update release
uses: ncipollo/release-action@v1
with:
token: "${{ secrets.GITHUB_TOKEN }}"
allowUpdates: true
name: ${{ env.RELEASE_NAME }}
prerelease: false
tag: ${{ env.RELEASE_NAME }}
body: |
# Changelog
${{ env.CHANGELOG }}
artifacts: |
ltx/ifc.pdf
37 changes: 37 additions & 0 deletions .github/workflows/on-release-created.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Creates a release branch whenever a release is created.

name: On Release Created
on:
release:
types:
- published

jobs:
create_branch:
runs-on: ubuntu-latest
steps:
- uses: peterjgrainger/action-create-branch@v2.0.1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
branch: 'release/${{ github.event.release.tag_name }}'

build_latex:
runs-on: ubuntu-latest
steps:
- name: Set up Git repository
uses: actions/checkout@v3
with:
fetch-depth: 0

- name: Compile document
uses: xu-cheng/latex-action@v2
with:
root_file: ifc.tex
working_directory: ltx

- name: Add PDF to Release
uses: softprops/action-gh-release@v0.1.13
if: startsWith(github.ref, 'refs/tags/')
with:
files: ltx/ifc.pdf
30 changes: 0 additions & 30 deletions .github/workflows/produce-branch-pdf.yml

This file was deleted.

29 changes: 0 additions & 29 deletions .github/workflows/produce-prerelease.yaml

This file was deleted.

17 changes: 0 additions & 17 deletions .github/workflows/produce-release-branch.yml

This file was deleted.

25 changes: 0 additions & 25 deletions .github/workflows/produce-release-pdf.yml

This file was deleted.

0 comments on commit 6cd6d70

Please sign in to comment.