Skip to content

Commit 6d99e92

Browse files
committed
Add workflow for automated version increments in pull-requests
1 parent 4584eba commit 6d99e92

File tree

2 files changed

+216
-0
lines changed

2 files changed

+216
-0
lines changed

.github/workflows/checkVersions.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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: {} # all none
16+
17+
jobs:
18+
versions-check-and-increment:
19+
name: Check and increment service versions
20+
runs-on: ubuntu-latest
21+
steps:
22+
23+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
24+
with:
25+
fetch-depth: 0 # required for jgit timestamp provider to work
26+
27+
- name: Set up Java
28+
uses: actions/setup-java@6a0805fcefea3d4657a47ac4c165951e33482018 # v4.2.2
29+
with:
30+
java-version: 17
31+
distribution: 'temurin'
32+
cache: maven
33+
34+
- name: Set up Maven
35+
uses: stCarolas/setup-maven@d6af6abeda15e98926a57b5aa970a96bb37f97d1 # v5
36+
with:
37+
maven-version: 3.9.9
38+
39+
- name: Check and increment versions
40+
uses: Wandalen/wretry.action@6feedb7dedadeb826de0f45ff482b53b379a7844 # master
41+
with:
42+
attempt_delay: 200
43+
attempt_limit: 10
44+
command: >
45+
mvn verify -DskipTests -Dcompare-version-with-baselines.skip=false
46+
org.eclipse.tycho:tycho-versions-plugin:bump-versions -Dtycho.bump-versions.increment=100
47+
--threads 1C --fail-at-end --batch-mode --no-transfer-progress --show-version
48+
49+
- name: Commit version increments, if any
50+
run: |
51+
set -x
52+
if [[ $(git status --ignore-submodules --porcelain) != '' ]]; then
53+
# Workspace is not clean, i.e. some version were changed
54+
55+
# Read 'releaseNumberSDK' property as stream version
56+
streamVersion=$(mvn help:evaluate -Dexpression=releaseNumberSDK --quiet -DforceStdout)
57+
58+
git config --global user.email '${{ inputs.botMail }}'
59+
git config --global user.name '${{ inputs.botName }}'
60+
git add --all
61+
git status
62+
git commit -m "Version bump(s) for ${streamVersion} stream"
63+
64+
git format-patch -1 HEAD --no-stat --output 'version_increments.patch'
65+
66+
echo '${{ github.event.pull_request.number }}' > 'github_pull_request_number.txt'
67+
68+
echo "::error title=Version increments are missing::Required version increments are missing and a commit to apply them is about to be pushed to your PR's branch."
69+
exit 1
70+
else
71+
echo 'No version increments required'
72+
fi
73+
74+
- uses: actions/upload-artifact@50769540e7f4bd5e21e526ee35c689e35e0d6874 # v4.4.0
75+
if: always()
76+
with:
77+
name: versions-git-patch
78+
path: |
79+
version_increments.patch
80+
github_pull_request_number.txt
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
name: Publish Version Check Results
2+
on:
3+
workflow_call:
4+
inputs:
5+
botGithubId:
6+
description: The id of the bot's github account that adds the information comment
7+
type: string
8+
required: true
9+
10+
secrets:
11+
githubBotPAT:
12+
description: The personal access token (with scope 'public_repo') of the bot to push a required change to a PR branch in a fork.
13+
required: true
14+
15+
permissions: {} # all none
16+
17+
env:
18+
COMMENT_FIRST_LINE: 'This pull request changes some projects for the first time in this development cycle'
19+
20+
jobs:
21+
versions-check-result:
22+
name: Publish Version Check Results
23+
runs-on: ubuntu-latest
24+
if: github.event.workflow_run.conclusion != 'skipped'
25+
steps:
26+
27+
- name: Search version increment git patch
28+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
29+
id: search-patch
30+
with:
31+
script: |
32+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
33+
run_id: context.payload.workflow_run.id,
34+
...context.repo
35+
})
36+
let artifact = allArtifacts.data.artifacts.find(artifact => artifact.name == 'versions-git-patch')
37+
return artifact?.id
38+
39+
- uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
40+
if: steps.search-patch.outputs.result
41+
with:
42+
ref: '${{ github.event.workflow_run.head_sha }}'
43+
persist-credentials: false #Opt out from persisting the default Github-token authentication in order to enable use of the bot's PAT when pushing below
44+
45+
- name: Download version increment git patch
46+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
47+
id: fetch-patch
48+
if: steps.search-patch.outputs.result
49+
with:
50+
script: |
51+
let download = await github.rest.actions.downloadArtifact({
52+
artifact_id: ${{ steps.search-patch.outputs.result }},
53+
archive_format: 'zip',
54+
...context.repo
55+
})
56+
let fs = require('fs')
57+
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/patch.zip`, Buffer.from(download.data))
58+
await exec.exec('unzip', ['patch.zip'])
59+
let pr_number = Number(fs.readFileSync('github_pull_request_number.txt'))
60+
core.setOutput('pull_request_number', pr_number)
61+
await io.rmRF('patch.zip')
62+
await io.rmRF('github_pull_request_number.txt')
63+
64+
- name: Apply and push version increment
65+
id: git-commit
66+
if: steps.search-patch.outputs.result
67+
run: |
68+
set -x
69+
# Set initial placeholder name/mail and read it from the patch later
70+
git config --global user.email 'foo@bar'
71+
git config --global user.name 'Foo Bar'
72+
73+
git am version_increments.patch
74+
75+
# Read the author's name+mail from the just applied patch and recommit it with both set as committer
76+
botMail=$(git log -1 --pretty=format:'%ae')
77+
botName=$(git log -1 --pretty=format:'%an')
78+
git config --global user.email "${botMail}"
79+
git config --global user.name "${botName}"
80+
git commit --amend --no-edit
81+
82+
fileList=$(git diff-tree --no-commit-id --name-only HEAD -r)
83+
echo "file-list<<EOF" >> $GITHUB_OUTPUT
84+
echo "$fileList" >> $GITHUB_OUTPUT
85+
echo "EOF" >> $GITHUB_OUTPUT
86+
87+
git push \
88+
"https://oauth2:${BOT_PA_TOKEN}@github.com/${{ github.event.workflow_run.head_repository.full_name }}.git" \
89+
'HEAD:refs/heads/${{ github.event.workflow_run.head_branch }}'
90+
env:
91+
BOT_PA_TOKEN: ${{ secrets.githubBotPAT }}
92+
93+
- name: Find existing information comment
94+
uses: peter-evans/find-comment@3eae4d37986fb5a8592848f6a574fdf654e61f9e # v3.1.0
95+
id: search-comment
96+
if: steps.fetch-patch.outputs.pull_request_number
97+
with:
98+
body-regex: '^${{ env.COMMENT_FIRST_LINE }}'
99+
issue-number: ${{ steps.fetch-patch.outputs.pull_request_number }}
100+
comment-author: ${{ inputs.botGithubId }}
101+
direction: last
102+
103+
- name: Add or update information comment
104+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
105+
if: always()
106+
with:
107+
github-token: ${{ secrets.githubBotPAT }}
108+
script: |
109+
const fs = require('fs')
110+
const fileList = `${{ steps.git-commit.outputs.file-list }}`
111+
if (fileList) { // if list is empty, no versions were changed
112+
const commentBody = `
113+
${{ env.COMMENT_FIRST_LINE }}.
114+
Therefore the following files need a version increment:
115+
\`\`\`
116+
${fileList}
117+
\`\`\`
118+
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_.
119+
<details>
120+
<summary>Git patch</summary>
121+
122+
\`\`\`
123+
${ fs.readFileSync( process.env.GITHUB_WORKSPACE + '/version_increments.patch', {encoding: 'utf8'}).trim() }
124+
\`\`\`
125+
</details>
126+
127+
Further information are available in [Common Build Issues - Missing version increments](https://github.com/eclipse-platform/eclipse.platform.releng.aggregator/wiki/Common-Build-Issues#missing-version-increments).
128+
`.trim()
129+
const prNumber = '${{ steps.fetch-patch.outputs.pull_request_number }}'
130+
const existingCommentId = '${{ steps.search-comment.outputs.comment-id }}'
131+
if (existingCommentId) {
132+
github.rest.issues.updateComment({...context.repo, comment_id: existingCommentId, body: commentBody })
133+
} else {
134+
github.rest.issues.createComment({...context.repo, issue_number: prNumber, body: commentBody })
135+
}
136+
}

0 commit comments

Comments
 (0)