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