summaryrefslogtreecommitdiffstats
path: root/.github/workflows/comment_bdf_info.yaml
blob: 2a79fa922d4c522b94df6d041c8b6049fbf609c2 (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
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: Download BDF info
        uses: actions/download-artifact@d3f86a106a0bac45b974a628896c90dbdf5c8093 # v4
        with:
          name: bdf-info
          path: bdf-info
          run-id: ${{ github.event.workflow_run.id }}
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Update pull request comment
        uses: actions/github-script@373c709c69115d41ff229c7e5df9f8788daa9553 # v9
        with:
          script: |
            const fs = require('fs');
            const marker = '<!-- bdf-info -->';
            const pullRequest = context.payload.workflow_run.pull_requests[0];

            if (!pullRequest) {
              core.info('The completed run is not associated with a pull request.');
              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,
              });
            }