Clothing Classifier #2
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
name: PR Issue Number and Label Handler | |
on: | |
pull_request: | |
types: [opened, edited] | |
schedule: | |
- cron: '0 0 * * *' # Run the script daily to check for first-time activation | |
jobs: | |
check-issue-number: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Check if issue number is present | |
id: check_issue | |
run: | | |
PR_BODY=$(jq -r .pull_request.body "$GITHUB_EVENT_PATH") | |
if [[ "$PR_BODY" =~ #[0-9]+ ]]; then | |
ISSUE_NUMBER=$(echo "$PR_BODY" | grep -oE '#[0-9]+' | sed 's/#//') | |
echo "::set-output name=issue_number::$ISSUE_NUMBER" | |
else | |
echo "::set-output name=issue_number::" | |
fi | |
- name: Add comment if no issue number is found | |
if: steps.check_issue.outputs.issue_number == '' | |
run: | | |
PR_URL=$(jq -r .pull_request.html_url "$GITHUB_EVENT_PATH") | |
curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-X POST -d '{"body":"Please add an issue number in the format #<issue_number> to the PR description."}' \ | |
"$PR_URL/comments" | |
- name: Get labels from issue | |
if: steps.check_issue.outputs.issue_number != '' | |
run: | | |
ISSUE_NUMBER=${{ steps.check_issue.outputs.issue_number }} | |
ISSUE_URL="https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER" | |
ISSUE_LABELS=$(curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$ISSUE_URL" | jq -r '.labels[].name') | |
echo "Issue labels: $ISSUE_LABELS" | |
echo "::set-output name=issue_labels::$ISSUE_LABELS" | |
- name: Apply labels to PR | |
if: steps.check_issue.outputs.issue_labels != '' | |
run: | | |
PR_URL=$(jq -r .pull_request.issue_url "$GITHUB_EVENT_PATH") | |
curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-X POST -d "{\"labels\":[$(echo ${{ steps.check_issue.outputs.issue_labels }} | jq -R . | jq -s .)]}" \ | |
"$PR_URL/labels" | |
- name: Run for open PRs (only first time) | |
if: github.event_name == 'schedule' | |
run: | | |
OPEN_PRS_URL="https://api.github.com/repos/${{ github.repository }}/pulls?state=open" | |
OPEN_PRS=$(curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$OPEN_PRS_URL") | |
for PR in $(echo "$OPEN_PRS" | jq -r '.[].url'); do | |
PR_BODY=$(curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$PR" | jq -r '.body') | |
if [[ "$PR_BODY" =~ #[0-9]+ ]]; then | |
ISSUE_NUMBER=$(echo "$PR_BODY" | grep -oE '#[0-9]+' | sed 's/#//') | |
ISSUE_URL="https://api.github.com/repos/${{ github.repository }}/issues/$ISSUE_NUMBER" | |
ISSUE_LABELS=$(curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" "$ISSUE_URL" | jq -r '.labels[].name') | |
curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-X POST -d "{\"labels\":[$(echo $ISSUE_LABELS | jq -R . | jq -s .)]}" \ | |
"$PR/labels" | |
else | |
curl -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
-X POST -d '{"body":"Please add an issue number in the format #<issue_number> to the PR description."}' \ | |
"$PR/comments" | |
fi | |
done |