Skip to content

Inherit API endpoint configuration from the Codespaces environment #4723

Merged
mislav merged 16 commits intocli:trunkfrom
marwan-at-work:marwan/localcs
Nov 22, 2021
Merged

Inherit API endpoint configuration from the Codespaces environment #4723
mislav merged 16 commits intocli:trunkfrom
marwan-at-work:marwan/localcs

Conversation

@marwan-at-work
Copy link
Contributor

Fixes #4722

One thing to note: is for some reason we have an explicit test to ensure that GITHUB_TOKEN is not picked up by a non-github.com URL by assuming it's GHES. This PR makes it so that a GHES server would end up reading GITHUB_TOKEN, is there a reason for not doing that?

Thanks!

@marwan-at-work marwan-at-work requested a review from a team as a code owner November 13, 2021 15:45
@marwan-at-work marwan-at-work requested review from vilmibm and removed request for a team November 13, 2021 15:45
return token, GITHUB_ENTERPRISE_TOKEN
}

return os.Getenv(GITHUB_TOKEN), GITHUB_TOKEN
Copy link
Contributor

Choose a reason for hiding this comment

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

for some reason we have an explicit test to ensure that GITHUB_TOKEN is not picked up by a non-github.com URL by assuming it's GHES. This PR makes it so that a GHES server would end up reading GITHUB_TOKEN, is there a reason for not doing that?

We intentionally never send GITHUB_TOKEN to non-dotcom instances to avoid ever leaking your github.com token anywhere. This should be reverted. https://github.com/github/pse-assessment-reviews/issues/505#issuecomment-686787043

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mislav sounds good. The only other solution is that the Codespace will need to send GITHUB_TOKEN under a different name that would be picked up by the CLI (hopefully that is not also a security issue)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mislav this is done

if apiURL == "" {
apiURL = githubAPI
}
vscsURL := os.Getenv("VSCS_TARGET_URL")
Copy link
Contributor

Choose a reason for hiding this comment

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

I like that all this is now configurable, but this introduces 3 new environment variables:

  • GITHUB_SERVER_URL
  • GITHUB_API_URL
  • VSCS_TARGET_URL

We already supported setting GH_HOST to switch between non-github.com hosts, so adding all of the above feels a bit redundant. Of course, now there is also the https://online.visualstudio.com/api endpoint in addition to the GitHub API, so that needs to be configurable, but I wonder should we still stick with a GH_ prefix for anything that affects the GitHub CLI and name this variable something like GH_VSCS_URL?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mislav those env var names are important because they are being created as part of the Codespaces environment and are expected to be automatically picked up by the CLI. Note also that the Codespaces Typescript Extension also picks them up and therefore any changes to the environment variable keys would require changes to both the extension and the CLI and our github/github repository which sends those env vars in the first place.

@josebalius
Copy link
Contributor

@mislav I am curious why it seems that @cli/codespaces was not pinged on this PR 🤔

@marwan-at-work
Copy link
Contributor Author

@josebalius not really sure either. But would definitely love a look from your side as well.


// New creates a new API client with the given token and HTTP client.
func New(token string, httpClient httpClient) *API {
serverURL := os.Getenv("GITHUB_SERVER_URL")
Copy link
Contributor

Choose a reason for hiding this comment

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

Just a thought, would it be better to pass these in and defer reading from env to the caller? It could make our tests easier if we wanted to use something httptest or something, i know we can set env vars there as well but curious what you think.

Copy link
Contributor

Choose a reason for hiding this comment

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

Agreed that it would be best if the API client was agnostic to the environment configuration and instead just accepted a configuration from the caller. In general, lower-level modules should not ever read from the environment directly.

Perhaps the api.API struct can have fields like ServerURL that can be overridden after calling api.New()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@mislav @josebalius ok i pulled out the os.Getenv into the caller. Let me know if that looks good.

Perhaps the api.API struct can have fields like ServerURL that can be overridden after calling api.New()?

I typically think it's better that you either create a constructor or have exposed fields. This way it's clear that you only have one way to create the targeted type.

@marwan-at-work marwan-at-work requested a review from a team as a code owner November 16, 2021 21:37
@marwan-at-work
Copy link
Contributor Author

@mislav just FYI, I upgraded us to go1.17 to use the new t.Setenv method. Let me know if I should rollback for any reason or if there's anything I need to do to make sure Go1.17 is used when releasing. Thanks!

Copy link
Contributor

@josebalius josebalius left a comment

Choose a reason for hiding this comment

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

LGTM, defer to @mislav on the Go version changes

@mislav mislav changed the title Make Codespaces APIs configurable Inherit API endpoint configuration from the Codespaces environment Nov 22, 2021
Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

I've reverted the Go 1.7 change to avoid raising the barrier to entry for open source contributions, but otherwise this looks great! Thank you 🙇

We will add an in-project version of t.Setenv when we revisit #3044

Copy link
Contributor

@mislav mislav left a comment

Choose a reason for hiding this comment

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

I've reverted the Go 1.7 change to avoid raising the barrier to entry for open source contributions, but otherwise this looks great! Thank you 🙇

We will add an in-project version of t.Setenv when we revisit #3044

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Make CS APIs Configurable

3 participants