-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor PR Body regexing into independent GH Action
- Loading branch information
Showing
2 changed files
with
60 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
name: PR Body Parse | ||
description: Parse a value from the body of the relevant PR for a specific key | ||
|
||
inputs: | ||
key-regex: | ||
description: "The regex for the key identifying the value to return, e.g. BRIEFCASE[-_]*REPO" | ||
required: true | ||
testing-pr-body: | ||
description: "Override value for body of PR; only for testing." | ||
required: false | ||
|
||
outputs: | ||
extracted-value: | ||
value: ${{ steps.parse.outputs.extracted-value }} | ||
description: "The value extracted from the PR's body for the specified key." | ||
|
||
runs: | ||
using: composite | ||
steps: | ||
- name: Parse PR Body for ${{ inputs.key-regex }} | ||
id: parse | ||
env: | ||
GITHUB_TOKEN: ${{ github.token }} | ||
shell: bash | ||
run: | | ||
# Retrieve PR Body (only PRs will have a value for github.event.pull_request.number) | ||
PR_BODY="${{ inputs.testing-pr-body }}" | ||
if [[ -z "${PR_BODY}" && -n "${{ github.event.pull_request.number }}" ]]; then | ||
PR_BODY=$(gh api repos/${{ github.repository }}/pulls/${{ github.event.pull_request.number }} --jq '.body') | ||
fi | ||
printf "::group::Retrieved PR Body\n%s\n::endgroup::\n" "${PR_BODY}" | ||
# Parse value with given regex | ||
EXTRACTED_VALUE=$(printf "%s" "${PR_BODY}" | perl -wlne '/${{ inputs.key-regex }}:\s*\K\S+/i and print $&') | ||
# Expose value as output | ||
echo "extracted-value=${EXTRACTED_VALUE}" | tee -a ${GITHUB_OUTPUT} |