danger / danger-js

⚠️ Stop saying "you forgot to …" in code review
http://danger.systems/js/
MIT License
5.22k stars 363 forks source link

[BUG] Danger doesn't work with App tokens anymore #1433

Closed jaymzh closed 2 months ago

jaymzh commented 3 months ago

Describe the bug This changelog line from version 0.21.1 claims:

- Add support for GithHub Apps API (no GET /user) - clintam

But that doesn't seem to be true anymore as when we try to use a token from an App, we get:

Request failed [403]: https://api.github.com/user
Response: {
  "message": "Resource not accessible by integration",
  "documentation_url": "https://docs.github.com/rest/users/users#get-the-authenticated-user"
}

Doing a naive grep through he codebase, I only see /user referenced in tests (or non-GH platforms), so I'm not quite clear where it's coming from.

To Reproduce Steps to reproduce the behavior:

  1. Use an App

Expected behavior

Screenshots

Your Environment

        # This is 11.3.1
        uses: docker://ghcr.io/danger/danger-js@sha256:24bec136a7129fa421fa4591aae7cc76ca60180ecea9d239c680c2fba338b2e9

GitHub Actions

Additional context Add any other context about the problem here.

jaymzh commented 3 months ago

@orta - Can you weigh in on if this is expected? I'm not bugging you for a fix, necessarily, I know this is all volunteer-time OSS, and I'm very grateful for it. But just knowing if this is expected or not would be super helpful. Thanks!

fbartho commented 3 months ago

@jaymzh this 403 error log isn't necessarily a direct indicator that App Tokens will never work.

It's a bit of a confusing log message, but basically, when DangerJS gets the token, it doesn't know what kind of token it is. It hits a series of APIs to figure that out, and this error log pops out in several conditions, but it's not a direct concern (alone). I believe the log is being generated inside of Github's API stack despite the error condition being handled in DangerJS.

Most of the projects I've used DangerJS for have that error log message despite Danger working fine. That said, I haven't used App Tokens except for one hello-world project years ago. I mostly use the built-in token, followed by a bot-account token.

fbartho commented 3 months ago

There are some examples of expected 403 errors in the tests here, though I haven't verified if they're exactly the same as your case!

https://github.com/danger/danger-js/blob/81645a6a7f37534d156ad495c639c914bf7053e1/source/platforms/github/_tests/_github_api.test.ts#L363

jaymzh commented 3 months ago

Huh, that's an interesting observation. OK, I'll test a bit more deeply, and see if I can validate it's actually functioning completely. Thanks!

jaymzh commented 3 months ago

OK after some testing, the problem is that when it can't determine its user, it fails to update existing comments and makes new ones. So this definitely impacts functionality.

fbartho commented 3 months ago

@jaymzh are you sure that your token has permission to edit/remove comments?

I don't fully remember, but when using the built-in Github Token (configured via permissions in the workflow), you could easily get into that state unless you added the right permission scope. Similarly, I had this issue when I first created a User Token for this, but it didn't have sufficient read + write / edit permissions.

DangerJS is used by so many people in so many ways, that behaving like this is actually an intentional feature. Indeed, some people/projects absolutely never want a comment to be edited or removed, and some CI providers don't even allow such edits -- even though edit/replace on comments is one of the top reasons that some people chose to use DangerJS.

So DangerJS needs to support multiple outcomes depending on how you configure it -- it's unfortunate that for your case, a configuration error (for your needs) is interpreted as a "valid" configuration for someone else's needs.

jaymzh commented 3 months ago

Yeah I wasn't claiming it was invalid, I totally see it as a valid possible config. I'll double check the perms on the app. Thanks.

jaymzh commented 3 months ago

It definitely has access (read-write to pull requests, which includes comments)

image
orta commented 3 months ago

Sorry - on a company onsite - I feel like it was not possible to find out the account of the app which was posting a very long time ago when I added support for app tokens as a GitHub app was new then.

Maybe I fixed it by doing some hardcoding the postee in Peril, but it does seem reasonable to either see if that's possible or I'm not against the idea of having app based tokens force Danger is simply look for its own comment to update via the post body?

jaymzh commented 3 months ago

I have an action that looks for it's own comments by a magic string (an HTML comment) in the body, and it works well. It's a much much simpler action than Danger, and JS isn't a strong language of mine, so I haven't quite been able to follow Danger's code well enough to make the change, but I think it'd be a reasonable solution.

fbartho commented 3 months ago

I'm tempted to suggest that we close this ticket, and instead delete the App Token method of using DangerJS, but perhaps I'm missing a valid use-case. (Peril covers some, while user-tokens cover others!) -- Because of the security sensitivity of a tool like DangerJS, I'm not sure we should offer a public App Token.

Sounds like this isn't a blocker for anyone right now. If somebody else comes along and is motivated, I'd be happy to help review solutions.

jaymzh commented 3 months ago

@fbartho - GH support is requiring me to use an App, actually. Here's the details:

fbartho commented 3 months ago

@jaymzh Thanks for sharing your use-case! If I might ask: Is this all in a large monorepo? Do you commonly have huge PRs with many files edited?

Random low-value thoughts that are running through my brain based on your story:

jaymzh commented 3 months ago

No I think I wasn't clear or you misunderstood.

The workflow is not editing a workflow.

Make a PR like this:

for f in .github/workflows/*; do
  echo '#test' >> .github/workflows/$f
done

When anythign requests the list of files in the PR, you'll get an error of too many files, even if that was only 9 files.

jaymzh commented 3 months ago

(it's quite common for us to modify several workflow files in a single PR - and SEVERAL of our checks fail, and we're trying to move them all to an App now because of this)

jaymzh commented 3 months ago

so I think this is only done in GitHubAPI.ts and only to find Danger's comments. And since there's a magic string in all of Danger's comments, I'm pretty sure it's not needed.

fbartho commented 3 months ago

I suspect I have misunderstood something! Would you mind sharing exactly where "this is only done in GitHubAPI.ts"? Maybe if you show me which magic string you're talking about I'll better understand.

jaymzh commented 3 months ago

Well in getDangerCommentIDs we call getUserID() so that we can use it in the filtering.

However, it's not necessary as we already check to make sure the body includes a check for the magic string DangerID: danger-id-Danger; we put in every comment in this filter. That combined with the filters to make sure it looks like a Danger comment in other ways should be sufficient.

Oh and the same logic is used to find inline PR comments. Again, I believe we can drop the call to /user (aka getUserID), which would make this work with Apps.

orta commented 3 months ago

Yeah, dropping the user check seems reasonable to me

orta commented 3 months ago

I've done this in #1435

jaymzh commented 3 months ago

That's awesome, thanks!

jaymzh commented 2 months ago

I've rolled this out and all works well. Thanks!