diff --git a/.github/workflows/_get_pr_id.yml b/.github/workflows/_get_pr_id.yml new file mode 100644 index 00000000..78b56d1c --- /dev/null +++ b/.github/workflows/_get_pr_id.yml @@ -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 + diff --git a/.github/workflows/test_get_pr_id.yml b/.github/workflows/test_get_pr_id.yml new file mode 100644 index 00000000..7886d890 --- /dev/null +++ b/.github/workflows/test_get_pr_id.yml @@ -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