llvm-project

Форк
0
/
issue-write.yml 
148 строк · 5.5 Кб
1
name: Comment on an issue
2

3
on:
4
  workflow_run:
5
    workflows:
6
      - "Check code formatting"
7
      - "Check for private emails used in PRs"
8
      - "PR Request Release Note"
9
    types:
10
      - completed
11

12
permissions:
13
  contents: read
14

15
jobs:
16
  pr-comment:
17
    runs-on: ubuntu-latest
18
    permissions:
19
      pull-requests: write
20
    if: >
21
      github.event.workflow_run.event == 'pull_request' &&
22
      (
23
        github.event.workflow_run.conclusion == 'success' ||
24
        github.event.workflow_run.conclusion == 'failure'
25
      )
26
    steps:
27
      - name: 'Download artifact'
28
        uses: actions/download-artifact@6b208ae046db98c579e8a3aa621ab581ff575935 # v4.1.1
29
        with:
30
          github-token: ${{ secrets.ISSUE_WRITE_DOWNLOAD_ARTIFACT }}
31
          run-id: ${{ github.event.workflow_run.id }}
32
          name: workflow-args
33

34
      - name: 'Comment on PR'
35
        uses: actions/github-script@v3
36
        with:
37
          github-token: ${{ secrets.GITHUB_TOKEN }}
38
          script: |
39
            var fs = require('fs');
40
            const comments = JSON.parse(fs.readFileSync('./comments'));
41
            if (!comments || comments.length == 0) {
42
              return;
43
            }
44

45
            let runInfo = await github.actions.getWorkflowRun({
46
              owner: context.repo.owner,
47
              repo: context.repo.repo,
48
              run_id: context.payload.workflow_run.id
49
            });
50

51
            console.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.
61
            const gql_query = `
62
              query($repo_owner : String!, $repo_name : String!, $branch: String!) {
63
                repository(owner: $repo_owner, name: $repo_name) {
64
                  ref (qualifiedName: $branch) {
65
                    associatedPullRequests(first: 100) {
66
                      nodes {
67
                        baseRepository {
68
                          owner {
69
                            login
70
                          }
71
                        }
72
                        number
73
                        state
74
                      }
75
                    }
76
                  }
77
                }
78
              }
79
            `
80
            const gql_variables = {
81
              repo_owner: runInfo.data.head_repository.owner.login,
82
              repo_name: runInfo.data.head_repository.name,
83
              branch: runInfo.data.head_branch
84
            }
85
            const gql_result = await github.graphql(gql_query, gql_variables);
86
            console.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.
92
            if (!gql_result.repository.ref) {
93
              console.log("Ref has been deleted");
94
              return;
95
            }
96
            console.log(gql_result.repository.ref.associatedPullRequests.nodes);
97

98
            var pr_number = 0;
99
            gql_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.
104
              if (pr.baseRepository.owner.login = context.repo.owner && pr.number > pr_number) {
105
                pr_number = pr.number;
106
              }
107
            });
108
            if (pr_number == 0) {
109
              console.log("Error retrieving pull request number");
110
              return;
111
            }
112
            
113
            await comments.forEach(function (comment) {
114
              if (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.
118
                github.issues.getComment({
119
                  owner: context.repo.owner,
120
                  repo: context.repo.repo,
121
                  comment_id: comment.id
122
                }).then((old_comment) => {
123
                  console.log(old_comment);
124
                  if (old_comment.data.user.login != "github-actions[bot]") {
125
                    console.log("Invalid comment id: " + comment.id);
126
                    return;
127
                  }
128
                  github.issues.updateComment({
129
                    owner: context.repo.owner,
130
                    repo: context.repo.repo,
131
                    issue_number: pr_number,
132
                    comment_id: comment.id,
133
                    body: comment.body
134
                  });
135
                });
136
              } else {
137
                github.issues.createComment({
138
                  owner: context.repo.owner,
139
                  repo: context.repo.repo,
140
                  issue_number: pr_number,
141
                  body: comment.body
142
                });
143
              }
144
            });
145

146
      - name: Dump comments file
147
        if: always()
148
        run: cat comments
149

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.