Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Customise GHE actions bot user ID #1404

Merged
merged 6 commits into from Dec 7, 2023

Conversation

dimitar-hristov
Copy link
Contributor

@dimitar-hristov dimitar-hristov commented Sep 10, 2023

Context

  • In GitHub actions, the user ID of the bot is hardcoded for github.com without an option to overwrite it for GitHub Enterprise. As a result when called here and here to update the PR comments, it fails to find the comments.

Changes

  • Adds logic to customise DANGER_GHE_ACTIONS_BOT_USER_ID .

@dimitar-hristov dimitar-hristov marked this pull request as ready for review September 10, 2023 09:55
@dimitar-hristov
Copy link
Contributor Author

Hey @orta 👋 When you have a moment, could you review this PR, please?

Copy link
Member

@fbartho fbartho left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No objections to the purpose of this PR — just gotta make sure it doesn’t break :P — Happy to take another look once you’ve ensured it won’t!

// This is the user.id of the github-actions app (https://github.com/apps/github-actions)
// that is used to comment when using danger in a GitHub Action
// with GITHUB_TOKEN (https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token)
return 41898282
}

const info = await this.getUserInfo()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reorganization will cause a breaking behavior change even if GHE_ACTIONS_BOT_USER_ID is unset.

Originally, if this.getUserInfo returns a logged in user then it would immediately return with info.id. Now, it will first check if GITHUB_WORKFLOW is set (and it’s always set on Github, I believe), and so it will always return a hardcoded ID

This would break our codebase, as we set an env-var, I believe DANGER_GITHUB_TOKEN or similar (not at my desk) to log in to a bot account with a PAT. This code-change would only allow comments from the GitHub-actions app-id of 41898282.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, now it makes more sense 😄 I think I found the env var you referred to - it seems to be DANGER_GITHUB_API_TOKEN.

I reverted the change, but also added a logic around it to ensure we call the GitHub API for the user only if the DANGER_GITHUB_API_TOKEN is set. Otherwise, I am not sure if we want to make the call, when there isn't a configured user. Do you think that makes sense?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately, the regular GITHUB_TOKEN might be a valid logged in user so it’s not sufficient to only focuse on DANGER_GITHUB_API_TOKEN, I think! — As I understand it, that’s why we call the getUserInfo API — so we figure out the permissions on the attached token — figure out if it’s a real user or what exactly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, ok, I reverted that part of my change. I still did the tests for completeness, but i don't think they are relevant to my change anymore.

The test that check if the configured token is a user is a bit weird, but I think it makes sense as it check that we have called the API with the correct creds (the ones used when creating the GitHub instance). And ensure that it correctly returns the mocked user ID.

Let me know what you think.

const userID = await api.getUserID()
expect(userID).toBe(1)
})
})

describe("Allows setting GHE_ACTIONS_BOT_USER_ID env variable", () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GHE_ACTIONS_BOT_USER_ID — this is a custom environment variable, right? (not set by github, set by the user).

The naming convention for DangerJS custom environment variables is to prefix it with DANGER_, so in this case, I would name it something like DANGER_GHE_ACTIONS_BOT_USER_ID

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, of course, once we need to document this env-var in appropriate places. Probably with a section for “GitHub Enterprise”

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep makes sense. I have update the name of the env variable and added some docs. Please let me know if I missed something.

@@ -363,9 +363,26 @@ describe("Peril", () => {
delete process.env.PERIL_BOT_USER_ID
})

it("Makes getUserId return undefined", async () => {
it("Makes getUserId return PERIL_BOT_USER_ID", async () => {
Copy link
Member

@fbartho fbartho Sep 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe shouldn’t always return PERIL_BOT_USER here, instead we need something like these test-cases.:

  1. A test case that matches the old test name where all vars are unset, GITHUB_WORKFLOW, GHE_ACTIONS_BOT_USER_ID, PERIL_BOT_USER = undefined
  2. A test case that confirms that when info.id is set, it returns that UserID And GITHUB_WORKFLOW, GHE_ACTIONS_BOT_USER_ID, PERIL_BOT_USER = undefined — this test case might be hard, sorry!
  3. PERIL_BOT_USER if nobody is logged in (GHE_ACTIONS_BOT_USER_ID is unset, but GITHUB_WORKFLOW is set, and PERIL_BOT_USER is set, info.id returns undefined)
  4. 41898282 if PERIL_BOT_USER is unset, GHE_ACTIONS_BOT_USER_ID is unset, but GITHUB_WORKFLOW is set, and info.id would be undefined.
  5. The test case you’ve already added below"Allows setting GHE_ACTIONS_BOT_USER_ID env variable"

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the tests should now be updated to cover all scenarios.

@dimitar-hristov
Copy link
Contributor Author

No objections to the purpose of this PR — just gotta make sure it doesn’t break :P — Happy to take another look once you’ve ensured it won’t!

Thanks for the quick response, @fbartho! I think all comments should now be addressed. Please have a look at the PR again when you have a moment and let me know if it is better. 🙂

@orta
Copy link
Member

orta commented Dec 7, 2023

I think this is good to go - thanks both of you 👍🏻

@orta orta merged commit 266c485 into danger:main Dec 7, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants