-
Notifications
You must be signed in to change notification settings - Fork 12
113 lines (105 loc) · 3.99 KB
/
comment.yaml
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
# Copyright 2025 Canonical Ltd.
# See LICENSE file for licensing details.
name: Comment on the pull request
on:
workflow_call:
workflow_run:
workflows: ["Workflow Unit tests"]
types: [completed]
env:
ARTIFACT_NAME: "report"
jobs:
download-artifacts:
name: Download Artifacts
runs-on: ubuntu-24.04
if: github.event.workflow_run.event == 'pull_request'
outputs:
artifacts: ${{ fromJson(steps.download-artifact.outputs.result) }}
steps:
- uses: actions/[email protected]
- id: download-artifact
name: Download test report artifact
uses: actions/[email protected]
with:
script: |
const fs = require('fs');
const owner = context.payload.workflow_run.repository.owner.login;
const repo = context.payload.workflow_run.repository.name;
const run_id = ${{ github.event.workflow_run.id }};
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: owner,
repo: repo,
run_id: run_id,
});
const report_artifacts = artifacts.data.artifacts.filter(artifact =>
artifact.name.startsWith('${{ env.ARTIFACT_NAME }}-')
)
const zips = await Promise.all(
report_artifacts.map(async artifact => {
const download = await github.rest.actions.downloadArtifact({
owner: owner,
repo: repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
const zipPath = `${artifact}.zip`;
fs.writeFileSync(zipPath, Buffer.from(download.data));
return zipPath;
})
);
return JSON.stringify(zips);
comment-on-prs:
name: Comment on PRs
runs-on: ubuntu-24.04
needs: download-artifacts
if: github.event.workflow_run.event == 'pull_request'
strategy:
matrix:
artifact: ${{fromJson(needs.download-artifacts.outputs.artifacts)}}
steps:
- name: Unzip
shell: bash
run: unzip ${{ matrix.artifact }}
- name: Comment on PR
uses: actions/[email protected]
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const comments = JSON.parse(fs.readFileSync('${{ env.ARTIFACT_NAME }}.json'));
const owner = context.payload.workflow_run.repository.owner.login;
const repo = context.payload.workflow_run.repository.name;
const pull = (await github.rest.pulls.list({
owner: owner,
repo: repo,
})).data.filter(pr => pr.head.sha == context.payload.workflow_run.head_sha)[0];
const issue_number = pull.number;
const createComment = async (body) => {
await github.rest.issues.createComment({
owner: owner,
repo: repo,
issue_number: issue_number,
body,
});
}
const deleteGithubActionsComments = async () => {
const existingComments = await github.rest.issues.listComments({
owner: owner,
repo: repo,
issue_number: issue_number,
});
const githubActionsComments = existingComments.data.filter(
comment => comment.user.login == 'github-actions[bot]'
);
for (const comment of githubActionsComments) {
await github.rest.issues.deleteComment({
owner: owner,
repo: repo,
comment_id: comment.id,
});
}
}
await deleteGithubActionsComments();
for (const comment of comments) {
await createComment(comment);
}