-
Notifications
You must be signed in to change notification settings - Fork 0
implement change provider - github #98
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
Closed
Closed
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
01b1183
implement change provider - github
rashmi-prithyani ca56f24
Refactor and address code review suggestions
rashmi-prithyani 59e5aec
Refactor GitHub provider to use Config pattern
rashmi-prithyani 1faf6c4
Refactor GitHub change provider to use Client wrapper pattern
rashmi-prithyani 5c812f8
make auth configurable
rashmi-prithyani 330aeb7
fix conflicts and re-do work on the change provider and add tests
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
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,37 @@ | ||
| load("@rules_go//go:def.bzl", "go_library", "go_test") | ||
|
|
||
| go_library( | ||
| name = "github", | ||
| srcs = [ | ||
| "config.go", | ||
| "convert.go", | ||
| "graphql.go", | ||
| "provider.go", | ||
| "validate.go", | ||
| ], | ||
| importpath = "github.com/uber/submitqueue/extension/changeprovider/github", | ||
| visibility = ["//visibility:public"], | ||
| deps = [ | ||
| "//entity", | ||
| "//entity/github", | ||
| "//extension/changeprovider", | ||
| "@com_github_uber_go_tally_v4//:tally", | ||
| "@org_uber_go_zap//:zap", | ||
| ], | ||
| ) | ||
|
|
||
| go_test( | ||
| name = "github_test", | ||
| srcs = [ | ||
| "config_test.go", | ||
| "provider_test.go", | ||
| ], | ||
| embed = [":github"], | ||
| deps = [ | ||
| "//entity", | ||
| "@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,47 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "net/http" | ||
| ) | ||
|
|
||
| // Client is a GitHub API client that encapsulates connection details and authentication. | ||
| // The client is protocol-agnostic - it provides helpers for both GraphQL and REST endpoints. | ||
| type Client struct { | ||
| httpClient *http.Client | ||
| baseURL string | ||
| } | ||
|
|
||
| // NewClient creates a new GitHub API client with a pre-configured HTTP client. | ||
| // The caller is responsible for configuring authentication in the HTTP client's Transport. | ||
| // | ||
| // Parameters: | ||
| // - httpClient: Configured HTTP client (with auth, timeout, transport, etc.) | ||
| // - baseURL: GitHub instance base URL (e.g., "https://api.github.com" or "https://ghe.company.com/api") | ||
| func NewClient(httpClient *http.Client, baseURL string) *Client { | ||
| return &Client{ | ||
| httpClient: httpClient, | ||
| baseURL: baseURL, | ||
| } | ||
| } | ||
|
|
||
| // HTTPClient returns the configured HTTP client. | ||
| func (c *Client) HTTPClient() *http.Client { | ||
| return c.httpClient | ||
| } | ||
|
|
||
| // BaseURL returns the configured GitHub base URL. | ||
| func (c *Client) BaseURL() string { | ||
| return c.baseURL | ||
| } | ||
|
|
||
| // GraphQLURL returns the GitHub GraphQL endpoint URL. | ||
| // Constructs the URL by appending "/graphql" to the base URL. | ||
| func (c *Client) GraphQLURL() string { | ||
| return c.baseURL + "/graphql" | ||
| } | ||
|
|
||
| // RESTURL constructs a GitHub REST API endpoint URL. | ||
| // The path should start with "/" (e.g., "/repos/uber/submitqueue/pulls/123"). | ||
| func (c *Client) RESTURL(path string) string { | ||
| return c.baseURL + path | ||
| } | ||
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,97 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/stretchr/testify/assert" | ||
| ) | ||
|
|
||
| func TestNewClient(t *testing.T) { | ||
| httpClient := &http.Client{Timeout: 30 * time.Second} | ||
| baseURL := "https://api.github.com" | ||
|
|
||
| client := NewClient(httpClient, baseURL) | ||
|
|
||
| assert.NotNil(t, client) | ||
| assert.Equal(t, httpClient, client.HTTPClient()) | ||
| assert.Equal(t, baseURL, client.BaseURL()) | ||
| } | ||
|
|
||
| func TestClient_GraphQLURL(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| baseURL string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "standard github", | ||
| baseURL: "https://api.github.com", | ||
| expected: "https://api.github.com/graphql", | ||
| }, | ||
| { | ||
| name: "github enterprise", | ||
| baseURL: "https://ghe.example.com/api", | ||
| expected: "https://ghe.example.com/api/graphql", | ||
| }, | ||
| { | ||
| name: "localhost", | ||
| baseURL: "http://localhost:8080", | ||
| expected: "http://localhost:8080/graphql", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| client := NewClient(&http.Client{}, tt.baseURL) | ||
| assert.Equal(t, tt.expected, client.GraphQLURL()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestClient_BaseURL(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| baseURL string | ||
| }{ | ||
| {name: "standard github", baseURL: "https://api.github.com"}, | ||
| {name: "github enterprise", baseURL: "https://ghe.example.com/api"}, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| client := NewClient(&http.Client{}, tt.baseURL) | ||
| assert.Equal(t, tt.baseURL, client.BaseURL()) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| func TestClient_RESTURL(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| baseURL string | ||
| path string | ||
| expected string | ||
| }{ | ||
| { | ||
| name: "repos endpoint", | ||
| baseURL: "https://api.github.com", | ||
| path: "/repos/uber/submitqueue/pulls/123", | ||
| expected: "https://api.github.com/repos/uber/submitqueue/pulls/123", | ||
| }, | ||
| { | ||
| name: "enterprise repos endpoint", | ||
| baseURL: "https://ghe.example.com/api", | ||
| path: "/repos/myorg/myrepo/pulls/456", | ||
| expected: "https://ghe.example.com/api/repos/myorg/myrepo/pulls/456", | ||
| }, | ||
| } | ||
|
|
||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| client := NewClient(&http.Client{}, tt.baseURL) | ||
| assert.Equal(t, tt.expected, client.RESTURL(tt.path)) | ||
| }) | ||
| } | ||
| } |
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,42 @@ | ||
| 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 { | ||
| linesModified := 0 | ||
| if file.Additions > 0 && file.Deletions > 0 { | ||
| linesModified = min(file.Additions, file.Deletions) | ||
| } | ||
|
|
||
| changedFiles = append(changedFiles, changeprovider.ChangedFile{ | ||
| Path: file.Path, | ||
| Patch: file.Patch, | ||
| LinesAdded: file.Additions, | ||
| LinesDeleted: file.Deletions, | ||
| LinesModified: linesModified, | ||
| }) | ||
| } | ||
|
|
||
| return changedFiles | ||
| } |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
any reason we need it? httpClient should be enough, it can have baseURL in it, i think we can get rid of config file completely.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I created this config file in response to this previous comment - #98 (comment)
If we are ok to pass in the base url as a param to provider struct, I can revert to the previous version. By itself I dont think the
httpCleinthas facility to configure a baseURL.