Skip to content

Commit

Permalink
#7: Make action more generic
Browse files Browse the repository at this point in the history
  • Loading branch information
takanuva15 committed Mar 3, 2022
1 parent 090a088 commit 99ae091
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/verify_pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -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`:
Expand All @@ -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 }}"`
Expand All @@ -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 }}
```

Expand Down
20 changes: 10 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand All @@ -33,23 +33,23 @@ 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
if ! curl --silent --fail --user "$OWNER":"$GITHUB_TOKEN" "$PR_API_URL" >/dev/null; then
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 }}
8 changes: 4 additions & 4 deletions verify_changelog_updated.sh → verify_file_updated.sh
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -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!"

0 comments on commit 99ae091

Please sign in to comment.