Skip to content

Commit

Permalink
[GH-61]: Add tooltip for pull request and issue links (#301)
Browse files Browse the repository at this point in the history
* MI-1845: Added tooltip for pull request and issue links

* MI-1845: Done review fixes

* Added the verbose flag to the npm install command in Makefile to fix failing CI

* Fixed lint errors

* Added regex for url valiation and constants for link types and states

* Fixed linting errors

* Removed unnecessary escape character

* Minor code improvement

* Fixed CI lint error

* Added babel config for optional chaining

* Added missing trailing comma

* Fixed typo

* [MI-1919] Fixes issue in link tooltip
1. Heading and description text overlays in tooltip when crosses a certain limit

* [MI-1995] Fixed failing CI in link tooltip PR
1. Fixed the error "'gitlab.com' can be anywhere in the URL, and arbitrary hosts may come before or after it."

* Fixed spacing around 'on' using js instead of using css

* [MI-2755] Review fixes on gitlab PR #301

* [MI 2837]: Done the review fixes of a gitlab PR #301 (#30)

* [MI-2837] Review fixes on gitlab PR #301

* [MI-2837]: Review fixes done
1. Written the test cases properly

* [MI-2837]: Review fixes done
1. Improved code readability
2. Improved code consistency

* [MI-2837]: Review fixes done
1. Improved code quality

* [MI-2937]: Added EOF

* [MI-2837]: Review fixes done
1. Improved code readability

---------

Co-authored-by: raghavaggarwal2308 <raghav.aggarwal@brightscout.com>

* [MI-2963]: Did the review fixes of gitlab PR #301 (#32)

* [MI-2963]: Did the review fixes of gitlab PR #301
1.Improved the unit tests
2.Improved the code readability

* [MI-2963]: Fixed linting error

* [MI-2963]: Review fixes done
1. Improved unit tests

* [MI-2963]: Review fixes done
1. Added default in switch cases

* [MI-2963]: Review fixes done
1. Dispatched the error

* [MI-2963]: Review fixes done
1. Improved code readability

* [MI-2963]:Review fixes done
1. Improved code readability

* [MI-2963]: Review fxes done
1. Improved the description of a unit test

* [MI-2986] Review fixes on Gitlab PR #301(Add link tooltip)
1. Replaced the deprecated icon component.
2. Added a return statement to avoid unnecessary code execution.

---------

Co-authored-by: raghavaggarwal2308 <raghav.aggarwal@brightscout.com>
Co-authored-by: Nityanand Rai <107465508+Nityanand13@users.noreply.github.com>
  • Loading branch information
3 people authored Nov 29, 2023
1 parent a7bba27 commit b5184fa
Show file tree
Hide file tree
Showing 18 changed files with 9,489 additions and 3,030 deletions.
56 changes: 56 additions & 0 deletions server/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ func (p *Plugin) initializeAPI() {
apiRouter.HandleFunc("/todo", p.checkAuth(p.attachUserContext(p.postToDo), ResponseTypeJSON)).Methods(http.MethodPost)
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("/issue", p.checkAuth(p.attachUserContext(p.getIssueByNumber), ResponseTypeJSON)).Methods(http.MethodGet)
apiRouter.HandleFunc("/mergerequest", p.checkAuth(p.attachUserContext(p.getMergeRequestByNumber), ResponseTypeJSON)).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 @@ -610,6 +612,60 @@ func (p *Plugin) updateSettings(c *UserContext, w http.ResponseWriter, r *http.R
p.writeAPIResponse(w, info.Settings)
}

func (p *Plugin) getIssueByNumber(c *UserContext, w http.ResponseWriter, r *http.Request) {
owner := r.FormValue("owner")
repo := r.FormValue("repo")
issueID, err := strconv.Atoi(r.FormValue("number"))
if err != nil {
c.Log.WithError(err).Warnf("Unable to convert issueID into int")
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Unable to convert issueID into int.", StatusCode: http.StatusInternalServerError})
return
}

var result *gitlab.Issue
if cErr := p.useGitlabClient(c.GitlabInfo, func(info *gitlab.UserInfo, token *oauth2.Token) error {
issue, err := p.GitlabClient.GetIssueByID(c.Ctx, c.GitlabInfo, owner, repo, issueID, token)
if err != nil {
return err
}
result = issue
return nil
}); cErr != nil {
c.Log.WithError(cErr).Warnf("Unable to get issue in GitLab API")
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Unable to get issue in GitLab API.", StatusCode: http.StatusInternalServerError})
return
}

p.writeAPIResponse(w, result)
}

func (p *Plugin) getMergeRequestByNumber(c *UserContext, w http.ResponseWriter, r *http.Request) {
owner := r.FormValue("owner")
repo := r.FormValue("repo")
mergeRequestID, err := strconv.Atoi(r.FormValue("number"))
if err != nil {
c.Log.WithError(err).Warnf("Unable to convert mergeRequestID into int")
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Unable to convert mergeRequestID into int.", StatusCode: http.StatusInternalServerError})
return
}

var result *gitlab.MergeRequest
if cErr := p.useGitlabClient(c.GitlabInfo, func(info *gitlab.UserInfo, token *oauth2.Token) error {
mergeRequest, err := p.GitlabClient.GetMergeRequestByID(c.Ctx, c.GitlabInfo, owner, repo, mergeRequestID, token)
if err != nil {
return err
}
result = mergeRequest
return nil
}); cErr != nil {
c.Log.WithError(cErr).Warnf("Unable to get merge request in GitLab API")
p.writeAPIError(w, &APIErrorResponse{ID: "", Message: "Unable to get merge request in GitLab API.", StatusCode: http.StatusInternalServerError})
return
}

p.writeAPIResponse(w, result)
}

type SubscriptionResponse struct {
RepositoryName string `json:"repository_name"`
RepositoryURL string `json:"repository_url"`
Expand Down
56 changes: 56 additions & 0 deletions server/gitlab/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,62 @@ func (g *gitlab) ResolveNamespaceAndProject(
return "", "", ErrNotFound
}

func (g *gitlab) GetIssueByID(ctx context.Context, user *UserInfo, owner, repo string, issueID int, token *oauth2.Token) (*Issue, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
projectPath := fmt.Sprintf("%s/%s", owner, repo)
issue, resp, err := client.Issues.GetIssue(projectPath, issueID)
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, errors.Wrap(err, "can't get issue in GitLab api")
}

gitlabIssue := &Issue{
Issue: issue,
}
if issue.Labels != nil {
labelsWithDetails, err := g.GetLabelDetails(client, issue.ProjectID, issue.Labels)
if err != nil {
return nil, err
}
gitlabIssue.LabelsWithDetails = labelsWithDetails
}

return gitlabIssue, nil
}

func (g *gitlab) GetMergeRequestByID(ctx context.Context, user *UserInfo, owner, repo string, mergeRequestID int, token *oauth2.Token) (*MergeRequest, error) {
client, err := g.GitlabConnect(*token)
if err != nil {
return nil, err
}
projectPath := fmt.Sprintf("%s/%s", owner, repo)
mergeRequest, resp, err := client.MergeRequests.GetMergeRequest(projectPath, mergeRequestID, nil)
if respErr := checkResponse(resp); respErr != nil {
return nil, respErr
}
if err != nil {
return nil, errors.Wrap(err, "can't get merge request in GitLab api")
}

gitlabMergeRequest := &MergeRequest{
MergeRequest: mergeRequest,
}
if mergeRequest.Labels != nil {
labelsWithDetails, err := g.GetLabelDetails(client, mergeRequest.ProjectID, mergeRequest.Labels)
if err != nil {
return nil, err
}
gitlabMergeRequest.LabelsWithDetails = labelsWithDetails
}

return gitlabMergeRequest, nil
}

// TriggerProjectPipeline runs a pipeline in a specific project
func (g *gitlab) TriggerProjectPipeline(userInfo *UserInfo, token *oauth2.Token, projectID string, ref string) (*PipelineInfo, error) {
client, err := g.GitlabConnect(*token)
Expand Down
2 changes: 2 additions & 0 deletions server/gitlab/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ var (
type Gitlab interface {
GitlabConnect(token oauth2.Token) (*internGitlab.Client, error)
GetCurrentUser(ctx context.Context, userID string, token oauth2.Token) (*UserInfo, error)
GetIssueByID(ctx context.Context, user *UserInfo, owner, repo string, issueID int, token *oauth2.Token) (*Issue, error)
GetMergeRequestByID(ctx context.Context, user *UserInfo, owner, repo string, mergeRequestID int, token *oauth2.Token) (*MergeRequest, error)
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)
Expand Down
30 changes: 30 additions & 0 deletions server/gitlab/mocks/mock_gitlab.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions server/mocks/mock_gitlab.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions webapp/.babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"presets": ["@babel/preset-env","@babel/preset-react"],
"plugins": ["@babel/plugin-proposal-class-properties"]
}
11 changes: 11 additions & 0 deletions webapp/jest.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"transform": {
"^.+\\.[t|j]sx?$": "babel-jest"
},
"moduleNameMapper": {
"^.+\\.(jpg|jpeg|png|gif|eot|otf|webp|svg|ttf|woff|woff2|mp4|webm|wav|mp3|m4a|aac|oga)$": "identity-obj-proxy",
"^.+\\.(css|less|scss)$": "identity-obj-proxy",
"^.*i18n.*\\.(json)$": "<rootDir>/tests/i18n_mock.json",
"^bundle-loader\\?lazy\\!(.*)$": "$1"
}
}
Loading

0 comments on commit b5184fa

Please sign in to comment.