leandrotoledo/python-telegram-bot

View on GitHub
.github/workflows/type_completeness.yml

Summary

Maintainability
Test Coverage
name: Check Type Completeness
on:
  pull_request:
    paths:
      - telegram/**
      - requirements.txt
      - requirements-opts.txt
  push:
    branches:
      - master

jobs:
  test-type-completeness:
    name:   test-type-completeness
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: git fetch --depth=1  # https://github.com/actions/checkout/issues/329#issuecomment-674881489
      - name: Set up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v5
        with:
          python-version: 3.9
          cache: 'pip'
          cache-dependency-path: '**/requirements*.txt'
      - name: Install Pyright
        run: |
          python -W ignore -m pip install pyright~=1.1.316
      - name: Get PR Completeness
        # Must run before base completeness, as base completeness will checkout the base branch
        # And we can't go back to the PR branch after that in case the PR is coming from a fork
        run: |
          pip install . -U
          pyright --verifytypes telegram --ignoreexternal --outputjson > pr.json || true
          pyright --verifytypes telegram --ignoreexternal > pr.readable || true
      - name: Get Base Completeness
        run: |
          git checkout ${{ github.base_ref }}
          pip install . -U
          pyright --verifytypes telegram --ignoreexternal --outputjson > base.json || true
      - name: Compare Completeness
        uses: jannekem/run-python-script-action@v1
        with:
          script: |
            import json
            import os
            from pathlib import Path

            base = float(
              json.load(open("base.json", "rb"))["typeCompleteness"]["completenessScore"]
            )
            pr = float(
              json.load(open("pr.json", "rb"))["typeCompleteness"]["completenessScore"]
            )
            base_text = f"This PR changes type completeness from {round(base, 3)} to {round(pr, 3)}."
            
            if base == 0:
                text = f"Something is broken in the workflow. Reported type completeness is 0. 💥"
                set_summary(text)
                print(Path("pr.readable").read_text(encoding="utf-8"))
                error(text)
                exit(1)
            
            if pr < (base - 0.001):
                text = f"{base_text} ❌"
                set_summary(text)
                print(Path("pr.readable").read_text(encoding="utf-8"))
                error(text)
                exit(1)
            elif pr > (base + 0.001):
                text = f"{base_text} ✨"
                set_summary(text)
                if pr < 1:
                    print(Path("pr.readable").read_text(encoding="utf-8"))
                print(text)
            else:
                text = f"{base_text} This is less than 0.1 percentage points. ✅"
                set_summary(text)
                print(Path("pr.readable").read_text(encoding="utf-8"))
                print(text)