forked from Kaliiiiiiiiii-Vinyzu/patchright
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpost_build_artifacts_comment.yml
More file actions
112 lines (98 loc) · 3.83 KB
/
Copy pathpost_build_artifacts_comment.yml
File metadata and controls
112 lines (98 loc) · 3.83 KB
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
name: Post Build Artifacts Comment
on:
workflow_run:
workflows: ["Build Test Package on PR"]
types: [completed]
permissions:
contents: read
actions: read
issues: write
pull-requests: write
concurrency:
group: build-artifacts-comment-${{ github.repository }}-${{ github.event.workflow_run.head_repository.full_name }}-${{ github.event.workflow_run.head_branch }}
cancel-in-progress: true
jobs:
comment:
if: github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Create or update PR comment
uses: actions/github-script@v7
with:
script: |
const { owner, repo } = context.repo;
const runId = context.payload.workflow_run.id;
const headSha = context.payload.workflow_run.head_sha;
const headBranch = context.payload.workflow_run.head_branch;
const headRepoOwner = context.payload.workflow_run.head_repository?.owner?.login;
// Resolve PR number (payload -> fallback by head owner:branch).
const prs = context.payload.workflow_run.pull_requests || [];
let prNumber = prs[0]?.number;
if (!prNumber) {
const head = `${headRepoOwner || owner}:${headBranch}`;
const byHead = await github.rest.pulls.list({
owner,
repo,
state: 'open',
head,
per_page: 1,
});
prNumber = byHead.data?.[0]?.number;
}
if (!prNumber) {
core.setFailed(`Could not resolve a pull request for workflow_run ${runId} (head_sha=${headSha}, head_branch=${headBranch}).`);
return;
}
// Find artifact id.
const artifacts = await github.paginate(github.rest.actions.listWorkflowRunArtifacts, {
owner,
repo,
run_id: runId,
per_page: 100,
});
const artifact = artifacts.find(a => a.name === 'build-artifacts' && !a.expired);
if (!artifact) {
core.setFailed(`Artifact 'build-artifacts' not found (or expired) for run ${runId}`);
return;
}
const marker = '<!-- patchright-artifacts -->';
const body = [
marker,
'**Patchright Driver Build**',
'__Do not download or use these artifacts in production! They are only for testing purposes on this Pull Request.__',
'',
'**Download**',
`- Download the PRs driver build via \`build-artifacts\`:\n [Download Driver Build Artifacts](${context.serverUrl}/${context.repo.owner}/${context.repo.repo}/actions/runs/${runId}/artifacts/${artifact.id})`,
'',
'**Debug info**',
`- Artifact ID: \`${artifact.id}\``,
`- Head SHA: \`${headSha}\``,
'',
].join('\n');
// Find existing bot comment with marker.
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number: Number(prNumber),
per_page: 100,
});
const existing = comments.find(c =>
c.user?.login === 'github-actions[bot]' &&
typeof c.body === 'string' &&
c.body.includes(marker)
);
if (existing) {
await github.rest.issues.updateComment({
owner,
repo,
comment_id: existing.id,
body,
});
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number: Number(prNumber),
body,
});
}