Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a check to make sure unit tests are considered when changing code #10337

Merged
merged 4 commits into from
Jun 21, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions .github/workflows/unittest-existence.yml
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should refactor https://github.com/dbt-labs/actions/blob/main/.github/workflows/changelog-existence.yml#L2 a bit to make it more generalized and use it here.
@emmyoop WDYT?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@


# **what?**
# Checks that a file has been committed under the /tests/unit directory
# as modification/addition to unit test.
# This workflow runs on pull_request_target because it requires
# secrets to post comments.

# **why?**
# Gentle nudge to think about adding unit tests for new code.
# It also makes sure that we can filter PRs by tag to find out what type of changes are hard to unittest.

# **when?**
# This will run for all PRs going into main. It will
# run when they are opened, reopened, when any label is added or removed
# and when new code is pushed to the branch. The action will then get
# skipped if the 'Hard to Unit Test' label or the 'Unit Test Not Needed' Label is present.

name: Check Changelog Entry

on:
pull_request_target:
types: [opened, reopened, labeled, unlabeled, synchronize]
branches:
- main
paths: ['core/dbt']

workflow_dispatch:

defaults:
run:
shell: bash

permissions:
contents: read
pull-requests: write

env:
comment: "Thank you for your pull request! We could not find a unittest file for this change. Please consider adding some. If it is hard to unittest, please add the 'Hard to Unit Test' label. If unit tests are not needed, please add the 'Unit Test Not Needed' label."


jobs:
unittest:
name: Check if Unit Test being added.
if: ${{ !contains(github.event.pull_request.labels.*.name, 'Unit Test Not Needed') && !contains(github.event.pull_request.labels.*.name, 'Hard to Unit Test')}}
runs-on: ubuntu-latest
steps:
- name: Check if unittest file was added
uses: dorny/paths-filter@v3
id: unittest_existence
with:
token: ${{ secrets.GITHUB_TOKEN }}
filters: |
exists:
- added|modified: 'tests/unit/**'

- name: Check for existing comment
if: steps.unittest_existence.outputs.exists == 'false'
uses: peter-evans/find-comment@v3
id: find_comment
with:
issue-number: ${{ github.event.pull_request.number }}
comment-author: "github-actions[bot]"
body-includes: ${{ env.comment }}
- name: Comment if no unittest file was added
if: steps.unittest_existence.outputs.exists == 'false' &&
steps.find_comment.outputs.exists == 'false'
run: |
gh issue comment ${{ github.event.pull_request.number }} --repo ${{ github.repository }} --body "${{ env.comment }}"
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Fail job if unittest is missing and required
if: steps.unittest_existence.outputs.exists == 'false'
uses: actions/github-script@v7
with:
script: core.setFailed('Unit Test required to merge.')
Loading