Skip to content

Commit

Permalink
[MI-3561] Fixed issue: Getting multiple DMs when Oauth token is revok…
Browse files Browse the repository at this point in the history
…ed (#42) (#408)

* [MI-3561] Fixed issue: Getting multiple DMs when Oauth token is revoked
- Created a single API to get the data for LHS and todo command.

* [MI-3561] Review fixes
1. Replaced 'babel-eslint' package with '@babel/eslint-parser' as the previous one was deprecated

* [MI-3561] Review fixes

* [MI-3561] Review fix
  • Loading branch information
raghavaggarwal2308 authored Nov 21, 2023
1 parent 7190bfd commit c56accf
Show file tree
Hide file tree
Showing 17 changed files with 177 additions and 397 deletions.
72 changes: 4 additions & 68 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (

"github.com/gorilla/mux"
"github.com/pkg/errors"
gitlabLib "github.com/xanzy/go-gitlab"
"golang.org/x/oauth2"

"github.com/mattermost/mattermost/server/public/model"
Expand Down Expand Up @@ -55,11 +54,8 @@ func (p *Plugin) initializeAPI() {

apiRouter.HandleFunc("/user", p.checkAuth(p.attachContext(p.getGitlabUser), ResponseTypeJSON)).Methods(http.MethodPost)
apiRouter.HandleFunc("/todo", p.checkAuth(p.attachUserContext(p.postToDo), ResponseTypeJSON)).Methods(http.MethodPost)
apiRouter.HandleFunc("/reviews", p.checkAuth(p.attachUserContext(p.getReviews), ResponseTypePlain)).Methods(http.MethodGet)
apiRouter.HandleFunc("/yourprs", p.checkAuth(p.attachUserContext(p.getYourPrs), ResponseTypePlain)).Methods(http.MethodGet)
apiRouter.HandleFunc("/lhs-data", p.checkAuth(p.attachUserContext(p.getLHSData), ResponseTypePlain)).Methods(http.MethodGet)
apiRouter.HandleFunc("/prdetails", p.checkAuth(p.attachUserContext(p.getPrDetails), ResponseTypePlain)).Methods(http.MethodPost)
apiRouter.HandleFunc("/yourassignments", p.checkAuth(p.attachUserContext(p.getYourAssignments), ResponseTypePlain)).Methods(http.MethodGet)
apiRouter.HandleFunc("/unreads", p.checkAuth(p.attachUserContext(p.getUnreads), ResponseTypePlain)).Methods(http.MethodGet)
apiRouter.HandleFunc("/settings", p.checkAuth(p.attachUserContext(p.updateSettings), ResponseTypePlain)).Methods(http.MethodPost)

apiRouter.HandleFunc("/channel/{channel_id:[A-Za-z0-9]+}/subscriptions", p.checkAuth(p.attachUserContext(p.getChannelSubscriptions), ResponseTypeJSON)).Methods(http.MethodGet)
Expand Down Expand Up @@ -530,66 +526,6 @@ func (p *Plugin) getConnected(c *Context, w http.ResponseWriter, r *http.Request
p.writeAPIResponse(w, resp)
}

func (p *Plugin) getUnreads(c *UserContext, w http.ResponseWriter, r *http.Request) {
var result []*gitlabLib.Todo
err := p.useGitlabClient(c.GitlabInfo, func(info *gitlab.UserInfo, token *oauth2.Token) error {
resp, err := p.GitlabClient.GetUnreads(c.Ctx, info, token)
if err != nil {
return err
}
result = resp
return nil
})

if err != nil {
c.Log.WithError(err).Warnf("Unable to list unreads in GitLab API")
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Unable to list unreads in GitLab API.", StatusCode: http.StatusInternalServerError})
return
}

p.writeAPIResponse(w, result)
}

func (p *Plugin) getReviews(c *UserContext, w http.ResponseWriter, r *http.Request) {
var result []*gitlab.MergeRequest
err := p.useGitlabClient(c.GitlabInfo, func(info *gitlab.UserInfo, token *oauth2.Token) error {
resp, err := p.GitlabClient.GetReviews(c.Ctx, info, token)
if err != nil {
return err
}
result = resp
return nil
})

if err != nil {
c.Log.WithError(err).Warnf("Unable to list merge-request where assignee in GitLab API")
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Unable to list merge-request in GitLab API.", StatusCode: http.StatusInternalServerError})
return
}

p.writeAPIResponse(w, result)
}

func (p *Plugin) getYourPrs(c *UserContext, w http.ResponseWriter, r *http.Request) {
var result []*gitlab.MergeRequest
err := p.useGitlabClient(c.GitlabInfo, func(info *gitlab.UserInfo, token *oauth2.Token) error {
resp, err := p.GitlabClient.GetYourPrs(c.Ctx, info, token)
if err != nil {
return err
}
result = resp
return nil
})

if err != nil {
c.Log.WithError(err).Warnf("Can't list merge-request where author in GitLab API")
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Unable to list merge-request in GitLab API.", StatusCode: http.StatusInternalServerError})
return
}

p.writeAPIResponse(w, result)
}

func (p *Plugin) getPrDetails(c *UserContext, w http.ResponseWriter, r *http.Request) {
var prList []*gitlab.PRDetails
if err := json.NewDecoder(r.Body).Decode(&prList); err != nil {
Expand All @@ -615,10 +551,10 @@ func (p *Plugin) getPrDetails(c *UserContext, w http.ResponseWriter, r *http.Req
p.writeAPIResponse(w, result)
}

func (p *Plugin) getYourAssignments(c *UserContext, w http.ResponseWriter, r *http.Request) {
var result []*gitlab.Issue
func (p *Plugin) getLHSData(c *UserContext, w http.ResponseWriter, r *http.Request) {
var result *gitlab.LHSContent
err := p.useGitlabClient(c.GitlabInfo, func(info *gitlab.UserInfo, token *oauth2.Token) error {
resp, err := p.GitlabClient.GetYourAssignments(c.Ctx, info, token)
resp, err := p.GitlabClient.GetLHSData(c.Ctx, info, token)
if err != nil {
return err
}
Expand Down
111 changes: 62 additions & 49 deletions server/gitlab/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,13 @@ type Issue struct {
LabelsWithDetails []*internGitlab.Label `json:"labels_with_details,omitempty"`
}

type LHSContent struct {
PRs []*MergeRequest `json:"prs"`
Reviews []*MergeRequest `json:"reviews"`
Assignments []*Issue `json:"assignments"`
Unreads []*internGitlab.Todo `json:"unreads"`
}

// NewGroupHook creates a webhook associated with a GitLab group
func (g *gitlab) NewGroupHook(ctx context.Context, user *UserInfo, token *oauth2.Token, groupName string, webhookOptions *AddWebhookOptions) (*WebhookInfo, error) {
client, err := g.GitlabConnect(*token)
Expand Down Expand Up @@ -282,12 +289,51 @@ func (g *gitlab) GetProject(ctx context.Context, user *UserInfo, token *oauth2.T
return project, nil
}

func (g *gitlab) GetReviews(ctx context.Context, user *UserInfo, token *oauth2.Token) ([]*MergeRequest, error) {
func (g *gitlab) GetLHSData(ctx context.Context, user *UserInfo, token *oauth2.Token) (*LHSContent, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}

grp, ctx := errgroup.WithContext(ctx)

var reviews []*MergeRequest
grp.Go(func() error {
reviews, err = g.GetReviews(ctx, user, client)
return err
})

var assignments []*Issue
grp.Go(func() error {
assignments, err = g.GetYourAssignments(ctx, user, client)
return err
})

var mergeRequests []*MergeRequest
grp.Go(func() error {
mergeRequests, err = g.GetYourPrs(ctx, user, client)
return err
})

var unreads []*internGitlab.Todo
grp.Go(func() error {
unreads, err = g.GetUnreads(ctx, user, client)
return err
})

if err := grp.Wait(); err != nil {
return nil, err
}

return &LHSContent{
Reviews: reviews,
PRs: mergeRequests,
Assignments: assignments,
Unreads: unreads,
}, nil
}

func (g *gitlab) GetReviews(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*MergeRequest, error) {
opened := stateOpened
scope := scopeAll

Expand All @@ -300,9 +346,7 @@ func (g *gitlab) GetReviews(ctx context.Context, user *UserInfo, token *oauth2.T
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
var current []*internGitlab.MergeRequest
var resp *internGitlab.Response
current, resp, err = client.MergeRequests.ListMergeRequests(opt)
current, resp, err := client.MergeRequests.ListMergeRequests(opt)
if err != nil {
return nil, err
}
Expand All @@ -320,9 +364,7 @@ func (g *gitlab) GetReviews(ctx context.Context, user *UserInfo, token *oauth2.T
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
var current []*internGitlab.MergeRequest
var resp *internGitlab.Response
current, resp, err = client.MergeRequests.ListGroupMergeRequests(g.gitlabGroup, opt)
current, resp, err := client.MergeRequests.ListGroupMergeRequests(g.gitlabGroup, opt)
if err != nil {
return nil, err
}
Expand All @@ -337,8 +379,7 @@ func (g *gitlab) GetReviews(ctx context.Context, user *UserInfo, token *oauth2.T
mergeRequests := []*MergeRequest{}
for _, mr := range mrs {
if mr.Labels != nil {
var labelsWithDetails []*internGitlab.Label
labelsWithDetails, err = g.GetLabelDetails(client, mr.ProjectID, mr.Labels)
labelsWithDetails, err := g.GetLabelDetails(client, mr.ProjectID, mr.Labels)
if err != nil {
return nil, err
}
Expand All @@ -350,20 +391,13 @@ func (g *gitlab) GetReviews(ctx context.Context, user *UserInfo, token *oauth2.T
}
}

return mergeRequests, err
return mergeRequests, nil
}

func (g *gitlab) GetYourPrs(ctx context.Context, user *UserInfo, token *oauth2.Token) ([]*MergeRequest, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}

func (g *gitlab) GetYourPrs(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*MergeRequest, error) {
opened := stateOpened
scope := scopeAll

var mrs []*internGitlab.MergeRequest

if g.gitlabGroup == "" {
opt := &internGitlab.ListMergeRequestsOptions{
AuthorID: &user.GitlabUserID,
Expand All @@ -372,9 +406,7 @@ func (g *gitlab) GetYourPrs(ctx context.Context, user *UserInfo, token *oauth2.T
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
var current []*internGitlab.MergeRequest
var resp *internGitlab.Response
current, resp, err = client.MergeRequests.ListMergeRequests(opt)
current, resp, err := client.MergeRequests.ListMergeRequests(opt)
if err != nil {
return nil, err
}
Expand All @@ -392,9 +424,7 @@ func (g *gitlab) GetYourPrs(ctx context.Context, user *UserInfo, token *oauth2.T
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
var current []*internGitlab.MergeRequest
var resp *internGitlab.Response
current, resp, err = client.MergeRequests.ListGroupMergeRequests(g.gitlabGroup, opt)
current, resp, err := client.MergeRequests.ListGroupMergeRequests(g.gitlabGroup, opt)
if err != nil {
return nil, err
}
Expand All @@ -409,8 +439,7 @@ func (g *gitlab) GetYourPrs(ctx context.Context, user *UserInfo, token *oauth2.T
mergeRequests := []*MergeRequest{}
for _, mr := range mrs {
if mr.Labels != nil {
var labelsWithDetails []*internGitlab.Label
labelsWithDetails, err = g.GetLabelDetails(client, mr.ProjectID, mr.Labels)
labelsWithDetails, err := g.GetLabelDetails(client, mr.ProjectID, mr.Labels)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -518,14 +547,9 @@ func (g *gitlab) fetchYourPrDetails(c context.Context, log logger.Logger, client
return nil
}

func (g *gitlab) GetYourAssignments(ctx context.Context, user *UserInfo, token *oauth2.Token) ([]*Issue, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
func (g *gitlab) GetYourAssignments(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*Issue, error) {
opened := stateOpened
scope := scopeAll

var issues []*internGitlab.Issue

if g.gitlabGroup == "" {
Expand All @@ -536,9 +560,7 @@ func (g *gitlab) GetYourAssignments(ctx context.Context, user *UserInfo, token *
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
var current []*internGitlab.Issue
var resp *internGitlab.Response
current, resp, err = client.Issues.ListIssues(opt)
current, resp, err := client.Issues.ListIssues(opt)
if err != nil {
return nil, err
}
Expand All @@ -556,9 +578,7 @@ func (g *gitlab) GetYourAssignments(ctx context.Context, user *UserInfo, token *
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
var current []*internGitlab.Issue
var resp *internGitlab.Response
current, resp, err = client.Issues.ListGroupIssues(g.gitlabGroup, opt)
current, resp, err := client.Issues.ListGroupIssues(g.gitlabGroup, opt)
if err != nil {
return nil, err
}
Expand All @@ -573,8 +593,7 @@ func (g *gitlab) GetYourAssignments(ctx context.Context, user *UserInfo, token *
var result []*Issue
for _, issue := range issues {
if issue.Labels != nil {
var labelsWithDetails []*internGitlab.Label
labelsWithDetails, err = g.GetLabelDetails(client, issue.ProjectID, issue.Labels)
labelsWithDetails, err := g.GetLabelDetails(client, issue.ProjectID, issue.Labels)
if err != nil {
return nil, err
}
Expand All @@ -588,20 +607,14 @@ func (g *gitlab) GetYourAssignments(ctx context.Context, user *UserInfo, token *
return result, nil
}

func (g *gitlab) GetUnreads(ctx context.Context, user *UserInfo, token *oauth2.Token) ([]*internGitlab.Todo, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}

func (g *gitlab) GetUnreads(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*internGitlab.Todo, error) {
var todos []*internGitlab.Todo

opt := &internGitlab.ListTodosOptions{
ListOptions: internGitlab.ListOptions{Page: 1, PerPage: perPage},
}
for {
var current []*internGitlab.Todo
var resp *internGitlab.Response
current, resp, err = client.Todos.ListTodos(opt)
current, resp, err := client.Todos.ListTodos(opt)
if err != nil {
return nil, errors.Wrap(err, "can't list todo in GitLab api")
}
Expand Down
9 changes: 5 additions & 4 deletions server/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ type Gitlab interface {
GetUserDetails(ctx context.Context, user *UserInfo, token *oauth2.Token) (*internGitlab.User, error)
GetProject(ctx context.Context, user *UserInfo, token *oauth2.Token, owner, repo string) (*internGitlab.Project, error)
GetYourPrDetails(ctx context.Context, log logger.Logger, user *UserInfo, token *oauth2.Token, prList []*PRDetails) ([]*PRDetails, error)
GetReviews(ctx context.Context, user *UserInfo, token *oauth2.Token) ([]*MergeRequest, error)
GetYourPrs(ctx context.Context, user *UserInfo, token *oauth2.Token) ([]*MergeRequest, error)
GetYourAssignments(ctx context.Context, user *UserInfo, token *oauth2.Token) ([]*Issue, error)
GetUnreads(ctx context.Context, user *UserInfo, token *oauth2.Token) ([]*internGitlab.Todo, error)
GetReviews(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*MergeRequest, error)
GetYourPrs(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*MergeRequest, error)
GetLHSData(ctx context.Context, user *UserInfo, token *oauth2.Token) (*LHSContent, error)
GetYourAssignments(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*Issue, error)
GetUnreads(ctx context.Context, user *UserInfo, client *internGitlab.Client) ([]*internGitlab.Todo, error)
GetProjectHooks(ctx context.Context, user *UserInfo, token *oauth2.Token, owner string, repo string) ([]*WebhookInfo, error)
GetGroupHooks(ctx context.Context, user *UserInfo, token *oauth2.Token, owner string) ([]*WebhookInfo, error)
NewProjectHook(ctx context.Context, user *UserInfo, token *oauth2.Token, projectID interface{}, projectHookOptions *AddWebhookOptions) (*WebhookInfo, error)
Expand Down
Loading

0 comments on commit c56accf

Please sign in to comment.