Skip to content

Check PR(\#452)

Check PR(\#452) #1

Workflow file for this run

name: Check PR
run-name: Check PR(\#${{ github.event.pull_request.number }})
on:
pull_request:
types:
# Label checks should run on `opened`, `reopened` and `closed`
# but title check should only run on `opened` or `reopened`.
- opened
- reopened
- closed
paths:
# Labels are added based on changes of specific path patterns.
# Make sure the paths are handled when you add a new path patterns.
- 'packages/**'
- '.github/**'
- 'pixi.toml'
- 'docs/**'
concurrency:
# Even if the pull request has same reference, it should also run again if it's a different PR.
group: ${{ github.workflow }}-${{ github.ref }}-${{ github.event.pull_request.number }}
permissions:
# The bot write comments, add labels or update the title of PR.
pull-requests: write
jobs:
check-pr-labels:
name: Labels
runs-on: ubuntu-24.04
env:
PR_NUM: ${{ github.event.pull_request.number }}
outputs:
all_labels: ${{ steps.all-labels.outputs.all_labels }}
new_labels: ${{ steps.new-labels.outputs.new_labels }}
if: ${{ github.event.action == 'opened' || github.event.action == 'closed' || github.event.action == 'reopened' }}
steps:
- uses: actions/checkout@v6
- run: git fetch origin ${{ github.base_ref }}
- id: all-labels
name: Collect all expected labels based on the change.
run: |
DIFFFILES=($(git diff --name-only origin/${{ github.base_ref }}));
labels=($(for diff_file in ${DIFFFILES[@]}; do
if [[ "${diff_file}" =~ ^packages\/([a-zA-Z]+)\/[a-zA-Z]+ ]]; then
echo ${BASH_REMATCH[1]};
elif [[ ${diff_file} =~ ^\.github* || ${diff_file} == "pixi\.toml" ]]; then
echo CI;
elif [[ ${diff_file} =~ ^docs\/* ]]; then
echo documentation;
fi
done | sort -u));
echo "Found labels to add: ${labels[@]}";
echo "all_labels=${labels[@]}" >> "$GITHUB_OUTPUT";
- id: new-labels
env:
all_labels: ${{ steps.all-labels.outputs.all_labels }}
run: |
EXISTING_LABELS='${{ toJson(github.event.pull_request.labels.*.name) }}';
new_labels=();
for label in ${all_labels[@]}; do
if [[ "$EXISTING_LABELS" == *"${label}"* ]]; then
echo "${label} is already added to the current PR.";
else
new_labels+=("${label}");
gh pr edit ${PR_NUM} --add-label ${label};
fi
done
echo "new_labels=${new_labels[@]}" >> "$GITHUB_OUTPUT";
- id: new-labels-report
env:
all_labels: ${{ steps.all-labels.outputs.all_labels }}
new_labels: ${{ steps.new-labels.outputs.new_labels }}
GH_TOKEN: ${{ github.token }}
if: ${{ steps.new-labels.outputs.new_labels }}
run: |
echo "Hi! Your PR was missing some labels 🔖 so I added them." >> pr-comment.md;
echo "It looks like you're working on these packages/features: ${new_labels}" >> pr-comment.md;
gh pr comment ${PR_NUM} -F pr-comment.md;
- id: export-comment
if: ${{ steps.new-labels.outputs.new_labels }}
uses: actions/upload-artifact@v7
with:
path: pr-comment.md
name: pr-comment.md
if-no-files-found: ignore
check-pr-title:
name: Title Prefix
runs-on: ubuntu-24.04
needs:
- check-pr-labels
if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' }} # Only when it's (re)opened.
env:
GH_TOKEN: ${{ github.token }}
PR_NUM: ${{ github.event.pull_request.number }}
all_labels: ${{ needs.check-pr-labels.outputs.all_labels }}
original_title: ${{ github.event.pull_request.title }}
outputs:
title_prefix: ${{ steps.title-prefix.outputs.title_prefix }}
steps:
- uses: actions/checkout@v6
- id: download-comment
uses: actions/download-artifact@v8
continue-on-error: true # pre-commit.md might not exist.
with:
path: ./
name: pr-comment.md
- run: if [[ -f pr-commit.md ]]; then echo "" >> pre-comment.md; echo "Found existing pr-comment file."; cat pr-comment.md; fi
- id: title-prefix
name: Compose title prefix.
run: |
labels=(${all_labels});
# Only one label
if [[ ${#labels[@]} -eq 1 ]]; then
label=${labels[0]};
# Do not drop ess prefix if it's essreduce... cause... just reduce doesn't make sense...
if [[ "${label}" == "essreduce" ]]; then
title_prefix="[${label}]";
# Only use technique part for prefix
elif [[ "${label}" =~ ^ess([a-zA-Z]+)$ ]]; then
title_prefix="[${BASH_REMATCH[1]}]";
else
title_prefix="[${label}]";
fi
# Handle ultiple labels
elif [[ ${#labels[@]} -gt 1 ]]; then
ess_labels=();
other_labels=();
label_to_title=();
for label in ${labels[@]}; do
# Do not drop ess prefix if it's essreduce... cause... just reduce doesn't make sense...
if [[ "${label}" == "essreduce" ]]; then
ess_labels+=(${label});
# Only use technique part for prefix
elif [[ "${label}" =~ ^ess([a-zA-Z]+)$ ]]; then
ess_labels+=(${BASH_REMATCH[1]});
else
other_labels+=(${label});
fi
done
# Collect only ess titles
ess_title_prefixes=()
if [[ ${#ess_labels[@]} -eq 1 ]]; then
ess_title_prefixes+=(${ess_labels[0]});
elif [[ ${#ess_labels[@]} -gt 1 ]]; then
ess_title_prefixes+=('ess');
fi
# Combine all title prefixes
label_to_title=(${ess_title_prefixes[@]} ${other_labels[@]});
# Capitalize title prefixes
title_prefixes=();
for title in ${label_to_title[@]}; do
title_prefixes+=(${title^^});
done
title_prefix="[${title_prefixes[@]}]";
fi
echo "title_prefix=${title_prefix}" >> "$GITHUB_OUTPUT";
- id: title-prefix-correct
name: Title already has correct prefix.
if: ${{ startsWith( env.original_title, steps.title-prefix.outputs.title_prefix ) }}
run: echo "Title already has prefix ${title_prefix}."
- id: report-title-prefix-unexpected
name: Title already has prefix but not expected one.
if: ${{ !startsWith( env.original_title, steps.title-prefix.outputs.title_prefix ) && startsWith( env.original_title, '[' ) }}
run: |
echo "Title already has prefix but not same as the expected one. Reporting in the comment...";
echo "Hi! Your PR has unexpected title. Recommended prefix is: ${title_prefix}." >> pr-comment.md;
echo "But feel free to use your own...! Bye 👋...!" >> pr-comment.md;
gh pr comment ${PR_NUM} \
--edit-last \
--create-if-none \
-F pr-comment.md;
- id: report-title-prefix-none
name: Add title prefix.
if: ${{ !startsWith( env.original_title, steps.title-prefix.outputs.title_prefix ) && !startsWith( env.original_title, '[' ) }}
env:
title_prefix: ${{ steps.title-prefix.outputs.title_prefix }}
run: |
echo "Title doesn't have expected prefix... adding one...";
gh pr edit ${PR_NUM} --title "${title_prefix} ${original_title}";
echo "Hi! Your PR title didn't have any prefix yet. I added the prefix: ${title_prefix} to the title." >> pr-comment.md;
echo "But feel free to use your own...! Bye 👋...!" >> pr-comment.md;
gh pr comment ${PR_NUM} \
--edit-last \
--create-if-none \
-F pr-comment.md;