Skip to content

Commit 08eb802

Browse files
authored
Merge pull request #38 from carstenartur/copilot/fix-authentication-issues
2 parents ff24088 + 4a0b0a2 commit 08eb802

2 files changed

Lines changed: 154 additions & 1 deletion

File tree

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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+
timeout-minutes: 10
25+
steps:
26+
27+
- name: Search version increment git patch
28+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
29+
id: search-patch
30+
with:
31+
github-token: ${{ secrets.GITHUB_TOKEN }}
32+
script: |
33+
let allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
34+
run_id: context.payload.workflow_run.id,
35+
...context.repo
36+
})
37+
let artifact = allArtifacts.data.artifacts.find(artifact => artifact.name == 'versions-git-patch')
38+
return artifact?.id
39+
40+
- uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
41+
if: steps.search-patch.outputs.result
42+
with:
43+
ref: '${{ github.event.workflow_run.head_sha }}'
44+
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
45+
46+
- name: Download version increment git patch
47+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
48+
id: fetch-patch
49+
if: steps.search-patch.outputs.result
50+
with:
51+
github-token: ${{ secrets.GITHUB_TOKEN }}
52+
script: |
53+
let download = await github.rest.actions.downloadArtifact({
54+
artifact_id: ${{ steps.search-patch.outputs.result }},
55+
archive_format: 'zip',
56+
...context.repo
57+
})
58+
let fs = require('fs')
59+
fs.writeFileSync(`${process.env.GITHUB_WORKSPACE}/patch.zip`, Buffer.from(download.data))
60+
await exec.exec('unzip', ['patch.zip'])
61+
let pr_number = Number(fs.readFileSync('github_pull_request_number.txt'))
62+
core.setOutput('pull_request_number', pr_number)
63+
await io.rmRF('patch.zip')
64+
await io.rmRF('github_pull_request_number.txt')
65+
66+
- name: Apply and push version increment
67+
id: git-commit
68+
if: steps.search-patch.outputs.result
69+
env:
70+
REPOSITORY_NAME: ${{ github.event.workflow_run.head_repository.full_name }}
71+
BRANCH_NAME: ${{ github.event.workflow_run.head_branch }}
72+
BOT_PA_TOKEN: ${{ secrets.githubBotPAT }}
73+
run: |
74+
set -x
75+
# Set initial placeholder name/mail and read it from the patch later
76+
git config --global user.email 'foo@bar'
77+
git config --global user.name 'Foo Bar'
78+
79+
git am version_increments.patch
80+
81+
# Read the author's name+mail from the just applied patch and recommit it with both set as committer
82+
botMail=$(git log -1 --pretty=format:'%ae')
83+
botName=$(git log -1 --pretty=format:'%an')
84+
git config --global user.email "${botMail}"
85+
git config --global user.name "${botName}"
86+
git commit --amend --no-edit
87+
88+
fileList=$(git diff-tree --no-commit-id --name-only HEAD -r)
89+
echo "file-list<<EOF" >> $GITHUB_OUTPUT
90+
echo "$fileList" >> $GITHUB_OUTPUT
91+
echo "EOF" >> $GITHUB_OUTPUT
92+
93+
git push \
94+
"https://oauth2:${BOT_PA_TOKEN}@github.com/${REPOSITORY_NAME}.git" \
95+
"HEAD:refs/heads/${BRANCH_NAME}"
96+
97+
- name: Find existing information comment
98+
uses: peter-evans/find-comment@b30e6a3c0ed37e7c023ccd3f1db5c6c0b0c23aad # v4.0.0
99+
id: search-comment
100+
if: always() && steps.fetch-patch.outputs.pull_request_number
101+
with:
102+
github-token: ${{ secrets.githubBotPAT }}
103+
body-regex: '^${{ env.COMMENT_FIRST_LINE }}'
104+
issue-number: ${{ steps.fetch-patch.outputs.pull_request_number }}
105+
comment-author: ${{ inputs.botGithubId }}
106+
direction: last
107+
108+
- name: Add or update information comment
109+
uses: actions/github-script@ed597411d8f924073f98dfc5c65a23a2325f34cd # v8.0.0
110+
if: always() && steps.search-patch.outputs.result
111+
env:
112+
FILELIST: ${{ steps.git-commit.outputs.file-list }}
113+
with:
114+
github-token: ${{ secrets.githubBotPAT }}
115+
script: |
116+
const fs = require('fs')
117+
const fileList = process.env.FILELIST
118+
if (fileList) { // if list is empty, no versions were changed
119+
const prNumber = '${{ steps.fetch-patch.outputs.pull_request_number }}'
120+
const pr = await github.rest.pulls.get({
121+
pull_number: prNumber,
122+
...context.repo
123+
})
124+
const applyChangeMessagePart = pr.data.maintainer_can_modify || pr.data.base.repo.full_name == pr.data.head.repo.full_name
125+
? "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_."
126+
: "> [!WARNING]\n> :construction: **This PR cannot be modified by maintainers** because edits are disabled or it is created from an organization repository. To obtain the required changes apply the _git patch_ manually as an additional commit."
127+
const commentBody = `
128+
${{ env.COMMENT_FIRST_LINE }}.
129+
Therefore the following files need a version increment:
130+
\`\`\`
131+
${fileList}
132+
\`\`\`
133+
${applyChangeMessagePart}
134+
<details>
135+
<summary>Git patch</summary>
136+
137+
\`\`\`
138+
${ fs.readFileSync( process.env.GITHUB_WORKSPACE + '/version_increments.patch', {encoding: 'utf8'}).trim() }
139+
\`\`\`
140+
</details>
141+
142+
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).
143+
`.trim()
144+
const existingCommentId = '${{ steps.search-comment.outputs.comment-id }}'
145+
if (existingCommentId) {
146+
github.rest.issues.updateComment({...context.repo, comment_id: existingCommentId, body: commentBody })
147+
} else {
148+
github.rest.issues.createComment({...context.repo, issue_number: prNumber, body: commentBody })
149+
}
150+
}

.github/workflows/version-increments.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@ on:
55
workflows: [ 'Pull-Request Checks' ]
66
types: [ completed ]
77

8+
permissions: {}
9+
810
jobs:
911
publish-version-check-results:
10-
uses: eclipse-platform/eclipse.platform.releng.aggregator/.github/workflows/publishVersionCheckResults.yml@master
12+
if: github.event.workflow_run.conclusion == 'success'
13+
uses: ./.github/workflows/publishVersionCheckResults.yml
1114
with:
1215
botGithubId: eclipse-jdt-bot
1316
secrets:

0 commit comments

Comments
 (0)