kachick / times_kachick

`#times_kachick channel in chat` as a public repository. Personal Note and TODOs
https://github.com/kachick/times_kachick/issues?q=is%3Aissue+is%3Aclosed
6 stars 0 forks source link

2022-10-22 - GitHub の新しい PAT (v2 / Fine-grained PAT) は、現状 GraphQL API を叩けない #195

Closed kachick closed 1 year ago

kachick commented 1 year ago

https://github.com/kachick/times_kachick/issues/163 を作った時にモチベーションの大きなところを占めていた PAT のスコープ問題への根本着手版みたいなのが beta として出された。 https://github.com/github/roadmap/issues/184 => https://github.blog/changelog/2022-10-18-introducing-fine-grained-personal-access-tokens beta とはいえかなり preferred なUIになってるし、遠くないうちに v2 で確定するのでは。

時を同じくして(大げさ)、https://github.com/kachick/renovate-config-asdf が GitLab で動かないという話を立て続けに貰うことになった。 GitLab のアカウントすら持ってなかったんだけど、これがバグってるからリバートすんぜ~みたいな GitLab スタッフの方のコメントみてぐぬぬとなったのでテスト用リポジトリ作ったりして動作確認等してた。 GitLab も便利そうね。 このときに、どうせ空権限で良いみたいだし動けばめっけもんだろと GITHUB_COM_TOKEN に Fine-grained PAT を設定してみた。

そこで何故か、 deno の fetch はうまくいくのに、 crystal の fetch がうまくいかないという謎の症状に出くわした。こうなるとrenovate のデバッグログ出さないと全くわからないので出す。

DEBUG: Datasource unauthorized (repository=kachick/sample-renovate-config-asdf)
       "datasource": "github-tags",
       "packageName": "crystal-lang/crystal",
       "url": "https://api.github.com/graphql"
DEBUG: Failed to look up github-tags dependency crystal-lang/crystal (repository=kachick/sample-renovate-config-asdf, packageFile=.tool-versions, dependency=crystal-lang/crystal)

https://github.com/kachick/renovate-config-asdf/blob/ff4bda0467b84ef9d7459dc89bae1e02a40f6a97/plugins/deno.json5#L9 https://github.com/renovatebot/renovate/blob/cc50beb0934874095fd2164b33dcb5fed7dbf421/lib/modules/datasource/github-releases/index.ts

https://github.com/kachick/renovate-config-asdf/blob/ff4bda0467b84ef9d7459dc89bae1e02a40f6a97/plugins/crystal.json5#L9 https://github.com/renovatebot/renovate/blob/cc50beb0934874095fd2164b33dcb5fed7dbf421/lib/modules/datasource/github-tags/index.ts

なるほど、つまり renovate bot の内部だと、 datasource が github-releases だと REST API を使い、github-tags だと GraphQL API を使ってるようだ。どちらもこんな感じのクエリで取れる筈なので、多分動くものを敢えて変えなくていいだろ的なノリなのかな・・・

query tags {
  repository(owner: "crystal-lang", name: "crystal") {
    releases(first: 3, orderBy: {field: CREATED_AT, direction: DESC}) {
      edges {
        node {
          name
        }
      }
    }
    refs(
      refPrefix: "refs/tags/"
      first: 3
      orderBy: {field: TAG_COMMIT_DATE, direction: DESC}
    ) {
      edges {
        node {
          name
        }
      }
    }
  }
}

実際これを beta の PAT 使って叩くとエラーメッセージを返してきた。

{
  "message": "Personal access tokens with fine grained access do not support the GraphQL API",
  "documentation_url": "https://docs.github.com/graphql/guides/forming-calls-with-graphql#authenticating-with-graphql"
}

classic PAT だとこう

{
  "data": {
    "repository": {
      "releases": {
        "edges": [
          {
            "node": {
              "name": "1.6.1"
            }
          },
          {
            "node": {
              "name": "1.6.0"
            }
          },
          {
            "node": {
              "name": "1.5.1"
            }
          }
        ]
      },
      "refs": {
        "edges": [
          {
            "node": {
              "name": "1.6.1"
            }
          },
          {
            "node": {
              "name": "1.6.0"
            }
          },
          {
            "node": {
              "name": "1.5.1"
            }
          }
        ]
      }
    }
  }
}

ちなみに GraphQL Query の body は JSON らしいのだけれど、これを escape するのが結構面倒なのと、文字列含めてまともに escape してくれる Web Tool が見あたらなかった。 ので、 https://docs.github.com/ja/graphql/overview/explorer から叩きつつ Chrome Developer tool でネットワークキャプチャして curl でコピーして --data-raw 部分から持ってきた。

いかにも最初からソースコードに当たったような書き方してたけど、実際にはログから当たりつけて API 叩いた後で見に行った。というのが https://github.com/kachick/renovate-config-asdf/commit/ff4bda0467b84ef9d7459dc89bae1e02a40f6a97 💨