forked from mattermost/mattermost-plugin-github
-
Notifications
You must be signed in to change notification settings - Fork 1
[MI-3072]:Converted the LHS APIs into GraphQL #32
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
13 commits
Select commit
Hold shift + click to select a range
9ebd818
[MI-3012]: Converted user PRs API into GraphQL
Kshitij-Katiyar ca962b2
[MI-3012]: Fixed review comments
Kshitij-Katiyar c589f80
[MI-3012]:fixed log message
Kshitij-Katiyar 6f1be31
[MI-3035]: Converted the get assignment API into GraphQL
Kshitij-Katiyar fd996dd
[MI-3035]:Fixed self review comments
Kshitij-Katiyar 3a6229f
[MI-3035]: Fixed review comments
Kshitij-Katiyar ba98e78
[MI-3072]:Converted review requested API to graphQL
Kshitij-Katiyar 27350c6
[MI-3072]:Combined all the graphQL queries
Kshitij-Katiyar 732dd38
[MI-3072]:Fixed CI errors
Kshitij-Katiyar de64ffc
[MI-3072]:Fixed review comments
Kshitij-Katiyar fdbbab2
[MI-3072]:Fixed CI errors
Kshitij-Katiyar c9a6c4c
[MI-3072]:Fixed review comments
Kshitij-Katiyar 5938bb4
[MI-3072]:Fixed review comments
Kshitij-Katiyar 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,61 @@ | ||
| package graphql | ||
|
|
||
| import ( | ||
| "context" | ||
| "net/url" | ||
| "path" | ||
|
|
||
| "github.com/mattermost/mattermost-server/v6/plugin" | ||
| "github.com/pkg/errors" | ||
| "github.com/shurcooL/githubv4" | ||
| "golang.org/x/oauth2" | ||
| ) | ||
|
|
||
| // Client encapsulates the third party package that communicates with Github GraphQL API | ||
| type Client struct { | ||
| client *githubv4.Client | ||
| org string | ||
| username string | ||
| api plugin.API | ||
| } | ||
|
|
||
| // NewClient creates and returns Client. The third party package that queries GraphQL is initialized here. | ||
| func NewClient(api plugin.API, token oauth2.Token, username, orgName, enterpriseBaseURL string) *Client { | ||
| ts := oauth2.StaticTokenSource(&token) | ||
| httpClient := oauth2.NewClient(context.Background(), ts) | ||
| var client Client | ||
|
|
||
| if enterpriseBaseURL == "" || orgName == "" { | ||
| client = Client{ | ||
| username: username, | ||
| client: githubv4.NewClient(httpClient), | ||
| api: api, | ||
| } | ||
| } else { | ||
| baseURL, err := url.Parse(enterpriseBaseURL) | ||
| if err != nil { | ||
| api.LogDebug("Not able to parse the URL", "Error", err.Error()) | ||
| return nil | ||
| } | ||
|
|
||
| baseURL.Path = path.Join(baseURL.Path, "api", "graphql") | ||
|
|
||
| client = Client{ | ||
| client: githubv4.NewEnterpriseClient(baseURL.String(), httpClient), | ||
| username: username, | ||
| org: orgName, | ||
| api: api, | ||
| } | ||
| } | ||
|
|
||
| return &client | ||
| } | ||
|
|
||
| // executeQuery takes a query struct and sends it to Github GraphQL API via helper package. | ||
| func (c *Client) executeQuery(qry interface{}, params map[string]interface{}) error { | ||
| if err := c.client.Query(context.Background(), qry, params); err != nil { | ||
| return errors.Wrap(err, "error in executing query") | ||
| } | ||
|
|
||
| return 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,91 @@ | ||
| package graphql | ||
|
|
||
| import ( | ||
| "github.com/shurcooL/githubv4" | ||
| ) | ||
|
|
||
| type ( | ||
| repositoryQuery struct { | ||
| Name githubv4.String | ||
| NameWithOwner githubv4.String | ||
| URL githubv4.URI | ||
| } | ||
|
|
||
| authorQuery struct { | ||
| Login githubv4.String | ||
| } | ||
|
|
||
| prSearchNodes struct { | ||
| PullRequest struct { | ||
| Body githubv4.String | ||
| Number githubv4.Int | ||
| AuthorAssociation githubv4.String | ||
| CreatedAt githubv4.DateTime | ||
| UpdatedAt githubv4.DateTime | ||
| Repository repositoryQuery | ||
| State githubv4.String | ||
| Title githubv4.String | ||
| Author authorQuery | ||
| URL githubv4.URI | ||
| } `graphql:"... on PullRequest"` | ||
| } | ||
| ) | ||
|
|
||
| type ( | ||
| assignmentSearchNodes struct { | ||
| Issue struct { | ||
| Body githubv4.String | ||
| Number githubv4.Int | ||
| AuthorAssociation githubv4.String | ||
| CreatedAt githubv4.DateTime | ||
| UpdatedAt githubv4.DateTime | ||
| Repository repositoryQuery | ||
| State githubv4.String | ||
| Title githubv4.String | ||
| Author authorQuery | ||
| URL githubv4.URI | ||
| } `graphql:"... on Issue"` | ||
|
|
||
| PullRequest struct { | ||
| Body githubv4.String | ||
| Number githubv4.Int | ||
| AuthorAssociation githubv4.String | ||
| CreatedAt githubv4.DateTime | ||
| UpdatedAt githubv4.DateTime | ||
| Repository repositoryQuery | ||
| State githubv4.String | ||
| Title githubv4.String | ||
| Author authorQuery | ||
| URL githubv4.URI | ||
| } `graphql:"... on PullRequest"` | ||
| } | ||
| ) | ||
|
|
||
| var mainQuery struct { | ||
| PullRequest struct { | ||
| IssueCount int | ||
| Nodes []prSearchNodes | ||
| PageInfo struct { | ||
| EndCursor githubv4.String | ||
| HasNextPage bool | ||
| } | ||
| } `graphql:"pullRequest: search(first:100, after:$reviewCursor, query: $prReviewQueryArg, type: ISSUE)"` | ||
|
|
||
| Assignee struct { | ||
| IssueCount int | ||
| Nodes []assignmentSearchNodes | ||
| PageInfo struct { | ||
| EndCursor githubv4.String | ||
| HasNextPage bool | ||
| } | ||
| } `graphql:"assignee: search(first:100, after:$assignmentsCursor, query: $assigneeQueryArg, type: ISSUE)"` | ||
|
|
||
| OpenPullRequest struct { | ||
| IssueCount int | ||
| Nodes []prSearchNodes | ||
| PageInfo struct { | ||
| EndCursor githubv4.String | ||
| HasNextPage bool | ||
| } | ||
| } `graphql:"graphql: search(first:100, after:$openPrsCursor, query: $prOpenQueryArg, type: ISSUE)"` | ||
| } |
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,119 @@ | ||
| package graphql | ||
|
|
||
| import ( | ||
| "fmt" | ||
|
|
||
| "github.com/google/go-github/v41/github" | ||
| "github.com/shurcooL/githubv4" | ||
| ) | ||
|
|
||
| const ( | ||
| queryParamReviewCursor = "reviewCursor" | ||
| queryParamAssignmentsCursor = "assignmentsCursor" | ||
| queryParamOpenPRsCursor = "openPrsCursor" | ||
|
|
||
| queryParamOpenPRQueryArg = "prOpenQueryArg" | ||
| queryParamReviewPRQueryArg = "prReviewQueryArg" | ||
| queryParamAssigneeQueryArg = "assigneeQueryArg" | ||
| ) | ||
|
|
||
| func (c *Client) GetLHSData() ([]*github.Issue, []*github.Issue, []*github.Issue, error) { | ||
|
Kshitij-Katiyar marked this conversation as resolved.
|
||
| params := map[string]interface{}{ | ||
| queryParamOpenPRQueryArg: githubv4.String(fmt.Sprintf("author:%s is:pr is:%s archived:false", c.username, githubv4.PullRequestStateOpen)), | ||
| queryParamReviewPRQueryArg: githubv4.String(fmt.Sprintf("review-requested:%s is:pr is:%s archived:false", c.username, githubv4.PullRequestStateOpen)), | ||
| queryParamAssigneeQueryArg: githubv4.String(fmt.Sprintf("assignee:%s is:%s archived:false", c.username, githubv4.PullRequestStateOpen)), | ||
| queryParamReviewCursor: (*githubv4.String)(nil), | ||
| queryParamAssignmentsCursor: (*githubv4.String)(nil), | ||
| queryParamOpenPRsCursor: (*githubv4.String)(nil), | ||
| } | ||
|
|
||
| if c.org != "" { | ||
| params[queryParamOpenPRQueryArg] = githubv4.String(fmt.Sprintf("org:%s %s", c.org, params[queryParamOpenPRQueryArg])) | ||
| params[queryParamReviewPRQueryArg] = githubv4.String(fmt.Sprintf("org:%s %s", c.org, params[queryParamReviewPRQueryArg])) | ||
| params[queryParamAssigneeQueryArg] = githubv4.String(fmt.Sprintf("org:%s %s", c.org, params[queryParamAssigneeQueryArg])) | ||
| } | ||
|
|
||
| var resultPR, resultAssignee, resultOpenPR []*github.Issue | ||
| flagPR, flagAssignee, flagOpenPR := false, false, false | ||
|
|
||
| for { | ||
| if flagPR && flagAssignee && flagOpenPR { | ||
| break | ||
| } | ||
|
|
||
| if err := c.executeQuery(&mainQuery, params); err != nil { | ||
| return nil, nil, nil, err | ||
| } | ||
|
|
||
| if !flagPR { | ||
| for _, resp := range mainQuery.PullRequest.Nodes { | ||
| resp := resp | ||
|
manojmalik20 marked this conversation as resolved.
|
||
| pr := getPROrIssue(&resp, nil) | ||
| resultPR = append(resultPR, pr) | ||
| } | ||
|
|
||
| if !mainQuery.PullRequest.PageInfo.HasNextPage { | ||
| flagPR = true | ||
| } | ||
|
|
||
| params[queryParamReviewCursor] = githubv4.NewString(mainQuery.PullRequest.PageInfo.EndCursor) | ||
| } | ||
|
|
||
| if !flagAssignee { | ||
| for _, resp := range mainQuery.Assignee.Nodes { | ||
| resp := resp | ||
| prOrIssue := getPROrIssue(nil, &resp) | ||
| resultAssignee = append(resultAssignee, prOrIssue) | ||
| } | ||
|
|
||
| if !mainQuery.Assignee.PageInfo.HasNextPage { | ||
| flagAssignee = true | ||
| } | ||
|
|
||
| params[queryParamAssignmentsCursor] = githubv4.NewString(mainQuery.Assignee.PageInfo.EndCursor) | ||
| } | ||
|
|
||
| if !flagOpenPR { | ||
| for _, resp := range mainQuery.OpenPullRequest.Nodes { | ||
| resp := resp | ||
| pr := getPROrIssue(&resp, nil) | ||
| resultOpenPR = append(resultOpenPR, pr) | ||
| } | ||
|
|
||
| if !mainQuery.OpenPullRequest.PageInfo.HasNextPage { | ||
| flagOpenPR = true | ||
| } | ||
|
|
||
| params[queryParamOpenPRsCursor] = githubv4.NewString(mainQuery.OpenPullRequest.PageInfo.EndCursor) | ||
| } | ||
| } | ||
|
|
||
| return resultPR, resultAssignee, resultOpenPR, nil | ||
| } | ||
|
|
||
| func getPROrIssue(prResp *prSearchNodes, assignmentResp *assignmentSearchNodes) *github.Issue { | ||
| resp := prResp.PullRequest | ||
| if assignmentResp != nil { | ||
| if assignmentResp.Issue.Number == 0 { | ||
| resp = assignmentResp.PullRequest | ||
| } | ||
| } | ||
|
|
||
| prNumber := int(resp.Number) | ||
| repositoryURL := resp.Repository.URL.String() | ||
| htmlURL := resp.URL.String() | ||
| title := string(resp.Title) | ||
| createdAt := resp.CreatedAt.Time | ||
| updatedAt := resp.UpdatedAt.Time | ||
| return &github.Issue{ | ||
| Number: &prNumber, | ||
| RepositoryURL: &repositoryURL, | ||
| Title: &title, | ||
| CreatedAt: &createdAt, | ||
| UpdatedAt: &updatedAt, | ||
| User: &github.User{ | ||
| Login: (*string)(&resp.Author.Login), | ||
| }, | ||
| HTMLURL: &htmlURL, | ||
| } | ||
| } | ||
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
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.