Skip to content

Commit cbfe74a

Browse files
committed
Add comment workflow
1 parent d65437b commit cbfe74a

File tree

1 file changed

+153
-0
lines changed

1 file changed

+153
-0
lines changed
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)