Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add atomic PR ID job - Callable Workflow #211

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions .github/workflows/_get_pr_id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
name: Get PR ID

on:
workflow_call:
inputs:
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/${{ github.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

76 changes: 76 additions & 0 deletions .github/workflows/test_get_pr_id.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
---
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:
main_commit:
uses: .github/workflows/_get_pr_id.yml
with:
commit-sha: "7d46c75af91adbfdfc70689f4d8b3405b26bda6b"


branch_commit:
uses: .github/workflows/_get_pr_id.yml
with:
commit-sha: "f30be53c2a5b3d61928c0f41a2e25605a9901d6a"


dne_commit:
uses: .github/workflows/_get_pr_id.yml
with:
commit-sha: "f30be53c2a5b3d61928c0f41a2e25605a9901d6b"


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

test_branch_pr:
name: Test Get PR ID - empty for non-main commit
runs-on: ubuntu-22.04
needs:
- branch_commit
steps:
- name: Assert
env:
ID: ${{ needs.branch_commit.outputs.id }}
run: |
if [[ "$ID" != "" ]]; then
exit 1
fi
exit 0


test_dne_commit:
name: Test Get PR ID - empty for non-existent commit
runs-on: ubuntu-22.04
needs:
- dne_commit
steps:
- name: Assert
env:
ID: ${{ needs.dne_commit.outputs.id }}
run: |
if [[ "$ID" != "" ]]; then
exit 1
fi
exit 0