2
0
お題は不問!Qiita Engineer Festa 2024で記事投稿!
Qiita Engineer Festa20242024年7月17日まで開催中!

フォークしたリポジトリからのPR作成時にpytest-coverage-commentのCIがコケる

Last updated at Posted at 2024-07-16

はじめに

以前、Github ActionsのCIに pytest-coverage-comment を導入しているOSSを支援していました。

その際に、当該リポジトリをフォークしてプルリクエスト(PR)を作成したところ、CIの実行時に以下のエラーが発生し、pytest-coverage-comment が失敗する事象に遭遇しました。
(pytest-coverage-comment のCIはPR作成をトリガーに実行される)

Error: HttpError: Resource not accessible by integration
Error: Resource not accessible by integration

この事象により、外部コントリビューターがPRを作成した際、実行されるCIが失敗する状態となっていました。

ただし、リポジトリのオーナーがPRを作成した場合は問題なく成功するとのことで、このエラーはフォークされたリポジトリからのPRに限定して発生しているようです。

原因

この問題については以下のIssueで議論されています。

内容を見てみると、pytest-coverage-comment は GITHUB_TOKEN を使用してPRにレポートを書き込む仕様になっているようです。

しかし、フォークされたリポジトリから作成されたPRに対して、GITHUB_TOKEN には読み取り権限しか与えられず、書き込み権限が制限されています。これは GitHub Actions のデフォルト設定によるものです。

この制限により、フォークされたリポジトリからのPRに対して自動的にコメントを投稿できないという問題が発生しており、現在も上記 Issue は未解決の状態です。

回避策

前述の議論の中で様々な回避策が提案されていますが、今回は暫定的に失敗を無視する設定を追加することにしました。以下がその設定例です。

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v4
    - name: Set up Python
      uses: actions/setup-python@v5
      with:
        python-version: '3.10'

    - name: Install dependencies
      run: |
        python -m pip install --upgrade pip
        pip install -r requirements.txt
        pip install -r dev-requirements.txt

    - name: Test with pytest
      run: |
        set -o pipefail
        python -m pytest --junitxml=pytest.xml --cov-report=term-missing --cov=src tests/ | tee pytest-coverage.txt

    - name: Pytest coverage comment
      continue-on-error: true   # 失敗しても続行
      uses: MishaKav/pytest-coverage-comment@v1.1.47
      with:
        pytest-coverage-path: ./pytest-coverage.txt
        junitxml-path: ./pytest.xml

おわりに

なお、Workflow Permissions の権限を Read and Write permissions に変更すれば解決するというコメントもありましたが、この変更はリポジトリのセキュリティリスクを高める可能性があるので今回は採用を見送りました。

image.png

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0