From d48d8721bd6fb0d875c12d74b5668b0a6137cb05 Mon Sep 17 00:00:00 2001 From: Adam Coffman Date: Tue, 17 Dec 2024 11:54:05 -0600 Subject: [PATCH] initial attempt at automatically sorting dictionary file --- .github/workflows/report-maker.yml | 64 ++++++++++++++++++++++++++++++ action.yml | 4 ++ 2 files changed, 68 insertions(+) diff --git a/.github/workflows/report-maker.yml b/.github/workflows/report-maker.yml index c3a1dfb..952107c 100644 --- a/.github/workflows/report-maker.yml +++ b/.github/workflows/report-maker.yml @@ -13,6 +13,9 @@ on: error_min: default: 0 type: number + sort_dictionary: + default: false + type: boolean gh_pat: type: string required: true @@ -196,3 +199,64 @@ jobs: No ${{ steps.setup2.outputs.error_name }}! :tada: _Comment updated at ${{ steps.build-components2.outputs.time }} with changes from ${{ steps.build-components2.outputs.commit_id }}_ edit-mode: replace + + sort-dictionary: + runs-on: ubuntu-latest + if: inputs.sort_dictionary + steps: + - name: "Check out PR branch" + id: checkout-pr-branch + uses: actions/checkout@v4 + + - name: "Check write permissions" + id: check-write-permissions + env: + GITHUB_TOKEN: ${{ inputs.gh_pat }} + run: | + pr_branch=$(git rev-parse --abbrev-ref HEAD) + # Attempt to push a dummy commit to test write permissions + git config --global user.name 'github-actions[bot]' + git config --global user.email 'github-actions[bot]@users.noreply.github.com' + # Try to create a temporary branch and push + branch_name="test-write-permissions-$(date +%s)" + git checkout -b "$branch_name" + git commit --allow-empty -m "Test write permissions" + if git push origin "$branch_name"; then + echo "Successfully pushed to branch - write permissions confirmed" + # Clean up test branch and check the PR branch back out + git push origin --delete "$branch_name" + git checkout "$pr_branch" + exit 0 + else + echo "Failed to push - insufficient write permissions" + exit 1 + fi + + - name: "Sort dictionary file" + id: sort-dictionary + # Only run the sort if we're going to be able to commit the result back + if: steps.check-permissions.outcome == 'success' + env: + GITHUB_TOKEN: ${{ inputs.gh_pat }} + run: | + dictionary_file="resources/dictionary.txt" + tmp_dictionary_file="resources/dictionary.txt.sorted" + pr_branch=$(git rev-parse --abbrev-ref HEAD) + if [ -e dictionary_file ]; then + sort $dictionary_file > $tmp_dictionary_file + diff $dictionary_file $tmp_dictionary_file + if [ $? -ne 0 ]; then + #The files are different, we need to commit + rm $dictionary_file + mv $tmp_dictionary_file $dictionary_file + git add $dictionary_file + git commit -m 'Sort dictionary file' + git pull --rebase --set-upstream origin $pr_branch --allow-unrelated-histories --strategy-option=ours + git push origin $pr_branch + else + echo "No changes in dictionary.txt" + fi + else + echo "Dictionary not found at expected location" + exit 1 + fi diff --git a/action.yml b/action.yml index 5f46096..225e106 100644 --- a/action.yml +++ b/action.yml @@ -7,6 +7,10 @@ inputs: description: "There are three types of reports that can be done and specified: 'spelling', 'urls', or 'quiz_format'." required: true type: string + sort_dictionary: + description: "Should this action automatically alphabetize your dictionary.txt" + default: false + type: boolean error_min: description: "What number of errors should make this check fail?" default: 0