diff --git a/.github/workflows/changelog-existence.yml b/.github/workflows/changelog-existence.yml new file mode 100644 index 0000000..d977629 --- /dev/null +++ b/.github/workflows/changelog-existence.yml @@ -0,0 +1,106 @@ +# **what?** +# Checks that a file has been committed under the /.changes directory +# as a new CHANGELOG entry. Cannot check for a specific filename as +# it is dynamically generated by change type and timestamp. +# + +# **why?** +# Ensure code change gets reflected in the CHANGELOG. + +# **when?** +# This will run when called in a workflow. + +# Example Usage including required permissions + +# permissions: +# contents: read +# pull-requests: write +# +# name: Check Changelog Entry +# jobs: +# changelog: +# uses: dbt-labs/actions/.github/workflows/changelog-check.yml@main +# with: +# changelog_comment: 'Thank you for your pull request! We could not find a changelog entry for this change. For details on how to document a change, see [the contributing guide](https://github.com/dbt-labs/dbt-core/blob/main/CONTRIBUTING.md#adding-changelog-entry).' +# skip_label: 'Skip Changelog' +# secrets: inherit # this is only acceptable because we own the action we're calling +# + +name: Check Changelog Entry Exists + +on: + workflow_call: + inputs: + changelog_comment: + description: The comment text when no changelog is found + type: string + required: true + skip_label: + description: Label on the PR that indicates CHANGELOGs are not required. + type: string + required: true + +jobs: + changelog_existence: + name: Check if Changelog Exists + if: "!contains(github.event.pull_request.labels.*.name, inputs.skip_label)" + runs-on: ubuntu-latest + + steps: + + - name: "[DEBUG] - Print Inputs" + shell: bash + id: echo_inputs + run: | + echo "all variables defined as inputs" + echo "Input Comment Text: ${{ inputs.changelog_comment }}" + echo "Input Label for Skipping: ${{ inputs.skip_label }}" + - name: Check if changelog file was added + # https://github.com/marketplace/actions/paths-changes-filter + # For each filter, it sets output variable named by the filter to the text: + # 'true' - if any of changed files matches any of filter rules + # 'false' - if none of changed files matches any of filter rules + uses: dorny/paths-filter@v2 + id: changelog_check + with: + token: ${{ secrets.GITHUB_TOKEN }} + filters: | + exists: + - added: '.changes/unreleased/**.yaml' + # this step uses the read permission from the GITHUB_TOKEN it inherits + - name: Check for comment + if: steps.changelog_check.outputs.exists == 'false' + uses: peter-evans/find-comment@v1 + id: changelog_comment + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: ${{ inputs.changelog_comment }} + + - name: Set if comment already exists + if: steps.changelog_check.outputs.exists == 'false' + shell: bash + id: comment_check + run: | + if [ '${{ steps.changelog_comment.outputs.comment-body }}' = '' ]; then + echo "::set-output name=exists::false" + echo "Comment does not exist for this PR" + else + echo "::set-output name=exists::true" + echo "Comment already exists for this PR" + fi + # this step uses the write permission on the PR from the GITHUB_TOKEN it inherits + - name: Create PR comment if changelog entry is missing, required, and does not exist + if: | + steps.changelog_check.outputs.exists == 'false' && + steps.comment_check.outputs.exists == 'false' + uses: peter-evans/create-or-update-comment@v1 + with: + issue-number: ${{ github.event.pull_request.number }} + body: ${{ inputs.changelog_comment }} + + - name: Fail job if changelog entry is missing and required + if: steps.changelog_check.outputs.exists == 'false' + uses: actions/github-script@v6 + with: + script: core.setFailed('Changelog entry required to merge.')