Skip to content

Build Impact Comment #2

Build Impact Comment

Build Impact Comment #2

# Copyright (c) Facebook, Inc. and its affiliates.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
name: Build Impact Comment
# zizmor:disable:dangerous-triggers -- only reads an artifact and posts a comment, no PR code execution.
on:
workflow_run:
workflows: [Build Impact Analysis]
types:
- completed
permissions:
pull-requests: write
issues: write
actions: read
jobs:
post-comment:
if: >
github.event.workflow_run.event == 'pull_request' &&
github.event.workflow_run.conclusion == 'success'
runs-on: ubuntu-latest
steps:
- name: Download comment artifact
uses: actions/download-artifact@v4
with:
name: build-impact-comment
github-token: ${{ github.token }}
run-id: ${{ github.event.workflow_run.id }}
- name: Get PR number
id: pr
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
HEAD_OWNER: ${{ github.event.workflow_run.head_repository.owner.login }}
HEAD_BRANCH: ${{ github.event.workflow_run.head_branch }}
run: |
pr_number=$(gh api \
"/repos/${REPO}/pulls?head=${HEAD_OWNER}:${HEAD_BRANCH}&state=open" \
-q '.[0].number // empty')
if [ -z "$pr_number" ]; then
echo "No open PR found for branch ${HEAD_BRANCH}"
exit 0
fi
echo "number=$pr_number" >> "$GITHUB_OUTPUT"
- name: Post or update PR comment
if: steps.pr.outputs.number
uses: actions/github-script@v7
env:
PR_NUMBER: ${{ steps.pr.outputs.number }}
with:
script: |
const fs = require('fs');
const comment = fs.readFileSync('comment.md', 'utf8');
const marker = '## Build Impact Analysis';
const prNumber = parseInt(process.env.PR_NUMBER, 10);
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.user.login === 'github-actions[bot]' && c.body.includes(marker)
);
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: comment,
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body: comment,
});
}