-
Notifications
You must be signed in to change notification settings - Fork 0
implement change provider #141
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
d12205d
feat(provider): implement change provider interface with GitHub integ…
rashmi-prithyani 3086713
make http client configurable with base url and bearer transport
rashmi-prithyani c9eac59
remove lines modified as github api doesnt give that and also replaced
rashmi-prithyani 008f7d2
make oauth and timeout setup happen outside the http client
rashmi-prithyani File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| load("@rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "httpclient", | ||
| srcs = ["transport.go"], | ||
| importpath = "github.com/uber/submitqueue/core/httpclient", | ||
| visibility = ["//visibility:public"], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "httpclient_test", | ||
| srcs = ["transport_test.go"], | ||
| embed = [":httpclient"], | ||
| deps = [ | ||
| "@com_github_stretchr_testify//assert", | ||
| "@com_github_stretchr_testify//require", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| package httpclient | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/url" | ||
| "strings" | ||
| ) | ||
|
|
||
| // BaseURLTransport is an http.RoundTripper that rewrites every request URL | ||
| // to resolve against a fixed base URL. This allows callers to make requests | ||
| // with relative paths (e.g. "/graphql") and have the transport prepend the | ||
| // configured base URL transparently. | ||
| type BaseURLTransport struct { | ||
| // BaseURL is the API base URL (e.g. "https://api.github.com"). | ||
| BaseURL *url.URL | ||
| // Next is the underlying RoundTripper. Defaults to http.DefaultTransport if nil. | ||
| Next http.RoundTripper | ||
| } | ||
|
|
||
| // RoundTrip rewrites req.URL to resolve against BaseURL, then delegates to Next. | ||
| // The base URL path and request path are joined explicitly so that base URLs | ||
| // with a path component (e.g. "https://ghe.example.com/api") are handled | ||
| // correctly regardless of whether the request path starts with "/". | ||
| func (t *BaseURLTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| newReq := req.Clone(req.Context()) | ||
|
|
||
| merged := *t.BaseURL | ||
| merged.Path = strings.TrimRight(t.BaseURL.Path, "/") + "/" + strings.TrimLeft(req.URL.Path, "/") | ||
| merged.RawQuery = req.URL.RawQuery | ||
| newReq.URL = &merged | ||
|
|
||
| next := t.Next | ||
| if next == nil { | ||
| next = http.DefaultTransport | ||
| } | ||
| return next.RoundTrip(newReq) | ||
| } | ||
|
|
||
| // NewClient builds an *http.Client with BaseURLTransport configured. | ||
| // Callers are responsible for layering additional transports (e.g. auth) and | ||
| // setting Timeout on the returned client. | ||
| func NewClient(rawBaseURL string) (*http.Client, error) { | ||
| u, err := url.Parse(rawBaseURL) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return &http.Client{Transport: &BaseURLTransport{ | ||
| BaseURL: u, | ||
| Next: http.DefaultTransport, | ||
| }}, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package httpclient | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "net/url" | ||
| "testing" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| // roundTripFunc is a test helper that implements http.RoundTripper via a function. | ||
| type roundTripFunc func(*http.Request) (*http.Response, error) | ||
|
|
||
| func (f roundTripFunc) RoundTrip(req *http.Request) (*http.Response, error) { | ||
| return f(req) | ||
| } | ||
|
|
||
| func TestBaseURLTransport_RewritesURL(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| baseURL string | ||
| requestPath string | ||
| wantURL string | ||
| }{ | ||
| { | ||
| name: "relative path resolved against base", | ||
| baseURL: "https://api.github.com", | ||
| requestPath: "/graphql", | ||
| wantURL: "https://api.github.com/graphql", | ||
| }, | ||
| { | ||
| name: "enterprise base URL", | ||
| baseURL: "https://ghe.example.com/api", | ||
| requestPath: "/graphql", | ||
| wantURL: "https://ghe.example.com/api/graphql", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| var capturedURL string | ||
| transport := &BaseURLTransport{ | ||
| BaseURL: mustParseURL(t, tt.baseURL), | ||
| Next: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| capturedURL = req.URL.String() | ||
| return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil | ||
| }), | ||
| } | ||
|
|
||
| req, err := http.NewRequest(http.MethodGet, tt.requestPath, nil) | ||
| require.NoError(t, err) | ||
|
|
||
| _, err = transport.RoundTrip(req) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, tt.wantURL, capturedURL) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestBaseURLTransport_DoesNotMutateOriginalRequest(t *testing.T) { | ||
| transport := &BaseURLTransport{ | ||
| BaseURL: mustParseURL(t, "https://api.github.com"), | ||
| Next: roundTripFunc(func(req *http.Request) (*http.Response, error) { | ||
| return &http.Response{StatusCode: http.StatusOK, Body: http.NoBody}, nil | ||
| }), | ||
| } | ||
|
|
||
| req, err := http.NewRequest(http.MethodGet, "/graphql", nil) | ||
| require.NoError(t, err) | ||
| originalURL := req.URL.String() | ||
|
|
||
| _, err = transport.RoundTrip(req) | ||
| require.NoError(t, err) | ||
| assert.Equal(t, originalURL, req.URL.String()) | ||
| } | ||
|
|
||
| func TestNewClient_InvalidURL(t *testing.T) { | ||
| _, err := NewClient("://invalid") | ||
| require.Error(t, err) | ||
| } | ||
|
|
||
| func mustParseURL(t *testing.T, raw string) *url.URL { | ||
| t.Helper() | ||
| u, err := url.Parse(raw) | ||
| require.NoError(t, err) | ||
| return u | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| load("@rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "github", | ||
| srcs = [ | ||
| "convert.go", | ||
| "graphql.go", | ||
| "provider.go", | ||
| "validate.go", | ||
| ], | ||
| importpath = "github.com/uber/submitqueue/extension/changeprovider/github", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//core/metrics", | ||
| "//entity", | ||
| "//entity/github", | ||
| "//extension/changeprovider", | ||
| "@com_github_uber_go_tally_v4//:tally", | ||
| "@org_uber_go_zap//:zap", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "github_test", | ||
| srcs = [ | ||
| "graphql_test.go", | ||
| "provider_test.go", | ||
| "validate_test.go", | ||
| ], | ||
| embed = [":github"], | ||
| deps = [ | ||
| "//core/httpclient", | ||
| "//entity", | ||
| "//entity/github", | ||
| "//extension/changeprovider", | ||
| "@com_github_stretchr_testify//assert", | ||
| "@com_github_stretchr_testify//require", | ||
| "@com_github_uber_go_tally_v4//:tally", | ||
| "@org_uber_go_zap//zaptest", | ||
| ], | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| entitygithub "github.com/uber/submitqueue/entity/github" | ||
| "github.com/uber/submitqueue/extension/changeprovider" | ||
| ) | ||
|
|
||
| // convertToChangeInfo converts GitHub PR data to ChangeInfo. | ||
| func convertToChangeInfo(parsed entitygithub.ChangeID, prData *pullRequestData) changeprovider.ChangeInfo { | ||
| changedFiles := convertFiles(prData.Files.Nodes) | ||
|
|
||
| return changeprovider.ChangeInfo{ | ||
| URI: parsed.String(), | ||
| User: changeprovider.User{ | ||
| Name: prData.Author.Name, | ||
| Email: prData.Author.Email, | ||
| }, | ||
| ChangedFiles: changedFiles, | ||
| } | ||
| } | ||
|
|
||
| // convertFiles converts GitHub file nodes to ChangedFile structs. | ||
| func convertFiles(nodes []fileNode) []changeprovider.ChangedFile { | ||
| changedFiles := make([]changeprovider.ChangedFile, 0, len(nodes)) | ||
|
|
||
| for _, file := range nodes { | ||
| changedFiles = append(changedFiles, changeprovider.ChangedFile{ | ||
| Path: file.Path, | ||
| Patch: file.Patch, | ||
| LinesAdded: file.Additions, | ||
| LinesDeleted: file.Deletions, | ||
| }) | ||
| } | ||
|
|
||
| return changedFiles | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.