forked from metatensor/metatensor
-
Notifications
You must be signed in to change notification settings - Fork 0
100 lines (86 loc) · 4.13 KB
/
comment-download.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
name: Comment on pull request
on:
workflow_run:
workflows: ['Documentation', 'Build Python wheels']
types: [completed]
permissions:
pull-requests: write
jobs:
pr_comment:
if: github.event.workflow_run.event == 'pull_request' && github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- uses: actions/github-script@v7
with:
script: |
async function getArtifactLink(artifactName, owner, repo, run_id) {
// get the list of artifacts
const artifacts = await github.paginate(
github.rest.actions.listWorkflowRunArtifacts, {owner, repo, run_id}
);
if (!artifacts.length) {
return core.error(`No artifacts found`);
}
for (const artifact of artifacts) {
if (artifact.name == artifactName) {
return `https://nightly.link/${owner}/${repo}/actions/artifacts/${artifact.id}.zip`;
}
}
return core.error(`failed to find ${artifactName} artifact`);
}
let link;
let body_message;
const {owner, repo} = context.repo;
const run_id = ${{github.event.workflow_run.id}};
const workflow_name = "${{github.event.workflow_run.name}}";
console.info(`${workflow_name} triggered this run`)
// Find the PR with the right sha, see https://github.com/orgs/community/discussions/25220
const response = await github.rest.search.issuesAndPullRequests({
q: 'repo:${{ github.repository }} is:pr sha:${{ github.event.workflow_run.head_sha }}',
per_page: 1,
})
const items = response.data.items
if (items.length < 1) {
return core.error("No matching pull requests found");
}
const pull_number = items[0].number;
console.info("Pull request number is", pull_number)
if (workflow_name == 'Documentation') {
link = await getArtifactLink('docs', owner, repo, run_id);
// Add horizontal line for docs only because assuming this is one
// triggered first. The wheels building should take much longer...
body_message = `\n----\n 📚 [Download documentation preview for this pull-request](${link})\n`;
} else { // wheels
link = await getArtifactLink('wheels', owner, repo, run_id);
body_message = `⚙️ [Download Python wheels for this pull-request (you can install these with pip)](${link})\n`;
}
const MESSAGE_SEPARATOR_START = `\r\n\r\n<!-- download-section ${workflow_name} start -->\r\n`;
const MESSAGE_SEPARATOR_END = `\r\n<!-- download-section ${workflow_name} end -->`;
const { data: pull } = await github.rest.pulls.get({
owner: owner,
repo: repo,
pull_number: pull_number,
});
let body = "";
if (pull.body) {
if (pull.body.indexOf(MESSAGE_SEPARATOR_START) === -1) {
// First time updating this description
body = pull.body + MESSAGE_SEPARATOR_START + body_message + MESSAGE_SEPARATOR_END;
}
else {
// we already updated this description before
body = pull.body.slice(0, pull.body.indexOf(MESSAGE_SEPARATOR_START));
body = body + MESSAGE_SEPARATOR_START + body_message + MESSAGE_SEPARATOR_END;
body = body + pull.body.slice(pull.body.indexOf(MESSAGE_SEPARATOR_END) + MESSAGE_SEPARATOR_END.length);
}
}
else {
// Pull Request description is empty
body = MESSAGE_SEPARATOR_START + body_message + MESSAGE_SEPARATOR_END;
}
github.rest.pulls.update({
owner: owner,
repo: repo,
pull_number: pull_number,
body: body,
});