|
| 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 "$ODY_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 comment post: '$FILE' is empty." |
| 57 | + fi |
0 commit comments