forked from sqlfluff/sqlfluff
-
Notifications
You must be signed in to change notification settings - Fork 0
107 lines (96 loc) · 3.84 KB
/
ci-pr-comments.yml
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
# This Workflow runs in a more secure context and comments
# on pull requests.
# https://securitylab.github.com/research/github-actions-preventing-pwn-requests/
name: Comment on the pull request
# Run on completion of the CI job.
# This workflow has access to write comments on PRs event when
# that PR is triggered by a forked repo.
on:
workflow_run:
workflows:
- CI
types:
- completed
jobs:
comment-on-pr:
runs-on: ubuntu-latest
if: >
github.event.workflow_run.event == 'pull_request'
steps:
- name: 'Download txt artifact'
uses: actions/github-script@v6
with:
script: |
const artifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: ${{github.event.workflow_run.id }},
});
const matchArtifact = artifacts.data.artifacts.filter((artifact) => {
return artifact.name == "txt-report"
})[0];
const download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: matchArtifact.id,
archive_format: 'zip',
});
var fs = require('fs');
fs.writeFileSync('${{github.workspace}}/cov-report.zip', Buffer.from(download.data));
- name: Unzip Downloaded Artifact
run: unzip cov-report.zip
- name: Update PR comment with coverage report.
uses: actions/github-script@v6
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
// First list the existing comments
const trigger_str = 'Coverage Results';
console.log("Getting existing comments...");
const { promises: fs } = require('fs');
const issue_number = await fs.readFile('pr-number.txt', 'utf8');
console.log("Issue number: " + issue_number);
const comments = await github.paginate(
github.rest.issues.listComments,
{
owner: 'sqlfluff',
repo: 'sqlfluff',
issue_number: Number(issue_number)
}
);
let comment_id = null;
console.log("Got %d comments", comments.length);
comments.forEach(comment => {
if (comment.body.indexOf(trigger_str) >= 0) {
console.log("Found target comment ID: %d", comment.id);
comment_id = comment.id;
} else {
console.log("Comment ID %d not valid with body:\n%s.", comment.id, comment.body);
}
});
const previous_outcome = await fs.readFile('outcome.txt', 'utf8');
console.log("Previous coverage step outcome: %s", previous_outcome);
if (previous_outcome == "success\n") {
status_emoji = "✅";
} else {
status_emoji = "⚠️";
}
const content = await fs.readFile('coverage-report.txt', 'utf8');
body = "# " + trigger_str + " " + status_emoji + "\n```\n" + content + "\n```\n";
if (comment_id > 0) {
console.log("Updating comment id: %d", comment_id);
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment_id,
body: body
});
} else {
console.log("No existing comment matched, creating a new one...");
await github.rest.issues.createComment({
issue_number: Number(issue_number),
owner: context.repo.owner,
repo: context.repo.repo,
body: body
});
}