Skip to content

Commit f3a87bf

Browse files
committed
Use dd-octo-sts to authenticate in GH for posting PR comments
1 parent d78359a commit f3a87bf

3 files changed

Lines changed: 66 additions & 71 deletions

File tree

.github/actions/pr_comment/action.yml

Lines changed: 0 additions & 59 deletions
This file was deleted.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
name: "Upsert PR Comment with Octo-STS"
2+
description: >
3+
Exchanges OIDC for an Octo-STS GitHub-App token and
4+
creates or updates a single comment on the PR.
5+
6+
inputs:
7+
body-file:
8+
description: "Path to file whose contents become the comment body"
9+
required: true
10+
repo: # optional; defaults to triggering repo
11+
description: "Repository (owner/repo)."
12+
required: false
13+
pr-number: # optional; defaults to triggering PR
14+
description: "Pull-request number."
15+
required: false
16+
17+
runs:
18+
using: "composite"
19+
steps:
20+
# 1. Get installation token from DD-Octo-STS
21+
- name: Obtain Octo-STS token
22+
id: octo-sts
23+
uses: DataDog/dd-octo-sts-action@08f2144903ced3254a3dafec2592563409ba2aa0 # v1.0.1
24+
with:
25+
audience: dd-octo-sts
26+
scope: DataDog/java-profiler
27+
policy: self.pr-comment
28+
29+
# 2. Upsert the comment
30+
- name: Upsert PR comment
31+
env:
32+
GH_TOKEN: ${{ steps.octo-sts.outputs.token }}
33+
BODY_FILE: ${{ inputs['body-file'] }}
34+
REPO: ${{ inputs.repo || github.repository }}
35+
PR: ${{ inputs['pr-number'] || github.event.pull_request.number }}
36+
shell: bash
37+
run: |
38+
if [[ -s "$BODY_FILE" ]]; then
39+
set -e
40+
# find last comment by this actor
41+
# first, build a jq filter that embeds the actor’s login
42+
filter=".[] | select(.user.login == \"${GITHUB_ACTOR}\") | .id"
43+
cid=$(gh api "repos/$REPO/issues/$PR/comments?per_page=100" \
44+
--jq "${filter}" | tail -n1)
45+
46+
if [[ -n "$cid" ]]; then
47+
gh api --method PATCH "repos/$REPO/issues/comments/$cid" \
48+
--raw-field body="$(< "$BODY_FILE")"
49+
echo "✏️ Updated comment $cid"
50+
else
51+
gh api --method POST "repos/$REPO/issues/$PR/comments" \
52+
--raw-field body="$(< "$BODY_FILE")"
53+
echo "💬 Created new comment"
54+
fi
55+
else
56+
echo "⚠️ Skipping empty comment"
57+
fi

.github/workflows/codecheck.yml

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ concurrency:
66

77
on:
88
pull_request:
9+
types: [opened, synchronize, reopened]
910

1011
permissions:
1112
contents: read
1213
pull-requests: write
1314
actions: read
15+
id-token: write
1416

1517
jobs:
1618
scan-build:
@@ -46,13 +48,10 @@ jobs:
4648
id: read-report
4749
run: |
4850
find ddprof-lib/build/reports/scan-build -name 'index.html' | xargs -I {} python .github/scripts/python_utils.py scanbuild_cleanup {} ${HEAD_REF} > comment.html
49-
- name: Post or update PR comment
50-
uses: ./.github/actions/pr_comment
51+
- name: Comment on PR
52+
uses: ./.github/actions/upsert-pr-comment
5153
with:
52-
github-token: ${{ secrets.GITHUB_TOKEN }}
53-
comment-id: "scan-build"
54-
commenter: "pr-comment-scanbuild"
55-
comment-file: 'comment.html'
54+
body-file: comment.html
5655

5756
cppcheck:
5857
if: needs.check-for-pr.outputs.skip != 'true'
@@ -90,13 +89,11 @@ jobs:
9089
path: |
9190
report.html
9291
report.xml
93-
- name: Post or update PR comment
94-
uses: ./.github/actions/pr_comment
92+
- name: Comment on PR
93+
uses: ./.github/actions/upsert-pr-comment
9594
with:
96-
github-token: ${{ secrets.GITHUB_TOKEN }}
97-
comment-id: "cppcheck"
98-
commenter: "pr-comment-cppcheck"
99-
comment-file: 'comment.html'
95+
body-file: comment.html
96+
10097
codeql:
10198
if: needs.check-for-pr.outputs.skip != 'true'
10299
runs-on: ubuntu-latest

0 commit comments

Comments
 (0)