Skip to content

Commit e6babb1

Browse files
committed
Add comment workflow
1 parent 5db5a5e commit e6babb1

2 files changed

Lines changed: 156 additions & 54 deletions

File tree

‎.github/workflows/build-pr-cmk.yml‎

Lines changed: 3 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
name: Build cmk binaries on PR
1919

2020
on:
21-
pull_request_target:
21+
pull_request:
2222
types: [opened, synchronize, reopened]
2323

2424
concurrency:
@@ -36,10 +36,9 @@ jobs:
3636
outcome: ${{ steps.meta.outputs.outcome }}
3737
artifact_url: ${{ steps.meta.outputs.artifact_url }}
3838
steps:
39-
- name: Checkout PR HEAD
39+
- name: Checkout PR code
4040
uses: actions/checkout@v4
4141
with:
42-
ref: ${{ github.event.pull_request.head.sha }}
4342
persist-credentials: false
4443

4544
- name: Set up Go
@@ -67,54 +66,4 @@ jobs:
6766
if: always()
6867
run: |
6968
echo "outcome=${{ steps.build.outcome }}" >> $GITHUB_OUTPUT
70-
echo "artifact_url=${{ steps.upload_artifact.outputs.artifact-url }}" >> $GITHUB_OUTPUT
71-
72-
comment:
73-
if: always()
74-
needs: build
75-
permissions:
76-
contents: read
77-
issues: write
78-
pull-requests: write
79-
runs-on: ubuntu-24.04
80-
steps:
81-
- name: Comment or update cmk build artifact on PR
82-
uses: actions/github-script@v7
83-
with:
84-
script: |
85-
const { execSync } = require('child_process');
86-
87-
const issue_number = context.payload.pull_request.number;
88-
const identifier = "cmk-build-artifact-comment";
89-
90-
const owner = context.payload.repository.owner.login; // base repo (pull_request_target)
91-
const repo = context.payload.repository.name;
92-
93-
const buildOutcome = "${{ needs.build.outputs.outcome }}";
94-
const artifactUrl = "${{ needs.build.outputs.artifact_url }}";
95-
const runId = "${{ github.run_id }}";
96-
97-
core.info(`Will comment on ${owner}/${repo}#${issue_number}`);
98-
core.info(`Outcome=${buildOutcome || '(empty)'} Artifact=${artifactUrl || '(none)'}`);
99-
100-
let body = `<!-- ${identifier} -->\n`;
101-
if (buildOutcome === 'success' && artifactUrl) {
102-
const expiryDate = execSync("date -d '+10 days' '+%B %d, %Y'").toString().trim();
103-
body += `✅ Build complete for PR #${issue_number}.\n\n`;
104-
body += `🔗 Download the [cmk binaries](${artifactUrl}) (expires on ${expiryDate})`;
105-
} else {
106-
body += `❌ Build failed for PR #${issue_number}.\n\n`;
107-
body += `See the run: https://github.com/${owner}/${repo}/actions/runs/${runId}`;
108-
}
109-
110-
const { data: comments } = await github.rest.issues.listComments({ owner, repo, issue_number });
111-
const existing = comments.find(c => c.user.login === 'github-actions[bot]' && c.body.includes(identifier));
112-
113-
if (existing) {
114-
core.info(`Updating comment id ${existing.id}`);
115-
await github.rest.issues.updateComment({ owner, repo, comment_id: existing.id, body });
116-
} else {
117-
core.info(`Creating new comment`);
118-
await github.rest.issues.createComment({ owner, repo, issue_number, body });
119-
}
120-
69+
echo "artifact_url=${{ steps.upload_artifact.outputs.artifact-url }}" >> $GITHUB_OUTPUT
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
name: Comment on PR build results
19+
20+
on:
21+
workflow_run:
22+
workflows: ["Build cmk binaries on PR"]
23+
types:
24+
- completed
25+
26+
permissions:
27+
contents: read
28+
issues: write
29+
pull-requests: write
30+
31+
jobs:
32+
comment:
33+
runs-on: ubuntu-24.04
34+
if: >
35+
github.event.workflow_run.event == 'pull_request'
36+
steps:
37+
- name: Download artifact metadata
38+
uses: actions/github-script@v7
39+
id: artifact-metadata
40+
with:
41+
script: |
42+
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
43+
owner: context.repo.owner,
44+
repo: context.repo.repo,
45+
run_id: context.payload.workflow_run.id,
46+
});
47+
48+
const prArtifact = artifacts.data.artifacts.find(a => a.name.startsWith('cmk-binaries.pr'));
49+
50+
if (prArtifact) {
51+
const prNumber = prArtifact.name.match(/pr(\d+)/)?.[1];
52+
return {
53+
artifact_url: prArtifact.archive_download_url,
54+
pr_number: prNumber,
55+
conclusion: context.payload.workflow_run.conclusion
56+
};
57+
}
58+
59+
return {
60+
pr_number: null,
61+
conclusion: context.payload.workflow_run.conclusion
62+
};
63+
64+
- name: Get PR number from workflow run
65+
id: get-pr
66+
uses: actions/github-script@v7
67+
with:
68+
script: |
69+
const metadata = ${{ steps.artifact-metadata.outputs.result }};
70+
71+
if (metadata.pr_number) {
72+
return metadata.pr_number;
73+
}
74+
75+
// Fallback: get PR from workflow run
76+
const pulls = await github.rest.pulls.list({
77+
owner: context.repo.owner,
78+
repo: context.repo.repo,
79+
state: 'open',
80+
head: `${context.repo.owner}:${context.payload.workflow_run.head_branch}`
81+
});
82+
83+
if (pulls.data.length > 0) {
84+
return pulls.data[0].number;
85+
}
86+
87+
return null;
88+
89+
- name: Comment or update build result on PR
90+
uses: actions/github-script@v7
91+
with:
92+
script: |
93+
const { execSync } = require('child_process');
94+
const prNumber = ${{ steps.get-pr.outputs.result }};
95+
96+
if (!prNumber) {
97+
core.warning('Could not determine PR number, skipping comment');
98+
return;
99+
}
100+
101+
const identifier = "cmk-build-artifact-comment";
102+
const owner = context.repo.owner;
103+
const repo = context.repo.repo;
104+
const conclusion = '${{ github.event.workflow_run.conclusion }}';
105+
const runId = '${{ github.event.workflow_run.id }}';
106+
const runUrl = `https://github.com/${owner}/${repo}/actions/runs/${runId}`;
107+
108+
core.info(`Commenting on PR #${prNumber}`);
109+
core.info(`Build conclusion: ${conclusion}`);
110+
111+
let body = `<!-- ${identifier} -->\n`;
112+
113+
if (conclusion === 'success') {
114+
const expiryDate = execSync("date -d '+10 days' '+%B %d, %Y'").toString().trim();
115+
body += `✅ Build complete for PR #${prNumber}.\n\n`;
116+
body += `📦 Binary artifacts are available in the [workflow run](${runUrl}) (expires on ${expiryDate}).\n\n`;
117+
body += `> **Note:** Download artifacts by clicking on the workflow run link above, then scroll to the "Artifacts" section.`;
118+
} else if (conclusion === 'failure') {
119+
body += `❌ Build failed for PR #${prNumber}.\n\n`;
120+
body += `See the [workflow run](${runUrl}) for details.`;
121+
} else {
122+
body += `⚠️ Build ${conclusion} for PR #${prNumber}.\n\n`;
123+
body += `See the [workflow run](${runUrl}) for details.`;
124+
}
125+
126+
const { data: comments } = await github.rest.issues.listComments({
127+
owner,
128+
repo,
129+
issue_number: prNumber
130+
});
131+
132+
const existing = comments.find(c =>
133+
c.user.login === 'github-actions[bot]' &&
134+
c.body.includes(identifier)
135+
);
136+
137+
if (existing) {
138+
core.info(`Updating existing comment id ${existing.id}`);
139+
await github.rest.issues.updateComment({
140+
owner,
141+
repo,
142+
comment_id: existing.id,
143+
body
144+
});
145+
} else {
146+
core.info(`Creating new comment`);
147+
await github.rest.issues.createComment({
148+
owner,
149+
repo,
150+
issue_number: prNumber,
151+
body
152+
});
153+
}

0 commit comments

Comments
 (0)