migueleliasweb / go-github-mock

A library to aid unittesting code that uses Golang's Github SDK
MIT License
94 stars 22 forks source link

document how to work with github enterprise server #51

Closed atkinsonm closed 1 year ago

atkinsonm commented 1 year ago

Working with GitHub enterprise server is entirely supported, but took a bit of digging for me to figure out. For example, I was attempting to mock the response for client.Repositories.ListByOrg, and so my first instinct was to use mock.GetOrgsReposByOrg in mock.WithRequestMatchPages. However, this was giving me a 404 error: 404 mock response not found for /api/v3/orgs/<orgname>/repos [], wantErr = <nil>. What I needed to use instead of mock.GetOrgsReposByOrg was

mock.EndpointPattern{
    Pattern: "/api/v3/orgs/{org}/repos",
    Method:  "GET",
},

which allowed me to fit the response expected by github.NewEnterpriseClient. For others looking to use this library for mocking calls to GHES, I think it would be helpful to include this in the documentation or include some option that would allow the endpoint patterns to work with GHES endpoints.

migueleliasweb commented 1 year ago

Hi @atkinsonm , you might be right the support for GHE is not quite tested yet.

TBH, I've never worked with it and just by going a quick dig around the generated file, I don't think there is any endpoints with /api/v3 as a prefix.

Do you know if this is a common pattern for the GHE stuff?

I'm happy to get some examples from you then I can create a test suite and fix the generation script.

atkinsonm commented 1 year ago

Yes, /api/v3 is the common path prefix for GHES according to https://docs.github.com/en/enterprise-server@3.8/rest/enterprise-admin. I think it's safe to assume that all GHES endpoint patterns match the ones in this library plus the prefix. Perhaps there's a way to inject that for all EndpointPatterns without duplicating them. A new option to NewMockedHTTPClient that prepends /api/v3 might be the way to do that.

The only other significant distinction with working with GHES is using go-github's c.NewEnterpriseClient(uri, uri, httpClient) instead of c.NewClient(httpClient), but that's outside of this library, which is why I think NewMockedHTTPClient might be the place to handle this case with the default still assuming github.com style paths.

migueleliasweb commented 1 year ago

After a quick read on the go-github's code, I think you're right, the prefix /api/v3 is common to the enterprise client. Turns out it is injected via the client itself. I've found it interesting: https://github.com/google/go-github/blob/8c7625e6a26563e0e031916cc44231912fc52e49/github/github.go#L362

I will give it a go on injecting the prefix via the client. I'm think on using a new function to bootstrap the enterprise client might be more clear, instead of adding an option to the NewMockedHTTPClient. Let me investigate a few options and come back to you.

Thanks for reaching out 👍 .

migueleliasweb commented 1 year ago

Hi @atkinsonm , could you check out https://github.com/migueleliasweb/go-github-mock/pull/53?

Let me know if it works for you.

migueleliasweb commented 1 year ago

Closed for now with https://github.com/migueleliasweb/go-github-mock/pull/53

migueleliasweb commented 1 year ago

@atkinsonm new release created :)