From 544d7420c0dafe5322c48e01d74726722bbc19ef Mon Sep 17 00:00:00 2001 From: Sangjoon Bob Lee Date: Wed, 18 Sep 2024 22:17:28 -0400 Subject: [PATCH] Use diffpy.utils GitHub CI workflows to address codecov 0% problem (#87) * Move tests folder to top * Revert commit * Add pytest-cov * Use the latest diffpy.utils CI workflow --- .github/workflows/check-news-item.yml | 30 ++------- .github/workflows/check-news.py | 61 ------------------- .github/workflows/docs.yml | 59 ------------------ .github/workflows/main.yml | 55 ----------------- .../matrix-and-codecov-on-merge-to-main.yml | 21 +++++++ .github/workflows/pre-commit.yml | 19 ------ .github/workflows/publish-docs-on-release.yml | 14 +++++ .github/workflows/tests-on-pr.yml | 16 +++++ requirements/test.txt | 1 + 9 files changed, 57 insertions(+), 219 deletions(-) delete mode 100644 .github/workflows/check-news.py delete mode 100644 .github/workflows/docs.yml delete mode 100644 .github/workflows/main.yml create mode 100644 .github/workflows/matrix-and-codecov-on-merge-to-main.yml delete mode 100644 .github/workflows/pre-commit.yml create mode 100644 .github/workflows/publish-docs-on-release.yml create mode 100644 .github/workflows/tests-on-pr.yml diff --git a/.github/workflows/check-news-item.yml b/.github/workflows/check-news-item.yml index e5679fe..3513cb3 100644 --- a/.github/workflows/check-news-item.yml +++ b/.github/workflows/check-news-item.yml @@ -1,32 +1,12 @@ -name: Check News Item +name: Check for News on: pull_request_target: branches: - main -permissions: - pull-requests: write - contents: read - jobs: - build: - runs-on: ubuntu-latest - name: Check News item - steps: - - # note: the checkout will pull code from the base branch. This step should not pull code from the merge commit - - uses: actions/checkout@v4 - - - name: Setup Python - uses: actions/setup-python@v5 - with: - python-version: '3.12' - cache: 'pip' - cache-dependency-path: 'pyproject.toml' - - run: pip install PyGithub - - run: python .github/workflows/check-news.py - env: - PR_NUMBER: "${{ github.event.number }}" - GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}" - GITHUB_REPOSITORY: "${{ env.GITHUB_REPOSITORY }}" + check-news-item: + uses: Billingegroup/release-scripts/.github/workflows/_check-news-item.yml@v0 + with: + project: diffpy.snmf diff --git a/.github/workflows/check-news.py b/.github/workflows/check-news.py deleted file mode 100644 index acf5837..0000000 --- a/.github/workflows/check-news.py +++ /dev/null @@ -1,61 +0,0 @@ -"""Check if the PR has a news item. - -Put a warning comment if it doesn't. -""" - -import os -from fnmatch import fnmatch - -from github import Github, PullRequest - - -def get_added_files(pr: PullRequest.PullRequest): - print(pr, pr.number) - for file in pr.get_files(): - if file.status == "added": - yield file.filename - - -def check_news_file(pr): - return any(map(lambda file_name: fnmatch(file_name, "news/*.rst"), get_added_files(pr))) - - -def get_pr_number(): - number = os.environ["PR_NUMBER"] - if not number: - raise Exception(f"Pull request number is not found `PR_NUMBER={number}") - return int(number) - - -def get_old_comment(pr: PullRequest.PullRequest): - for comment in pr.get_issue_comments(): - if ("github-actions" in comment.user.login) and ("No news item is found" in comment.body): - return comment - - -def main(): - # using an access token - gh = Github(os.environ["GITHUB_TOKEN"]) - repo = gh.get_repo(os.environ["GITHUB_REPOSITORY"]) - pr = repo.get_pull(get_pr_number()) - has_news_added = check_news_file(pr) - old_comment = get_old_comment(pr) - - if old_comment: - print("Found an existing comment from bot") - if has_news_added: - print("Delete warning from bot, since news items is added.") - old_comment.delete() - elif not has_news_added: - print("No news item found") - - pr.create_issue_comment( - """\ -**Warning!** No news item is found for this PR. If this is an user facing change/feature/fix, - please add a news item by copying the format from `news/TEMPLATE.rst`. -""" - ) - - -if __name__ == "__main__": - main() diff --git a/.github/workflows/docs.yml b/.github/workflows/docs.yml deleted file mode 100644 index f7f7590..0000000 --- a/.github/workflows/docs.yml +++ /dev/null @@ -1,59 +0,0 @@ -name: Build Documentation - -on: - push: - branches: - - main - release: - -jobs: - test: - runs-on: ubuntu-latest - defaults: - run: - shell: bash -l {0} - steps: - - uses: actions/checkout@v3 - with: - fetch-depth: 0 - - - uses: conda-incubator/setup-miniconda@v2 - with: - activate-environment: build - environment-file: ./environment.yml - python-version: 3 - auto-update-conda: true - - - name: install requirements - run: | - conda config --set always_yes yes --set changeps1 no - conda config --add channels conda-forge - conda create -n build python=3.12 - conda activate build - conda install --file requirements/build.txt - conda install --file requirements/run.txt - conda install --file requirements/test.txt - conda install --file requirements/docs.txt - python -m pip install . --no-deps - - - name: build documents - run: make -C doc html - - - name: Run tests and upload coverage - shell: bash -l {0} - run: | - conda activate build - coverage run -m pytest -vv -s - coverage report -m - codecov - - - name: Upload coverage to Codecov - uses: codecov/codecov-action@v4 - env: - CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} - - - name: Deploy - uses: peaceiris/actions-gh-pages@v4 - with: - github_token: ${{ secrets.GITHUB_TOKEN }} - publish_dir: ./doc/build/html diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml deleted file mode 100644 index 581821e..0000000 --- a/.github/workflows/main.yml +++ /dev/null @@ -1,55 +0,0 @@ -name: CI - -on: - push: - branches: - - main - - CI - pull_request: - workflow_dispatch: - -jobs: - miniconda: - name: Miniconda ${{ matrix.os }} - runs-on: ${{ matrix.os }} - strategy: - matrix: - os: ["ubuntu-latest"] - steps: - - name: check out diffpy.snmf - uses: actions/checkout@v3 - with: - repository: diffpy/diffpy.snmf - path: . - fetch-depth: 0 # avoid shallow clone with no tags - - - name: initialize miniconda - # this uses a marketplace action that sets up miniconda in a way that makes - # it easier to use. I tried setting it up without this and it was a pain - uses: conda-incubator/setup-miniconda@v2 - with: - activate-environment: test - # environment.yml file is needed by this action. Because I don't want - # maintain this but rather maintain the requirements files it just has - # basic things in it like conda and pip - environment-file: ./environment.yml - python-version: 3 - auto-activate-base: false - - - name: install diffpy.snmf requirements - shell: bash -l {0} - run: | - conda config --set always_yes yes --set changeps1 no - conda config --add channels conda-forge - conda activate test - conda install --file requirements/run.txt - conda install --file requirements/test.txt - pip install . - - - name: Validate diffpy.snmf - shell: bash -l {0} - run: | - conda activate test - coverage run -m pytest -vv -s - coverage report -m - codecov diff --git a/.github/workflows/matrix-and-codecov-on-merge-to-main.yml b/.github/workflows/matrix-and-codecov-on-merge-to-main.yml new file mode 100644 index 0000000..e9e742d --- /dev/null +++ b/.github/workflows/matrix-and-codecov-on-merge-to-main.yml @@ -0,0 +1,21 @@ +name: CI + +on: + push: + branches: + - main + release: + types: + - prereleased + - published + workflow_dispatch: + +jobs: + CI: + uses: Billingegroup/release-scripts/.github/workflows/_matrix-and-codecov-on-merge-to-main.yml@v0 + with: + project: diffpy.snmf + c_extension: false + headless: false + secrets: + CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml deleted file mode 100644 index f2ff7e4..0000000 --- a/.github/workflows/pre-commit.yml +++ /dev/null @@ -1,19 +0,0 @@ -name: pre-commit - -on: - pull_request: - push: - workflow_dispatch: - -jobs: - pre-commit: - # pull requests are a duplicate of a branch push if within the same repo. - if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name != github.repository - - runs-on: ubuntu-latest - steps: - - uses: actions/checkout@v3 - - uses: actions/setup-python@v4 - - uses: pre-commit/action@v3.0.0 - with: - extra_args: --all-files diff --git a/.github/workflows/publish-docs-on-release.yml b/.github/workflows/publish-docs-on-release.yml new file mode 100644 index 0000000..098a344 --- /dev/null +++ b/.github/workflows/publish-docs-on-release.yml @@ -0,0 +1,14 @@ +name: Build and Deploy Docs + +on: + release: + types: + - published + workflow_dispatch: + +jobs: + publish-docs-on-release: + uses: Billingegroup/release-scripts/.github/workflows/_publish-docs-on-release.yml@v0 + with: + project: diffpy.snmf + c_extension: false diff --git a/.github/workflows/tests-on-pr.yml b/.github/workflows/tests-on-pr.yml new file mode 100644 index 0000000..988d86b --- /dev/null +++ b/.github/workflows/tests-on-pr.yml @@ -0,0 +1,16 @@ +name: Tests on PR + +on: + push: + branches: + - main + pull_request: + workflow_dispatch: + +jobs: + tests-on-pr: + uses: Billingegroup/release-scripts/.github/workflows/_tests-on-pr.yml@v0 + with: + project: diffpy.snmf + c_extension: false + headless: false diff --git a/requirements/test.txt b/requirements/test.txt index 6f9ccf8..f33bd0e 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -3,3 +3,4 @@ pytest codecov coverage pytest-env +pytest-cov