refactor: use identity check for comparison to a singleton #80
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Python CI | |
on: | |
push: | |
branches: [ master ] | |
paths-ignore: | |
# Can we use '**.md' ? | |
- 'README.md' | |
- 'SECURITY.md' | |
- 'CONTRIBUTING.md' | |
- 'CODE_OF_CONDUCT.md' | |
- 'LICENSE' | |
- '.gitignore' | |
pull_request: | |
branches: [ master ] | |
jobs: | |
pre_ci: | |
name: Prepare CI environment | |
runs-on: ubuntu-latest | |
outputs: | |
commit_message: ${{ steps.get_commit_message.outputs.commit_message }} | |
branch: ${{ steps.extract_branch.outputs.branch }} | |
steps: | |
- name: Checkout Project | |
uses: actions/checkout@v3 | |
with: | |
# We need to fetch with a depth of 2 for pull_request so we can do HEAD^2 | |
fetch-depth: 2 | |
- name: "Get commit message" | |
id: get_commit_message | |
env: | |
COMMIT_PUSH: ${{ github.event.head_commit.message }} | |
run: | | |
COMMIT_MESSAGE="${COMMIT_PUSH:-$(git log --format=%B -n 1 HEAD^2)}" | |
echo "::set-output name=commit_message::${COMMIT_MESSAGE}" | |
- name: Extract branch name | |
id: extract_branch | |
shell: bash | |
run: | | |
TMP_PULL_HEAD_REF="${{ github.head_ref }}" | |
TMP_GITHUB_REF="${GITHUB_REF#refs/heads/}" | |
EXPORT_VALUE="" | |
if [ "${TMP_PULL_HEAD_REF}" != "" ] | |
then | |
EXPORT_VALUE="${TMP_PULL_HEAD_REF}" | |
else | |
EXPORT_VALUE="${TMP_GITHUB_REF}" | |
fi | |
echo "##[set-output name=branch;]${EXPORT_VALUE}" | |
test-code: | |
name: Test code | |
runs-on: ubuntu-latest | |
# Map a step output to a job output | |
outputs: | |
coverage: ${{ steps.coverage.outputs.coverage }} | |
coverage-rounded-display: ${{ steps.coverage.outputs.coverage-rounded-display }} | |
steps: | |
- uses: actions/setup-python@v2 | |
with: | |
python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax | |
- uses: actions/checkout@v3 | |
- name: Cache pip | |
uses: actions/cache@v3 | |
with: | |
# This path is specific to Ubuntu | |
path: ~/.cache/pip | |
# Look to see if there is a cache hit for the corresponding requirements file | |
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} | |
restore-keys: | | |
${{ runner.os }}-pip- | |
${{ runner.os }}- | |
- name: Install dependencies | |
run: | | |
pip install -r pywisp_emibcn/requirements.txt | |
pip install pytest coverage | |
pip install . | |
- name: Run tests | |
run: | | |
coverage run --branch --include pywisp_emibcn/\* -m pytest | |
coverage xml | |
- name: Code Coverage Summary Report (Text & Value) | |
uses: irongut/CodeCoverageSummary@v1.0.5 | |
with: | |
filename: coverage.xml | |
output: both | |
- name: Check code coverage | |
id: coverage | |
env: | |
VALUE: "Branch Rate" | |
run: | | |
COVERAGE="$( cat code-coverage-results.txt | egrep "^${VALUE}" | sed -e 's#^.* \([0-9]*\)%.*#\1#' )" | |
echo "##[set-output name=coverage;]${COVERAGE}" | |
echo "##[set-output name=coverage-rounded-display;]${COVERAGE}%" | |
- name: Code Coverage Summary Report (Markdown) | |
uses: irongut/CodeCoverageSummary@v1.0.5 | |
if: ${{ github.event_name == 'pull_request' && github.actor == github.repository_owner }} | |
with: | |
filename: coverage.xml | |
output: file | |
format: markdown | |
badge: false | |
- name: Comment PR with code coverage | |
uses: machine-learning-apps/pr-comment@1.0.0 | |
if: ${{ github.event_name == 'pull_request' && github.actor == github.repository_owner }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
with: | |
path: code-coverage-results.md | |
test-build: | |
name: Test build | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/setup-python@v2 | |
with: | |
python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax | |
- uses: actions/checkout@v3 | |
- name: Cache pip | |
uses: actions/cache@v3 | |
with: | |
# This path is specific to Ubuntu | |
path: ~/.cache/pip | |
# Look to see if there is a cache hit for the corresponding requirements file | |
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} | |
restore-keys: | | |
${{ runner.os }}-pip- | |
${{ runner.os }}- | |
- name: Install dependencies | |
run: | | |
pip install -r pywisp_emibcn/requirements.txt | |
pip install build | |
- name: Build package | |
run: | | |
python3 -m build | |
test: | |
name: Tests waiter | |
needs: [test-code, test-build] | |
runs-on: ubuntu-latest | |
outputs: | |
coverage: ${{ needs.test-code.outputs.coverage }} | |
coverage-rounded-display: ${{ needs.test-code.outputs.coverage-rounded-display }} | |
steps: | |
- name: Check test coverage | |
env: | |
COVERAGE: ${{ needs.test-code.outputs.coverage }} | |
COVERAGE_ROUNDED: ${{ needs.test-code.outputs.coverage-rounded-display }} | |
run: | | |
echo "Coverage: ${COVERAGE}" | |
echo "Coverage Rounded: ${COVERAGE_ROUNDED}" | |
badge: | |
# Only generate and publish if these conditions are met: | |
# - The test step ended successfully | |
# - One of these is met: | |
# - This is a pull request event and the pull actor is the same as the repo owner | |
# - This is a push event and the push event is on branch 'master' | |
name: Generate badge image with test coverage value | |
needs: [test, pre_ci] | |
if: ${{ ( github.event_name == 'pull_request' && github.actor == github.repository_owner ) || github.ref == 'refs/heads/master' }} | |
outputs: | |
url: ${{ steps.url.outputs.url }} | |
markdown: ${{ steps.url.outputs.markdown }} | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
with: | |
ref: badges | |
path: badges | |
# Use the output from the `coverage` step | |
- name: Generate the badge SVG image | |
uses: emibcn/badge-action@v2.0.2 | |
id: badge | |
with: | |
label: 'Coverage' | |
status: ${{ needs.test.outputs.coverage-rounded-display }} | |
color: ${{ | |
needs.test.outputs.coverage > 90 && 'green' || | |
needs.test.outputs.coverage > 80 && 'yellow,green' || | |
needs.test.outputs.coverage > 70 && 'yellow' || | |
needs.test.outputs.coverage > 60 && 'orange,yellow' || | |
needs.test.outputs.coverage > 50 && 'orange' || | |
needs.test.outputs.coverage > 40 && 'red,orange' || | |
needs.test.outputs.coverage > 30 && 'red,red,orange' || | |
needs.test.outputs.coverage > 20 && 'red,red,red,orange' || | |
'red' }} | |
path: badges/test-coverage.svg | |
- name: Commit badge | |
env: | |
BRANCH: ${{ needs.pre_ci.outputs.branch }} | |
FILE: 'test-coverage.svg' | |
working-directory: ./badges | |
run: | | |
git config --local user.email "action@github.com" | |
git config --local user.name "GitHub Action" | |
mkdir -p "${BRANCH}" | |
mv "${FILE}" "${BRANCH}" | |
git add "${BRANCH}/${FILE}" | |
# Will give error if badge did not changed | |
git commit -m "Add/Update badge" || true | |
- name: Push badge commit | |
uses: ad-m/github-push-action@master | |
with: | |
github_token: ${{ secrets.GITHUB_TOKEN }} | |
branch: badges | |
directory: badges | |
- name: Badge image URL | |
id: url | |
env: | |
BRANCH: ${{ needs.pre_ci.outputs.branch }} | |
FILE: 'test-coverage.svg' | |
COVERAGE: ${{ needs.test.outputs.coverage-rounded-display }} | |
run: | | |
URL="https://raw.githubusercontent.com/${GITHUB_REPOSITORY}/badges/${BRANCH}/${FILE}" | |
MARKDOWN="![Coverage ${COVERAGE}](${URL})" | |
echo "Badge URL: ${URL}" | |
echo "Badge image for Markdown: ${MARKDOWN}" | |
echo "##[set-output name=url;]${URL}" | |
echo "##[set-output name=markdown;]${MARKDOWN}" | |
comment_pr_badge: | |
name: Comment on PR with generated badge | |
needs: [pre_ci, badge] | |
if: ${{ github.event_name == 'pull_request' && github.actor == github.repository_owner }} | |
runs-on: ubuntu-latest | |
steps: | |
- name: Generate comment file with test coverage badge | |
shell: bash | |
env: | |
BADGE: ${{ needs.badge.outputs.markdown }} | |
run: | | |
echo "Badge: ${BADGE}" | |
echo "${BADGE}" > output.md | |
- name: Comment PR with test coverage badge | |
uses: marocchino/sticky-pull-request-comment@v2 | |
with: | |
recreate: true | |
path: output.md | |
publish: | |
# Only generate and publish if these conditions are met: | |
# - Commit message does not contains #no-publish | |
# - The test step ended successfully | |
# - One of these is met: | |
# - This is a pull request event and the pull has been merged | |
# - This is a push event and the push event is on branch 'master' | |
name: Publish code to PyPi | |
needs: [pre_ci, test] | |
if: ${{ !contains(github.event.head_commit.message, '#no-publish') && (( github.event_name == 'pull_request' && github.event.pull_request.merged == true ) || github.ref == 'refs/heads/master' ) }} | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/setup-python@v2 | |
with: | |
python-version: '3.x' # Version range or exact version of a Python version to use, using SemVer's version range syntax | |
- uses: actions/checkout@v3 | |
- name: Cache pip | |
uses: actions/cache@v3 | |
with: | |
# This path is specific to Ubuntu | |
path: ~/.cache/pip | |
# Look to see if there is a cache hit for the corresponding requirements file | |
key: ${{ runner.os }}-pip-${{ hashFiles('requirements.txt') }} | |
restore-keys: | | |
${{ runner.os }}-pip- | |
${{ runner.os }}- | |
- name: Install dependencies | |
run: | | |
pip install -r pywisp_emibcn/requirements.txt | |
pip install build twine | |
- name: Build package | |
run: | | |
python3 -m build | |
- name: Publish package | |
env: | |
# Use dedicated PyPi token from secrets | |
TWINE_USERNAME: __token__ | |
TWINE_PASSWORD: ${{ secrets.PYWISP_EMIBCN }} | |
run: | | |
python3 -m twine upload --repository pypi dist/* |