Skip to content

Commit 6d0f0bb

Browse files
committed
Add workflow for automated version increments in pull-requests
1 parent 9e574ad commit 6d0f0bb

File tree

2 files changed

+202
-0
lines changed

2 files changed

+202
-0
lines changed

.github/workflows/checkVersions.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Check for version increments and apply them if necessary
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
botName:
7+
description: The name of the bot that adds the necessary version increment changes
8+
type: string
9+
required: true
10+
botMail:
11+
description: The name of the bot that adds the necessary version increment changes
12+
type: string
13+
required: true
14+
15+
permissions: read-all
16+
17+
jobs:
18+
versions-check-and-increment:
19+
name: Check and increment service versions
20+
if: github.event_name == 'pull_request'
21+
runs-on: ubuntu-latest
22+
steps:
23+
24+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
25+
with:
26+
fetch-depth: 0 # required for jgit timestamp provider to work
27+
28+
- name: Set up Java
29+
uses: actions/setup-java@6a0805fcefea3d4657a47ac4c165951e33482018 # v4.2.2
30+
with:
31+
java-version: 17
32+
distribution: 'temurin'
33+
cache: maven
34+
35+
- name: Set up Maven
36+
uses: stCarolas/setup-maven@d6af6abeda15e98926a57b5aa970a96bb37f97d1 # v5
37+
with:
38+
maven-version: 3.9.9
39+
40+
- name: Check and increment versions
41+
uses: Wandalen/wretry.action@6feedb7dedadeb826de0f45ff482b53b379a7844 # master
42+
with:
43+
attempt_delay: 200
44+
attempt_limit: 10
45+
command: >
46+
mvn generate-sources -Pfast-version-check
47+
org.eclipse.tycho:tycho-versions-plugin:bump-versions -Dtycho.bump-versions.increment=100
48+
--threads 1C --fail-at-end --batch-mode --no-transfer-progress --show-version
49+
50+
- name: Commit version increments, if any
51+
run: |
52+
set -x
53+
if [[ $(git status --ignore-submodules --porcelain) != '' ]]; then
54+
# Workspace is not clean, i.e. some version were changed
55+
56+
# Read 'releaseNumberSDK' property as stream version
57+
streamVersion=$(mvn help:evaluate -Dexpression=releaseNumberSDK --quiet -DforceStdout)
58+
59+
git config --global user.email '${{ inputs.botMail }}'
60+
git config --global user.name '${{ inputs.botName }}'
61+
git add --all
62+
git status
63+
git commit -m "Bump version(s) for ${streamVersion} stream"
64+
65+
git format-patch -1 HEAD --no-stat --output 'version_increments.patch'
66+
67+
echo '${{ github.event.pull_request.number }}' > 'github_pull_request_number.txt'
68+
69+
else
70+
echo 'No version increments required'
71+
fi
72+
73+
- uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
74+
with:
75+
name: versions-git-patch
76+
path: |
77+
version_increments.patch
78+
github_pull_request_number.txt
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
name: Publish Version Check Results
2+
on:
3+
workflow_call:
4+
secrets:
5+
githubBotPAT:
6+
description: The personal access token (with scope 'public_repo') of the bot to push a required change to a PR branch in a fork.
7+
required: false
8+
9+
permissions: read-all
10+
11+
jobs:
12+
versions-check-result:
13+
name: Publish Version Check Results
14+
runs-on: ubuntu-latest
15+
if: github.event.workflow_run.conclusion != 'skipped'
16+
17+
steps:
18+
- name: Dump GitHub context
19+
run: echo '${{ toJSON(github) }}'
20+
21+
- name: Search version increment git patch
22+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
23+
id: search-patch
24+
with:
25+
script: |
26+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
27+
run_id: context.payload.workflow_run.id,
28+
...context.repo
29+
})
30+
let artifact = allArtifacts.data.artifacts.find(artifact => artifact.name == 'versions-git-patch')
31+
return artifact?.id
32+
33+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
34+
if: steps.search-patch.outputs.result
35+
with:
36+
ref: '${{ github.event.workflow_run.head_sha }}'
37+
persist-credentials: true # (Explicit) opt-in for persisting the bot's PAT for authentication when pushing below
38+
token: ${{ secrets.githubBotPAT }}
39+
40+
- name: Download version increment git patch
41+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
42+
id: fetch-patch
43+
if: steps.search-patch.outputs.result
44+
with:
45+
script: |
46+
let download = await github.rest.actions.downloadArtifact({
47+
artifact_id: ${{ steps.search-patch.outputs.result }},
48+
archive_format: 'zip',
49+
...context.repo
50+
})
51+
let fs = require('fs')
52+
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/patch.zip`, Buffer.from(download.data))
53+
await exec.exec('unzip', ['patch.zip'])
54+
let pr_number = Number(fs.readFileSync('github_pull_request_number.txt'))
55+
core.setOutput('pull_request_number', pr_number)
56+
await io.rmRF('patch.zip')
57+
await io.rmRF('github_pull_request_number.txt')
58+
59+
- name: Apply and push version increment
60+
id: git-commit
61+
if: steps.search-patch.outputs.result
62+
run: |
63+
set -x
64+
# Set initial placeholder name/mail and read it from the patch later
65+
git config --global user.email 'foo@bar'
66+
git config --global user.name 'Foo Bar'
67+
68+
git am version_increments.patch
69+
70+
# Read the author's name+mail from the just applied patch and recommit it with both set as committer
71+
botMail=$(git log -1 --pretty=format:'%ae')
72+
botName=$(git log -1 --pretty=format:'%an')
73+
git config --global user.email "${botMail}"
74+
git config --global user.name "${botName}"
75+
git commit --amend --no-edit
76+
77+
fileList=$(git diff-tree --no-commit-id --name-only HEAD -r)
78+
echo "file-list<<EOF" >> $GITHUB_OUTPUT
79+
echo "$fileList" >> $GITHUB_OUTPUT
80+
echo "EOF" >> $GITHUB_OUTPUT
81+
82+
git remote add 'fork' https://github.com/${{ github.event.workflow_run.head_repository.full_name }}.git
83+
git remote -v
84+
git push 'fork' 'HEAD:refs/heads/${{ github.event.workflow_run.head_branch }}'
85+
86+
- name: Add information comment
87+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
88+
if: steps.search-patch.outputs.result
89+
with:
90+
github-token: ${{ secrets.githubBotPAT }}
91+
script: |
92+
let prNumber = ${{ steps.fetch-patch.outputs.pull_request_number }}
93+
core.info('Context issue number: ' + prNumber)
94+
const fs = require('fs')
95+
const fileList = `${{ steps.git-commit.outputs.file-list }}`
96+
if (fileList) { // if list is empty, no versions were changed
97+
github.rest.issues.createComment({
98+
issue_number: prNumber, ...context.repo, body: `
99+
This pull request changes some projects for the first time in this development.
100+
Therefore the following files need a version increment:
101+
\`\`\`
102+
${fileList}
103+
\`\`\`
104+
An additional commit containing all the necessary changes was pushed to the top of this PR's branch. To obtain these changes (for example if you want to push more changes) either fetch from your fork or apply the _git patch_.
105+
<details>
106+
<summary>Git patch</summary>
107+
108+
\`\`\`
109+
${ fs.readFileSync( process.env.GITHUB_WORKSPACE + '/version_increments.patch', {encoding: 'utf8'}).trim() }
110+
\`\`\`
111+
</details>
112+
`.trim()
113+
})
114+
}
115+
116+
117+
# TODO: Add an FAQ about version bumps?
118+
# - Why are the version bumps necessary at all?
119+
# According to the rules of semantic versioning at least the service version of a bundle should be increased if anything changes between releases.
120+
# - Why is the service version bumped by +100
121+
# Allows service version increments on maintanance branches that stay semnatically smaller than the next releases version
122+
# - Why a separate commit?
123+
# Easier to identify in Git history and the actual change is easier to revert (version must not be reverted)
124+
# TODO: make comment updatable?

0 commit comments

Comments
 (0)