Skip to content

Commit

Permalink
initial add of atomic PR ID job
Browse files Browse the repository at this point in the history
  • Loading branch information
joseph-flinn committed Nov 21, 2023
1 parent 7d46c75 commit f30be53
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
40 changes: 40 additions & 0 deletions .github/workflows/_get_pr_id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
---
name: Get PR ID

on:
workflow_call:
inputs:
repository:
type: string
required: true
commit-sha:
type: string
required: true
outputs:
id:
description: "ID of PR that was associated to the merge to main that triggered the job"
value: ${{ jobs.pr.outputs.id }}

jobs:
pr:
name: Get PR ID
runs-on: ubuntu-22.04
outputs:
id: ${{ steps.pr.outputs.id }}
steps:
- name: Get PR ID
id: pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
commit_message=$(
curl -s -L \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/${{ inputs.repository }}/commits/${{ inputs.commit-sha }} | \
jq -r ".commit.message"
)
ID=$(echo "$commit_message" | head -1 | grep -o "(#.*)" | grep -o "[0-9]*")
echo "id=$ID" >> $GITHUB_OUTPUT
33 changes: 33 additions & 0 deletions .github/workflows/test_get_pr_id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
name: Test - Get PR ID

on:
pull_request:
branches:
- main
paths:
- .github/workflows/_get_pr_id.yml
- .github/workflows/test_get_pr_id.yml

jobs:
pr:
uses: .github/workflows/_get_pr_id.yml
with:
repository: "bitwarden/gh-actions"
commit-sha: "7d46c75af91adbfdfc70689f4d8b3405b26bda6b"

test_pr:
name: Test Get PR ID
runs-on: ubuntu-22.04
needs:
- pr
steps:
- name: Assert PR ID
env:
ID: ${{ needs.pr.outputs.id }}
run: |
if [[ "$ID" != "196" ]]; then
exit 1
fi
exit 0

0 comments on commit f30be53

Please sign in to comment.