summaryrefslogtreecommitdiffstats
path: root/.github/workflows/comment_bdf_info.yaml
blob: e2f63b3c2a942ccff4ca62905ffc4b5bf3310d6c (plain)
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
name: Comment BDF info

on:
  workflow_run:
    workflows: [BDF info]
    types: [completed]

permissions:
  actions: read
  pull-requests: write

jobs:
  comment:
    if: >-
      ${{ github.event.workflow_run.event == 'pull_request' &&
          github.event.workflow_run.conclusion == 'success' }}
    runs-on: ubuntu-latest

    steps:
      - name: Check for BDF info
        id: artifact
        uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553 # v9
        with:
          script: |
            const artifacts = await github.paginate(
              github.rest.actions.listWorkflowRunArtifacts,
              {
                owner: context.repo.owner,
                repo: context.repo.repo,
                run_id: context.payload.workflow_run.id,
                per_page: 100,
              },
            );
            core.setOutput(
              'exists',
              artifacts.some((artifact) => artifact.name === 'bdf-info'),
            );

      - name: Download BDF info
        if: ${{ steps.artifact.outputs.exists == 'true' }}
        uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v5
        with:
          name: bdf-info
          path: bdf-info
          run-id: ${{ github.event.workflow_run.id }}
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Update pull request comment
        if: ${{ steps.artifact.outputs.exists == 'true' }}
        uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553 # v9
        with:
          script: |
            const fs = require('fs');
            const marker = '<!-- bdf-info -->';
            const workflowRun = context.payload.workflow_run;
            let pullRequest = workflowRun.pull_requests[0];

            if (!pullRequest) {
              const headRepository = workflowRun.head_repository;

              if (!headRepository) {
                core.info('The completed run has no head repository.');
                return;
              }

              const pullRequests = await github.paginate(
                github.rest.pulls.list,
                {
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  state: 'open',
                  head: `${headRepository.owner.login}:${workflowRun.head_branch}`,
                  per_page: 100,
                },
              );
              pullRequest = pullRequests.find(
                (candidate) => candidate.head.sha === workflowRun.head_sha,
              );
            }

            if (!pullRequest) {
              core.info('No open pull request matches the completed run.');
              return;
            }

            const maxOutputLength = 60000;
            let output = fs.readFileSync('bdf-info/bdf-info.md', 'utf8');
            let truncated = false;

            if (output.length > maxOutputLength) {
              output = output.slice(0, maxOutputLength);
              truncated = true;
            }

            // Prevent decoder output from ending the Markdown code block.
            output = output.replaceAll('```', '``\\`');
            const truncationNotice = truncated
              ? '\n\n[Output truncated to fit in a pull request comment.]'
              : '';
            const body = `${marker}
            ## BDF information

            \`\`\`text
            ${output}${truncationNotice}
            \`\`\``;
            const comments = await github.paginate(
              github.rest.issues.listComments,
              {
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: pullRequest.number,
                per_page: 100,
              },
            );
            const previousComment = comments.find(
              (comment) => comment.user?.login === 'github-actions[bot]' &&
                comment.body?.startsWith(marker),
            );

            const params = {
              owner: context.repo.owner,
              repo: context.repo.repo,
              body,
            };

            if (previousComment) {
              await github.rest.issues.updateComment({
                ...params,
                comment_id: previousComment.id,
              });
            } else {
              await github.rest.issues.createComment({
                ...params,
                issue_number: pullRequest.number,
              });
            }