Skip to content

[Request]

[Request] #576

name: Submit Notification Icon Adaption
on:
issues:
types: [opened, edited, reopened, closed, unlabeled]
pull_request_target:
types: [closed, reopened]
permissions:
contents: write
issues: write
pull-requests: write
jobs:
process-issue:
if: >-
github.event_name == 'issues' &&
contains(github.event.issue.labels.*.name, 'submit-adaption') &&
!contains(github.event.issue.labels.*.name, 'submit-adaption-done') &&
(github.event.action == 'opened' || github.event.action == 'reopened' ||
(github.event.action == 'edited' &&
github.event.issue.state == 'open' &&
github.actor != 'github-actions[bot]') ||
(github.event.action == 'unlabeled' &&
github.event.label.name == 'submit-adaption-violation' &&
!contains(github.event.issue.labels.*.name, 'submit-adaption-pre')))
runs-on: ubuntu-latest
concurrency:
group: submit-adaption-issue-${{ github.event.issue.number }}
cancel-in-progress: false
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_NUMBER: ${{ github.event.issue.number }}
steps:
- name: Resolve Existing Pull Request
id: existing-pull-request
env:
HEAD_REF: anip/submit-${{ github.event.issue.number }}
ISSUE_ACTION: ${{ github.event.action }}
run: |
pr_data=$(gh pr list --repo "$GITHUB_REPOSITORY" --head "$HEAD_REF" --state all --limit 1 --json number,state --jq '.[] | [.number, .state] | @tsv')
if [[ -z "$pr_data" ]]; then
echo 'found=false' >> "$GITHUB_OUTPUT"
exit 0
fi
read -r pr_number pr_state <<< "$pr_data"
if [[ "$ISSUE_ACTION" == 'reopened' && "$pr_state" == 'CLOSED' ]]; then
gh pr reopen "$pr_number" --repo "$GITHUB_REPOSITORY"
fi
echo 'found=true' >> "$GITHUB_OUTPUT"
echo "number=$pr_number" >> "$GITHUB_OUTPUT"
- uses: actions/checkout@v4
with:
fetch-depth: 0
- uses: actions/setup-node@v4
with:
node-version: 22
- name: Validate Submission and Prepare Files
id: submission
env:
ANIP_FORMATTED_ISSUE_BODY_PATH: ${{ runner.temp }}/submit-adaption-issue.md
run: node .github/scripts/submit-adaption.mjs --apply "$GITHUB_EVENT_PATH"
- name: Mark Invalid Submission
if: steps.submission.outputs.valid == 'false'
env:
VALIDATION_ERRORS: ${{ steps.submission.outputs.errors }}
run: |
gh issue edit "$ISSUE_NUMBER" --add-label submit-adaption-violation
{
echo 'The automated submission check failed:'
echo
echo "$VALIDATION_ERRORS"
echo
echo 'Checking failed, please correct the issue form values, then ask an administrator to remove `submit-adaption-violation` to run the check again.'
} > "$RUNNER_TEMP/submit-adaption-error.md"
gh issue comment "$ISSUE_NUMBER" --body-file "$RUNNER_TEMP/submit-adaption-error.md"
- name: Collapse Issue Icon Resource
if: >-
steps.submission.outputs.valid == 'true' &&
steps.submission.outputs.body_formatted == 'true'
run: gh issue edit "$ISSUE_NUMBER" --body-file "$RUNNER_TEMP/submit-adaption-issue.md"
- name: Create Signed Submission Commit
if: steps.submission.outputs.valid == 'true'
env:
BRANCH: ${{ steps.submission.outputs.branch }}
PACKAGE_NAME: ${{ steps.submission.outputs.package_name }}
SUBMITTER: ${{ github.event.issue.user.login }}
run: |
base_oid=$(git rev-parse HEAD)
if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$BRANCH" >/dev/null 2>&1; then
gh api --method PATCH "repos/$GITHUB_REPOSITORY/git/refs/heads/$BRANCH" -f sha="$base_oid" -F force=true >/dev/null
else
gh api --method POST "repos/$GITHUB_REPOSITORY/git/refs" -f ref="refs/heads/$BRANCH" -f sha="$base_oid" >/dev/null
fi
additions_file="$RUNNER_TEMP/submit-adaption-additions.jsonl"
content_file="$RUNNER_TEMP/submit-adaption-content.base64"
: > "$additions_file"
while IFS= read -r -d '' path; do
base64 -w 0 "$path" > "$content_file"
jq -cn --arg path "$path" --rawfile contents "$content_file" \
'{ path: $path, contents: $contents }' >> "$additions_file"
done < <(git ls-files --modified --others --exclude-standard -z -- icons)
if [[ ! -s "$additions_file" ]]; then
echo 'No icon resource changes were prepared.' >&2
exit 1
fi
query='mutation($input: CreateCommitOnBranchInput!) { createCommitOnBranch(input: $input) { commit { oid } } }'
jq -n \
--arg query "$query" \
--arg repository "$GITHUB_REPOSITORY" \
--arg branch "$BRANCH" \
--arg expectedHeadOid "$base_oid" \
--arg headline "feat(icons): add $PACKAGE_NAME by @$SUBMITTER" \
--slurpfile additions "$additions_file" \
'{
query: $query,
variables: {
input: {
branch: { repositoryNameWithOwner: $repository, branchName: $branch },
expectedHeadOid: $expectedHeadOid,
message: { headline: $headline },
fileChanges: { additions: $additions }
}
}
}' > "$RUNNER_TEMP/submit-adaption-commit.json"
gh api graphql --input "$RUNNER_TEMP/submit-adaption-commit.json" --jq '.data.createCommitOnBranch.commit.oid'
- name: Create or Update Pull Request
if: steps.submission.outputs.valid == 'true'
id: pull-request
env:
APP_LABEL: ${{ steps.submission.outputs.app_label }}
BRANCH: ${{ steps.submission.outputs.branch }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
EXISTING_PR_NUMBER: ${{ steps.existing-pull-request.outputs.number }}
HAS_EXISTING_PR: ${{ steps.existing-pull-request.outputs.found }}
PACKAGE_NAME: ${{ steps.submission.outputs.package_name }}
SUMMARY: ${{ steps.submission.outputs.summary }}
run: |
{
echo "$SUMMARY"
echo
echo "Submitted from #$ISSUE_NUMBER."
echo
echo 'Having merge conflicts? Admins can send the command below to resolve them.'
echo '```'
echo '/anip resolve-conflict'
echo '```'
} > "$RUNNER_TEMP/submit-adaption-pr.md"
if [[ "$HAS_EXISTING_PR" == 'true' ]]; then
pr_number="$EXISTING_PR_NUMBER"
gh pr edit "$pr_number" --repo "$GITHUB_REPOSITORY" --title "[Submission] Add $APP_LABEL ($PACKAGE_NAME)" --body-file "$RUNNER_TEMP/submit-adaption-pr.md"
else
pr_url=$(gh pr create --base "$DEFAULT_BRANCH" --head "$BRANCH" --title "[Submission] Add $APP_LABEL ($PACKAGE_NAME)" --body-file "$RUNNER_TEMP/submit-adaption-pr.md")
pr_number="${pr_url##*/}"
fi
echo "number=$pr_number" >> "$GITHUB_OUTPUT"
- name: Mark Submission Under Review
if: >-
steps.submission.outputs.valid == 'true' &&
steps.existing-pull-request.outputs.found != 'true'
env:
PR_NUMBER: ${{ steps.pull-request.outputs.number }}
run: |
gh issue edit "$ISSUE_NUMBER" --add-label submit-adaption-pre
gh issue comment "$ISSUE_NUMBER" --body "Hello, thank you for your contribution. Your submission has been approved and a PR (#$PR_NUMBER) has been created. Please wait for the administrator to process it."
close-submission-pull-request:
if: >-
github.event_name == 'issues' &&
github.event.action == 'closed' &&
contains(github.event.issue.labels.*.name, 'submit-adaption') &&
contains(github.event.issue.labels.*.name, 'submit-adaption-pre') &&
!contains(github.event.issue.labels.*.name, 'submit-adaption-done')
runs-on: ubuntu-latest
concurrency:
group: submit-adaption-issue-${{ github.event.issue.number }}
cancel-in-progress: false
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEAD_REF: anip/submit-${{ github.event.issue.number }}
steps:
- name: Close Pull Request
run: |
pr_data=$(gh pr list --repo "$GITHUB_REPOSITORY" --head "$HEAD_REF" --state all --limit 1 --json number,state --jq '.[] | [.number, .state] | @tsv')
if [[ -z "$pr_data" ]]; then
exit 0
fi
read -r pr_number pr_state <<< "$pr_data"
if [[ "$pr_state" == 'OPEN' ]]; then
gh pr close "$pr_number" --repo "$GITHUB_REPOSITORY"
fi
sync-unmerged-submission-issue:
if: >-
github.event_name == 'pull_request_target' &&
github.event.pull_request.head.repo.full_name == github.repository &&
startsWith(github.event.pull_request.head.ref, 'anip/submit-') &&
(github.event.action == 'reopened' ||
(github.event.action == 'closed' &&
github.event.pull_request.merged == false))
runs-on: ubuntu-latest
concurrency:
group: submit-adaption-sync-${{ github.event.pull_request.number }}
cancel-in-progress: true
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEAD_REF: ${{ github.event.pull_request.head.ref }}
PR_BODY: ${{ github.event.pull_request.body }}
PR_NUMBER: ${{ github.event.pull_request.number }}
steps:
- name: Synchronize Submission Issue State
run: |
issue_number="${HEAD_REF#anip/submit-}"
if [[ ! "$issue_number" =~ ^[0-9]+$ ]] || [[ "$PR_BODY" != *"#$issue_number"* ]]; then
echo 'Pull request does not contain a valid submission issue reference.' >&2
exit 1
fi
labels=$(gh issue view "$issue_number" --repo "$GITHUB_REPOSITORY" --json labels --jq '.labels[].name')
if ! grep -Fxq submit-adaption <<< "$labels" ||
! grep -Fxq submit-adaption-pre <<< "$labels" ||
grep -Fxq submit-adaption-done <<< "$labels"; then
echo 'Referenced issue is not an active automated submission.' >&2
exit 1
fi
pr_state=$(gh pr view "$PR_NUMBER" --repo "$GITHUB_REPOSITORY" --json state --jq '.state')
issue_state=$(gh issue view "$issue_number" --repo "$GITHUB_REPOSITORY" --json state --jq '.state')
if [[ "$pr_state" == 'OPEN' && "$issue_state" == 'CLOSED' ]]; then
gh issue reopen "$issue_number" --repo "$GITHUB_REPOSITORY"
elif [[ "$pr_state" == 'CLOSED' && "$issue_state" == 'OPEN' ]]; then
gh issue close "$issue_number" --repo "$GITHUB_REPOSITORY" --reason 'not planned'
fi
finish-merged-submission:
if: >-
github.event_name == 'pull_request_target' &&
github.event.pull_request.merged == true &&
github.event.pull_request.head.repo.full_name == github.repository &&
startsWith(github.event.pull_request.head.ref, 'anip/submit-')
runs-on: ubuntu-latest
outputs:
contributor: ${{ steps.submission-context.outputs.contributor }}
issue_number: ${{ steps.submission-context.outputs.issue_number }}
concurrency:
group: submit-adaption-merge-${{ github.event.pull_request.number }}
cancel-in-progress: false
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
HEAD_REF: ${{ github.event.pull_request.head.ref }}
PR_BODY: ${{ github.event.pull_request.body }}
steps:
- name: Resolve Submission Context
id: submission-context
run: |
issue_number="${HEAD_REF#anip/submit-}"
if [[ ! "$issue_number" =~ ^[0-9]+$ ]] || [[ "$PR_BODY" != *"#$issue_number"* ]]; then
echo 'Pull request does not contain a valid submission issue reference.' >&2
exit 1
fi
if ! gh issue view "$issue_number" --repo "$GITHUB_REPOSITORY" --json labels --jq '.labels[].name' | grep -Fxq submit-adaption-pre; then
echo 'Referenced issue is not an active automated submission.' >&2
exit 1
fi
contributor=$(gh issue view "$issue_number" --repo "$GITHUB_REPOSITORY" --json author --jq '.author.login')
if [[ -z "$contributor" || "$contributor" == 'null' ]]; then
echo 'Unable to resolve the submission issue author.' >&2
exit 1
fi
echo "issue_number=$issue_number" >> "$GITHUB_OUTPUT"
echo "contributor=$contributor" >> "$GITHUB_OUTPUT"
- name: Delete Temporary Branch
run: |
if gh api "repos/$GITHUB_REPOSITORY/git/ref/heads/$HEAD_REF" >/dev/null 2>&1; then
gh api --method DELETE "repos/$GITHUB_REPOSITORY/git/refs/heads/$HEAD_REF"
fi
release-merged-submission:
needs: finish-merged-submission
uses: ./.github/workflows/release-icon-resources.yml
with:
sha: ${{ github.event.pull_request.merge_commit_sha }}
contributor: ${{ needs.finish-merged-submission.outputs.contributor }}
complete-merged-submission:
needs: [finish-merged-submission, release-merged-submission]
runs-on: ubuntu-latest
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
ISSUE_NUMBER: ${{ needs.finish-merged-submission.outputs.issue_number }}
steps:
- name: Complete Submission Lifecycle
run: |
gh issue edit "$ISSUE_NUMBER" --repo "$GITHUB_REPOSITORY" --remove-label submit-adaption-pre --add-label submit-adaption-done
gh issue comment "$ISSUE_NUMBER" --repo "$GITHUB_REPOSITORY" --body "Submission completed by merged pull request #${{ github.event.pull_request.number }}."
gh issue close "$ISSUE_NUMBER" --repo "$GITHUB_REPOSITORY" --reason completed