From e881f95160102487f457c6ee82f9e3303410704f Mon Sep 17 00:00:00 2001 From: delarea Date: Sun, 18 Jun 2023 11:47:50 +0300 Subject: [PATCH 01/16] implement get pull request by ID --- vcsclient/azurerepos.go | 50 ++++++++++++++++++++++++++---------- vcsclient/bitbucketcloud.go | 38 +++++++++++++++++++++++++++ vcsclient/bitbucketserver.go | 23 +++++++++++++++++ vcsclient/github.go | 24 +++++++++++++++++ vcsclient/gitlab.go | 15 +++++++++++ vcsclient/logger.go | 1 + vcsclient/vcsclient.go | 6 +++++ 7 files changed, 143 insertions(+), 14 deletions(-) diff --git a/vcsclient/azurerepos.go b/vcsclient/azurerepos.go index cefdacf2..507c2ae5 100644 --- a/vcsclient/azurerepos.go +++ b/vcsclient/azurerepos.go @@ -249,24 +249,29 @@ func (client *AzureReposClient) ListOpenPullRequests(ctx context.Context, _, rep } var pullRequestsInfo []PullRequestInfo for _, pullRequest := range *pullRequests { - // Trim the branches prefix and get the actual branches name - shortSourceName := (*pullRequest.SourceRefName)[strings.LastIndex(*pullRequest.SourceRefName, "/")+1:] - shortTargetName := (*pullRequest.TargetRefName)[strings.LastIndex(*pullRequest.TargetRefName, "/")+1:] - pullRequestsInfo = append(pullRequestsInfo, PullRequestInfo{ - ID: int64(*pullRequest.PullRequestId), - Source: BranchInfo{ - Name: shortSourceName, - Repository: repository, - }, - Target: BranchInfo{ - Name: shortTargetName, - Repository: repository, - }, - }) + pullRequestDetails := parsePullRequestDetails(pullRequest, repository) + pullRequestsInfo = append(pullRequestsInfo, pullRequestDetails) } return pullRequestsInfo, nil } +func (client *AzureReposClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { + azureReposGitClient, err := client.buildAzureReposClient(ctx) + if err != nil { + return + } + client.logger.Debug(fetchingPullRequestById, repository) + pullRequest, err := azureReposGitClient.GetPullRequestById(ctx, git.GetPullRequestByIdArgs{ + PullRequestId: &pullRequestId, + Project: &client.vcsInfo.Project, + }) + if err != nil { + return + } + pullRequestInfo = parsePullRequestDetails(*pullRequest, repository) + return +} + // GetLatestCommit on Azure Repos func (client *AzureReposClient) GetLatestCommit(ctx context.Context, _, repository, branch string) (CommitInfo, error) { azureReposGitClient, err := client.buildAzureReposClient(ctx) @@ -518,6 +523,23 @@ func (client *AzureReposClient) GetModifiedFiles(ctx context.Context, _, reposit return fileNamesList, nil } +func parsePullRequestDetails(pullRequest git.GitPullRequest, repository string) PullRequestInfo { + // Trim the branches prefix and get the actual branches name + shortSourceName := (*pullRequest.SourceRefName)[strings.LastIndex(*pullRequest.SourceRefName, "/")+1:] + shortTargetName := (*pullRequest.TargetRefName)[strings.LastIndex(*pullRequest.TargetRefName, "/")+1:] + return PullRequestInfo{ + ID: int64(*pullRequest.PullRequestId), + Source: BranchInfo{ + Name: shortSourceName, + Repository: repository, + }, + Target: BranchInfo{ + Name: shortTargetName, + Repository: repository, + }, + } +} + // mapStatusToString maps commit status enum to string, specific for azure. func mapStatusToString(status CommitStatus) string { conversionMap := map[CommitStatus]string{ diff --git a/vcsclient/bitbucketcloud.go b/vcsclient/bitbucketcloud.go index 9f2072fb..8e1855e4 100644 --- a/vcsclient/bitbucketcloud.go +++ b/vcsclient/bitbucketcloud.go @@ -320,6 +320,44 @@ func (client *BitbucketCloudClient) ListOpenPullRequests(ctx context.Context, ow return mapBitbucketCloudPullRequestToPullRequestInfo(&parsedPullRequests), nil } +func (client *BitbucketCloudClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { + err = validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository}) + if err != nil { + return + } + bitbucketClient := client.buildBitbucketCloudClient(ctx) + client.logger.Debug(fetchingPullRequestById, repository) + options := &bitbucket.PullRequestsOptions{ + Owner: owner, + RepoSlug: repository, + ID: string(rune(pullRequestId)), + } + pullRequestRaw, err := bitbucketClient.Repositories.PullRequests.Get(options) + if err != nil { + return + } + parsedPullRequest, err := vcsutils.RemapFields[pullRequestsResponse](pullRequestRaw, "json") + if err != nil { + return + } + pullRequestDetails := parsedPullRequest.Values[0] + pullRequestInfo = PullRequestInfo{ + ID: pullRequestDetails.ID, + Source: BranchInfo{ + Name: pullRequestDetails.Source.Name.Str, + Repository: pullRequestDetails.Source.Repository.Name, + }, + Target: BranchInfo{ + Name: pullRequestDetails.Target.Name.Str, + Repository: pullRequestDetails.Target.Repository.Name, + }, + } + if err != nil { + return + } + return +} + // AddPullRequestComment on Bitbucket cloud func (client *BitbucketCloudClient) AddPullRequestComment(ctx context.Context, owner, repository, content string, pullRequestID int) error { err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository, "content": content}) diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index 6e59a5a4..9a80ef05 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -360,6 +360,29 @@ func (client *BitbucketServerClient) ListOpenPullRequests(ctx context.Context, o return results, nil } +// GetPullRequestInfoById on bitbucket server +func (client *BitbucketServerClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { + client.logger.Debug("fetching pull request by ID in ", repository) + bitbucketClient, err := client.buildBitbucketClient(ctx) + if err != nil { + return + } + apiResponse, err := bitbucketClient.GetPullRequest(owner, repository, pullRequestId) + if err != nil { + return + } + pullRequest, err := bitbucketv1.GetPullRequestResponse(apiResponse) + if err != nil { + return + } + pullRequestInfo = PullRequestInfo{ + ID: int64(pullRequest.ID), + Source: BranchInfo{Name: pullRequest.FromRef.ID, Repository: pullRequest.ToRef.Repository.Slug}, + Target: BranchInfo{Name: pullRequest.ToRef.ID, Repository: pullRequest.ToRef.Repository.Slug}, + } + return +} + // AddPullRequestComment on Bitbucket server func (client *BitbucketServerClient) AddPullRequestComment(ctx context.Context, owner, repository, content string, pullRequestID int) error { err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository, "content": content}) diff --git a/vcsclient/github.go b/vcsclient/github.go index fdf23e3a..a6f75b09 100644 --- a/vcsclient/github.go +++ b/vcsclient/github.go @@ -322,6 +322,30 @@ func (client *GitHubClient) ListPullRequestComments(ctx context.Context, owner, return mapGitHubCommentToCommentInfoList(commentsList) } +func (client *GitHubClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) { + ghClient, err := client.buildGithubClient(ctx) + if err != nil { + return PullRequestInfo{}, err + } + client.logger.Debug(fetchingPullRequestById, repository) + pullRequest, _, err := ghClient.PullRequests.Get(ctx, owner, repository, pullRequestId) + if err != nil { + return PullRequestInfo{}, err + } + prInfo := PullRequestInfo{ + ID: int64(pullRequestId), + Source: BranchInfo{ + Name: *pullRequest.Head.Label, + Repository: *pullRequest.Head.Repo.Name, + }, + Target: BranchInfo{ + Name: *pullRequest.Base.Label, + Repository: *pullRequest.Base.Repo.Name, + }, + } + return prInfo, nil +} + // GetLatestCommit on GitHub func (client *GitHubClient) GetLatestCommit(ctx context.Context, owner, repository, branch string) (CommitInfo, error) { err := validateParametersNotBlank(map[string]string{ diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index 2cc7e3cf..2b3c6ef2 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -252,6 +252,21 @@ func (client *GitLabClient) ListOpenPullRequests(ctx context.Context, _, reposit return mapGitLabMergeRequestToPullRequestInfoList(mergeRequests), nil } +// GetPullRequestInfoById on GitLab +func (client *GitLabClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { + client.logger.Debug("fetching merge requests by ID in", repository) + mergeRequest, _, err := client.glClient.MergeRequests.GetMergeRequest(ctx, pullRequestId, nil) + if err != nil { + return + } + pullRequestInfo = PullRequestInfo{ + ID: int64(mergeRequest.ID), + Source: BranchInfo{Name: mergeRequest.SourceBranch}, + Target: BranchInfo{Name: mergeRequest.TargetBranch}, + } + return +} + // AddPullRequestComment on GitLab func (client *GitLabClient) AddPullRequestComment(ctx context.Context, owner, repository, content string, pullRequestID int) error { err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository, "content": content}) diff --git a/vcsclient/logger.go b/vcsclient/logger.go index 1f722415..ec67143f 100644 --- a/vcsclient/logger.go +++ b/vcsclient/logger.go @@ -5,6 +5,7 @@ const ( successfulRepoExtraction = "Extracted repository successfully" creatingPullRequest = "Creating new pull request:" fetchingOpenPullRequests = "Fetching open pull requests in" + fetchingPullRequestById = "Fetching pull requests by id in " uploadingCodeScanning = "Uploading code scanning for:" ) diff --git a/vcsclient/vcsclient.go b/vcsclient/vcsclient.go index ca7f4e9d..71e6676b 100644 --- a/vcsclient/vcsclient.go +++ b/vcsclient/vcsclient.go @@ -165,6 +165,12 @@ type VcsClient interface { // repository - VCS repository name ListOpenPullRequests(ctx context.Context, owner, repository string) ([]PullRequestInfo, error) + // GetPullRequestInfoById Gets specific pull request info. + // owner - User or organization + // repository - VCS repository name + // pullRequestId - ID of the pull request + GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) + // GetLatestCommit Gets the most recent commit of a branch // owner - User or organization // repository - VCS repository name From a081d63b5d1b8867b7d43aa6bcf2eb2e7053f7f9 Mon Sep 17 00:00:00 2001 From: delarea Date: Mon, 19 Jun 2023 17:54:05 +0300 Subject: [PATCH 02/16] extract branch from label in GitHub & add test --- vcsclient/github.go | 66 ++- vcsclient/github_test.go | 22 + .../github/pull_request_info_response.json | 536 ++++++++++++++++++ 3 files changed, 600 insertions(+), 24 deletions(-) create mode 100644 vcsclient/testdata/github/pull_request_info_response.json diff --git a/vcsclient/github.go b/vcsclient/github.go index a6f75b09..af69726e 100644 --- a/vcsclient/github.go +++ b/vcsclient/github.go @@ -3,6 +3,7 @@ package vcsclient import ( "context" "encoding/json" + "errors" "fmt" "github.com/jfrog/gofrog/datastructures" "io" @@ -288,6 +289,47 @@ func (client *GitHubClient) ListOpenPullRequests(ctx context.Context, owner, rep return mapGitHubPullRequestToPullRequestInfoList(pullRequests) } +func (client *GitHubClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) { + ghClient, err := client.buildGithubClient(ctx) + if err != nil { + return PullRequestInfo{}, err + } + client.logger.Debug(fetchingPullRequestById, repository) + pullRequest, _, err := ghClient.PullRequests.Get(ctx, owner, repository, pullRequestId) + if err != nil { + return PullRequestInfo{}, err + } + + sourceBranch, err1 := extractBranchFromLabel(*pullRequest.Head.Label) + targetBranch, err2 := extractBranchFromLabel(*pullRequest.Base.Label) + badLabelErrors := errors.Join(err1, err2) + if badLabelErrors != nil { + return PullRequestInfo{}, err + } + + prInfo := PullRequestInfo{ + ID: int64(pullRequestId), + Source: BranchInfo{ + Name: sourceBranch, + Repository: *pullRequest.Head.Repo.Name, + }, + Target: BranchInfo{ + Name: targetBranch, + Repository: *pullRequest.Base.Repo.Name, + }, + } + return prInfo, nil +} + +// Extracts branch name from the following expected label format repo:branch +func extractBranchFromLabel(label string) (string, error) { + split := strings.Split(label, ":") + if len(split) < 1 { + return "", fmt.Errorf("bad label format %s", label) + } + return split[1], nil +} + // AddPullRequestComment on GitHub func (client *GitHubClient) AddPullRequestComment(ctx context.Context, owner, repository, content string, pullRequestID int) error { err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository, "content": content}) @@ -322,30 +364,6 @@ func (client *GitHubClient) ListPullRequestComments(ctx context.Context, owner, return mapGitHubCommentToCommentInfoList(commentsList) } -func (client *GitHubClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) { - ghClient, err := client.buildGithubClient(ctx) - if err != nil { - return PullRequestInfo{}, err - } - client.logger.Debug(fetchingPullRequestById, repository) - pullRequest, _, err := ghClient.PullRequests.Get(ctx, owner, repository, pullRequestId) - if err != nil { - return PullRequestInfo{}, err - } - prInfo := PullRequestInfo{ - ID: int64(pullRequestId), - Source: BranchInfo{ - Name: *pullRequest.Head.Label, - Repository: *pullRequest.Head.Repo.Name, - }, - Target: BranchInfo{ - Name: *pullRequest.Base.Label, - Repository: *pullRequest.Base.Repo.Name, - }, - } - return prInfo, nil -} - // GetLatestCommit on GitHub func (client *GitHubClient) GetLatestCommit(ctx context.Context, owner, repository, branch string) (CommitInfo, error) { err := validateParametersNotBlank(map[string]string{ diff --git a/vcsclient/github_test.go b/vcsclient/github_test.go index 8d7dc4a1..d16d9ef8 100644 --- a/vcsclient/github_test.go +++ b/vcsclient/github_test.go @@ -531,6 +531,28 @@ func TestGitHubClient_ListOpenPullRequests(t *testing.T) { assert.Error(t, err) } +func TestGitHubClient_GetPullRequestInfoById(t *testing.T) { + ctx := context.Background() + pullRequestId := 1 + repoName := "Hello-World" + response, err := os.ReadFile(filepath.Join("testdata", "github", "pull_request_info_response.json")) + assert.NoError(t, err) + client, cleanUp := createServerAndClient(t, vcsutils.GitHub, false, response, + fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repoName, pullRequestId), createGitHubHandler) + defer cleanUp() + + result, err := client.GetPullRequestInfoById(ctx, owner, repoName, pullRequestId) + require.NoError(t, err) + assert.True(t, reflect.DeepEqual(PullRequestInfo{ + ID: 1, + Source: BranchInfo{Name: "new-topic", Repository: "Hello-World"}, + Target: BranchInfo{Name: "master", Repository: "Hello-World"}, + }, result)) + + _, err = createBadGitHubClient(t).ListPullRequestComments(ctx, owner, repoName, 1) + assert.Error(t, err) +} + func TestGitHubClient_ListPullRequestComments(t *testing.T) { ctx := context.Background() response, err := os.ReadFile(filepath.Join("testdata", "github", "pull_request_comments_list_response.json")) diff --git a/vcsclient/testdata/github/pull_request_info_response.json b/vcsclient/testdata/github/pull_request_info_response.json new file mode 100644 index 00000000..cd5f5c8a --- /dev/null +++ b/vcsclient/testdata/github/pull_request_info_response.json @@ -0,0 +1,536 @@ +{ + "url": "https://api.github.com/repos/octocat/Hello-World/pulls/1347", + "id": 1, + "node_id": "MDExOlB1bGxSZXF1ZXN0MQ==", + "html_url": "https://github.com/octocat/Hello-World/pull/1347", + "diff_url": "https://github.com/octocat/Hello-World/pull/1347.diff", + "patch_url": "https://github.com/octocat/Hello-World/pull/1347.patch", + "issue_url": "https://api.github.com/repos/octocat/Hello-World/issues/1347", + "commits_url": "https://api.github.com/repos/octocat/Hello-World/pulls/1347/commits", + "review_comments_url": "https://api.github.com/repos/octocat/Hello-World/pulls/1347/comments", + "review_comment_url": "https://api.github.com/repos/octocat/Hello-World/pulls/comments{/number}", + "comments_url": "https://api.github.com/repos/octocat/Hello-World/issues/1347/comments", + "statuses_url": "https://api.github.com/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e", + "number": 1347, + "state": "open", + "locked": true, + "title": "Amazing new feature", + "user": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "body": "Please pull these awesome changes in!", + "labels": [ + { + "id": 208045946, + "node_id": "MDU6TGFiZWwyMDgwNDU5NDY=", + "url": "https://api.github.com/repos/octocat/Hello-World/labels/bug", + "name": "bug", + "description": "Something isn't working", + "color": "f29513", + "default": true + } + ], + "milestone": { + "url": "https://api.github.com/repos/octocat/Hello-World/milestones/1", + "html_url": "https://github.com/octocat/Hello-World/milestones/v1.0", + "labels_url": "https://api.github.com/repos/octocat/Hello-World/milestones/1/labels", + "id": 1002604, + "node_id": "MDk6TWlsZXN0b25lMTAwMjYwNA==", + "number": 1, + "state": "open", + "title": "v1.0", + "description": "Tracking milestone for version 1.0", + "creator": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "open_issues": 4, + "closed_issues": 8, + "created_at": "2011-04-10T20:09:31Z", + "updated_at": "2014-03-03T18:58:10Z", + "closed_at": "2013-02-12T13:22:01Z", + "due_on": "2012-10-09T23:39:01Z" + }, + "active_lock_reason": "too heated", + "created_at": "2011-01-26T19:01:12Z", + "updated_at": "2011-01-26T19:01:12Z", + "closed_at": "2011-01-26T19:01:12Z", + "merged_at": "2011-01-26T19:01:12Z", + "merge_commit_sha": "e5bd3914e2e596debea16f433f57875b5b90bcd6", + "assignee": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "assignees": [ + { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + { + "login": "hubot", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/hubot_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/hubot", + "html_url": "https://github.com/hubot", + "followers_url": "https://api.github.com/users/hubot/followers", + "following_url": "https://api.github.com/users/hubot/following{/other_user}", + "gists_url": "https://api.github.com/users/hubot/gists{/gist_id}", + "starred_url": "https://api.github.com/users/hubot/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/hubot/subscriptions", + "organizations_url": "https://api.github.com/users/hubot/orgs", + "repos_url": "https://api.github.com/users/hubot/repos", + "events_url": "https://api.github.com/users/hubot/events{/privacy}", + "received_events_url": "https://api.github.com/users/hubot/received_events", + "type": "User", + "site_admin": true + } + ], + "requested_reviewers": [ + { + "login": "other_user", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/other_user_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/other_user", + "html_url": "https://github.com/other_user", + "followers_url": "https://api.github.com/users/other_user/followers", + "following_url": "https://api.github.com/users/other_user/following{/other_user}", + "gists_url": "https://api.github.com/users/other_user/gists{/gist_id}", + "starred_url": "https://api.github.com/users/other_user/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/other_user/subscriptions", + "organizations_url": "https://api.github.com/users/other_user/orgs", + "repos_url": "https://api.github.com/users/other_user/repos", + "events_url": "https://api.github.com/users/other_user/events{/privacy}", + "received_events_url": "https://api.github.com/users/other_user/received_events", + "type": "User", + "site_admin": false + } + ], + "requested_teams": [ + { + "id": 1, + "node_id": "MDQ6VGVhbTE=", + "url": "https://api.github.com/teams/1", + "html_url": "https://github.com/orgs/github/teams/justice-league", + "name": "Justice League", + "slug": "justice-league", + "description": "A great team.", + "privacy": "closed", + "notification_setting": "notifications_enabled", + "permission": "admin", + "members_url": "https://api.github.com/teams/1/members{/member}", + "repositories_url": "https://api.github.com/teams/1/repos" + } + ], + "head": { + "label": "octocat:new-topic", + "ref": "new-topic", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "user": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "repo": { + "id": 1296269, + "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", + "name": "Hello-World", + "full_name": "octocat/Hello-World", + "owner": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/octocat/Hello-World", + "description": "This your first repo!", + "fork": false, + "url": "https://api.github.com/repos/octocat/Hello-World", + "archive_url": "https://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}", + "assignees_url": "https://api.github.com/repos/octocat/Hello-World/assignees{/user}", + "blobs_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}", + "branches_url": "https://api.github.com/repos/octocat/Hello-World/branches{/branch}", + "collaborators_url": "https://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}", + "comments_url": "https://api.github.com/repos/octocat/Hello-World/comments{/number}", + "commits_url": "https://api.github.com/repos/octocat/Hello-World/commits{/sha}", + "compare_url": "https://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}", + "contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/{+path}", + "contributors_url": "https://api.github.com/repos/octocat/Hello-World/contributors", + "deployments_url": "https://api.github.com/repos/octocat/Hello-World/deployments", + "downloads_url": "https://api.github.com/repos/octocat/Hello-World/downloads", + "events_url": "https://api.github.com/repos/octocat/Hello-World/events", + "forks_url": "https://api.github.com/repos/octocat/Hello-World/forks", + "git_commits_url": "https://api.github.com/repos/octocat/Hello-World/git/commits{/sha}", + "git_refs_url": "https://api.github.com/repos/octocat/Hello-World/git/refs{/sha}", + "git_tags_url": "https://api.github.com/repos/octocat/Hello-World/git/tags{/sha}", + "git_url": "git:github.com/octocat/Hello-World.git", + "issue_comment_url": "https://api.github.com/repos/octocat/Hello-World/issues/comments{/number}", + "issue_events_url": "https://api.github.com/repos/octocat/Hello-World/issues/events{/number}", + "issues_url": "https://api.github.com/repos/octocat/Hello-World/issues{/number}", + "keys_url": "https://api.github.com/repos/octocat/Hello-World/keys{/key_id}", + "labels_url": "https://api.github.com/repos/octocat/Hello-World/labels{/name}", + "languages_url": "https://api.github.com/repos/octocat/Hello-World/languages", + "merges_url": "https://api.github.com/repos/octocat/Hello-World/merges", + "milestones_url": "https://api.github.com/repos/octocat/Hello-World/milestones{/number}", + "notifications_url": "https://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}", + "pulls_url": "https://api.github.com/repos/octocat/Hello-World/pulls{/number}", + "releases_url": "https://api.github.com/repos/octocat/Hello-World/releases{/id}", + "ssh_url": "git@github.com:octocat/Hello-World.git", + "stargazers_url": "https://api.github.com/repos/octocat/Hello-World/stargazers", + "statuses_url": "https://api.github.com/repos/octocat/Hello-World/statuses/{sha}", + "subscribers_url": "https://api.github.com/repos/octocat/Hello-World/subscribers", + "subscription_url": "https://api.github.com/repos/octocat/Hello-World/subscription", + "tags_url": "https://api.github.com/repos/octocat/Hello-World/tags", + "teams_url": "https://api.github.com/repos/octocat/Hello-World/teams", + "trees_url": "https://api.github.com/repos/octocat/Hello-World/git/trees{/sha}", + "clone_url": "https://github.com/octocat/Hello-World.git", + "mirror_url": "git:git.example.com/octocat/Hello-World", + "hooks_url": "https://api.github.com/repos/octocat/Hello-World/hooks", + "svn_url": "https://svn.github.com/octocat/Hello-World", + "homepage": "https://github.com", + "language": null, + "forks_count": 9, + "stargazers_count": 80, + "watchers_count": 80, + "size": 108, + "default_branch": "master", + "open_issues_count": 0, + "topics": [ + "octocat", + "atom", + "electron", + "api" + ], + "has_issues": true, + "has_projects": true, + "has_wiki": true, + "has_pages": false, + "has_downloads": true, + "has_discussions": false, + "archived": false, + "disabled": false, + "pushed_at": "2011-01-26T19:06:43Z", + "created_at": "2011-01-26T19:01:12Z", + "updated_at": "2011-01-26T19:14:43Z", + "permissions": { + "admin": false, + "push": false, + "pull": true + }, + "allow_rebase_merge": true, + "temp_clone_token": "ABTLWHOULUVAXGTRYU7OC2876QJ2O", + "allow_squash_merge": true, + "allow_merge_commit": true, + "allow_forking": true, + "forks": 123, + "open_issues": 123, + "license": { + "key": "mit", + "name": "MIT License", + "url": "https://api.github.com/licenses/mit", + "spdx_id": "MIT", + "node_id": "MDc6TGljZW5zZW1pdA==" + }, + "watchers": 123 + } + }, + "base": { + "label": "octocat:master", + "ref": "master", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "user": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "repo": { + "id": 1296269, + "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", + "name": "Hello-World", + "full_name": "octocat/Hello-World", + "owner": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/octocat/Hello-World", + "description": "This your first repo!", + "fork": false, + "url": "https://api.github.com/repos/octocat/Hello-World", + "archive_url": "https://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}", + "assignees_url": "https://api.github.com/repos/octocat/Hello-World/assignees{/user}", + "blobs_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}", + "branches_url": "https://api.github.com/repos/octocat/Hello-World/branches{/branch}", + "collaborators_url": "https://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}", + "comments_url": "https://api.github.com/repos/octocat/Hello-World/comments{/number}", + "commits_url": "https://api.github.com/repos/octocat/Hello-World/commits{/sha}", + "compare_url": "https://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}", + "contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/{+path}", + "contributors_url": "https://api.github.com/repos/octocat/Hello-World/contributors", + "deployments_url": "https://api.github.com/repos/octocat/Hello-World/deployments", + "downloads_url": "https://api.github.com/repos/octocat/Hello-World/downloads", + "events_url": "https://api.github.com/repos/octocat/Hello-World/events", + "forks_url": "https://api.github.com/repos/octocat/Hello-World/forks", + "git_commits_url": "https://api.github.com/repos/octocat/Hello-World/git/commits{/sha}", + "git_refs_url": "https://api.github.com/repos/octocat/Hello-World/git/refs{/sha}", + "git_tags_url": "https://api.github.com/repos/octocat/Hello-World/git/tags{/sha}", + "git_url": "git:github.com/octocat/Hello-World.git", + "issue_comment_url": "https://api.github.com/repos/octocat/Hello-World/issues/comments{/number}", + "issue_events_url": "https://api.github.com/repos/octocat/Hello-World/issues/events{/number}", + "issues_url": "https://api.github.com/repos/octocat/Hello-World/issues{/number}", + "keys_url": "https://api.github.com/repos/octocat/Hello-World/keys{/key_id}", + "labels_url": "https://api.github.com/repos/octocat/Hello-World/labels{/name}", + "languages_url": "https://api.github.com/repos/octocat/Hello-World/languages", + "merges_url": "https://api.github.com/repos/octocat/Hello-World/merges", + "milestones_url": "https://api.github.com/repos/octocat/Hello-World/milestones{/number}", + "notifications_url": "https://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}", + "pulls_url": "https://api.github.com/repos/octocat/Hello-World/pulls{/number}", + "releases_url": "https://api.github.com/repos/octocat/Hello-World/releases{/id}", + "ssh_url": "git@github.com:octocat/Hello-World.git", + "stargazers_url": "https://api.github.com/repos/octocat/Hello-World/stargazers", + "statuses_url": "https://api.github.com/repos/octocat/Hello-World/statuses/{sha}", + "subscribers_url": "https://api.github.com/repos/octocat/Hello-World/subscribers", + "subscription_url": "https://api.github.com/repos/octocat/Hello-World/subscription", + "tags_url": "https://api.github.com/repos/octocat/Hello-World/tags", + "teams_url": "https://api.github.com/repos/octocat/Hello-World/teams", + "trees_url": "https://api.github.com/repos/octocat/Hello-World/git/trees{/sha}", + "clone_url": "https://github.com/octocat/Hello-World.git", + "mirror_url": "git:git.example.com/octocat/Hello-World", + "hooks_url": "https://api.github.com/repos/octocat/Hello-World/hooks", + "svn_url": "https://svn.github.com/octocat/Hello-World", + "homepage": "https://github.com", + "language": null, + "forks_count": 9, + "stargazers_count": 80, + "watchers_count": 80, + "size": 108, + "default_branch": "master", + "open_issues_count": 0, + "topics": [ + "octocat", + "atom", + "electron", + "api" + ], + "has_issues": true, + "has_projects": true, + "has_wiki": true, + "has_pages": false, + "has_downloads": true, + "has_discussions": false, + "archived": false, + "disabled": false, + "pushed_at": "2011-01-26T19:06:43Z", + "created_at": "2011-01-26T19:01:12Z", + "updated_at": "2011-01-26T19:14:43Z", + "permissions": { + "admin": false, + "push": false, + "pull": true + }, + "allow_rebase_merge": true, + "temp_clone_token": "ABTLWHOULUVAXGTRYU7OC2876QJ2O", + "allow_squash_merge": true, + "allow_merge_commit": true, + "forks": 123, + "open_issues": 123, + "license": { + "key": "mit", + "name": "MIT License", + "url": "https://api.github.com/licenses/mit", + "spdx_id": "MIT", + "node_id": "MDc6TGljZW5zZW1pdA==" + }, + "watchers": 123 + } + }, + "_links": { + "self": { + "href": "https://api.github.com/repos/octocat/Hello-World/pulls/1347" + }, + "html": { + "href": "https://github.com/octocat/Hello-World/pull/1347" + }, + "issue": { + "href": "https://api.github.com/repos/octocat/Hello-World/issues/1347" + }, + "comments": { + "href": "https://api.github.com/repos/octocat/Hello-World/issues/1347/comments" + }, + "review_comments": { + "href": "https://api.github.com/repos/octocat/Hello-World/pulls/1347/comments" + }, + "review_comment": { + "href": "https://api.github.com/repos/octocat/Hello-World/pulls/comments{/number}" + }, + "commits": { + "href": "https://api.github.com/repos/octocat/Hello-World/pulls/1347/commits" + }, + "statuses": { + "href": "https://api.github.com/repos/octocat/Hello-World/statuses/6dcb09b5b57875f334f61aebed695e2e4193db5e" + } + }, + "author_association": "OWNER", + "auto_merge": null, + "draft": false, + "merged": false, + "mergeable": true, + "rebaseable": true, + "mergeable_state": "clean", + "merged_by": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "comments": 10, + "review_comments": 0, + "maintainer_can_modify": true, + "commits": 3, + "additions": 100, + "deletions": 3, + "changed_files": 5 +} \ No newline at end of file From 9e5394351518fe2cde0b9ee6dcf6d8784e4dbf82 Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 20 Jun 2023 10:55:57 +0300 Subject: [PATCH 03/16] gitlab_test.go --- vcsclient/gitlab.go | 2 +- vcsclient/gitlab_test.go | 20 +++ .../gitlab/get_merge_request_response.json | 135 ++++++++++++++++++ 3 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 vcsclient/testdata/gitlab/get_merge_request_response.json diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index 2b3c6ef2..a06a09fe 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -255,7 +255,7 @@ func (client *GitLabClient) ListOpenPullRequests(ctx context.Context, _, reposit // GetPullRequestInfoById on GitLab func (client *GitLabClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { client.logger.Debug("fetching merge requests by ID in", repository) - mergeRequest, _, err := client.glClient.MergeRequests.GetMergeRequest(ctx, pullRequestId, nil) + mergeRequest, _, err := client.glClient.MergeRequests.GetMergeRequest(getProjectID(owner, repository), pullRequestId, nil, gitlab.WithContext(ctx)) if err != nil { return } diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index e7a5c2d3..87f87dbe 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -221,6 +221,26 @@ func TestGitLabClient_ListOpenPullRequests(t *testing.T) { }, result[0])) } +func TestGitLabClient_GetPullRequestInfoById(t *testing.T) { + ctx := context.Background() + repoName := "repo" + pullRequestId := 1 + response, err := os.ReadFile(filepath.Join("testdata", "gitlab", "get_merge_request_response.json")) + assert.NoError(t, err) + + client, cleanUp := createServerAndClient(t, vcsutils.GitLab, false, response, + fmt.Sprintf("/api/v4/projects/%s/merge_requests/%d", url.PathEscape(owner+"/"+repoName), pullRequestId), createGitLabHandler) + defer cleanUp() + + result, err := client.GetPullRequestInfoById(ctx, owner, repoName, pullRequestId) + require.NoError(t, err) + assert.True(t, reflect.DeepEqual(PullRequestInfo{ + ID: 1, + Source: BranchInfo{Name: "manual-job-rules", Repository: ""}, + Target: BranchInfo{Name: "master", Repository: ""}, + }, result)) +} + func TestGitLabClient_GetLatestCommit(t *testing.T) { ctx := context.Background() response, err := os.ReadFile(filepath.Join("testdata", "gitlab", "commit_list_response.json")) diff --git a/vcsclient/testdata/gitlab/get_merge_request_response.json b/vcsclient/testdata/gitlab/get_merge_request_response.json new file mode 100644 index 00000000..47afe42c --- /dev/null +++ b/vcsclient/testdata/gitlab/get_merge_request_response.json @@ -0,0 +1,135 @@ +{ + "id": 1, + "iid": 133, + "project_id": 15513260, + "title": "Manual job rules", + "description": "", + "state": "opened", + "created_at": "2022-05-13T07:26:38.402Z", + "updated_at": "2022-05-14T03:38:31.354Z", + "merged_by": null, + "merge_user": null, + "merged_at": null, + "prepared_at": "2018-09-04T11:16:17.520Z", + "closed_by": null, + "closed_at": null, + "target_branch": "master", + "source_branch": "manual-job-rules", + "user_notes_count": 0, + "upvotes": 0, + "downvotes": 0, + "author": { + "id": 4155490, + "username": "marcel.amirault", + "name": "Marcel Amirault", + "state": "active", + "avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/4155490/avatar.png", + "web_url": "https://gitlab.com/marcel.amirault" + }, + "assignees": [], + "assignee": null, + "reviewers": [], + "source_project_id": 15513260, + "target_project_id": 15513260, + "labels": [], + "draft": false, + "work_in_progress": false, + "milestone": null, + "merge_when_pipeline_succeeds": false, + "merge_status": "can_be_merged", + "detailed_merge_status": "can_be_merged", + "sha": "e82eb4a098e32c796079ca3915e07487fc4db24c", + "merge_commit_sha": null, + "squash_commit_sha": null, + "discussion_locked": null, + "should_remove_source_branch": null, + "force_remove_source_branch": true, + "reference": "!133", + "references": { + "short": "!133", + "relative": "!133", + "full": "marcel.amirault/test-project!133" + }, + "web_url": "https://gitlab.com/marcel.amirault/test-project/-/merge_requests/133", + "time_stats": { + "time_estimate": 0, + "total_time_spent": 0, + "human_time_estimate": null, + "human_total_time_spent": null + }, + "squash": false, + "task_completion_status": { + "count": 0, + "completed_count": 0 + }, + "has_conflicts": false, + "blocking_discussions_resolved": true, + "approvals_before_merge": null, + "subscribed": true, + "changes_count": "1", + "latest_build_started_at": "2022-05-13T09:46:50.032Z", + "latest_build_finished_at": null, + "first_deployed_to_production_at": null, + "pipeline": { + "id": 538317940, + "iid": 1877, + "project_id": 15513260, + "sha": "1604b0c46c395822e4e9478777f8e54ac99fe5b9", + "ref": "refs/merge-requests/133/merge", + "status": "failed", + "source": "merge_request_event", + "created_at": "2022-05-13T09:46:39.560Z", + "updated_at": "2022-05-13T09:47:20.706Z", + "web_url": "https://gitlab.com/marcel.amirault/test-project/-/pipelines/538317940" + }, + "head_pipeline": { + "id": 538317940, + "iid": 1877, + "project_id": 15513260, + "sha": "1604b0c46c395822e4e9478777f8e54ac99fe5b9", + "ref": "refs/merge-requests/133/merge", + "status": "failed", + "source": "merge_request_event", + "created_at": "2022-05-13T09:46:39.560Z", + "updated_at": "2022-05-13T09:47:20.706Z", + "web_url": "https://gitlab.com/marcel.amirault/test-project/-/pipelines/538317940", + "before_sha": "1604b0c46c395822e4e9478777f8e54ac99fe5b9", + "tag": false, + "yaml_errors": null, + "user": { + "id": 4155490, + "username": "marcel.amirault", + "name": "Marcel Amirault", + "state": "active", + "avatar_url": "https://gitlab.com/uploads/-/system/user/avatar/4155490/avatar.png", + "web_url": "https://gitlab.com/marcel.amirault" + }, + "started_at": "2022-05-13T09:46:50.032Z", + "finished_at": "2022-05-13T09:47:20.697Z", + "committed_at": null, + "duration": 30, + "queued_duration": 10, + "coverage": null, + "detailed_status": { + "icon": "status_failed", + "text": "failed", + "label": "failed", + "group": "failed", + "tooltip": "failed", + "has_details": true, + "details_path": "/marcel.amirault/test-project/-/pipelines/538317940", + "illustration": null, + "favicon": "/assets/ci_favicons/favicon_status_failed-41304d7f7e3828808b0c26771f0309e55296819a9beea3ea9fbf6689d9857c12.png" + } + }, + "diff_refs": { + "base_sha": "1162f719d711319a2efb2a35566f3bfdadee8bab", + "head_sha": "e82eb4a098e32c796079ca3915e07487fc4db24c", + "start_sha": "1162f719d711319a2efb2a35566f3bfdadee8bab" + }, + "merge_error": null, + "first_contribution": false, + "user": { + "can_merge": true + } +} \ No newline at end of file From 668849fce8bde3ebc7af6e9114fb8a622acba19b Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 20 Jun 2023 10:56:53 +0300 Subject: [PATCH 04/16] Rename method --- vcsclient/azurerepos.go | 2 +- vcsclient/bitbucketcloud.go | 2 +- vcsclient/bitbucketserver.go | 2 +- vcsclient/github.go | 2 +- vcsclient/github_test.go | 2 +- vcsclient/gitlab.go | 2 +- vcsclient/gitlab_test.go | 2 +- vcsclient/vcsclient.go | 4 ++-- 8 files changed, 9 insertions(+), 9 deletions(-) diff --git a/vcsclient/azurerepos.go b/vcsclient/azurerepos.go index 507c2ae5..4a088d52 100644 --- a/vcsclient/azurerepos.go +++ b/vcsclient/azurerepos.go @@ -255,7 +255,7 @@ func (client *AzureReposClient) ListOpenPullRequests(ctx context.Context, _, rep return pullRequestsInfo, nil } -func (client *AzureReposClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { +func (client *AzureReposClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { azureReposGitClient, err := client.buildAzureReposClient(ctx) if err != nil { return diff --git a/vcsclient/bitbucketcloud.go b/vcsclient/bitbucketcloud.go index 8e1855e4..7bbe5066 100644 --- a/vcsclient/bitbucketcloud.go +++ b/vcsclient/bitbucketcloud.go @@ -320,7 +320,7 @@ func (client *BitbucketCloudClient) ListOpenPullRequests(ctx context.Context, ow return mapBitbucketCloudPullRequestToPullRequestInfo(&parsedPullRequests), nil } -func (client *BitbucketCloudClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { +func (client *BitbucketCloudClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { err = validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository}) if err != nil { return diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index 9a80ef05..29323cd7 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -361,7 +361,7 @@ func (client *BitbucketServerClient) ListOpenPullRequests(ctx context.Context, o } // GetPullRequestInfoById on bitbucket server -func (client *BitbucketServerClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { +func (client *BitbucketServerClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { client.logger.Debug("fetching pull request by ID in ", repository) bitbucketClient, err := client.buildBitbucketClient(ctx) if err != nil { diff --git a/vcsclient/github.go b/vcsclient/github.go index af69726e..a080a39a 100644 --- a/vcsclient/github.go +++ b/vcsclient/github.go @@ -289,7 +289,7 @@ func (client *GitHubClient) ListOpenPullRequests(ctx context.Context, owner, rep return mapGitHubPullRequestToPullRequestInfoList(pullRequests) } -func (client *GitHubClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) { +func (client *GitHubClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) { ghClient, err := client.buildGithubClient(ctx) if err != nil { return PullRequestInfo{}, err diff --git a/vcsclient/github_test.go b/vcsclient/github_test.go index d16d9ef8..b3e1da8b 100644 --- a/vcsclient/github_test.go +++ b/vcsclient/github_test.go @@ -541,7 +541,7 @@ func TestGitHubClient_GetPullRequestInfoById(t *testing.T) { fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repoName, pullRequestId), createGitHubHandler) defer cleanUp() - result, err := client.GetPullRequestInfoById(ctx, owner, repoName, pullRequestId) + result, err := client.GetPullRequest(ctx, owner, repoName, pullRequestId) require.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ ID: 1, diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index a06a09fe..41630e82 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -253,7 +253,7 @@ func (client *GitLabClient) ListOpenPullRequests(ctx context.Context, _, reposit } // GetPullRequestInfoById on GitLab -func (client *GitLabClient) GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { +func (client *GitLabClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { client.logger.Debug("fetching merge requests by ID in", repository) mergeRequest, _, err := client.glClient.MergeRequests.GetMergeRequest(getProjectID(owner, repository), pullRequestId, nil, gitlab.WithContext(ctx)) if err != nil { diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index 87f87dbe..0e1a6daf 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -232,7 +232,7 @@ func TestGitLabClient_GetPullRequestInfoById(t *testing.T) { fmt.Sprintf("/api/v4/projects/%s/merge_requests/%d", url.PathEscape(owner+"/"+repoName), pullRequestId), createGitLabHandler) defer cleanUp() - result, err := client.GetPullRequestInfoById(ctx, owner, repoName, pullRequestId) + result, err := client.GetPullRequest(ctx, owner, repoName, pullRequestId) require.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ ID: 1, diff --git a/vcsclient/vcsclient.go b/vcsclient/vcsclient.go index 71e6676b..6fdd4cfa 100644 --- a/vcsclient/vcsclient.go +++ b/vcsclient/vcsclient.go @@ -165,11 +165,11 @@ type VcsClient interface { // repository - VCS repository name ListOpenPullRequests(ctx context.Context, owner, repository string) ([]PullRequestInfo, error) - // GetPullRequestInfoById Gets specific pull request info. + // GetPullRequest Gets specific pull request info. // owner - User or organization // repository - VCS repository name // pullRequestId - ID of the pull request - GetPullRequestInfoById(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) + GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) // GetLatestCommit Gets the most recent commit of a branch // owner - User or organization From 7439f2bc08db3a1c121fc6f122934e5c888a190a Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 20 Jun 2023 11:50:39 +0300 Subject: [PATCH 05/16] bitbucketserver_test.go --- vcsclient/bitbucketserver_test.go | 20 ++ .../get_pull_request_response.json | 258 ++++++++++++++++++ 2 files changed, 278 insertions(+) create mode 100644 vcsclient/testdata/bitbucketserver/get_pull_request_response.json diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index 41bef5bc..4d2749d7 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -217,6 +217,26 @@ func TestBitbucketServer_ListOpenPullRequests(t *testing.T) { }, result[0])) } +func TestBitbucketServerClient_GetPullRequest(t *testing.T) { + ctx := context.Background() + response, err := os.ReadFile(filepath.Join("testdata", "bitbucketserver", "get_pull_request_response.json")) + assert.NoError(t, err) + pullRequestId := 6 + + client, cleanUp := createServerAndClient(t, vcsutils.BitbucketServer, true, response, + fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", owner, repo1, pullRequestId), createBitbucketServerHandler) + defer cleanUp() + + result, err := client.GetPullRequest(ctx, owner, repo1, pullRequestId) + + require.NoError(t, err) + assert.True(t, reflect.DeepEqual(PullRequestInfo{ + ID: 6, + Source: BranchInfo{Name: "refs/heads/new_vul_2", Repository: "repoName"}, + Target: BranchInfo{Name: "refs/heads/master", Repository: "repoName"}, + }, result)) +} + func TestBitbucketServer_ListPullRequestComments(t *testing.T) { ctx := context.Background() response, err := os.ReadFile(filepath.Join("testdata", "bitbucketserver", "pull_request_comments_list_response.json")) diff --git a/vcsclient/testdata/bitbucketserver/get_pull_request_response.json b/vcsclient/testdata/bitbucketserver/get_pull_request_response.json new file mode 100644 index 00000000..b90da2f0 --- /dev/null +++ b/vcsclient/testdata/bitbucketserver/get_pull_request_response.json @@ -0,0 +1,258 @@ +{ + "id": 6, + "version": 0, + "title": "New vul 2", + "description": "* add vul\n* test", + "state": "OPEN", + "open": true, + "closed": false, + "createdDate": 1686651080688, + "updatedDate": 1686651080688, + "fromRef": { + "id": "refs/heads/new_vul_2", + "displayId": "new_vul_2", + "latestCommit": "7121b72f7c2a4bdd953bcddd80c037cb598db690", + "type": "BRANCH", + "repository": { + "slug": "repoName", + "id": 35731, + "name": "repoName", + "hierarchyId": "38d6f6b604eee2af0b68", + "scmId": "git", + "state": "AVAILABLE", + "statusMessage": "Available", + "forkable": true, + "origin": { + "slug": "repoName", + "id": 115, + "name": "repoName", + "hierarchyId": "38d6f3b609eee2af0b68", + "scmId": "git", + "state": "AVAILABLE", + "statusMessage": "Available", + "forkable": true, + "project": { + "key": "repoName", + "id": 100, + "name": "repoName", + "description": "repoName", + "public": false, + "type": "NORMAL", + "links": { + "self": [ + { + "href": "https://git.bbServerHost.info/projects/repoName" + } + ] + } + }, + "public": false, + "archived": false, + "links": { + "clone": [ + { + "href": "https://git.bbServerHost.info/scm/repoName/repoName.git", + "name": "http" + }, + { + "href": "ssh://git@git.bbServerHost.info/repoName/repoName.git", + "name": "ssh" + } + ], + "self": [ + { + "href": "https://git.bbServerHost.info/projects/repoName/repos/repoName/browse" + } + ] + } + }, + "project": { + "key": "~owner", + "id": 2424, + "name": "some name", + "type": "PERSONAL", + "owner": { + "name": "owner", + "emailAddress": "owner@bbServerHost.com", + "active": true, + "displayName": "some name", + "id": 2118757, + "slug": "owner", + "type": "NORMAL", + "links": { + "self": [ + { + "href": "https://git.bbServerHost.info/users/owner" + } + ] + } + }, + "links": { + "self": [ + { + "href": "https://git.bbServerHost.info/users/owner" + } + ] + } + }, + "public": false, + "archived": false, + "links": { + "clone": [ + { + "href": "ssh://git@git.bbServerHost.info/~owner/repoName.git", + "name": "ssh" + }, + { + "href": "https://git.bbServerHost.info/scm/~owner/repoName.git", + "name": "http" + } + ], + "self": [ + { + "href": "https://git.bbServerHost.info/users/owner/repos/repoName/browse" + } + ] + } + } + }, + "toRef": { + "id": "refs/heads/master", + "displayId": "master", + "latestCommit": "49ee0968441e2cae00e714c1d7852a6565ce12c7", + "type": "BRANCH", + "repository": { + "slug": "repoName", + "id": 32731, + "name": "repoName", + "hierarchyId": "38d6f4b609eee2af0b68", + "scmId": "git", + "state": "AVAILABLE", + "statusMessage": "Available", + "forkable": true, + "origin": { + "slug": "repoName", + "id": 1215, + "name": "repoName", + "hierarchyId": "38f6f6b609eee2af0b68", + "scmId": "git", + "state": "AVAILABLE", + "statusMessage": "Available", + "forkable": true, + "project": { + "key": "repoName", + "id": 1210, + "name": "repoName", + "description": "repoName", + "public": false, + "type": "NORMAL", + "links": { + "self": [ + { + "href": "https://git.bbServerHost.info/projects/repoName" + } + ] + } + }, + "public": false, + "archived": false, + "links": { + "clone": [ + { + "href": "https://git.bbServerHost.info/scm/repoName/repoName.git", + "name": "http" + }, + { + "href": "ssh://git@git.bbServerHost.info/repoName/repoName.git", + "name": "ssh" + } + ], + "self": [ + { + "href": "https://git.bbServerHost.info/projects/repoName/repos/repoName/browse" + } + ] + } + }, + "project": { + "key": "~owner", + "id": 2424, + "name": "some name", + "type": "PERSONAL", + "owner": { + "name": "owner", + "emailAddress": "owner@bbServerHost.com", + "active": true, + "displayName": "some name", + "id": 2198757, + "slug": "owner", + "type": "NORMAL", + "links": { + "self": [ + { + "href": "https://git.bbServerHost.info/users/owner" + } + ] + } + }, + "links": { + "self": [ + { + "href": "https://git.bbServerHost.info/users/owner" + } + ] + } + }, + "public": false, + "archived": false, + "links": { + "clone": [ + { + "href": "ssh://git@git.bbServerHost.info/~owner/repoName.git", + "name": "ssh" + }, + { + "href": "https://git.bbServerHost.info/scm/~owner/repoName.git", + "name": "http" + } + ], + "self": [ + { + "href": "https://git.bbServerHost.info/users/owner/repos/repoName/browse" + } + ] + } + } + }, + "locked": false, + "author": { + "user": { + "name": "owner", + "emailAddress": "owner@bbServerHost.com", + "active": true, + "displayName": "some name", + "id": 2114757, + "slug": "owner", + "type": "NORMAL", + "links": { + "self": [ + { + "href": "https://git.bbServerHost.info/users/owner" + } + ] + } + }, + "role": "AUTHOR", + "approved": false, + "status": "UNAPPROVED" + }, + "reviewers": [], + "participants": [], + "links": { + "self": [ + { + "href": "https://git.bbServerHost.info/users/owner/repos/repoName/pull-requests/6" + } + ] + } +} \ No newline at end of file From 6fa9859ad2ac4495afc5778d94a64a3e3e7c2a91 Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 20 Jun 2023 14:05:29 +0300 Subject: [PATCH 06/16] bitbucket cloud --- vcsclient/bitbucketcloud.go | 7 +- vcsclient/bitbucketcloud_test.go | 18 ++ .../get_pull_request_response.json | 161 ++++++++++++++++++ 3 files changed, 183 insertions(+), 3 deletions(-) create mode 100644 vcsclient/testdata/bitbucketcloud/get_pull_request_response.json diff --git a/vcsclient/bitbucketcloud.go b/vcsclient/bitbucketcloud.go index 7bbe5066..f3febf31 100644 --- a/vcsclient/bitbucketcloud.go +++ b/vcsclient/bitbucketcloud.go @@ -9,6 +9,7 @@ import ( "net/http" "net/url" "sort" + "strconv" "strings" "time" @@ -327,20 +328,20 @@ func (client *BitbucketCloudClient) GetPullRequest(ctx context.Context, owner, r } bitbucketClient := client.buildBitbucketCloudClient(ctx) client.logger.Debug(fetchingPullRequestById, repository) + prIdStr := strconv.Itoa(pullRequestId) options := &bitbucket.PullRequestsOptions{ Owner: owner, RepoSlug: repository, - ID: string(rune(pullRequestId)), + ID: prIdStr, } pullRequestRaw, err := bitbucketClient.Repositories.PullRequests.Get(options) if err != nil { return } - parsedPullRequest, err := vcsutils.RemapFields[pullRequestsResponse](pullRequestRaw, "json") + pullRequestDetails, err := vcsutils.RemapFields[pullRequestsDetails](pullRequestRaw, "json") if err != nil { return } - pullRequestDetails := parsedPullRequest.Values[0] pullRequestInfo = PullRequestInfo{ ID: pullRequestDetails.ID, Source: BranchInfo{ diff --git a/vcsclient/bitbucketcloud_test.go b/vcsclient/bitbucketcloud_test.go index 5497afaa..ff80f1cc 100644 --- a/vcsclient/bitbucketcloud_test.go +++ b/vcsclient/bitbucketcloud_test.go @@ -174,6 +174,24 @@ func TestBitbucketCloud_ListOpenPullRequests(t *testing.T) { }, result[0])) } +func TestBitbucketCloudClient_GetPullRequest(t *testing.T) { + pullRequestId := 1 + repoName := "froggit" + ctx := context.Background() + response, err := os.ReadFile(filepath.Join("testdata", "bitbucketcloud", "get_pull_request_response.json")) + assert.NoError(t, err) + client, cleanUp := createServerAndClient(t, vcsutils.BitbucketCloud, true, response, + fmt.Sprintf("/repositories/%s/%s/pullrequests/%d", owner, repoName, pullRequestId), createBitbucketCloudHandler) + defer cleanUp() + result, err := client.GetPullRequest(ctx, owner, repoName, pullRequestId) + require.NoError(t, err) + assert.True(t, reflect.DeepEqual(PullRequestInfo{ + ID: int64(pullRequestId), + Source: BranchInfo{Name: "pr", Repository: "workspace/froggit"}, + Target: BranchInfo{Name: "main", Repository: "workspace/froggit"}, + }, result)) +} + func TestBitbucketCloud_AddPullRequestComment(t *testing.T) { ctx := context.Background() client, cleanUp := createServerAndClient(t, vcsutils.BitbucketCloud, true, nil, "/repositories/jfrog/repo-1/pullrequests/1/comments", createBitbucketCloudHandler) diff --git a/vcsclient/testdata/bitbucketcloud/get_pull_request_response.json b/vcsclient/testdata/bitbucketcloud/get_pull_request_response.json new file mode 100644 index 00000000..97db1e97 --- /dev/null +++ b/vcsclient/testdata/bitbucketcloud/get_pull_request_response.json @@ -0,0 +1,161 @@ +{ + "comment_count": 0, + "task_count": 0, + "type": "pullrequest", + "id": 1, + "title": "s", + "description": "s", + "rendered": { + "title": { + "type": "rendered", + "raw": "s", + "markup": "markdown", + "html": "

s

" + }, + "description": { + "type": "rendered", + "raw": "s", + "markup": "markdown", + "html": "

s

" + } + }, + "state": "CLOSED", + "merge_commit": null, + "close_source_branch": false, + "closed_by": null, + "author": { + "display_name": "fname lname", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/users/%7B0488ed53-e751-4504-a343-1a43d72590a4%7D" + }, + "avatar": { + "href": "https://secure.gravatar.com/avatar/4a0ffac6cc540dd6983b35ec3bd4df94?d=https%3A%2F%2Favatar-management--avatars.us-west-2.prod.public.atl-paas.net%2Finitials%2FED-1.png" + }, + "html": { + "href": "https://bitbucket.org/%7B0488ed53-e751-4504-a343-1a43d72590a4%7D/" + } + }, + "type": "user", + "uuid": "{0488ed53-e751-4504-a343-1a43d72590a4}", + "account_id": "63f32b584e86f362d399cdc2", + "nickname": "fname lname" + }, + "reason": "", + "created_on": "2023-06-20T09:00:47.082738+00:00", + "updated_on": "2023-06-20T09:00:47.725250+00:00", + "destination": { + "branch": { + "name": "main" + }, + "commit": { + "type": "commit", + "hash": "9a1e81efaa1c", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/commit/9a1e81efaa1c" + }, + "html": { + "href": "https://bitbucket.org/workspace/froggit/commits/9a1e81efaa1c" + } + } + }, + "repository": { + "type": "repository", + "full_name": "workspace/froggit", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit" + }, + "html": { + "href": "https://bitbucket.org/workspace/froggit" + }, + "avatar": { + "href": "https://bytebucket.org/ravatar/%7Bacb2b52e-67f2-48bf-96f1-b441b5acb810%7D?ts=default" + } + }, + "name": "froggit", + "uuid": "{acb2b52e-67f2-48bf-96f1-b441b5acb810}" + } + }, + "source": { + "branch": { + "name": "pr" + }, + "commit": { + "type": "commit", + "hash": "18f5e1ecb37e", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/commit/18f5e1ecb37e" + }, + "html": { + "href": "https://bitbucket.org/workspace/froggit/commits/18f5e1ecb37e" + } + } + }, + "repository": { + "type": "repository", + "full_name": "workspace/froggit", + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit" + }, + "html": { + "href": "https://bitbucket.org/workspace/froggit" + }, + "avatar": { + "href": "https://bytebucket.org/ravatar/%7Bacb2b52e-67f2-48bf-96f1-b441b5acb810%7D?ts=default" + } + }, + "name": "froggit", + "uuid": "{acb2b52e-67f2-48bf-96f1-b441b5acb810}" + } + }, + "reviewers": [], + "participants": [], + "links": { + "self": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/pullrequests/1" + }, + "html": { + "href": "https://bitbucket.org/workspace/froggit/pull-requests/1" + }, + "commits": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/pullrequests/1/commits" + }, + "approve": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/pullrequests/1/approve" + }, + "request-changes": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/pullrequests/1/request-changes" + }, + "diff": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/diff/workspace/froggit:18f5e1ecb37e%0D9a1e81efaa1c?from_pullrequest_id=1&topic=true" + }, + "diffstat": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/diffstat/workspace/froggit:18f5e1ecb37e%0D9a1e81efaa1c?from_pullrequest_id=1&topic=true" + }, + "comments": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/pullrequests/1/comments" + }, + "activity": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/pullrequests/1/activity" + }, + "merge": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/pullrequests/1/merge" + }, + "decline": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/pullrequests/1/decline" + }, + "statuses": { + "href": "https://api.bitbucket.org/2.0/repositories/workspace/froggit/pullrequests/1/statuses" + } + }, + "summary": { + "type": "rendered", + "raw": "s", + "markup": "markdown", + "html": "

s

" + } +} \ No newline at end of file From 827c03ac263954fadd3674e8cca8ca5e45bbc027 Mon Sep 17 00:00:00 2001 From: delarea Date: Tue, 20 Jun 2023 15:07:53 +0300 Subject: [PATCH 07/16] azurerepos_test.go --- vcsclient/azurerepos_test.go | 25 +++++++++++++++++++ .../azurerepos/resourcesResponse.json | 9 +++++++ 2 files changed, 34 insertions(+) diff --git a/vcsclient/azurerepos_test.go b/vcsclient/azurerepos_test.go index 2b2ce7f0..2a9796a2 100644 --- a/vcsclient/azurerepos_test.go +++ b/vcsclient/azurerepos_test.go @@ -194,6 +194,31 @@ func TestAzureRepos_TestListOpenPullRequests(t *testing.T) { assert.Error(t, err) } +func TestAzureReposClient_GetPullRequest(t *testing.T) { + pullRequestId := 1 + repoName := "repoName" + sourceName := "source" + targetName := "master" + res := git.GitPullRequest{ + SourceRefName: &sourceName, + TargetRefName: &targetName, + PullRequestId: &pullRequestId, + } + jsonRes, err := json.Marshal(res) + assert.NoError(t, err) + ctx := context.Background() + client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, jsonRes, fmt.Sprintf("getPullRequests/%d", pullRequestId), createAzureReposHandler) + defer cleanUp() + pullRequestsInfo, err := client.GetPullRequest(ctx, "", repoName, pullRequestId) + assert.NoError(t, err) + assert.True(t, reflect.DeepEqual(pullRequestsInfo, PullRequestInfo{ID: 1, Source: BranchInfo{Name: sourceName, Repository: repoName}, Target: BranchInfo{Name: targetName, Repository: repoName}})) + + badClient, cleanUp := createBadAzureReposClient(t, []byte{}) + defer cleanUp() + _, err = badClient.ListOpenPullRequests(ctx, "", repo1) + assert.Error(t, err) +} + func TestListPullRequestComments(t *testing.T) { type ListPullRequestCommentsResponse struct { Value []git.GitPullRequestCommentThread diff --git a/vcsclient/testdata/azurerepos/resourcesResponse.json b/vcsclient/testdata/azurerepos/resourcesResponse.json index 78bf637b..6715abbb 100644 --- a/vcsclient/testdata/azurerepos/resourcesResponse.json +++ b/vcsclient/testdata/azurerepos/resourcesResponse.json @@ -99,6 +99,15 @@ "minVersion": "3.2", "maxVersion": "7.1", "releasedVersion": "0.0" + } , { + "id": "01a46dea-7d46-4d40-bc84-319e7c260d99", + "area": "Location", + "resourceName": "ResourceAreas", + "routeTemplate": "_apis/{resource}/{areaId}/getPullRequests/{pullRequestId}", + "resourceVersion": 1, + "minVersion": "3.2", + "maxVersion": "7.1", + "releasedVersion": "0.0" } ], "count": 2 From 1f23a580fb05dd776d3aff9c741ed9513ba6d0e1 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 21 Jun 2023 17:49:48 +0300 Subject: [PATCH 08/16] Update tests --- vcsclient/azurerepos_test.go | 2 +- vcsclient/bitbucketserver_test.go | 3 +++ vcsclient/github_test.go | 4 ++-- vcsclient/gitlab_test.go | 2 +- 4 files changed, 7 insertions(+), 4 deletions(-) diff --git a/vcsclient/azurerepos_test.go b/vcsclient/azurerepos_test.go index 2a9796a2..2bc789b7 100644 --- a/vcsclient/azurerepos_test.go +++ b/vcsclient/azurerepos_test.go @@ -215,7 +215,7 @@ func TestAzureReposClient_GetPullRequest(t *testing.T) { badClient, cleanUp := createBadAzureReposClient(t, []byte{}) defer cleanUp() - _, err = badClient.ListOpenPullRequests(ctx, "", repo1) + _, err = badClient.GetPullRequest(ctx, "", repo1, pullRequestId) assert.Error(t, err) } diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index 4d2749d7..be69e4c0 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -235,6 +235,9 @@ func TestBitbucketServerClient_GetPullRequest(t *testing.T) { Source: BranchInfo{Name: "refs/heads/new_vul_2", Repository: "repoName"}, Target: BranchInfo{Name: "refs/heads/master", Repository: "repoName"}, }, result)) + + _, err = createBadBitbucketServerClient(t).GetPullRequest(ctx, owner, repo1, pullRequestId) + assert.Error(t, err) } func TestBitbucketServer_ListPullRequestComments(t *testing.T) { diff --git a/vcsclient/github_test.go b/vcsclient/github_test.go index b3e1da8b..fb01b022 100644 --- a/vcsclient/github_test.go +++ b/vcsclient/github_test.go @@ -531,7 +531,7 @@ func TestGitHubClient_ListOpenPullRequests(t *testing.T) { assert.Error(t, err) } -func TestGitHubClient_GetPullRequestInfoById(t *testing.T) { +func TestGitHubClient_GetPullRequest(t *testing.T) { ctx := context.Background() pullRequestId := 1 repoName := "Hello-World" @@ -549,7 +549,7 @@ func TestGitHubClient_GetPullRequestInfoById(t *testing.T) { Target: BranchInfo{Name: "master", Repository: "Hello-World"}, }, result)) - _, err = createBadGitHubClient(t).ListPullRequestComments(ctx, owner, repoName, 1) + _, err = createBadGitHubClient(t).GetPullRequest(ctx, owner, repoName, 1) assert.Error(t, err) } diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index 0dd0d71e..b0966453 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -221,7 +221,7 @@ func TestGitLabClient_ListOpenPullRequests(t *testing.T) { }, result[0])) } -func TestGitLabClient_GetPullRequestInfoById(t *testing.T) { +func TestGitLabClient_GetPullRequest(t *testing.T) { ctx := context.Background() repoName := "repo" pullRequestId := 1 From bd717ee93231731f5b5bdc714546eed60428c79d Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 22 Jun 2023 10:26:40 +0300 Subject: [PATCH 09/16] Add negative tests --- vcsclient/bitbucketcloud.go | 3 --- vcsclient/bitbucketcloud_test.go | 20 +++++++++++++++++++- vcsclient/bitbucketserver_test.go | 11 +++++++++-- 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/vcsclient/bitbucketcloud.go b/vcsclient/bitbucketcloud.go index f3febf31..dca2949c 100644 --- a/vcsclient/bitbucketcloud.go +++ b/vcsclient/bitbucketcloud.go @@ -353,9 +353,6 @@ func (client *BitbucketCloudClient) GetPullRequest(ctx context.Context, owner, r Repository: pullRequestDetails.Target.Repository.Name, }, } - if err != nil { - return - } return } diff --git a/vcsclient/bitbucketcloud_test.go b/vcsclient/bitbucketcloud_test.go index ff80f1cc..ac44222a 100644 --- a/vcsclient/bitbucketcloud_test.go +++ b/vcsclient/bitbucketcloud_test.go @@ -178,18 +178,36 @@ func TestBitbucketCloudClient_GetPullRequest(t *testing.T) { pullRequestId := 1 repoName := "froggit" ctx := context.Background() + + // Successful Response response, err := os.ReadFile(filepath.Join("testdata", "bitbucketcloud", "get_pull_request_response.json")) assert.NoError(t, err) client, cleanUp := createServerAndClient(t, vcsutils.BitbucketCloud, true, response, fmt.Sprintf("/repositories/%s/%s/pullrequests/%d", owner, repoName, pullRequestId), createBitbucketCloudHandler) defer cleanUp() result, err := client.GetPullRequest(ctx, owner, repoName, pullRequestId) - require.NoError(t, err) + assert.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ ID: int64(pullRequestId), Source: BranchInfo{Name: "pr", Repository: "workspace/froggit"}, Target: BranchInfo{Name: "main", Repository: "workspace/froggit"}, }, result)) + + // Bad Response + badClient, badClientCleanUp := createServerAndClient(t, vcsutils.BitbucketCloud, true, "{", + fmt.Sprintf("/repositories/%s/%s/pullrequests/%d", owner, repoName, pullRequestId), createBitbucketCloudHandler) + defer badClientCleanUp() + _, err = badClient.GetPullRequest(ctx, owner, repoName, pullRequestId) + assert.Error(t, err) + + // Bad Fields + badRepoName := "" + badParseClient, badParseClientCleanUp := createServerAndClient(t, vcsutils.BitbucketCloud, true, response, + fmt.Sprintf("/repositories/%s/%s/pullrequests/%d", owner, badRepoName, pullRequestId), createBitbucketCloudHandler) + defer badParseClientCleanUp() + _, err = badParseClient.GetPullRequest(ctx, owner, badRepoName, pullRequestId) + assert.Error(t, err) + } func TestBitbucketCloud_AddPullRequestComment(t *testing.T) { diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index be69e4c0..a32b7be5 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -223,12 +223,11 @@ func TestBitbucketServerClient_GetPullRequest(t *testing.T) { assert.NoError(t, err) pullRequestId := 6 + // Successful client, cleanUp := createServerAndClient(t, vcsutils.BitbucketServer, true, response, fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", owner, repo1, pullRequestId), createBitbucketServerHandler) defer cleanUp() - result, err := client.GetPullRequest(ctx, owner, repo1, pullRequestId) - require.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ ID: 6, @@ -236,6 +235,14 @@ func TestBitbucketServerClient_GetPullRequest(t *testing.T) { Target: BranchInfo{Name: "refs/heads/master", Repository: "repoName"}, }, result)) + // Bad response + badClient, badClientCleanUp := createServerAndClient(t, vcsutils.BitbucketServer, true, "{", + fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", owner, repo1, pullRequestId), createBitbucketServerHandler) + defer badClientCleanUp() + _, err2 := badClient.GetPullRequest(ctx, owner, repo1, pullRequestId) + require.Error(t, err2) + + // Bad Client _, err = createBadBitbucketServerClient(t).GetPullRequest(ctx, owner, repo1, pullRequestId) assert.Error(t, err) } From 63c6a1fef8e8b2153395812b1bc73995e6f3841b Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 22 Jun 2023 10:53:42 +0300 Subject: [PATCH 10/16] Add negative tests BB server, gitlab & github --- vcsclient/bitbucketserver_test.go | 2 +- vcsclient/github.go | 6 +- vcsclient/github_test.go | 27 ++- vcsclient/gitlab_test.go | 14 +- ...pull_request_info_response_bad_labels.json | 196 ++++++++++++++++++ 5 files changed, 234 insertions(+), 11 deletions(-) create mode 100644 vcsclient/testdata/github/pull_request_info_response_bad_labels.json diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index a32b7be5..cfa60252 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -230,7 +230,7 @@ func TestBitbucketServerClient_GetPullRequest(t *testing.T) { result, err := client.GetPullRequest(ctx, owner, repo1, pullRequestId) require.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ - ID: 6, + ID: int64(pullRequestId), Source: BranchInfo{Name: "refs/heads/new_vul_2", Repository: "repoName"}, Target: BranchInfo{Name: "refs/heads/master", Repository: "repoName"}, }, result)) diff --git a/vcsclient/github.go b/vcsclient/github.go index a080a39a..d1dc5039 100644 --- a/vcsclient/github.go +++ b/vcsclient/github.go @@ -302,8 +302,8 @@ func (client *GitHubClient) GetPullRequest(ctx context.Context, owner, repositor sourceBranch, err1 := extractBranchFromLabel(*pullRequest.Head.Label) targetBranch, err2 := extractBranchFromLabel(*pullRequest.Base.Label) - badLabelErrors := errors.Join(err1, err2) - if badLabelErrors != nil { + err = errors.Join(err1, err2) + if err != nil { return PullRequestInfo{}, err } @@ -324,7 +324,7 @@ func (client *GitHubClient) GetPullRequest(ctx context.Context, owner, repositor // Extracts branch name from the following expected label format repo:branch func extractBranchFromLabel(label string) (string, error) { split := strings.Split(label, ":") - if len(split) < 1 { + if len(split) <= 1 { return "", fmt.Errorf("bad label format %s", label) } return split[1], nil diff --git a/vcsclient/github_test.go b/vcsclient/github_test.go index fb01b022..5ee08c2d 100644 --- a/vcsclient/github_test.go +++ b/vcsclient/github_test.go @@ -535,22 +535,41 @@ func TestGitHubClient_GetPullRequest(t *testing.T) { ctx := context.Background() pullRequestId := 1 repoName := "Hello-World" + + // Successful response response, err := os.ReadFile(filepath.Join("testdata", "github", "pull_request_info_response.json")) assert.NoError(t, err) client, cleanUp := createServerAndClient(t, vcsutils.GitHub, false, response, fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repoName, pullRequestId), createGitHubHandler) defer cleanUp() - result, err := client.GetPullRequest(ctx, owner, repoName, pullRequestId) - require.NoError(t, err) + assert.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ - ID: 1, + ID: int64(pullRequestId), Source: BranchInfo{Name: "new-topic", Repository: "Hello-World"}, Target: BranchInfo{Name: "master", Repository: "Hello-World"}, }, result)) - _, err = createBadGitHubClient(t).GetPullRequest(ctx, owner, repoName, 1) + // Bad Labels + badLabels, err := os.ReadFile(filepath.Join("testdata", "github", "pull_request_info_response_bad_labels.json")) + assert.NoError(t, err) + badLabelsClient, badLabelClientCleanUp := createServerAndClient(t, vcsutils.GitHub, false, badLabels, + fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repoName, pullRequestId), createGitHubHandler) + defer badLabelClientCleanUp() + _, err = badLabelsClient.GetPullRequest(ctx, owner, repoName, pullRequestId) assert.Error(t, err) + + // Bad client + _, err = createBadGitHubClient(t).GetPullRequest(ctx, owner, repoName, pullRequestId) + assert.Error(t, err) + + // Bad Response + badResponseClient, badResponseCleanUp := createServerAndClient(t, vcsutils.GitHub, false, "{", + fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repoName, pullRequestId), createGitHubHandler) + defer badResponseCleanUp() + _, err = badResponseClient.GetPullRequest(ctx, owner, repoName, pullRequestId) + assert.Error(t, err) + } func TestGitHubClient_ListPullRequestComments(t *testing.T) { diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index b0966453..f5820f37 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -225,20 +225,28 @@ func TestGitLabClient_GetPullRequest(t *testing.T) { ctx := context.Background() repoName := "repo" pullRequestId := 1 + + // Successful response response, err := os.ReadFile(filepath.Join("testdata", "gitlab", "get_merge_request_response.json")) assert.NoError(t, err) - client, cleanUp := createServerAndClient(t, vcsutils.GitLab, false, response, fmt.Sprintf("/api/v4/projects/%s/merge_requests/%d", url.PathEscape(owner+"/"+repoName), pullRequestId), createGitLabHandler) defer cleanUp() - result, err := client.GetPullRequest(ctx, owner, repoName, pullRequestId) - require.NoError(t, err) + assert.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ ID: 1, Source: BranchInfo{Name: "manual-job-rules", Repository: ""}, Target: BranchInfo{Name: "master", Repository: ""}, }, result)) + + // Bad client + badClient, badClientCleanUp := createServerAndClient(t, vcsutils.GitLab, false, "", + fmt.Sprintf("/api/v4/projects/%s/merge_requests/%d", url.PathEscape(owner+"/"+repoName), pullRequestId), createGitLabHandler) + defer badClientCleanUp() + _, err = badClient.GetPullRequest(ctx, owner, repoName, pullRequestId) + assert.Error(t, err) + } func TestGitLabClient_GetLatestCommit(t *testing.T) { diff --git a/vcsclient/testdata/github/pull_request_info_response_bad_labels.json b/vcsclient/testdata/github/pull_request_info_response_bad_labels.json new file mode 100644 index 00000000..e3ac3f30 --- /dev/null +++ b/vcsclient/testdata/github/pull_request_info_response_bad_labels.json @@ -0,0 +1,196 @@ +{ + "url": "https://api.github.com/repos/octocat/Hello-World/pulls/1347", + "id": 1, + "node_id": "MDExOlB1bGxSZXF1ZXN0MQ==", + "number": 1347, + "state": "open", + "locked": true, + "title": "Amazing new feature", + "user": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "head": { + "label": "octocatnew-topic", + "ref": "new-topic", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "user": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "repo": { + "id": 1296269, + "node_id": "MDEwOlJlcG9zaXRvcnkxMjk2MjY5", + "name": "Hello-World", + "full_name": "octocat/Hello-World", + "owner": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + }, + "private": false, + "html_url": "https://github.com/octocat/Hello-World", + "description": "This your first repo!", + "fork": false, + "url": "https://api.github.com/repos/octocat/Hello-World", + "archive_url": "https://api.github.com/repos/octocat/Hello-World/{archive_format}{/ref}", + "assignees_url": "https://api.github.com/repos/octocat/Hello-World/assignees{/user}", + "blobs_url": "https://api.github.com/repos/octocat/Hello-World/git/blobs{/sha}", + "branches_url": "https://api.github.com/repos/octocat/Hello-World/branches{/branch}", + "collaborators_url": "https://api.github.com/repos/octocat/Hello-World/collaborators{/collaborator}", + "comments_url": "https://api.github.com/repos/octocat/Hello-World/comments{/number}", + "commits_url": "https://api.github.com/repos/octocat/Hello-World/commits{/sha}", + "compare_url": "https://api.github.com/repos/octocat/Hello-World/compare/{base}...{head}", + "contents_url": "https://api.github.com/repos/octocat/Hello-World/contents/{+path}", + "contributors_url": "https://api.github.com/repos/octocat/Hello-World/contributors", + "deployments_url": "https://api.github.com/repos/octocat/Hello-World/deployments", + "downloads_url": "https://api.github.com/repos/octocat/Hello-World/downloads", + "events_url": "https://api.github.com/repos/octocat/Hello-World/events", + "forks_url": "https://api.github.com/repos/octocat/Hello-World/forks", + "git_commits_url": "https://api.github.com/repos/octocat/Hello-World/git/commits{/sha}", + "git_refs_url": "https://api.github.com/repos/octocat/Hello-World/git/refs{/sha}", + "git_tags_url": "https://api.github.com/repos/octocat/Hello-World/git/tags{/sha}", + "git_url": "git:github.com/octocat/Hello-World.git", + "issue_comment_url": "https://api.github.com/repos/octocat/Hello-World/issues/comments{/number}", + "issue_events_url": "https://api.github.com/repos/octocat/Hello-World/issues/events{/number}", + "issues_url": "https://api.github.com/repos/octocat/Hello-World/issues{/number}", + "keys_url": "https://api.github.com/repos/octocat/Hello-World/keys{/key_id}", + "labels_url": "https://api.github.com/repos/octocat/Hello-World/labels{/name}", + "languages_url": "https://api.github.com/repos/octocat/Hello-World/languages", + "merges_url": "https://api.github.com/repos/octocat/Hello-World/merges", + "milestones_url": "https://api.github.com/repos/octocat/Hello-World/milestones{/number}", + "notifications_url": "https://api.github.com/repos/octocat/Hello-World/notifications{?since,all,participating}", + "pulls_url": "https://api.github.com/repos/octocat/Hello-World/pulls{/number}", + "releases_url": "https://api.github.com/repos/octocat/Hello-World/releases{/id}", + "ssh_url": "git@github.com:octocat/Hello-World.git", + "stargazers_url": "https://api.github.com/repos/octocat/Hello-World/stargazers", + "statuses_url": "https://api.github.com/repos/octocat/Hello-World/statuses/{sha}", + "subscribers_url": "https://api.github.com/repos/octocat/Hello-World/subscribers", + "subscription_url": "https://api.github.com/repos/octocat/Hello-World/subscription", + "tags_url": "https://api.github.com/repos/octocat/Hello-World/tags", + "teams_url": "https://api.github.com/repos/octocat/Hello-World/teams", + "trees_url": "https://api.github.com/repos/octocat/Hello-World/git/trees{/sha}", + "clone_url": "https://github.com/octocat/Hello-World.git", + "mirror_url": "git:git.example.com/octocat/Hello-World", + "hooks_url": "https://api.github.com/repos/octocat/Hello-World/hooks", + "svn_url": "https://svn.github.com/octocat/Hello-World", + "homepage": "https://github.com", + "language": null, + "forks_count": 9, + "stargazers_count": 80, + "watchers_count": 80, + "size": 108, + "default_branch": "master", + "open_issues_count": 0, + "topics": [ + "octocat", + "atom", + "electron", + "api" + ], + "has_issues": true, + "has_projects": true, + "has_wiki": true, + "has_pages": false, + "has_downloads": true, + "has_discussions": false, + "archived": false, + "disabled": false, + "pushed_at": "2011-01-26T19:06:43Z", + "created_at": "2011-01-26T19:01:12Z", + "updated_at": "2011-01-26T19:14:43Z", + "permissions": { + "admin": false, + "push": false, + "pull": true + }, + "allow_rebase_merge": true, + "temp_clone_token": "ABTLWHOULUVAXGTRYU7OC2876QJ2O", + "allow_squash_merge": true, + "allow_merge_commit": true, + "allow_forking": true, + "forks": 123, + "open_issues": 123, + "license": { + "key": "mit", + "name": "MIT License", + "url": "https://api.github.com/licenses/mit", + "spdx_id": "MIT", + "node_id": "MDc6TGljZW5zZW1pdA==" + }, + "watchers": 123 + } + }, + "base": { + "label": "octocat:master", + "ref": "master", + "sha": "6dcb09b5b57875f334f61aebed695e2e4193db5e", + "user": { + "login": "octocat", + "id": 1, + "node_id": "MDQ6VXNlcjE=", + "avatar_url": "https://github.com/images/error/octocat_happy.gif", + "gravatar_id": "", + "url": "https://api.github.com/users/octocat", + "html_url": "https://github.com/octocat", + "followers_url": "https://api.github.com/users/octocat/followers", + "following_url": "https://api.github.com/users/octocat/following{/other_user}", + "gists_url": "https://api.github.com/users/octocat/gists{/gist_id}", + "starred_url": "https://api.github.com/users/octocat/starred{/owner}{/repo}", + "subscriptions_url": "https://api.github.com/users/octocat/subscriptions", + "organizations_url": "https://api.github.com/users/octocat/orgs", + "repos_url": "https://api.github.com/users/octocat/repos", + "events_url": "https://api.github.com/users/octocat/events{/privacy}", + "received_events_url": "https://api.github.com/users/octocat/received_events", + "type": "User", + "site_admin": false + } + } +} \ No newline at end of file From 8d152fc6959ec2f4c947803c6651eeb71483166c Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 22 Jun 2023 12:34:30 +0300 Subject: [PATCH 11/16] remove bad context param --- vcsclient/gitlab.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index f6b558ce..d09005b1 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -255,7 +255,7 @@ func (client *GitLabClient) ListOpenPullRequests(ctx context.Context, _, reposit // GetPullRequestInfoById on GitLab func (client *GitLabClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { client.logger.Debug("fetching merge requests by ID in", repository) - mergeRequest, _, err := client.glClient.MergeRequests.GetMergeRequest(getProjectID(owner, repository), pullRequestId, nil, gitlab.WithContext(ctx)) + mergeRequest, _, err := client.glClient.MergeRequests.GetMergeRequest(getProjectID(owner, repository), pullRequestId, nil) if err != nil { return } From 272b19ef4bc23401f867900bba7f57ce5a8520d9 Mon Sep 17 00:00:00 2001 From: delarea Date: Thu, 22 Jun 2023 14:43:03 +0300 Subject: [PATCH 12/16] Add new feature to README.md --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index dd084031..a52c2908 100644 --- a/README.md +++ b/README.md @@ -41,6 +41,7 @@ Currently supported providers are: [GitHub](#github), [Bitbucket Server](#bitbuc - [Get Commit Status](#get-commit-status) - [Create Pull Request](#create-pull-request) - [List Open Pull Requests](#list-open-pull-requests) + - [Get Pull Request](#get-pull-request) - [Add Pull Request Comment](#add-pull-request-comment) - [List Pull Request Comments](#list-pull-request-comments) - [Get Latest Commit](#get-latest-commit) @@ -345,6 +346,21 @@ repository := "jfrog-cli" openPullRequests, err := client.ListOpenPullRequests(ctx, owner, repository) ``` +#### Get Pull Request + +```go +// Go context +ctx := context.Background() +// Organization or username +owner := "jfrog" +// VCS repository +repository := "jfrog-cli" +// Pull Request ID +pullRequestId := 1 + +openPullRequests, err := client.GetPullReequest(ctx, owner, repository, pullRequestId) +``` + ##### Add Pull Request Comment ```go From ad44cd9c3bd29db9f3f0e21c80201008ecc52c14 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 28 Jun 2023 11:01:26 +0300 Subject: [PATCH 13/16] CR --- README.md | 4 ++-- go.mod | 4 ++-- go.sum | 11 ++++++----- vcsclient/azurerepos.go | 2 +- vcsclient/azurerepos_test.go | 4 ++-- vcsclient/bitbucketcloud.go | 2 +- vcsclient/bitbucketcloud_test.go | 6 +++--- vcsclient/bitbucketserver.go | 4 ++-- vcsclient/bitbucketserver_test.go | 6 +++--- vcsclient/github.go | 6 +++--- vcsclient/github_test.go | 6 +++--- vcsclient/gitlab.go | 8 ++++---- vcsclient/gitlab_test.go | 4 ++-- vcsclient/logger.go | 2 +- vcsclient/vcsclient.go | 4 ++-- 15 files changed, 37 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index a52c2908..ebcbddf3 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ Currently supported providers are: [GitHub](#github), [Bitbucket Server](#bitbuc - [Get Commit Status](#get-commit-status) - [Create Pull Request](#create-pull-request) - [List Open Pull Requests](#list-open-pull-requests) - - [Get Pull Request](#get-pull-request) + - [Get Pull Request By ID](#get-pull-request) - [Add Pull Request Comment](#add-pull-request-comment) - [List Pull Request Comments](#list-pull-request-comments) - [Get Latest Commit](#get-latest-commit) @@ -346,7 +346,7 @@ repository := "jfrog-cli" openPullRequests, err := client.ListOpenPullRequests(ctx, owner, repository) ``` -#### Get Pull Request +#### Get Pull Request By ID ```go // Go context diff --git a/go.mod b/go.mod index 5773290e..005a5e4b 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,12 @@ module github.com/jfrog/froggit-go go 1.20 require ( - github.com/gfleury/go-bitbucket-v1 v0.0.0-20220418082332-711d7d5e805f + github.com/gfleury/go-bitbucket-v1 v0.0.0-20230626192437-8d7be5866751 github.com/go-git/go-git/v5 v5.5.2 github.com/google/go-github/v45 v45.2.0 github.com/google/uuid v1.3.0 github.com/grokify/mogo v0.40.4 - github.com/jfrog/gofrog v1.2.5 + github.com/jfrog/gofrog v1.3.0 github.com/ktrysmt/go-bitbucket v0.9.32 github.com/microsoft/azure-devops-go-api/azuredevops v1.0.0-b5 github.com/mitchellh/mapstructure v1.5.0 diff --git a/go.sum b/go.sum index 4097841b..6abb94d1 100644 --- a/go.sum +++ b/go.sum @@ -10,7 +10,6 @@ github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuW github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5 h1:0CwZNZbxp69SHPdPJAN/hZIm0C4OItdklCFmMRWYpio= github.com/armon/go-socks5 v0.0.0-20160902184237-e75332964ef5/go.mod h1:wHh0iHkYZB8zMSxRWpUBQtwG5a7fFgvEO+odwuTv2gs= github.com/bwesterb/go-ristretto v1.2.0/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= -github.com/cloudflare/circl v1.1.0 h1:bZgT/A+cikZnKIwn7xL2OBj012Bmvho/o6RpRvv3GKY= github.com/cloudflare/circl v1.1.0/go.mod h1:prBCrKB9DV4poKZY1l9zBXg2QJY7mvgRvtMxxK7fi4I= github.com/cloudflare/circl v1.3.3 h1:fE/Qz0QdIGqeWfnwq0RE0R7MI51s0M2E4Ga9kq5AEMs= github.com/cloudflare/circl v1.3.3/go.mod h1:5XYMA4rFBvNIrhs50XuiBJ15vF2pZn4nnUKZrLbUZFA= @@ -23,8 +22,8 @@ github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FM github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4= github.com/fatih/color v1.13.0 h1:8LOYc1KYPPmyKMuN8QV2DNRWNbLo6LZ0iLs8+mlH53w= github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk= -github.com/gfleury/go-bitbucket-v1 v0.0.0-20220418082332-711d7d5e805f h1:xVRGXRHjGaqT9M+mNNQrsoku+p2z/+Ei/b2gs7ZCbZw= -github.com/gfleury/go-bitbucket-v1 v0.0.0-20220418082332-711d7d5e805f/go.mod h1:LB3osS9X2JMYmTzcCArHHLrndBAfcVLQAvUddfs+ONs= +github.com/gfleury/go-bitbucket-v1 v0.0.0-20230626192437-8d7be5866751 h1:Ea58sAu8LX/NS7z3aeL+YX/7+FSd9jsxq6Ecpz7QEDM= +github.com/gfleury/go-bitbucket-v1 v0.0.0-20230626192437-8d7be5866751/go.mod h1:IqOZzks2wlWCIai0esXnZPdPwxF2yOz0HcCYw5I4pCg= github.com/gliderlabs/ssh v0.3.5 h1:OcaySEmAQJgyYcArR+gGGTHCyE7nvhEMTlYY+Dp8CpY= github.com/gliderlabs/ssh v0.3.5/go.mod h1:8XB4KraRrX39qHhT6yxPsHedjA08I/uBVwj4xC+/+z4= github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4= @@ -69,8 +68,8 @@ github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99 h1:BQSFePA1RWJOlocH6Fxy8MmwDt+yVQYULKfN0RoTN8A= github.com/jbenet/go-context v0.0.0-20150711004518-d14ea06fba99/go.mod h1:1lJo3i6rXxKeerYnT8Nvf0QmHCRC1n8sfWVwXF2Frvo= github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4= -github.com/jfrog/gofrog v1.2.5 h1:jCgJC0iGQ8bU7jCC+YEFJTNINyngApIrhd8BjZAVRIE= -github.com/jfrog/gofrog v1.2.5/go.mod h1:o00tSRff6IapTgaCMuX1Cs9MH08Y1JqnsKgRtx91Gc4= +github.com/jfrog/gofrog v1.3.0 h1:o4zgsBZE4QyDbz2M7D4K6fXPTBJht+8lE87mS9bw7Gk= +github.com/jfrog/gofrog v1.3.0/go.mod h1:IFMc+V/yf7rA5WZ74CSbXe+Lgf0iApEQLxRZVzKRUR0= github.com/k0kubun/colorstring v0.0.0-20150214042306-9440f1994b88/go.mod h1:3w7q1U84EfirKl04SVQ/s7nPm1ZPhiXd34z40TNz36k= github.com/k0kubun/pp v2.3.0+incompatible/go.mod h1:GWse8YhT0p8pT4ir3ZgBbfZild3tgzSScAn6HmfYukg= github.com/kevinburke/ssh_config v1.2.0 h1:x584FjTGwHzMwvHx18PXxbBVzfnxogHaAReU4gf13a4= @@ -204,6 +203,7 @@ golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuX golang.org/x/term v0.0.0-20220722155259-a9ba230a4035/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.2.0/go.mod h1:TVmDHMZPmdnySmBfhjOoOdhjzdE1h4u1VwSiw2l1Nuc= golang.org/x/term v0.5.0 h1:n2a8QNdAb0sZNpU9R1ALUXBbY+w51fCQDN+7EdxNBsY= +golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= @@ -211,6 +211,7 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo= +golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= golang.org/x/time v0.1.0 h1:xYY+Bajn2a7VBmTM5GikTmnK8ZuX8YgnQCqZpbBNtmA= golang.org/x/time v0.1.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ= diff --git a/vcsclient/azurerepos.go b/vcsclient/azurerepos.go index 4a088d52..2515f27f 100644 --- a/vcsclient/azurerepos.go +++ b/vcsclient/azurerepos.go @@ -255,7 +255,7 @@ func (client *AzureReposClient) ListOpenPullRequests(ctx context.Context, _, rep return pullRequestsInfo, nil } -func (client *AzureReposClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { +func (client *AzureReposClient) GetPullRequestByID(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { azureReposGitClient, err := client.buildAzureReposClient(ctx) if err != nil { return diff --git a/vcsclient/azurerepos_test.go b/vcsclient/azurerepos_test.go index 2bc789b7..5b848eee 100644 --- a/vcsclient/azurerepos_test.go +++ b/vcsclient/azurerepos_test.go @@ -209,13 +209,13 @@ func TestAzureReposClient_GetPullRequest(t *testing.T) { ctx := context.Background() client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, jsonRes, fmt.Sprintf("getPullRequests/%d", pullRequestId), createAzureReposHandler) defer cleanUp() - pullRequestsInfo, err := client.GetPullRequest(ctx, "", repoName, pullRequestId) + pullRequestsInfo, err := client.GetPullRequestByID(ctx, "", repoName, pullRequestId) assert.NoError(t, err) assert.True(t, reflect.DeepEqual(pullRequestsInfo, PullRequestInfo{ID: 1, Source: BranchInfo{Name: sourceName, Repository: repoName}, Target: BranchInfo{Name: targetName, Repository: repoName}})) badClient, cleanUp := createBadAzureReposClient(t, []byte{}) defer cleanUp() - _, err = badClient.GetPullRequest(ctx, "", repo1, pullRequestId) + _, err = badClient.GetPullRequestByID(ctx, "", repo1, pullRequestId) assert.Error(t, err) } diff --git a/vcsclient/bitbucketcloud.go b/vcsclient/bitbucketcloud.go index dca2949c..c86dcc9b 100644 --- a/vcsclient/bitbucketcloud.go +++ b/vcsclient/bitbucketcloud.go @@ -321,7 +321,7 @@ func (client *BitbucketCloudClient) ListOpenPullRequests(ctx context.Context, ow return mapBitbucketCloudPullRequestToPullRequestInfo(&parsedPullRequests), nil } -func (client *BitbucketCloudClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { +func (client *BitbucketCloudClient) GetPullRequestByID(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { err = validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository}) if err != nil { return diff --git a/vcsclient/bitbucketcloud_test.go b/vcsclient/bitbucketcloud_test.go index ac44222a..88d4c6cb 100644 --- a/vcsclient/bitbucketcloud_test.go +++ b/vcsclient/bitbucketcloud_test.go @@ -185,7 +185,7 @@ func TestBitbucketCloudClient_GetPullRequest(t *testing.T) { client, cleanUp := createServerAndClient(t, vcsutils.BitbucketCloud, true, response, fmt.Sprintf("/repositories/%s/%s/pullrequests/%d", owner, repoName, pullRequestId), createBitbucketCloudHandler) defer cleanUp() - result, err := client.GetPullRequest(ctx, owner, repoName, pullRequestId) + result, err := client.GetPullRequestByID(ctx, owner, repoName, pullRequestId) assert.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ ID: int64(pullRequestId), @@ -197,7 +197,7 @@ func TestBitbucketCloudClient_GetPullRequest(t *testing.T) { badClient, badClientCleanUp := createServerAndClient(t, vcsutils.BitbucketCloud, true, "{", fmt.Sprintf("/repositories/%s/%s/pullrequests/%d", owner, repoName, pullRequestId), createBitbucketCloudHandler) defer badClientCleanUp() - _, err = badClient.GetPullRequest(ctx, owner, repoName, pullRequestId) + _, err = badClient.GetPullRequestByID(ctx, owner, repoName, pullRequestId) assert.Error(t, err) // Bad Fields @@ -205,7 +205,7 @@ func TestBitbucketCloudClient_GetPullRequest(t *testing.T) { badParseClient, badParseClientCleanUp := createServerAndClient(t, vcsutils.BitbucketCloud, true, response, fmt.Sprintf("/repositories/%s/%s/pullrequests/%d", owner, badRepoName, pullRequestId), createBitbucketCloudHandler) defer badParseClientCleanUp() - _, err = badParseClient.GetPullRequest(ctx, owner, badRepoName, pullRequestId) + _, err = badParseClient.GetPullRequestByID(ctx, owner, badRepoName, pullRequestId) assert.Error(t, err) } diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index 29323cd7..e55dbba8 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -361,14 +361,14 @@ func (client *BitbucketServerClient) ListOpenPullRequests(ctx context.Context, o } // GetPullRequestInfoById on bitbucket server -func (client *BitbucketServerClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { +func (client *BitbucketServerClient) GetPullRequestByID(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { client.logger.Debug("fetching pull request by ID in ", repository) bitbucketClient, err := client.buildBitbucketClient(ctx) if err != nil { return } apiResponse, err := bitbucketClient.GetPullRequest(owner, repository, pullRequestId) - if err != nil { + if err != nil || apiResponse.Status != string(rune(http.StatusOK)) { return } pullRequest, err := bitbucketv1.GetPullRequestResponse(apiResponse) diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index cfa60252..ff8efaff 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -227,7 +227,7 @@ func TestBitbucketServerClient_GetPullRequest(t *testing.T) { client, cleanUp := createServerAndClient(t, vcsutils.BitbucketServer, true, response, fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", owner, repo1, pullRequestId), createBitbucketServerHandler) defer cleanUp() - result, err := client.GetPullRequest(ctx, owner, repo1, pullRequestId) + result, err := client.GetPullRequestByID(ctx, owner, repo1, pullRequestId) require.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ ID: int64(pullRequestId), @@ -239,11 +239,11 @@ func TestBitbucketServerClient_GetPullRequest(t *testing.T) { badClient, badClientCleanUp := createServerAndClient(t, vcsutils.BitbucketServer, true, "{", fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", owner, repo1, pullRequestId), createBitbucketServerHandler) defer badClientCleanUp() - _, err2 := badClient.GetPullRequest(ctx, owner, repo1, pullRequestId) + _, err2 := badClient.GetPullRequestByID(ctx, owner, repo1, pullRequestId) require.Error(t, err2) // Bad Client - _, err = createBadBitbucketServerClient(t).GetPullRequest(ctx, owner, repo1, pullRequestId) + _, err = createBadBitbucketServerClient(t).GetPullRequestByID(ctx, owner, repo1, pullRequestId) assert.Error(t, err) } diff --git a/vcsclient/github.go b/vcsclient/github.go index d1dc5039..ce2090a2 100644 --- a/vcsclient/github.go +++ b/vcsclient/github.go @@ -289,14 +289,14 @@ func (client *GitHubClient) ListOpenPullRequests(ctx context.Context, owner, rep return mapGitHubPullRequestToPullRequestInfoList(pullRequests) } -func (client *GitHubClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) { +func (client *GitHubClient) GetPullRequestByID(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) { ghClient, err := client.buildGithubClient(ctx) if err != nil { return PullRequestInfo{}, err } client.logger.Debug(fetchingPullRequestById, repository) - pullRequest, _, err := ghClient.PullRequests.Get(ctx, owner, repository, pullRequestId) - if err != nil { + pullRequest, response, err := ghClient.PullRequests.Get(ctx, owner, repository, pullRequestId) + if err != nil || response.Status != string(rune(http.StatusOK)) { return PullRequestInfo{}, err } diff --git a/vcsclient/github_test.go b/vcsclient/github_test.go index 5ee08c2d..ce5c1e87 100644 --- a/vcsclient/github_test.go +++ b/vcsclient/github_test.go @@ -556,18 +556,18 @@ func TestGitHubClient_GetPullRequest(t *testing.T) { badLabelsClient, badLabelClientCleanUp := createServerAndClient(t, vcsutils.GitHub, false, badLabels, fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repoName, pullRequestId), createGitHubHandler) defer badLabelClientCleanUp() - _, err = badLabelsClient.GetPullRequest(ctx, owner, repoName, pullRequestId) + _, err = badLabelsClient.GetPullRequestByID(ctx, owner, repoName, pullRequestId) assert.Error(t, err) // Bad client - _, err = createBadGitHubClient(t).GetPullRequest(ctx, owner, repoName, pullRequestId) + _, err = createBadGitHubClient(t).GetPullRequestByID(ctx, owner, repoName, pullRequestId) assert.Error(t, err) // Bad Response badResponseClient, badResponseCleanUp := createServerAndClient(t, vcsutils.GitHub, false, "{", fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repoName, pullRequestId), createGitHubHandler) defer badResponseCleanUp() - _, err = badResponseClient.GetPullRequest(ctx, owner, repoName, pullRequestId) + _, err = badResponseClient.GetPullRequestByID(ctx, owner, repoName, pullRequestId) assert.Error(t, err) } diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index d09005b1..121f91cd 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -253,11 +253,11 @@ func (client *GitLabClient) ListOpenPullRequests(ctx context.Context, _, reposit } // GetPullRequestInfoById on GitLab -func (client *GitLabClient) GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { +func (client *GitLabClient) GetPullRequestByID(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { client.logger.Debug("fetching merge requests by ID in", repository) - mergeRequest, _, err := client.glClient.MergeRequests.GetMergeRequest(getProjectID(owner, repository), pullRequestId, nil) - if err != nil { - return + mergeRequest, response, err := client.glClient.MergeRequests.GetMergeRequest(getProjectID(owner, repository), pullRequestId, nil) + if err != nil || response.Status != string(rune(http.StatusOK)) { + return PullRequestInfo{}, err } pullRequestInfo = PullRequestInfo{ ID: int64(mergeRequest.ID), diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index f5820f37..b56cd2b1 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -232,7 +232,7 @@ func TestGitLabClient_GetPullRequest(t *testing.T) { client, cleanUp := createServerAndClient(t, vcsutils.GitLab, false, response, fmt.Sprintf("/api/v4/projects/%s/merge_requests/%d", url.PathEscape(owner+"/"+repoName), pullRequestId), createGitLabHandler) defer cleanUp() - result, err := client.GetPullRequest(ctx, owner, repoName, pullRequestId) + result, err := client.GetPullRequestByID(ctx, owner, repoName, pullRequestId) assert.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ ID: 1, @@ -244,7 +244,7 @@ func TestGitLabClient_GetPullRequest(t *testing.T) { badClient, badClientCleanUp := createServerAndClient(t, vcsutils.GitLab, false, "", fmt.Sprintf("/api/v4/projects/%s/merge_requests/%d", url.PathEscape(owner+"/"+repoName), pullRequestId), createGitLabHandler) defer badClientCleanUp() - _, err = badClient.GetPullRequest(ctx, owner, repoName, pullRequestId) + _, err = badClient.GetPullRequestByID(ctx, owner, repoName, pullRequestId) assert.Error(t, err) } diff --git a/vcsclient/logger.go b/vcsclient/logger.go index ec67143f..448f8e19 100644 --- a/vcsclient/logger.go +++ b/vcsclient/logger.go @@ -5,7 +5,7 @@ const ( successfulRepoExtraction = "Extracted repository successfully" creatingPullRequest = "Creating new pull request:" fetchingOpenPullRequests = "Fetching open pull requests in" - fetchingPullRequestById = "Fetching pull requests by id in " + fetchingPullRequestById = "Fetching pull requests by id in" uploadingCodeScanning = "Uploading code scanning for:" ) diff --git a/vcsclient/vcsclient.go b/vcsclient/vcsclient.go index 6fdd4cfa..115c07b7 100644 --- a/vcsclient/vcsclient.go +++ b/vcsclient/vcsclient.go @@ -165,11 +165,11 @@ type VcsClient interface { // repository - VCS repository name ListOpenPullRequests(ctx context.Context, owner, repository string) ([]PullRequestInfo, error) - // GetPullRequest Gets specific pull request info. + // GetPullRequestByID Gets pull request info by ID. // owner - User or organization // repository - VCS repository name // pullRequestId - ID of the pull request - GetPullRequest(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) + GetPullRequestByID(ctx context.Context, owner, repository string, pullRequestId int) (PullRequestInfo, error) // GetLatestCommit Gets the most recent commit of a branch // owner - User or organization From 32895a72a2e053bae2334637d1a3303ea0ec5a0d Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 28 Jun 2023 11:03:35 +0300 Subject: [PATCH 14/16] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index ebcbddf3..4fa92044 100644 --- a/README.md +++ b/README.md @@ -358,7 +358,7 @@ repository := "jfrog-cli" // Pull Request ID pullRequestId := 1 -openPullRequests, err := client.GetPullReequest(ctx, owner, repository, pullRequestId) +openPullRequests, err := client.GetPullRequestByID(ctx, owner, repository, pullRequestId) ``` ##### Add Pull Request Comment From 59f2e56faad54551286ab84e32c1655ea8823528 Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 28 Jun 2023 11:23:46 +0300 Subject: [PATCH 15/16] Fix status check --- vcsclient/bitbucketcloud.go | 2 +- vcsclient/bitbucketserver.go | 2 +- vcsclient/github.go | 2 +- vcsclient/github_test.go | 4 ++-- vcsclient/gitlab.go | 2 +- vcsclient/gitlab_test.go | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/vcsclient/bitbucketcloud.go b/vcsclient/bitbucketcloud.go index c86dcc9b..e1c0d52f 100644 --- a/vcsclient/bitbucketcloud.go +++ b/vcsclient/bitbucketcloud.go @@ -6,6 +6,7 @@ import ( "encoding/json" "fmt" "github.com/jfrog/gofrog/datastructures" + "github.com/ktrysmt/go-bitbucket" "net/http" "net/url" "sort" @@ -16,7 +17,6 @@ import ( "github.com/mitchellh/mapstructure" "github.com/jfrog/froggit-go/vcsutils" - "github.com/ktrysmt/go-bitbucket" ) // BitbucketCloudClient API version 2.0 diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index e55dbba8..71947140 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -368,7 +368,7 @@ func (client *BitbucketServerClient) GetPullRequestByID(ctx context.Context, own return } apiResponse, err := bitbucketClient.GetPullRequest(owner, repository, pullRequestId) - if err != nil || apiResponse.Status != string(rune(http.StatusOK)) { + if err != nil || apiResponse.StatusCode != http.StatusOK { return } pullRequest, err := bitbucketv1.GetPullRequestResponse(apiResponse) diff --git a/vcsclient/github.go b/vcsclient/github.go index ce2090a2..2e8de2d2 100644 --- a/vcsclient/github.go +++ b/vcsclient/github.go @@ -296,7 +296,7 @@ func (client *GitHubClient) GetPullRequestByID(ctx context.Context, owner, repos } client.logger.Debug(fetchingPullRequestById, repository) pullRequest, response, err := ghClient.PullRequests.Get(ctx, owner, repository, pullRequestId) - if err != nil || response.Status != string(rune(http.StatusOK)) { + if err != nil || response.StatusCode != http.StatusOK { return PullRequestInfo{}, err } diff --git a/vcsclient/github_test.go b/vcsclient/github_test.go index ce5c1e87..6dd7283f 100644 --- a/vcsclient/github_test.go +++ b/vcsclient/github_test.go @@ -531,7 +531,7 @@ func TestGitHubClient_ListOpenPullRequests(t *testing.T) { assert.Error(t, err) } -func TestGitHubClient_GetPullRequest(t *testing.T) { +func TestGitHubClient_GetPullRequestByID(t *testing.T) { ctx := context.Background() pullRequestId := 1 repoName := "Hello-World" @@ -542,7 +542,7 @@ func TestGitHubClient_GetPullRequest(t *testing.T) { client, cleanUp := createServerAndClient(t, vcsutils.GitHub, false, response, fmt.Sprintf("/repos/%s/%s/pulls/%d", owner, repoName, pullRequestId), createGitHubHandler) defer cleanUp() - result, err := client.GetPullRequest(ctx, owner, repoName, pullRequestId) + result, err := client.GetPullRequestByID(ctx, owner, repoName, pullRequestId) assert.NoError(t, err) assert.True(t, reflect.DeepEqual(PullRequestInfo{ ID: int64(pullRequestId), diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index 121f91cd..efe0b4e0 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -256,7 +256,7 @@ func (client *GitLabClient) ListOpenPullRequests(ctx context.Context, _, reposit func (client *GitLabClient) GetPullRequestByID(ctx context.Context, owner, repository string, pullRequestId int) (pullRequestInfo PullRequestInfo, err error) { client.logger.Debug("fetching merge requests by ID in", repository) mergeRequest, response, err := client.glClient.MergeRequests.GetMergeRequest(getProjectID(owner, repository), pullRequestId, nil) - if err != nil || response.Status != string(rune(http.StatusOK)) { + if err != nil || response.StatusCode != http.StatusOK { return PullRequestInfo{}, err } pullRequestInfo = PullRequestInfo{ diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index b56cd2b1..d549c6cc 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -221,7 +221,7 @@ func TestGitLabClient_ListOpenPullRequests(t *testing.T) { }, result[0])) } -func TestGitLabClient_GetPullRequest(t *testing.T) { +func TestGitLabClient_GetPullRequestByID(t *testing.T) { ctx := context.Background() repoName := "repo" pullRequestId := 1 From 4f838bbc989d53c816560624ccf6e5ae0ea8328e Mon Sep 17 00:00:00 2001 From: delarea Date: Wed, 28 Jun 2023 16:57:46 +0300 Subject: [PATCH 16/16] update docs order --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 84b5f507..cce4ddc6 100644 --- a/README.md +++ b/README.md @@ -41,8 +41,8 @@ Currently supported providers are: [GitHub](#github), [Bitbucket Server](#bitbuc - [Get Commit Status](#get-commit-status) - [Create Pull Request](#create-pull-request) - [Update Pull Request](#update-pull-request) - - [List Open Pull Requests](#list-open-pull-requests) - [Get Pull Request By ID](#get-pull-request) + - [List Open Pull Requests](#list-open-pull-requests) - [Add Pull Request Comment](#add-pull-request-comment) - [List Pull Request Comments](#list-pull-request-comments) - [Get Latest Commit](#get-latest-commit)