diff --git a/.github/workflows/report-maker.yml b/.github/workflows/report-maker.yml index c3a1dfb..daf4d89 100644 --- a/.github/workflows/report-maker.yml +++ b/.github/workflows/report-maker.yml @@ -13,12 +13,15 @@ on: error_min: default: 0 type: number - gh_pat: - type: string - required: true + sort_dictionary: + default: false + type: boolean branch_name: type: string default: ${GITHUB_REF#refs/heads/} + secrets: + gh_pat: + required: true jobs: status-update: runs-on: ubuntu-latest @@ -196,3 +199,52 @@ 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 && inputs.check_type == 'spelling' + steps: + - name: "Check out PR branch" + id: checkout-pr-branch + uses: actions/checkout@v4 + + - name: "Check write permissions" + id: check-write-permissions + run: | + sudo apt-get install -y jq + WRITE_PERMISSION=$(curl -s -H "Authorization: token ${{ secrets.gh_pat }}" https://api.github.com/repos/${{ github.repository }} | jq '.permissions.push') + if [ $WRITE_PERMISSION != "true" ]; then + echo "Do not have write permissions to the repo" + 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-write-permissions.outcome == 'success' + run: | + dictionary_file="resources/dictionary.txt" + tmp_dictionary_file="resources/dictionary.txt.sorted" + pr_branch=${{ inputs.branch_name }} + git fetch origin + git checkout $pr_branch + if [ -e $dictionary_file ]; then + sort -f $dictionary_file > $tmp_dictionary_file + if ! diff $dictionary_file $tmp_dictionary_file ; then + #The files are different, we need to commit + rm $dictionary_file + mv $tmp_dictionary_file $dictionary_file + git config --global user.name 'github-actions[bot]' + git config --global user.email 'github-actions[bot]@users.noreply.github.com' + 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 + exit 0 + else + echo "No changes in dictionary.txt" + exit 0 + 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