summaryrefslogtreecommitdiff
path: root/.github/workflows/benchmark-comment.yml
blob: f45b188e7dbc2afebc334f5faf5d100d74945cb4 (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
name: Benchmark Comment

on:
  workflow_run:
    workflows: [Benchmark]
    types: [completed]

jobs:
  comment-pr:
    name: Post benchmark comment
    # Only run when triggered by a pull_request event and the benchmark succeeded.
    if: >
      github.event.workflow_run.conclusion == 'success' &&
      github.event.workflow_run.event == 'pull_request'
    runs-on: ubuntu-latest

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

    steps:
      - uses: actions/checkout@v4

      - name: Download benchmark results
        uses: actions/download-artifact@v4
        with:
          run-id: ${{ github.event.workflow_run.id }}
          name: benchmark-results
          path: current-results
          github-token: ${{ secrets.GITHUB_TOKEN }}

      - name: Download PR number
        uses: actions/download-artifact@v4
        with:
          run-id: ${{ github.event.workflow_run.id }}
          name: pr-number
          path: .
          github-token: ${{ secrets.GITHUB_TOKEN }}

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

      - name: Fetch baseline from benchmark-data release
        run: |
          gh release download benchmark-data \
            --repo atlarge-research/opendc \
            -A benchmark-history.json \
            -O baseline.json \
            || { echo "Warning: Could not fetch baseline, using empty baseline"; echo "[]" > baseline.json; }
        env:
          GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

      - name: Generate comparison comment
        run: |
          python3 .github/scripts/compare_benchmarks.py \
            current-results/results.json \
            baseline.json \
            > comment.md

      - name: Post or update PR comment
        uses: actions/github-script@v7
        with:
          script: |
            const fs = require('fs');
            const body = fs.readFileSync('comment.md', 'utf8');
            const marker = '<!-- opendc-benchmark-comment -->';
            const fullBody = marker + '\n' + body;
            const prNumber = parseInt(fs.readFileSync('pr-number.txt', 'utf8').trim());

            core.info(`Posting comment to ${context.repo.owner}/${context.repo.repo} PR #${prNumber}`);
            core.info(`Comment body length: ${body.length} chars`);

            const { data: comments } = await github.rest.issues.listComments({
              owner: context.repo.owner,
              repo: context.repo.repo,
              issue_number: prNumber,
            });

            const existing = comments.find(c => c.body.includes(marker));
            if (existing) {
              core.info(`Updating existing comment ${existing.id}`);
              await github.rest.issues.updateComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                comment_id: existing.id,
                body: fullBody,
              });
            } else {
              core.info('Creating new comment');
              await github.rest.issues.createComment({
                owner: context.repo.owner,
                repo: context.repo.repo,
                issue_number: prNumber,
                body: fullBody,
              });
            }