llvm-project
148 строк · 5.5 Кб
1name: Comment on an issue
2
3on:
4workflow_run:
5workflows:
6- "Check code formatting"
7- "Check for private emails used in PRs"
8- "PR Request Release Note"
9types:
10- completed
11
12permissions:
13contents: read
14
15jobs:
16pr-comment:
17runs-on: ubuntu-latest
18permissions:
19pull-requests: write
20if: >
21github.event.workflow_run.event == 'pull_request' &&
22(
23github.event.workflow_run.conclusion == 'success' ||
24github.event.workflow_run.conclusion == 'failure'
25)
26steps:
27- name: 'Download artifact'
28uses: actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935 # v4.1.1
29with:
30github-token: ${{ secrets.ISSUE_WRITE_DOWNLOAD_ARTIFACT }}
31run-id: ${{ github.event.workflow_run.id }}
32name: workflow-args
33
34- name: 'Comment on PR'
35uses: actions/github-script@v3
36with:
37github-token: ${{ secrets.GITHUB_TOKEN }}
38script: |
39var fs = require('fs');
40const comments = JSON.parse(fs.readFileSync('./comments'));
41if (!comments || comments.length == 0) {
42return;
43}
44
45let runInfo = await github.actions.getWorkflowRun({
46owner: context.repo.owner,
47repo: context.repo.repo,
48run_id: context.payload.workflow_run.id
49});
50
51console.log(runInfo);
52
53
54// Query to find the number of the pull request that triggered this job.
55// The associated pull requests are based off of the branch name, so if
56// you create a pull request for a branch, close it, and then create
57// another pull request with the same branch, then this query will return
58// two associated pull requests. This is why we have to fetch all the
59// associated pull requests and then iterate through them to find the
60// one that is open.
61const gql_query = `
62query($repo_owner : String!, $repo_name : String!, $branch: String!) {
63repository(owner: $repo_owner, name: $repo_name) {
64ref (qualifiedName: $branch) {
65associatedPullRequests(first: 100) {
66nodes {
67baseRepository {
68owner {
69login
70}
71}
72number
73state
74}
75}
76}
77}
78}
79`
80const gql_variables = {
81repo_owner: runInfo.data.head_repository.owner.login,
82repo_name: runInfo.data.head_repository.name,
83branch: runInfo.data.head_branch
84}
85const gql_result = await github.graphql(gql_query, gql_variables);
86console.log(gql_result);
87// If the branch for the PR was deleted before this job has a chance
88// to run, then the ref will be null. This can happen if someone:
89// 1. Rebase the PR, which triggers some workflow.
90// 2. Immediately merges the PR and deletes the branch.
91// 3. The workflow finishes and triggers this job.
92if (!gql_result.repository.ref) {
93console.log("Ref has been deleted");
94return;
95}
96console.log(gql_result.repository.ref.associatedPullRequests.nodes);
97
98var pr_number = 0;
99gql_result.repository.ref.associatedPullRequests.nodes.forEach((pr) => {
100
101// The largest PR number is the one we care about. The only way
102// to have more than one associated pull requests is if all the
103// old pull requests are in the closed state.
104if (pr.baseRepository.owner.login = context.repo.owner && pr.number > pr_number) {
105pr_number = pr.number;
106}
107});
108if (pr_number == 0) {
109console.log("Error retrieving pull request number");
110return;
111}
112
113await comments.forEach(function (comment) {
114if (comment.id) {
115// Security check: Ensure that this comment was created by
116// the github-actions bot, so a malicious input won't overwrite
117// a user's comment.
118github.issues.getComment({
119owner: context.repo.owner,
120repo: context.repo.repo,
121comment_id: comment.id
122}).then((old_comment) => {
123console.log(old_comment);
124if (old_comment.data.user.login != "github-actions[bot]") {
125console.log("Invalid comment id: " + comment.id);
126return;
127}
128github.issues.updateComment({
129owner: context.repo.owner,
130repo: context.repo.repo,
131issue_number: pr_number,
132comment_id: comment.id,
133body: comment.body
134});
135});
136} else {
137github.issues.createComment({
138owner: context.repo.owner,
139repo: context.repo.repo,
140issue_number: pr_number,
141body: comment.body
142});
143}
144});
145
146- name: Dump comments file
147if: always()
148run: cat comments
149