O hirunewani blog

Q. gh pr createでレビュワーをチームに変更したら、Github Actions上で動作しなくなった

Created at

gh pr createでレビュワーをチームにする場合、追加で`read:org`の権限が必要になるため、デフォルトのsecrets.GITHUB_TOKENでは動作しなくなる。`repo`と`read:org`の権限を持ったPersonal Access Tokenを使うことで解決できる。

Table of Contents

状況

動作していないworkflowの調査を受けて実施していたところ、他のリポジトリでも同様のworkflowがしばらく動作していないことに気づいた。

次のようにgh pr createを利用してPRを作成するworkflowのいくつかが動作していなかった。

- name: Create PR
  run: gh pr create --base main --title "Add auto-generated code" --reviewer my-org/some-team
  env:
    GH_TOKEN: ${{ secrets.GH_TOKEN }}

原因

原因は、Actionsのログを見れば、次のようなエラーが出力されておりread:orgの権限が必要であることが明白だった。

GraphQL: Your token has not been granted the required scopes to execute this query. The ‘id’ field requires one of the following scopes: [‘read:org’, ‘read:discussion’], but your token has only been granted the: [‘repo’] scopes. Please modify your token’s scopes at: https://github.com/settings/tokens., Your token has not been granted the required scopes to execute this query. The ‘slug’ field requires one of the following scopes: [‘read:org’, ‘read:discussion’], but your token has only been granted the: [‘repo’] scopes. Please modify your token’s scopes at: https://github.com/settings/tokens.

ただし別のリポジトリでは、次のようなエラーが出ていた。

could not request reviewer: ‘my-org/some-team’ not found

前者はrepoの権限のみを持ったPATを利用しており、後者はsecrets.GITHUB_TOKENを利用していた。

対応

2つのリポジトリの差分は深掘りしていないが、secret.GITHUB_TOKENread:orgを持っておらず持たせる手段もないため、どちらもread:orgの権限が必要だろうと推測した。

実際、reporead:orgの権限を持ったPATを利用してworkflowを実行したところ、問題なくPRが作成された。

余談

チームをレビュワーとして指定した場合に動かなくなる問題については、次のIssueで報告されている。

https://github.com/cli/cli/issues/6395

チーム情報を取得するのにread:orgの権限が必要になるのは理解できると思うが、レビュワーを変更するだけという認識だとread:orgの権限が必要になるのは意外性があるため、混乱が生じるのだと思う。

実際、今回のケースもレビュワーを変更しただけという感覚であり、これで動かなくなるとは思ってもいなかったため、動作していないことに気づかれていなかったのだと思う。