-
Notifications
You must be signed in to change notification settings - Fork 12
121 lines (111 loc) · 4.72 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
114
115
116
117
118
119
120
121
# 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]
permissions:
# Needed to add and delete comments on a PRs
pull-requests: write
env:
ARTIFACT_NAME: "report"
jobs:
setup-env:
name: Setup environment
runs-on: ubuntu-24.04
outputs:
IS_PR: ${{ steps.call.outputs.IS_PR || steps.run.outputs.IS_PR }}
ORG: ${{ steps.call.outputs.ORG || steps.run.outputs.ORG }}
REPO: ${{ steps.call.outputs.REPO || steps.run.outputs.REPO }}
RUN_ID: ${{ steps.call.outputs.RUN_ID || steps.run.outputs.RUN_ID }}
REPO_FULL_NAME: ${{ steps.call.outputs.REPO_FULL_NAME || steps.run.outputs.REPO_FULL_NAME }}
SHA: ${{ steps.call.outputs.SHA || steps.run.outputs.SHA }}
steps:
- name: Workflow call setup from a Pull Request
id: call
if: github.event_name == 'pull_request'
run: |
echo "Setting up from a workflow call"
echo "RUN_ID=${{ github.run_id }}" >> $GITHUB_OUTPUT
REPO_FULL_NAME="${{ github.repository }}"
parts=(${REPO_FULL_NAME//\// })
echo "ORG=${parts[0]}" >> $GITHUB_OUTPUT
echo "REPO=${parts[1]}" >> $GITHUB_OUTPUT
echo "SHA=${{ github.event.pull_request.head.sha }}" >> $GITHUB_OUTPUT
echo "IS_PR=${{ github.event_name == 'pull_request' }}" >> $GITHUB_OUTPUT
- name: Workflow run setup
id: run
if: github.event_name == 'workflow_run'
run: |
echo "RUN_ID=${{ github.event.workflow_run.id }}" >> $GITHUB_OUTPUT
echo "ORG=${{ github.event.workflow_run.organization.login }}" >> $GITHUB_OUTPUT
echo "REPO=${{ github.event.workflow_run.repository.name }}" >> $GITHUB_OUTPUT
echo "REPO_FULL_NAME=${{ github.event.workflow_run.repository.full_name }}" >> $GITHUB_OUTPUT
echo "SHA=${{ github.event.workflow_run.head_sha }}" >> $GITHUB_OUTPUT
echo "IS_PR=${{ github.event.workflow_run.event == 'pull_request' }}" >> $GITHUB_OUTPUT
comment-on-prs:
name: Comment on PRs
runs-on: ubuntu-24.04
needs: setup-env
if: ${{ needs.setup-env.outputs.IS_PR }}
steps:
- name: Download artifacts
uses: actions/download-artifact@v4.1.8
with:
repository: ${{ needs.setup-env.outputs.REPO_FULL_NAME }}
run-id: ${{ needs.setup-env.outputs.RUN_ID }}
pattern: "${{ env.ARTIFACT_NAME }}-*"
- name: Comment on PR
uses: actions/github-script@v7.0.1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
const fs = require('fs');
const artifact = '${{ env.ARTIFACT_NAME }}';
const owner = '${{ needs.setup-env.outputs.ORG }}';
const repo = '${{ needs.setup-env.outputs.REPO }}';
const sha = '${{ needs.setup-env.outputs.SHA }}';
const header = `## Test results for commit ${sha}\n\n`;
const comments = fs.readdirSync('.')
.filter(d => fs.statSync(d).isDirectory() && d.startsWith(`${artifact}-`))
.map(dir => `${dir}/${artifact}.json`)
.filter(fs.existsSync)
.flatMap(filePath => JSON.parse(fs.readFileSync(filePath, 'utf8')));
const pull = (await github.rest.pulls.list({
owner: owner,
repo: repo,
})).data.filter(pr => pr.head.sha == 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: (header + 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]' &&
!comment.body.startsWith(header),
);
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);
}