From 55698a81d3cd2e8d8e1e0632dfe5bf8670d350d0 Mon Sep 17 00:00:00 2001 From: takanuva15 Date: Wed, 2 Mar 2022 18:57:59 -0500 Subject: [PATCH] #7: Make action more generic --- .github/workflows/verify_pr.yml | 2 +- CHANGELOG.md | 2 ++ README.md | 18 ++++++++--------- action.yml | 20 +++++++++---------- ...gelog_updated.sh => verify_file_updated.sh | 8 ++++---- 5 files changed, 26 insertions(+), 24 deletions(-) rename verify_changelog_updated.sh => verify_file_updated.sh (56%) diff --git a/.github/workflows/verify_pr.yml b/.github/workflows/verify_pr.yml index acf4086..cec35ef 100644 --- a/.github/workflows/verify_pr.yml +++ b/.github/workflows/verify_pr.yml @@ -6,6 +6,6 @@ jobs: runs-on: ubuntu-latest steps: - name: Verify Changelog Updated - uses: takanuva15/verify-changelog-updated@v1 + uses: takanuva15/verify-file-updated@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/CHANGELOG.md b/CHANGELOG.md index cf6403d..75c6fd3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased] +### Changed +- Updated this action to be more generic since it can apply to any file in a repo ## [1.2.0] - 2021-04-11 ### Added diff --git a/README.md b/README.md index 71d9e39..ea5b6ce 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -# verify-changelog-updated GitHub Action -A simple GitHub Action that runs a sanity check on your PRs to verify that the changelog file has been updated. (If you're anything like me, you may have merged a PR or two while completely forgetting to update the changelog) +# verify-file-updated GitHub Action +This started out as a simple GitHub Action to check whether the changelog file had been updated in PR builds since, if you're anything like me, you may have merged a PR or two while completely forgetting to update the changelog. Since this action's code is pretty generic though, it can apply to any file in your repo such as the README or CONTRIBUTING file - thus I updated this action to reflect the fact that you can check for any file. ## Getting Started Assuming you have a file named `CHANGELOG.md` within the root of your repository, add the following to a new file `.github\workflows\verify_pr.yml`: @@ -17,15 +17,15 @@ jobs: name: Verify Changelog Updated steps: - name: Verify Changelog Updated - uses: takanuva15/verify-changelog-updated@v1 + uses: takanuva15/verify-file-updated@v1 ``` Commit the file and create a PR to `main` - you should see the action run. ## Additional Usage Parameters There are three additional parameters you can specify for the GitHub action: -- `excused_label`: A label that, when added to the PR, will exempt the PR from needing to satisfy the changelog check. +- `excused_label`: A label that, when added to the PR, will exempt the PR from needing to satisfy this check. - Default is `changelog update optional` -- `changelog_filename`: The name of the changelog file. The GitHub Action will verify whether this file has been updated within the PR. +- `filename_to_check`: The name of the file to check. The GitHub Action will verify whether this file has been updated within the PR. - Default is `CHANGELOG.md` - `token`: The authentication token to use for accessing the PR's information. A token is required in order to view the files changed in a PR. - Default is `"${{ github.token }}"` @@ -35,11 +35,11 @@ There are three additional parameters you can specify for the GitHub action: Sample usage: ```yaml steps: - - name: Verify Changelog Updated - uses: takanuva15/verify-changelog-updated@v1 + - name: Verify Readme Updated + uses: takanuva15/verify-file-updated@v1 with: - excused_label: my_custom_label - changelog_filename: changelog.txt + excused_label: readme update optional + filename_to_check: README.md token: ${{ secrets.MY_TOKEN }} ``` diff --git a/action.yml b/action.yml index bf1cfd5..622c1d8 100644 --- a/action.yml +++ b/action.yml @@ -1,15 +1,15 @@ -name: Verify Changelog Updated -description: Verify that the changelog file has been updated in the current PR +name: Verify File Updated +description: Verify that the user-specified file has been updated in the current PR branding: icon: check-circle color: orange inputs: excused_label: - description: PR label that exempts the PR from the changelog update requirement if present + description: PR label that exempts the PR from the user-specified file-update requirement if present required: false default: changelog update optional - changelog_filename: - description: Name of the changelog file (case-sensitive) + filename_to_check: + description: Name of the user-specified file to check for (case-sensitive) required: false default: CHANGELOG.md token: @@ -24,7 +24,7 @@ runs: run: | if [ ${{ github.event.pull_request != null }} == false ]; then echo "PR info could not be found! This action should only be run in the context of a PR. Please verify this" \ - "action is only being triggered on 'pull_request'. (See this action\"s README for more info)" >&2 + "action is only being triggered on 'pull_request'. (See this action\'s README for more info)" >&2 exit 1 fi @@ -33,11 +33,11 @@ runs: shell: bash run: | if [ ${{ contains(github.event.pull_request.labels.*.name, inputs.excused_label) }} == true ]; then - echo "PR has label '${{ inputs.excused_label }}', so no changelog update is required" + echo "PR has label '${{ inputs.excused_label }}', so no update to '${{ inputs.filename_to_check }}' is required" echo "::set-output name=pr_excused::true" fi - - name: Verify that the changelog file has been updated (if PR not excused) + - name: Verify that the file has been updated (if PR not excused) shell: bash run: | if [ "${{ steps.pr_excused_check.outputs.pr_excused }}" != true ]; then @@ -45,11 +45,11 @@ runs: echo "Could not fetch info about the current PR. Perhaps an invalid token was provided?" >&2 exit 1 fi - $GITHUB_ACTION_PATH/verify_changelog_updated.sh + $GITHUB_ACTION_PATH/verify_file_updated.sh fi env: PR_API_URL: ${{ github.event.pull_request._links.self.href }} GITHUB_TOKEN: ${{ inputs.token }} OWNER: ${{ github.repository_owner }} EXCUSED_LABEL: ${{ inputs.excused_label }} - CHANGELOG_FILENAME: ${{ inputs.changelog_filename }} + FILENAME_TO_CHECK: ${{ inputs.filename_to_check }} diff --git a/verify_changelog_updated.sh b/verify_file_updated.sh similarity index 56% rename from verify_changelog_updated.sh rename to verify_file_updated.sh index 1dc7bd8..dce53ce 100755 --- a/verify_changelog_updated.sh +++ b/verify_file_updated.sh @@ -5,15 +5,15 @@ echoerr() { printf "%s\n" "$*" >&2; } #credit to https://stackoverflow.com/a/61922402 for providing a starting point for this script -# Fetch all changed files in this PR and verify the changelog file is among the changes +# Fetch all changed files in this PR and verify the user-specified file is among the changes pr_files_json=$(curl -s -u "$OWNER":"$GITHUB_TOKEN" -H "Accept: application/vnd.github.v3+json" "$PR_API_URL/files") pr_files=$(jq -r '.[] | .filename' <<<"$pr_files_json") # shellcheck disable=SC2199 -if [[ ! ${pr_files[@]} =~ ${CHANGELOG_FILENAME} ]]; then - echoerr "Could not find '${CHANGELOG_FILENAME}' among the changed files in this PR! '${CHANGELOG_FILENAME}' must " \ +if [[ ! ${pr_files[@]} =~ ${FILENAME_TO_CHECK} ]]; then + echoerr "Could not find '${FILENAME_TO_CHECK}' among the changed files in this PR! '${FILENAME_TO_CHECK}' must " \ "be updated within the PR to pass this check." exit 1 fi -echo "Detected '${CHANGELOG_FILENAME}' among the changed files in this PR. Verification succeeded!" +echo "Detected '${FILENAME_TO_CHECK}' among the changed files in this PR. Verification succeeded!"