Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ require (
github.com/rudderlabs/analytics-go v3.3.2+incompatible // indirect
github.com/segmentio/backo-go v1.0.1 // indirect
github.com/shopspring/decimal v1.2.0 // indirect
github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/stretchr/objx v0.4.0 // indirect
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -304,10 +304,14 @@ github.com/shopspring/decimal v1.2.0/go.mod h1:DKyhrW/HYNuLGql+MJL6WCR6knT2jwCFR
github.com/shurcooL/component v0.0.0-20170202220835-f88ec8f54cc4/go.mod h1:XhFIlyj5a1fBNx5aJTbKoIq0mNaPvOagO+HjB3EtxrY=
github.com/shurcooL/events v0.0.0-20181021180414-410e4ca65f48/go.mod h1:5u70Mqkb5O5cxEA8nxTsgrgLehJeAw6Oc4Ab1c/P1HM=
github.com/shurcooL/github_flavored_markdown v0.0.0-20181002035957-2122de532470/go.mod h1:2dOwnU2uBioM+SGy2aZoq1f/Sd1l9OkAeAUvjSyvgU0=
github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9 h1:nCBaIs5/R0HFP5+aPW/SzFUF8z0oKuCXmuDmHWaxzjY=
github.com/shurcooL/githubv4 v0.0.0-20230424031643-6cea62ecd5a9/go.mod h1:hAF0iLZy4td2EX+/8Tw+4nodhlMrwN3HupfaXj3zkGo=
github.com/shurcooL/go v0.0.0-20180423040247-9e1955d9fb6e/go.mod h1:TDJrrUr11Vxrven61rcy3hJMUqaf/CLWYhHNPmT14Lk=
github.com/shurcooL/go-goon v0.0.0-20170922171312-37c2f522c041/go.mod h1:N5mDOmsrJOB+vfqUK+7DmDyjhSLIIBnXo9lvZJj3MWQ=
github.com/shurcooL/gofontwoff v0.0.0-20180329035133-29b52fc0a18d/go.mod h1:05UtEgK5zq39gLST6uB0cf3NEHjETfB4Fgr3Gx5R9Vw=
github.com/shurcooL/gopherjslib v0.0.0-20160914041154-feb6d3990c2c/go.mod h1:8d3azKNyqcHP1GaQE/c6dDgjkgSx2BZ4IoEi4F1reUI=
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29 h1:B1PEwpArrNp4dkQrfxh/abbBAOZBVp0ds+fBEOUOqOc=
github.com/shurcooL/graphql v0.0.0-20220606043923-3cf50f8a0a29/go.mod h1:AuYgA5Kyo4c7HfUmvRGs/6rGlMMV/6B1bVnB9JxJEEg=
github.com/shurcooL/highlight_diff v0.0.0-20170515013008-09bb4053de1b/go.mod h1:ZpfEhSmds4ytuByIcDnOLkTHGUI6KNqRNPDLHDk+mUU=
github.com/shurcooL/highlight_go v0.0.0-20181028180052-98c3abbbae20/go.mod h1:UDKB5a1T23gOMUJrI+uSuH0VRDStOiUVSjBTRDVBVag=
github.com/shurcooL/home v0.0.0-20181020052607-80b7ffcb30f9/go.mod h1:+rgNQw2P9ARFAs37qieuu7ohDNQ3gds9msbT2yn85sg=
Expand Down
56 changes: 11 additions & 45 deletions server/plugin/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,38 +696,6 @@ func (p *Plugin) getUnreadsData(c *UserContext) []*FilteredNotification {
return filteredNotifications
}

func (p *Plugin) getReviewsData(c *UserContext) []*github.Issue {
config := p.getConfiguration()

githubClient := p.githubConnectUser(c.Context.Ctx, c.GHInfo)
username := c.GHInfo.GitHubUsername

query := getReviewSearchQuery(username, config.GitHubOrg)
result, _, err := githubClient.Search.Issues(c.Ctx, query, &github.SearchOptions{})
if err != nil {
c.Log.WithError(err).With(logger.LogContext{"query": query}).Warnf("Failed to search for review")
return []*github.Issue{}
}

return result.Issues
}

func (p *Plugin) getYourPrsData(c *UserContext) []*github.Issue {
config := p.getConfiguration()

githubClient := p.githubConnectUser(c.Context.Ctx, c.GHInfo)
username := c.GHInfo.GitHubUsername

query := getYourPrsSearchQuery(username, config.GitHubOrg)
result, _, err := githubClient.Search.Issues(c.Ctx, query, &github.SearchOptions{})
if err != nil {
c.Log.WithError(err).With(logger.LogContext{"query": query}).Warnf("Failed to search for PRs")
return []*github.Issue{}
}

return result.Issues
}

func (p *Plugin) getPrsDetails(c *UserContext, w http.ResponseWriter, r *http.Request) {
githubClient := p.githubConnectUser(c.Context.Ctx, c.GHInfo)

Expand Down Expand Up @@ -970,27 +938,25 @@ func (p *Plugin) createIssueComment(c *UserContext, w http.ResponseWriter, r *ht
p.writeJSON(w, result)
}

func (p *Plugin) getYourAssignmentsData(c *UserContext) []*github.Issue {
config := p.getConfiguration()

githubClient := p.githubConnectUser(c.Context.Ctx, c.GHInfo)
func (p *Plugin) getLHSData(c *UserContext) (reviewResp []*github.Issue, assignmentResp []*github.Issue, openPRResp []*github.Issue) {
var err error
graphQLClient := p.graphQLConnect(c.GHInfo)

username := c.GHInfo.GitHubUsername
query := getYourAssigneeSearchQuery(username, config.GitHubOrg)
result, _, err := githubClient.Search.Issues(c.Ctx, query, &github.SearchOptions{})
reviewResp, assignmentResp, openPRResp, err = graphQLClient.GetLHSData()
if err != nil {
c.Log.WithError(err).With(logger.LogContext{"query": query}).Warnf("Failed to search for assignments")
return []*github.Issue{}
c.Log.WithError(err).Warnf("Failed to search for LHS data")
return []*github.Issue{}, []*github.Issue{}, []*github.Issue{}
}

return result.Issues
return reviewResp, assignmentResp, openPRResp
}

func (p *Plugin) getSidebarData(c *UserContext) *SidebarContent {
reviweResp, assignmentResp, openPRResp := p.getLHSData(c)
return &SidebarContent{
Assignments: p.getYourAssignmentsData(c),
PRs: p.getYourPrsData(c),
Reviews: p.getReviewsData(c),
Assignments: assignmentResp,
PRs: openPRResp,
Reviews: reviweResp,
Unreads: p.getUnreadsData(c),
}
}
Expand Down
61 changes: 61 additions & 0 deletions server/plugin/internal/graphql/client.go
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")
Comment thread
Kshitij-Katiyar marked this conversation as resolved.

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
}
91 changes: 91 additions & 0 deletions server/plugin/internal/graphql/lhs_query.go
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)"`
}
119 changes: 119 additions & 0 deletions server/plugin/internal/graphql/lhs_request.go
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) {
Comment thread
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
Comment thread
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,
}
}
6 changes: 6 additions & 0 deletions server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"golang.org/x/oauth2"

root "github.com/mattermost/mattermost-plugin-github"
"github.com/mattermost/mattermost-plugin-github/server/plugin/internal/graphql"
)

const (
Expand Down Expand Up @@ -160,6 +161,11 @@ func (p *Plugin) githubConnectUser(ctx context.Context, info *GitHubUserInfo) *g
return p.githubConnectToken(tok)
}

func (p *Plugin) graphQLConnect(info *GitHubUserInfo) *graphql.Client {
conf := p.getConfiguration()
return graphql.NewClient(p.API, *info.Token, info.GitHubUsername, conf.GitHubOrg, conf.EnterpriseBaseURL)
}

func (p *Plugin) githubConnectToken(token oauth2.Token) *github.Client {
config := p.getConfiguration()

Expand Down
Loading