-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
* Do not check labels in an action. * Implement get-labels mini-action * Add "check" jobs that, for PRs, verify that * The PR touches relevant jobs * The PR does not have "skip" labels
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Composite action to check if testing for this PR is relevant based on touched paths | ||
|
||
This action is meant to be used inside a PR testing workflow, as | ||
|
||
```yaml | ||
jobs: | ||
check_paths: | ||
steps: | ||
- name: check_relevance | ||
if: ${{ github.event_name == "pull_request" }} | ||
uses: ./.github/actions/check-pr-paths | ||
with: | ||
paths: | | ||
path1 | ||
path2 | ||
testing: | ||
needs: check_paths | ||
if: ${{ github.event_name != "pull_request" || needs.check_paths.outputs.touched == 'true'}} | ||
steps: | ||
- name: testing | ||
``` | ||
Check failure on line 21 in .github/actions/check-pr-paths/README.md GitHub Actions / linterFenced code blocks should be surrounded by blank lines
|
||
The input `paths1 is a list of paths that the action uses to see if | ||
the PR changes any relevant file. The action sets an output `touched`, | ||
Check failure on line 23 in .github/actions/check-pr-paths/README.md GitHub Actions / linterSpaces inside code span elements
|
||
which is `true` if the PR touches any of the listed paths and `false` | ||
Check failure on line 24 in .github/actions/check-pr-paths/README.md GitHub Actions / linterSpaces inside code span elements
|
||
otherwise. | ||
|
||
This action allows to skip an entire workflow, but, unlike using a 'paths' filter at the | ||
workflow trigger level, it ensures that the workflow DOES run, | ||
hence avoiding the issue where certain checks are not present on the PR. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
name: check-pr-paths | ||
description: 'Check that the PR modifies relevant files' | ||
inputs: | ||
paths: | ||
description: 'List of paths that are considered relevant' | ||
required: true | ||
default: '' | ||
token: | ||
description: 'GitHub token for authentication' | ||
required: true | ||
outputs: | ||
touched: | ||
description: 'Output indicating if this testing for PR is relevant' | ||
value: ${{ steps.check_paths.outputs.value }} | ||
|
||
# Note: inputs are available as env vars in the shell run steps, convertet to uppercase | ||
runs: | ||
using: "composite" | ||
steps: | ||
- name: check_paths | ||
shell: bash | ||
env: | ||
PR_NUMBER: ${{ github.event.number }} | ||
GITHUB_REPO: ${{ github.repository }} | ||
TOKEN: ${{ inputs.token }} | ||
run: | | ||
# Convert the input paths into an array, and create a regex from it | ||
IFS=$'\n' read -r -a paths <<< "$INPUT_PATHS" | ||
pattern=$(IFS=\|; echo "${paths[*]}") | ||
# Use the GitHub API to get the list of changed files | ||
response=$(curl -s -H "Authorization: token $TOKEN" \ | ||
"https://api.github.com/repos/$GITHUB_REPO/pulls/$PR_NUMBER/files") | ||
# Extract filenames using grep and sed | ||
changed_files=$(echo "$response" | grep -o '"filename": *"[^"]*"' | sed 's/"filename": *//; s/"//g') | ||
# Check for matches and echo the matching files | ||
matching_files=$(echo "$changed_files" | grep -E "^($pattern)" || echo "") | ||
if [[ -n "$matching_files" ]]; then | ||
echo "Found relevant files: $matching_files" # Print the matching files | ||
echo "touched=true" >> $GITHUB_OUTPUT | ||
else | ||
echo "No relevant files touched by this PR." | ||
echo "touched=false" >> $GITHUB_OUTPUT | ||
fi |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Composite action to retrieve labels of a PR if event is `pull_request` | ||
|
||
This action is meant to be used inside a PR testing workflow, as | ||
|
||
```yaml | ||
jobs: | ||
get_labels: | ||
steps: | ||
- name: get_labels | ||
uses: ./.github/actions/get-labels | ||
testing: | ||
needs: get_labels | ||
if: ${{ github.event_name != "pull_request" || | ||
!contains(needs.get_labels.outputs.labels,"label1" || | ||
!contains(needs.get_labels.outputs.labels,"label1" }} | ||
``` | ||
Check failure on line 16 in .github/actions/get-labels/README.md GitHub Actions / linterFenced code blocks should be surrounded by blank lines
|
||
The action sets the output `labels`, which is a comma-separated list | ||
containing the PR labels. If the event that triggered the workflow is | ||
not a PR, the output will be an empty string |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: get-labels | ||
description: 'Check that the PR modifies does not have skip labels' | ||
outputs: | ||
labels: | ||
description: 'Comma-separated list of labels on the PR or empty string (if not a PR event)' | ||
value: ${{ steps.get_labels.outputs.labels }} | ||
|
||
# Note: inputs are available as env vars in the shell run steps, convertet to uppercase | ||
runs: | ||
using: "composite" | ||
steps: | ||
- name: get_labels | ||
shell: sh | ||
env: | ||
PR_LABELS: ${{ toJson(github.event.pull_request.labels.*.name) }} # Pass PR label names as JSON | ||
run: | | ||
echo 'labels=\"$(echo \"$PR_LABELS\" | jq -r "join(\",\")")\"' >> $GITHUB_OUTPUT |