summaryrefslogtreecommitdiff
path: root/.github/workflows/coverage_comment.yml
blob: 287cc558b50b51ad85549ff0fdddf44813eb9fa5 (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
name: Coverage Comment

on:
  workflow_run:
    workflows: ["Coverage Tests"]
    types:
      - completed

permissions:
  contents: read
  actions: read
  pull-requests: write

jobs:
  comment:
    if: >
      github.event.workflow_run.event == 'pull_request' &&
      github.event.workflow_run.conclusion == 'success' &&
      github.repository == 'django/django'
    name: Post Coverage Comment
    runs-on: ubuntu-latest
    timeout-minutes: 60
    steps:
      - name: Download coverage artifacts
        uses: actions/download-artifact@v4
        with:
          name: coverage-artifacts
          github-token: ${{ secrets.GITHUB_TOKEN }}
          run-id: ${{ github.event.workflow_run.id }}

      - name: Read PR number
        id: pr
        run: |
          pr_number=$(cat pr_number.txt)
          echo "number=$pr_number" >> $GITHUB_OUTPUT

      - name: Post/update PR comment
        env:
          PR_NUMBER: ${{ steps.pr.outputs.number }}
        uses: actions/github-script@v8
        with:
          script: |
            const fs = require('fs');
            const reportPath = 'diff-cover-report.md';
            let body = 'No coverage data available.';
            if (fs.existsSync(reportPath)) {
              body = fs.readFileSync(reportPath, 'utf8');
              const divider = '-------------';
              // Start after the second divder, where changed files are listed.
              const start = body.indexOf(divider, 1);
              body = body.substring(start);
            }
            const commentBody = (
              '#### Uncovered lines in changed files\n\n```\n' + body + '```\n' +
              + '**Note:** Missing lines are warnings only. Some database-specific lines may not be measured. [More information](https://docs.djangoproject.com/en/dev/internals/contributing/writing-code/unit-tests/#code-coverage-on-pull-requests)'
            );

            const prNumber = parseInt(process.env.PR_NUMBER);
            if (isNaN(prNumber)) {
              core.setFailed('PR number is not available or invalid.');
              return;
            }
            
            const { data: comments } = await github.rest.issues.listComments({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: prNumber,
            });
            
            for (const c of comments) {
              if ((c.body || '').includes('📊 Coverage Report for Changed Files')) {
                await github.rest.issues.deleteComment({
                  owner: context.repo.owner,
                  repo: context.repo.repo,
                  comment_id: c.id,
                });
              }
            }

            await github.rest.issues.createComment({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: prNumber,
              body: commentBody,
            });