-
-
Notifications
You must be signed in to change notification settings - Fork 4
154 lines (128 loc) · 5.42 KB
/
build_artifact_comment.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
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
name: Write build artifact comments
on:
workflow_run:
workflows:
- 🧪 Run tests and deploy demo data
types:
- completed
permissions:
contents: read
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
jobs:
on-success:
if: github.event.workflow_run.event == 'pull_request'
permissions:
pull-requests: write
runs-on: ubuntu-latest
steps:
- name: 'Download artifact'
id: download_artifact
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const allArtifacts = await github.rest.actions.listWorkflowRunArtifacts({
owner: context.repo.owner,
repo: context.repo.repo,
run_id: context.payload.workflow_run.id,
});
let matchArtifacts = allArtifacts.data.artifacts.filter((artifact) => {
return artifact.name.startsWith('comment_artifacts-');
});
if (matchArtifacts.length > 0)
{
for (const artifact of matchArtifacts) {
const suffix = artifact.name.split('comment_artifacts-')[1]; // Extract the suffix from the artifact name
// Download each matching artifact
let download = await github.rest.actions.downloadArtifact({
owner: context.repo.owner,
repo: context.repo.repo,
artifact_id: artifact.id,
archive_format: 'zip',
});
// Write each artifact to a zip file named after its suffix
const zipFilePath = `${process.env.GITHUB_WORKSPACE}/data-${suffix}.zip`;
fs.writeFileSync(zipFilePath, Buffer.from(download.data));
console.log(`Downloaded and saved artifact: ${artifact.name} to ${zipFilePath}`);
}
}
else
{
core.setOutput('artifact_id', 0);
}
- name: 'Unzip artifact'
run: |
unzip data-*.zip
- name: 'Post artifact download link as comment on PR'
uses: actions/github-script@v7
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
script: |
function updateCommentSection(prComment, marker, body) {
// Create a regular expression to match the section between start and end markers
const sectionRegex = new RegExp(`(<!-- ====startsection ${marker}-->\\n)([\\s\\S]*?)(<!-- ====endsection ${marker}-->)`, 'm');
// Check if the section with the marker exists
if (sectionRegex.test(prComment)) {
// Replace the existing body text between the markers
return prComment.replace(sectionRegex, `$1${body}\n$3`);
} else {
// If the section doesn't exist, append the new section to the end
return prComment.trim() + `\n\n<!-- ====startsection ${marker}-->\n${body}\n<!-- ====endsection ${marker}-->\n`;
}
}
const fs = require('fs');
const path = require('path');
// Read all files matching the pattern 'comment*.json'
const files = fs.readdirSync('.').filter(file => file.startsWith('comment') && file.endsWith('.json'));
if (files.length === 0) {
console.log('No comment*.json files found');
return;
}
// Take the PR number from the first file
const dataSample = JSON.parse(fs.readFileSync(files[0], 'utf8'));
const prNumber = dataSample.pr_number;
const prComments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
const PREFIX = "<!-- !!## stickymarker ##!! -->";
// Find the comment that starts with the specified PREFIX
const comment = prComments.data?.find(c => c.body.startsWith(PREFIX));
let newPrComment;
if (!!comment) {
newPrComment = comment.body;
} else {
newPrComment = PREFIX + "\n";
}
// Loop through all the comment*.json files
for (const file of files) {
try {
const data = JSON.parse(fs.readFileSync(file, 'utf8'));
const marker = data.marker;
const body = data.body;
console.debug(data);
newPrComment = updateCommentSection(newPrComment, marker, body);
} catch (error) {
console.error(`Failed to process file ${file}: ${error.message}`);
}
}
// Update or create the PR comment after processing all the files
if (!!comment) {
// Update the existing comment
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: comment.id,
body: newPrComment
});
} else {
// Create a new comment
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: newPrComment
});
}