Github Tutorial #4021
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: Labeling new issue | |
on: | |
issues: | |
types: ['opened','edited'] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
# Checkout Repo | |
- name: Checkout Repo | |
uses: actions/checkout@v4 | |
with: | |
sparse-checkout: | | |
.github/workflows/auto-label.json5 | |
.github/workflows/excluded_labels.txt | |
sparse-checkout-cone-mode: false | |
# Check Issue Body has text, set unresolved label if false | |
- name: Check Issue Body | |
id: check_issue_body | |
run: | | |
ISSUE_BODY=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}" \ | |
| jq -r '.body') | |
if [ -z "$ISSUE_BODY" ] || [ "$ISSUE_BODY" == "null" ]; then | |
echo "HAS_TEXT=false" >> $GITHUB_ENV | |
curl -s -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github+json" \ | |
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/labels" \ | |
-d '{"labels":["Unresolved"]}' | |
curl -s -X POST -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
-H "Accept: application/vnd.github+json" \ | |
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments" \ | |
-d '{"body": "Your issue is empty. Please follow the rules of the task or you will be disqualified."}' | |
else | |
echo "HAS_TEXT=true" >> $GITHUB_ENV | |
fi | |
# Run Auto-labeler if theres body text to check | |
- name: Assign Labels | |
if: env.HAS_TEXT == 'true' | |
uses: Renato66/auto-label@v3 | |
with: | |
repo-token: ${{ secrets.GITHUB_TOKEN }} | |
# Fetch updated issue details | |
- name: Fetch Updated Issue Details | |
uses: octokit/[email protected] | |
with: | |
route: GET /repos/${{ github.repository }}/issues/${{ github.event.issue.number }} | |
token: ${{ secrets.GITHUB_TOKEN }} | |
# Get labels from issue and filter out excluded labels | |
- name: Get Filtered Label Count | |
id: get_labels | |
run: | | |
EXCLUDED_LABELS=$(cat .github/workflows/excluded_labels.txt) | |
LABELS_COUNT=$(curl -s -H "Authorization: token ${{ secrets.GITHUB_TOKEN }}" \ | |
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.issue.number }}" \ | |
| jq --argjson excluded "$EXCLUDED_LABELS" \ | |
'[.labels[] | select(.name as $n | $excluded | index($n) | not) | .name] | length') | |
echo "LABELS_COUNT=$LABELS_COUNT" >> $GITHUB_ENV | |
# Check label count | |
- name: Check Label Count | |
id: check_label_count | |
run: | | |
echo "Labels count is: $LABELS_COUNT" | |
if [ "$LABELS_COUNT" -gt 0 ]; then | |
echo "LABELS_SET=true" >> $GITHUB_ENV | |
else | |
echo "LABELS_SET=false" >> $GITHUB_ENV | |
fi | |
# Close the issue | |
- name: Close Issue | |
id: close_issue | |
if: ${{ env.LABELS_SET == 'true' }} | |
uses: octokit/[email protected] | |
with: | |
route: PATCH /repos/${{ github.repository }}/issues/${{ github.event.issue.number }} | |
headers: '{"accept":"application/vnd.github+json"}' | |
data: | | |
{ | |
"state": "closed" | |
} | |
token: ${{ secrets.GITHUB_TOKEN }} | |
# Comment Closed Issue | |
- name: Comment Closed Issue | |
if: ${{ steps.close_issue.outcome == 'success' }} | |
uses: octokit/[email protected] | |
with: | |
route: POST /repos/${{ github.repository }}/issues/${{ github.event.issue.number }}/comments | |
headers: '{"accept":"application/vnd.github+json"}' | |
data: | | |
{ | |
"body": "Your issue has been accepted and automatically closed. Please join our Discord (https://discord.gg/XHPtHTDy)! Also head to https://DevDapp.com and log in to complete your profile! Stay tuned for updates so you can keep on earning! " | |
} | |
token: ${{ secrets.GITHUB_TOKEN }} |