palantir / bulldozer

GitHub Pull Request Auto-Merge Bot
Apache License 2.0
745 stars 100 forks source link

Could not refresh token error #417

Closed Thakurvaibhav closed 1 year ago

Thakurvaibhav commented 1 year ago

Hi,

I have installed the app on GitHub and deployed it on a Kubernetes cluster. The container spec looks something like this:

   containers:
     - name: bulldozer
       image: palantirtechnologies/bulldozer:1.17.0
       imagePullPolicy: Always
       env:
            - name: GITHUB_APP_WEBHOOK_SECRET
              value: "<redacted>"
            - name: GITHUB_APP_INTEGRATION_ID
              value: "33xxxx"
            - name: BULLDOZER_OPTIONS_APP_NAME
              value: Bulldozer-Test-VT-01
            - name: GITHUB_APP_PRIVATE_KEY
              valueFrom:
                secretKeyRef:
                  key: id_rsa
                  name: bulldozer-key-secret

Whenever a GitHub webhook gets delivered to the container, I see the following error:

{"level":"error","rid":"chlfpc6t7i77lt1s4g3g","github_event_type":"pull_request","github_delivery_id":"adbb3e90-f85d-11ed-91c9-1a1fae2e544c","error":"failed to get pull request Thakurvaibh 
 av/myapp#1: Get \"https://api.github.com/app/repos/Thakurvaibhav/myapp/pulls/1\": could not refresh installation id 0's token: received non 2xx response status \"401 Unauthorized\" when fe 
 tching https://api.github.com/app/app/installations/0/access_tokens\ngithub.com/palantir/bulldozer/server/handler.(*PullRequest).Handle\n\t/home/runner/work/bulldozer/bulldozer/server/hand 
 ler/pull_request.go:62\ngithub.com/palantir/go-githubapp/githubapp.Dispatch.Execute\n\t/home/runner/work/bulldozer/bulldozer/vendor/github.com/palantir/go-githubapp/githubapp/scheduler.go: 
 55\ngithub.com/palantir/go-githubapp/githubapp.(*scheduler).safeExecute\n\t/home/runner/work/bulldozer/bulldozer/vendor/github.com/palantir/go-githubapp/githubapp/scheduler.go:183\ngithub. 
 com/palantir/go-githubapp/githubapp.QueueAsyncScheduler.func1\n\t/home/runner/work/bulldozer/bulldozer/vendor/github.com/palantir/go-githubapp/githubapp/scheduler.go:257\nruntime.goexit\n\ 
 t/opt/hostedtoolcache/go/1.19.3/x64/src/runtime/asm_amd64.s:1594","time":"2023-05-22T05:25:05.047019047Z","message":"Unexpected error handling webhook"}

The logs suggest that the installation id is set as 0 but I have explicitly configured the env var GITHUB_APP_INTEGRATION_ID to a 33xxxx value Can someone please help with what could be wrong here?

Thakurvaibhav commented 1 year ago

I fixed it by removing the webhook from the repo

bluekeyes commented 1 year ago

Glad you found a fix!

For anyone else who encounters this issue, here's some background. There are two IDs involved when using a GitHub App: the integration or app ID, which identifies your application and is what you configure on the server, and the installation ID, which represents an installation of the application on a particular user or organization. The installation ID being zero is what's causing this error.

I think this means that the application is not installed on your repository, which can happen if you manually added the webhook. If that's what you did, you should remove the hook and follow these steps to install the application on your repository. Installing an application automatically configures the webhook in addition to creating the necessary permissions and the installation ID. Unfortunately, GitHub doesn't show these automatic app hooks on the normal webhooks page, so it can be a bit confusing.

Once installed, the installation ID is sent as part of the webhook payload, so you shouldn't have to configure anything else in the server.