From 0540a10853d01a77da3ae86821eb4846bb76b635 Mon Sep 17 00:00:00 2001 From: gail Date: Tue, 4 Jun 2024 15:00:28 +0300 Subject: [PATCH 01/14] Add new API for getting commits with query options. --- go.mod | 4 ++- vcsclient/azurerepos.go | 31 ++++++++++++++---- vcsclient/azurerepos_test.go | 52 +++++++++++++++++++++++++++++++ vcsclient/bitbucketcloud.go | 4 +++ vcsclient/bitbucketserver.go | 23 ++++++++++++++ vcsclient/bitbucketserver_test.go | 49 +++++++++++++++++++++++++++++ vcsclient/github.go | 39 +++++++++++++++++++---- vcsclient/github_test.go | 44 ++++++++++++++++++++++++++ vcsclient/gitlab.go | 27 +++++++++++++++- vcsclient/gitlab_test.go | 43 +++++++++++++++++++++++++ vcsclient/vcsclient.go | 21 +++++++++++++ 11 files changed, 323 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index 2ee143b4..1a1d41aa 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,8 @@ module github.com/jfrog/froggit-go -go 1.20 +go 1.21 + +toolchain go1.21.10 require ( github.com/gfleury/go-bitbucket-v1 v0.0.0-20230825095122-9bc1711434ab diff --git a/vcsclient/azurerepos.go b/vcsclient/azurerepos.go index d9e36de6..c6b44607 100644 --- a/vcsclient/azurerepos.go +++ b/vcsclient/azurerepos.go @@ -401,20 +401,39 @@ func (client *AzureReposClient) GetLatestCommit(ctx context.Context, _, reposito // GetCommits on Azure Repos func (client *AzureReposClient) GetCommits(ctx context.Context, _, repository, branch string) ([]CommitInfo, error) { + commitArgs := git.GetCommitsArgs{ + RepositoryId: &repository, + Project: &client.vcsInfo.Project, + SearchCriteria: &git.GitQueryCommitsCriteria{ItemVersion: &git.GitVersionDescriptor{Version: &branch, VersionType: &git.GitVersionTypeValues.Branch}}, + } + return client.getCommitsWithQueryOptions(ctx, commitArgs) +} + +func (client *AzureReposClient) GetCommitsWithQueryOptions(ctx context.Context, _, repository string, listOptions GitCommitsQueryOptions) ([]CommitInfo, error) { + return client.getCommitsWithQueryOptions(ctx, convertToGetCommitsArgs(repository, client.vcsInfo.Project, listOptions)) +} + +// TODO add pagination support +func convertToGetCommitsArgs(repository, project string, options GitCommitsQueryOptions) git.GetCommitsArgs { + since := options.Since.Format(time.RFC3339) + return git.GetCommitsArgs{ + RepositoryId: &repository, + Project: &project, + SearchCriteria: &git.GitQueryCommitsCriteria{FromDate: &since}, + } +} + +func (client *AzureReposClient) getCommitsWithQueryOptions(ctx context.Context, commitArgs git.GetCommitsArgs) ([]CommitInfo, error) { azureReposGitClient, err := client.buildAzureReposClient(ctx) if err != nil { return nil, err } - commits, err := azureReposGitClient.GetCommits(ctx, git.GetCommitsArgs{ - RepositoryId: &repository, - Project: &client.vcsInfo.Project, - SearchCriteria: &git.GitQueryCommitsCriteria{ItemVersion: &git.GitVersionDescriptor{Version: &branch, VersionType: &git.GitVersionTypeValues.Branch}}, - }) + commits, err := azureReposGitClient.GetCommits(ctx, commitArgs) if err != nil { return nil, err } if commits == nil { - return nil, fmt.Errorf("could not retrieve commits for <%s/%s>", repository, branch) + return nil, fmt.Errorf("could not retrieve commits for <%s/%s>", *commitArgs.RepositoryId, *commitArgs.SearchCriteria.ItemVersion.Version) } var commitsInfo []CommitInfo diff --git a/vcsclient/azurerepos_test.go b/vcsclient/azurerepos_test.go index 4e2df8c8..45c36a16 100644 --- a/vcsclient/azurerepos_test.go +++ b/vcsclient/azurerepos_test.go @@ -500,6 +500,58 @@ func TestAzureRepos_TestGetCommits(t *testing.T) { assert.Error(t, err) } +func TestAzureRepos_GetCommitsWithQueryOptions(t *testing.T) { + ctx := context.Background() + response, err := os.ReadFile(filepath.Join("testdata", "azurerepos", "commits.json")) + assert.NoError(t, err) + + client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, response, "getCommits", createAzureReposHandler) + defer cleanUp() + + options := GitCommitsQueryOptions{ + Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + ListOptions: ListOptions{ + Page: 1, + PerPage: 30, + }, + } + + commits, err := client.GetCommitsWithQueryOptions(ctx, "", repo1, options) + assert.Equal(t, CommitInfo{ + Hash: "86d6919952702f9ab03bc95b45687f145a663de0", + AuthorName: "Test User", + CommitterName: "Test User", + Url: "https://dev.azure.com/testuser/0b8072c4-ad86-4edb-a8f2-06dbc07e3e2d/_apis/git/repositories/94c1dba8-d9d9-4600-94b4-1a51acb43220/commits/86d6919952702f9ab03bc95b45687f145a663de0", + Timestamp: 1667812601, + Message: "Updated package.json", + AuthorEmail: "testuser@jfrog.com", + }, commits[0]) + assert.Equal(t, CommitInfo{ + Hash: "4aa8367809020c4e97af29e2b57f7528d5d27702", + AuthorName: "Test User", + CommitterName: "Test User", + Url: "https://dev.azure.com/testuser/0b8072c4-ad86-4edb-a8f2-06dbc07e3e2d/_apis/git/repositories/94c1dba8-d9d9-4600-94b4-1a51acb43220/commits/4aa8367809020c4e97af29e2b57f7528d5d27702", + Timestamp: 1667201343, + Message: "Set up CI with Azure Pipelines", + AuthorEmail: "testuser@jfrog.com", + }, commits[1]) + assert.Equal(t, CommitInfo{ + Hash: "3779104c35804e15b6fdf4fee303e717cd6c1352", + AuthorName: "Test User", + CommitterName: "Test User", + Url: "https://dev.azure.com/testuser/0b8072c4-ad86-4edb-a8f2-06dbc07e3e2d/_apis/git/repositories/94c1dba8-d9d9-4600-94b4-1a51acb43220/commits/3779104c35804e15b6fdf4fee303e717cd6c1352", + Timestamp: 1667201200, + Message: "first commit", + AuthorEmail: "testuser@jfrog.com", + }, commits[2]) + assert.NoError(t, err) + + badClient, cleanUp := createBadAzureReposClient(t, []byte{}) + defer cleanUp() + _, err = badClient.GetCommitsWithQueryOptions(ctx, "", repo1, options) + assert.Error(t, err) +} + func TestAzureReposClient_AddSshKeyToRepository(t *testing.T) { ctx := context.Background() client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, "", "getLatestCommit", createAzureReposHandler) diff --git a/vcsclient/bitbucketcloud.go b/vcsclient/bitbucketcloud.go index a76efd1e..f6068194 100644 --- a/vcsclient/bitbucketcloud.go +++ b/vcsclient/bitbucketcloud.go @@ -497,6 +497,10 @@ func (client *BitbucketCloudClient) GetCommits(_ context.Context, _, _, _ string return nil, errBitbucketGetCommitsNotSupported } +func (client *BitbucketCloudClient) GetCommitsWithQueryOptions(ctx context.Context, owner, repository string, listOptions GitCommitsQueryOptions) ([]CommitInfo, error) { + return nil, nil +} + // GetRepositoryInfo on Bitbucket cloud func (client *BitbucketCloudClient) GetRepositoryInfo(ctx context.Context, owner, repository string) (RepositoryInfo, error) { if err := validateParametersNotBlank(map[string]string{"owner": owner, "repository": repository}); err != nil { diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index 34239fd9..e206cc95 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -553,6 +553,22 @@ func (client *BitbucketServerClient) GetCommits(ctx context.Context, owner, repo "limit": vcsutils.NumberOfCommitsToFetch, "until": branch, } + return client.getCommitsWithQueryOptions(ctx, owner, repository, options) +} + +func (client *BitbucketServerClient) GetCommitsWithQueryOptions(ctx context.Context, owner, repository string, listOptions GitCommitsQueryOptions) ([]CommitInfo, error) { + err := validateParametersNotBlank(map[string]string{ + "owner": owner, + "repository": repository, + }) + if err != nil { + return nil, err + } + + return client.getCommitsWithQueryOptions(ctx, owner, repository, convertToBitbucketOptionsMap(listOptions)) +} + +func (client *BitbucketServerClient) getCommitsWithQueryOptions(ctx context.Context, owner, repository string, options map[string]interface{}) ([]CommitInfo, error) { bitbucketClient := client.buildBitbucketClient(ctx) apiResponse, err := bitbucketClient.GetCommits(owner, repository, options) @@ -570,6 +586,13 @@ func (client *BitbucketServerClient) GetCommits(ctx context.Context, owner, repo } return commitsInfo, nil } +func convertToBitbucketOptionsMap(listOptions GitCommitsQueryOptions) map[string]interface{} { + return map[string]interface{}{ + "limit": listOptions.PerPage, + "since": listOptions.Since.Format(time.RFC3339), + "start": listOptions.Page, + } +} // GetRepositoryInfo on Bitbucket server func (client *BitbucketServerClient) GetRepositoryInfo(ctx context.Context, owner, repository string) (RepositoryInfo, error) { diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index 2bd23999..3db5559c 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -391,6 +391,55 @@ func TestBitbucketServer_GetCommits(t *testing.T) { assert.Error(t, err) } +func TestBitbucketServer_GetCommitsWithQueryOptions(t *testing.T) { + ctx := context.Background() + response, err := os.ReadFile(filepath.Join("testdata", "bitbucketserver", "commit_list_response.json")) + assert.NoError(t, err) + + client, serverUrl, cleanUp := createServerWithUrlAndClientReturningStatus(t, vcsutils.BitbucketServer, false, + response, + fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/commits?limit=30&limit=30&since=2021-01-01T00%%3A00%%3A00Z&start=1", owner, repo1), + http.StatusOK, createBitbucketServerHandler) + defer cleanUp() + + options := GitCommitsQueryOptions{ + Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + ListOptions: ListOptions{ + Page: 1, + PerPage: 30, + }, + } + + result, err := client.GetCommitsWithQueryOptions(ctx, owner, repo1, options) + + assert.NoError(t, err) + expectedUrl := fmt.Sprintf("%s/projects/jfrog/repos/repo-1"+ + "/commits/def0123abcdef4567abcdef8987abcdef6543abc", serverUrl) + assert.Equal(t, CommitInfo{ + Hash: "def0123abcdef4567abcdef8987abcdef6543abc", + AuthorName: "charlie", + CommitterName: "mark", + Url: expectedUrl, + Timestamp: 1548720847610, + Message: "More work on feature 1", + ParentHashes: []string{"abcdef0123abcdef4567abcdef8987abcdef6543", "qwerty0123abcdef4567abcdef8987abcdef6543"}, + AuthorEmail: "charlie@example.com", + }, result[0]) + assert.Equal(t, CommitInfo{ + Hash: "def0123abcdef4567abcdef8987abcdef6543abc", + AuthorName: "marly", + CommitterName: "marly", + Url: expectedUrl, + Timestamp: 1548720847610, + Message: "More work on feature 2", + ParentHashes: []string{"abcdef0123abcdef4567abcdef8987abcdef6543", "qwerty0123abcdef4567abcdef8987abcdef6543"}, + AuthorEmail: "marly@example.com", + }, result[1]) + + _, err = createBadBitbucketServerClient(t).GetCommits(ctx, owner, repo1, "master") + assert.Error(t, err) +} + func TestBitbucketServer_GetLatestCommitNotFound(t *testing.T) { ctx := context.Background() response := []byte(`{ diff --git a/vcsclient/github.go b/vcsclient/github.go index f02cf3dd..c4507c83 100644 --- a/vcsclient/github.go +++ b/vcsclient/github.go @@ -690,21 +690,48 @@ func (client *GitHubClient) GetCommits(ctx context.Context, owner, repository, b var commitsInfo []CommitInfo err = client.runWithRateLimitRetries(func() (*github.Response, error) { var ghResponse *github.Response - commitsInfo, ghResponse, err = client.executeGetCommits(ctx, owner, repository, branch) + listOptions := &github.CommitsListOptions{ + SHA: branch, + ListOptions: github.ListOptions{ + Page: 1, + PerPage: vcsutils.NumberOfCommitsToFetch, + }, + } + commitsInfo, ghResponse, err = client.executeGetCommits(ctx, owner, repository, listOptions) return ghResponse, err }) return commitsInfo, err } -func (client *GitHubClient) executeGetCommits(ctx context.Context, owner, repository, branch string) ([]CommitInfo, *github.Response, error) { - listOptions := &github.CommitsListOptions{ - SHA: branch, +// GetCommitsWithQueryOptions on GitHub +func (client *GitHubClient) GetCommitsWithQueryOptions(ctx context.Context, owner, repository string, listOptions GitCommitsQueryOptions) ([]CommitInfo, error) { + err := validateParametersNotBlank(map[string]string{ + "owner": owner, + "repository": repository, + }) + if err != nil { + return nil, err + } + var commitsInfo []CommitInfo + err = client.runWithRateLimitRetries(func() (*github.Response, error) { + var ghResponse *github.Response + commitsInfo, ghResponse, err = client.executeGetCommits(ctx, owner, repository, convertToGitHubCommitsListOptions(listOptions)) + return ghResponse, err + }) + return commitsInfo, err +} + +func convertToGitHubCommitsListOptions(listOptions GitCommitsQueryOptions) *github.CommitsListOptions { + return &github.CommitsListOptions{ + Since: listOptions.Since, ListOptions: github.ListOptions{ - Page: 1, - PerPage: vcsutils.NumberOfCommitsToFetch, + Page: listOptions.Page, + PerPage: listOptions.PerPage, }, } +} +func (client *GitHubClient) executeGetCommits(ctx context.Context, owner, repository string, listOptions *github.CommitsListOptions) ([]CommitInfo, *github.Response, error) { commits, ghResponse, err := client.ghClient.Repositories.ListCommits(ctx, owner, repository, listOptions) if err != nil { return nil, ghResponse, err diff --git a/vcsclient/github_test.go b/vcsclient/github_test.go index 6112ab12..bad6c8eb 100644 --- a/vcsclient/github_test.go +++ b/vcsclient/github_test.go @@ -393,6 +393,50 @@ func TestGitHubClient_GetCommits(t *testing.T) { assert.Error(t, err) } +func TestGitHubClient_GetCommitsWithQueryOptions(t *testing.T) { + ctx := context.Background() + response, err := os.ReadFile(filepath.Join("testdata", "github", "commit_list_response.json")) + assert.NoError(t, err) + + client, cleanUp := createServerAndClient(t, vcsutils.GitHub, false, response, + fmt.Sprintf("/repos/%s/%s/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z", owner, repo1), createGitHubHandler) + defer cleanUp() + + options := GitCommitsQueryOptions{ + Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + ListOptions: ListOptions{ + Page: 1, + PerPage: 30, + }, + } + result, err := client.GetCommitsWithQueryOptions(ctx, owner, repo1, options) + + assert.NoError(t, err) + assert.Equal(t, CommitInfo{ + Hash: "6dcb09b5b57875f334f61aebed695e2e4193db5e", + AuthorName: "Monalisa Octocat", + CommitterName: "Joconde Octocat", + Url: "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + Timestamp: 1302796850, + Message: "Fix all the bugs", + ParentHashes: []string{"6dcb09b5b57875f334f61aebed695e2e4193db5e"}, + AuthorEmail: "support@github.com", + }, result[0]) + assert.Equal(t, CommitInfo{ + Hash: "6dcb09b5b57875f334f61aebed695e2e4193db5e", + AuthorName: "Leonardo De Vinci", + CommitterName: "Leonardo De Vinci", + Url: "https://api.github.com/repos/octocat/Hello-World/commits/6dcb09b5b57875f334f61aebed695e2e4193db5e", + Timestamp: 1302796850, + Message: "Fix all the bugs", + ParentHashes: []string{"6dcb09b5b57875f334f61aebed695e2e4193db5e"}, + AuthorEmail: "vinci@github.com", + }, result[1]) + + _, err = createBadGitHubClient(t).GetCommits(ctx, owner, repo1, "master") + assert.Error(t, err) +} + func TestGitHubClient_GetLatestCommitNotFound(t *testing.T) { ctx := context.Background() response := []byte(`{ diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index 48b5a767..3f740f6a 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -532,8 +532,33 @@ func (client *GitLabClient) GetCommits(ctx context.Context, owner, repository, b PerPage: vcsutils.NumberOfCommitsToFetch, }, } + return client.getCommitsWithQueryOptions(ctx, owner, repository, listOptions) +} + +func (client *GitLabClient) GetCommitsWithQueryOptions(ctx context.Context, owner, repository string, listOptions GitCommitsQueryOptions) ([]CommitInfo, error) { + err := validateParametersNotBlank(map[string]string{ + "owner": owner, + "repository": repository, + }) + if err != nil { + return nil, err + } + + return client.getCommitsWithQueryOptions(ctx, owner, repository, convertToListCommitsOptions(listOptions)) +} + +func convertToListCommitsOptions(options GitCommitsQueryOptions) *gitlab.ListCommitsOptions { + return &gitlab.ListCommitsOptions{ + ListOptions: gitlab.ListOptions{ + Page: options.Page, + PerPage: options.PerPage, + }, + Since: &options.Since, + } +} - commits, _, err := client.glClient.Commits.ListCommits(getProjectID(owner, repository), listOptions, gitlab.WithContext(ctx)) +func (client *GitLabClient) getCommitsWithQueryOptions(ctx context.Context, owner, repository string, options *gitlab.ListCommitsOptions) ([]CommitInfo, error) { + commits, _, err := client.glClient.Commits.ListCommits(getProjectID(owner, repository), options, gitlab.WithContext(ctx)) if err != nil { return nil, err } diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index 16764a8e..9b05f1d8 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -394,6 +394,49 @@ func TestGitLabClient_GetCommits(t *testing.T) { }, result[1]) } +func TestGitLabClient_GetCommitsWithQueryOptions(t *testing.T) { + ctx := context.Background() + response, err := os.ReadFile(filepath.Join("testdata", "gitlab", "commit_list_response.json")) + assert.NoError(t, err) + + client, cleanUp := createServerAndClient(t, vcsutils.GitLab, false, response, + fmt.Sprintf("/api/v4/projects/%s/repository/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z", + url.PathEscape(owner+"/"+repo1)), createGitLabHandler) + defer cleanUp() + + options := GitCommitsQueryOptions{ + Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + ListOptions: ListOptions{ + Page: 1, + PerPage: 30, + }, + } + + result, err := client.GetCommitsWithQueryOptions(ctx, owner, repo1, options) + + assert.NoError(t, err) + assert.Equal(t, CommitInfo{ + Hash: "ed899a2f4b50b4370feeea94676502b42383c746", + AuthorName: "Example User", + CommitterName: "Administrator", + Url: "https://gitlab.example.com/thedude/gitlab-foss/-/commit/ed899a2f4b50b4370feeea94676502b42383c746", + Timestamp: 1348131022, + Message: "Replace sanitize with escape once", + ParentHashes: []string{"6104942438c14ec7bd21c6cd5bd995272b3faff6"}, + AuthorEmail: "user@example.com", + }, result[0]) + assert.Equal(t, CommitInfo{ + Hash: "6104942438c14ec7bd21c6cd5bd995272b3faff6", + AuthorName: "randx", + CommitterName: "ExampleName", + Url: "https://gitlab.example.com/thedude/gitlab-foss/-/commit/ed899a2f4b50b4370feeea94676502b42383c746", + Timestamp: 1348131022, + Message: "Sanitize for network graph", + ParentHashes: []string{"ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"}, + AuthorEmail: "user@example.com", + }, result[1]) +} + func TestGitLabClient_GetLatestCommitNotFound(t *testing.T) { ctx := context.Background() response := []byte(`{ diff --git a/vcsclient/vcsclient.go b/vcsclient/vcsclient.go index 4b6b9a65..7830d50b 100644 --- a/vcsclient/vcsclient.go +++ b/vcsclient/vcsclient.go @@ -225,6 +225,12 @@ type VcsClient interface { // branch - The name of the branch GetCommits(ctx context.Context, owner, repository, branch string) ([]CommitInfo, error) + // GetCommitsWithQueryOptions Gets repository commits considering GitCommitsQueryOptions provided by the user. + // owner - User or organization + // repository - VCS repository name + // listOptions - Optional parameters for the 'ListCommits' method + GetCommitsWithQueryOptions(ctx context.Context, owner, repository string, options GitCommitsQueryOptions) ([]CommitInfo, error) + // AddSshKeyToRepository Adds a public ssh key to a repository // owner - User or organization // repository - VCS repository name @@ -399,6 +405,21 @@ type LabelInfo struct { Color string } +// GitCommitsQueryOptions specifies the optional parameters fot the commit list. +type GitCommitsQueryOptions struct { + // Since when should Commits be included in the response. + Since time.Time + ListOptions +} + +// ListOptions specifies the optional parameters to various List methods that support offset pagination. +type ListOptions struct { + // For paginated result sets, page of results to retrieve. + Page int + // For paginated result sets, the number of results to include per page. + PerPage int +} + func validateParametersNotBlank(paramNameValueMap map[string]string) error { var errorMessages []string for k, v := range paramNameValueMap { From 3299831446873ff8cd64b1a17ef46306d767b02b Mon Sep 17 00:00:00 2001 From: gail Date: Tue, 18 Jun 2024 17:52:13 +0300 Subject: [PATCH 02/14] Add Until to GitCommitsQueryOptions. --- vcsclient/azurerepos.go | 4 +++- vcsclient/bitbucketserver.go | 2 ++ vcsclient/github.go | 1 + vcsclient/gitlab.go | 1 + vcsclient/vcsclient.go | 2 ++ 5 files changed, 9 insertions(+), 1 deletion(-) diff --git a/vcsclient/azurerepos.go b/vcsclient/azurerepos.go index c6b44607..60168127 100644 --- a/vcsclient/azurerepos.go +++ b/vcsclient/azurerepos.go @@ -415,11 +415,13 @@ func (client *AzureReposClient) GetCommitsWithQueryOptions(ctx context.Context, // TODO add pagination support func convertToGetCommitsArgs(repository, project string, options GitCommitsQueryOptions) git.GetCommitsArgs { + // TODO formting necessary? since := options.Since.Format(time.RFC3339) + until := options.Until.Format(time.RFC3339) return git.GetCommitsArgs{ RepositoryId: &repository, Project: &project, - SearchCriteria: &git.GitQueryCommitsCriteria{FromDate: &since}, + SearchCriteria: &git.GitQueryCommitsCriteria{FromDate: &since, ToDate: &until}, } } diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index e206cc95..fe7e1c4f 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -590,6 +590,8 @@ func convertToBitbucketOptionsMap(listOptions GitCommitsQueryOptions) map[string return map[string]interface{}{ "limit": listOptions.PerPage, "since": listOptions.Since.Format(time.RFC3339), + // TODO is until? + "until": listOptions.Until.Format(time.RFC3339), "start": listOptions.Page, } } diff --git a/vcsclient/github.go b/vcsclient/github.go index c4507c83..ce109324 100644 --- a/vcsclient/github.go +++ b/vcsclient/github.go @@ -724,6 +724,7 @@ func (client *GitHubClient) GetCommitsWithQueryOptions(ctx context.Context, owne func convertToGitHubCommitsListOptions(listOptions GitCommitsQueryOptions) *github.CommitsListOptions { return &github.CommitsListOptions{ Since: listOptions.Since, + Until: listOptions.Until, ListOptions: github.ListOptions{ Page: listOptions.Page, PerPage: listOptions.PerPage, diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index 3f740f6a..ff771c8d 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -554,6 +554,7 @@ func convertToListCommitsOptions(options GitCommitsQueryOptions) *gitlab.ListCom PerPage: options.PerPage, }, Since: &options.Since, + Until: &options.Until, } } diff --git a/vcsclient/vcsclient.go b/vcsclient/vcsclient.go index 7830d50b..6ddba6f3 100644 --- a/vcsclient/vcsclient.go +++ b/vcsclient/vcsclient.go @@ -409,6 +409,8 @@ type LabelInfo struct { type GitCommitsQueryOptions struct { // Since when should Commits be included in the response. Since time.Time + // Until when should Commits be included in the response. + Until time.Time ListOptions } From 9d2d2e5d62eb956addcc7bfe39a457628ae7973c Mon Sep 17 00:00:00 2001 From: gail Date: Sat, 22 Jun 2024 19:58:24 +0300 Subject: [PATCH 03/14] remove azure. --- go.sum | 10 +++++++ vcsclient/azurerepos.go | 37 +++++++------------------ vcsclient/azurerepos_test.go | 52 ------------------------------------ vcsclient/bitbucketcloud.go | 2 +- vcsclient/bitbucketcommon.go | 4 +++ vcsclient/bitbucketserver.go | 2 -- 6 files changed, 25 insertions(+), 82 deletions(-) diff --git a/go.sum b/go.sum index ae77b3d6..618aff70 100644 --- a/go.sum +++ b/go.sum @@ -614,11 +614,13 @@ github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3 github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= +github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apache/arrow/go/v10 v10.0.1/go.mod h1:YvhnlEePVnBS4+0z3fhPfUy7W1Ikj0Ih0vcRo/gZ1M0= github.com/apache/arrow/go/v11 v11.0.0/go.mod h1:Eg5OsL5H+e299f7u5ssuXsuHQVEGC4xei5aX110hRiI= github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU= 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/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= @@ -657,6 +659,7 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= +github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -680,6 +683,7 @@ github.com/gfleury/go-bitbucket-v1 v0.0.0-20230825095122-9bc1711434ab h1:+7KwW/y github.com/gfleury/go-bitbucket-v1 v0.0.0-20230825095122-9bc1711434ab/go.mod h1:IqOZzks2wlWCIai0esXnZPdPwxF2yOz0HcCYw5I4pCg= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= 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-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g= github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks= github.com/go-fonts/liberation v0.1.1/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY= @@ -690,6 +694,7 @@ github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmS github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= +github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -758,6 +763,7 @@ github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8 github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github/v56 v56.0.0 h1:TysL7dMa/r7wsQi44BjqlwaHvwlFlqkK8CtBWCX3gb4= github.com/google/go-github/v56 v56.0.0/go.mod h1:D8cdcX98YWJvi7TLo7zM4/h8ZTx6u6fwGEkCdisopo0= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= @@ -850,6 +856,7 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -871,6 +878,7 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= +github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY= github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= github.com/phpdave11/gofpdi v1.0.13/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= @@ -894,6 +902,7 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= @@ -1133,6 +1142,7 @@ golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= +golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= diff --git a/vcsclient/azurerepos.go b/vcsclient/azurerepos.go index 60168127..593b4688 100644 --- a/vcsclient/azurerepos.go +++ b/vcsclient/azurerepos.go @@ -401,41 +401,20 @@ func (client *AzureReposClient) GetLatestCommit(ctx context.Context, _, reposito // GetCommits on Azure Repos func (client *AzureReposClient) GetCommits(ctx context.Context, _, repository, branch string) ([]CommitInfo, error) { - commitArgs := git.GetCommitsArgs{ - RepositoryId: &repository, - Project: &client.vcsInfo.Project, - SearchCriteria: &git.GitQueryCommitsCriteria{ItemVersion: &git.GitVersionDescriptor{Version: &branch, VersionType: &git.GitVersionTypeValues.Branch}}, - } - return client.getCommitsWithQueryOptions(ctx, commitArgs) -} - -func (client *AzureReposClient) GetCommitsWithQueryOptions(ctx context.Context, _, repository string, listOptions GitCommitsQueryOptions) ([]CommitInfo, error) { - return client.getCommitsWithQueryOptions(ctx, convertToGetCommitsArgs(repository, client.vcsInfo.Project, listOptions)) -} - -// TODO add pagination support -func convertToGetCommitsArgs(repository, project string, options GitCommitsQueryOptions) git.GetCommitsArgs { - // TODO formting necessary? - since := options.Since.Format(time.RFC3339) - until := options.Until.Format(time.RFC3339) - return git.GetCommitsArgs{ - RepositoryId: &repository, - Project: &project, - SearchCriteria: &git.GitQueryCommitsCriteria{FromDate: &since, ToDate: &until}, - } -} - -func (client *AzureReposClient) getCommitsWithQueryOptions(ctx context.Context, commitArgs git.GetCommitsArgs) ([]CommitInfo, error) { azureReposGitClient, err := client.buildAzureReposClient(ctx) if err != nil { return nil, err } - commits, err := azureReposGitClient.GetCommits(ctx, commitArgs) + commits, err := azureReposGitClient.GetCommits(ctx, git.GetCommitsArgs{ + RepositoryId: &repository, + Project: &client.vcsInfo.Project, + SearchCriteria: &git.GitQueryCommitsCriteria{ItemVersion: &git.GitVersionDescriptor{Version: &branch, VersionType: &git.GitVersionTypeValues.Branch}}, + }) if err != nil { return nil, err } if commits == nil { - return nil, fmt.Errorf("could not retrieve commits for <%s/%s>", *commitArgs.RepositoryId, *commitArgs.SearchCriteria.ItemVersion.Version) + return nil, fmt.Errorf("could not retrieve commits for <%s/%s>", repository, branch) } var commitsInfo []CommitInfo @@ -446,6 +425,10 @@ func (client *AzureReposClient) getCommitsWithQueryOptions(ctx context.Context, return commitsInfo, nil } +func (client *AzureReposClient) GetCommitsWithQueryOptions(ctx context.Context, _, repository string, listOptions GitCommitsQueryOptions) ([]CommitInfo, error) { + return nil, errAzureGetCommitsWithOptionsNotSupported +} + func mapAzureReposCommitsToCommitInfo(commit git.GitCommitRef) CommitInfo { var authorName, authorEmail string if commit.Author != nil { diff --git a/vcsclient/azurerepos_test.go b/vcsclient/azurerepos_test.go index 45c36a16..4e2df8c8 100644 --- a/vcsclient/azurerepos_test.go +++ b/vcsclient/azurerepos_test.go @@ -500,58 +500,6 @@ func TestAzureRepos_TestGetCommits(t *testing.T) { assert.Error(t, err) } -func TestAzureRepos_GetCommitsWithQueryOptions(t *testing.T) { - ctx := context.Background() - response, err := os.ReadFile(filepath.Join("testdata", "azurerepos", "commits.json")) - assert.NoError(t, err) - - client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, response, "getCommits", createAzureReposHandler) - defer cleanUp() - - options := GitCommitsQueryOptions{ - Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), - ListOptions: ListOptions{ - Page: 1, - PerPage: 30, - }, - } - - commits, err := client.GetCommitsWithQueryOptions(ctx, "", repo1, options) - assert.Equal(t, CommitInfo{ - Hash: "86d6919952702f9ab03bc95b45687f145a663de0", - AuthorName: "Test User", - CommitterName: "Test User", - Url: "https://dev.azure.com/testuser/0b8072c4-ad86-4edb-a8f2-06dbc07e3e2d/_apis/git/repositories/94c1dba8-d9d9-4600-94b4-1a51acb43220/commits/86d6919952702f9ab03bc95b45687f145a663de0", - Timestamp: 1667812601, - Message: "Updated package.json", - AuthorEmail: "testuser@jfrog.com", - }, commits[0]) - assert.Equal(t, CommitInfo{ - Hash: "4aa8367809020c4e97af29e2b57f7528d5d27702", - AuthorName: "Test User", - CommitterName: "Test User", - Url: "https://dev.azure.com/testuser/0b8072c4-ad86-4edb-a8f2-06dbc07e3e2d/_apis/git/repositories/94c1dba8-d9d9-4600-94b4-1a51acb43220/commits/4aa8367809020c4e97af29e2b57f7528d5d27702", - Timestamp: 1667201343, - Message: "Set up CI with Azure Pipelines", - AuthorEmail: "testuser@jfrog.com", - }, commits[1]) - assert.Equal(t, CommitInfo{ - Hash: "3779104c35804e15b6fdf4fee303e717cd6c1352", - AuthorName: "Test User", - CommitterName: "Test User", - Url: "https://dev.azure.com/testuser/0b8072c4-ad86-4edb-a8f2-06dbc07e3e2d/_apis/git/repositories/94c1dba8-d9d9-4600-94b4-1a51acb43220/commits/3779104c35804e15b6fdf4fee303e717cd6c1352", - Timestamp: 1667201200, - Message: "first commit", - AuthorEmail: "testuser@jfrog.com", - }, commits[2]) - assert.NoError(t, err) - - badClient, cleanUp := createBadAzureReposClient(t, []byte{}) - defer cleanUp() - _, err = badClient.GetCommitsWithQueryOptions(ctx, "", repo1, options) - assert.Error(t, err) -} - func TestAzureReposClient_AddSshKeyToRepository(t *testing.T) { ctx := context.Background() client, cleanUp := createServerAndClient(t, vcsutils.AzureRepos, true, "", "getLatestCommit", createAzureReposHandler) diff --git a/vcsclient/bitbucketcloud.go b/vcsclient/bitbucketcloud.go index f6068194..8ad185e0 100644 --- a/vcsclient/bitbucketcloud.go +++ b/vcsclient/bitbucketcloud.go @@ -498,7 +498,7 @@ func (client *BitbucketCloudClient) GetCommits(_ context.Context, _, _, _ string } func (client *BitbucketCloudClient) GetCommitsWithQueryOptions(ctx context.Context, owner, repository string, listOptions GitCommitsQueryOptions) ([]CommitInfo, error) { - return nil, nil + return nil, errBitbucketGetCommitsWithOptionsNotSupported } // GetRepositoryInfo on Bitbucket cloud diff --git a/vcsclient/bitbucketcommon.go b/vcsclient/bitbucketcommon.go index 4897cc36..f86b2f5f 100644 --- a/vcsclient/bitbucketcommon.go +++ b/vcsclient/bitbucketcommon.go @@ -9,6 +9,7 @@ import ( const ( notSupportedOnBitbucket = "currently not supported on Bitbucket" + notSupportedOnAzure = "currently not supported on Azure" bitbucketPrContentSizeLimit = 32768 ) @@ -17,10 +18,13 @@ var ( errBitbucketCodeScanningNotSupported = fmt.Errorf("code scanning is %s", notSupportedOnBitbucket) errBitbucketDownloadFileFromRepoNotSupported = fmt.Errorf("download file from repo is %s", notSupportedOnBitbucket) errBitbucketGetCommitsNotSupported = fmt.Errorf("get commits is %s", notSupportedOnBitbucket) + errBitbucketGetCommitsWithOptionsNotSupported = fmt.Errorf("get commits with options is %s", notSupportedOnBitbucket) errBitbucketGetRepoEnvironmentInfoNotSupported = fmt.Errorf("get repository environment info is %s", notSupportedOnBitbucket) errBitbucketListPullRequestReviewCommentsNotSupported = fmt.Errorf("list pull request review comments is %s", notSupportedOnBitbucket) errBitbucketAddPullRequestReviewCommentsNotSupported = fmt.Errorf("add pull request review comment is %s", notSupportedOnBitbucket) errBitbucketDeletePullRequestComment = fmt.Errorf("delete pull request comment is %s", notSupportedOnBitbucket) + + errAzureGetCommitsWithOptionsNotSupported = fmt.Errorf("get commits with options is %s", notSupportedOnAzure) ) type BitbucketCommitInfo struct { diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index fe7e1c4f..1a51a6fe 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -564,7 +564,6 @@ func (client *BitbucketServerClient) GetCommitsWithQueryOptions(ctx context.Cont if err != nil { return nil, err } - return client.getCommitsWithQueryOptions(ctx, owner, repository, convertToBitbucketOptionsMap(listOptions)) } @@ -590,7 +589,6 @@ func convertToBitbucketOptionsMap(listOptions GitCommitsQueryOptions) map[string return map[string]interface{}{ "limit": listOptions.PerPage, "since": listOptions.Since.Format(time.RFC3339), - // TODO is until? "until": listOptions.Until.Format(time.RFC3339), "start": listOptions.Page, } From 780df794baec19ac098d263be7059a10d1b749f9 Mon Sep 17 00:00:00 2001 From: gail Date: Sat, 22 Jun 2024 20:08:35 +0300 Subject: [PATCH 04/14] Add to README. --- README.md | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/README.md b/README.md index 424d8e36..7c1ada5b 100644 --- a/README.md +++ b/README.md @@ -51,6 +51,7 @@ Currently supported providers are: [GitHub](#github), [Bitbucket Server](#bitbuc - [Delete Pull Request Comment](#delete-pull-request-comment) - [Delete Pull Request Review Comments](#delete-pull-request-review-comments) - [Get Commits](#get-commits) + - [Get Commits With Options](#get-commits-with-options) - [Get Latest Commit](#get-latest-commit) - [Get Commit By SHA](#get-commit-by-sha) - [Get List of Modified Files](#get-list-of-modified-files) @@ -544,6 +545,29 @@ branch := "dev" commitInfo, err := client.GetCommits(ctx, owner, repository, branch) ``` +#### Get Commits With Options + +```go +// Go context +ctx := context.Background() +// Organization or username +owner := "jfrog" +// VCS repository +repository := "jfrog-cli" + +// Commits query options +options := GitCommitsQueryOptions{ + Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + Until: time.Now(), + ListOptions: ListOptions{ + Page: 1, + PerPage: 30, + }, + } + +result, err := client.GetCommitsWithQueryOptions(ctx, owner, repository, options) +``` + #### Get Latest Commit ```go From b9d5fa12b3f84b94e175e74b50156611f7b68e29 Mon Sep 17 00:00:00 2001 From: gail Date: Sun, 23 Jun 2024 16:54:06 +0300 Subject: [PATCH 05/14] minor fix. --- go.mod | 2 +- vcsclient/bitbucketserver.go | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 1a1d41aa..10dfe834 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module github.com/jfrog/froggit-go go 1.21 -toolchain go1.21.10 +toolchain go1.21.11 require ( github.com/gfleury/go-bitbucket-v1 v0.0.0-20230825095122-9bc1711434ab diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index 1a51a6fe..a6c4bdd9 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -585,6 +585,7 @@ func (client *BitbucketServerClient) getCommitsWithQueryOptions(ctx context.Cont } return commitsInfo, nil } + func convertToBitbucketOptionsMap(listOptions GitCommitsQueryOptions) map[string]interface{} { return map[string]interface{}{ "limit": listOptions.PerPage, From 118fd414543e44bae2d3a67a804454ab17547bfa Mon Sep 17 00:00:00 2001 From: gail Date: Sun, 23 Jun 2024 17:00:42 +0300 Subject: [PATCH 06/14] revert go.mod+sum --- go.mod | 4 +--- go.sum | 10 ---------- 2 files changed, 1 insertion(+), 13 deletions(-) diff --git a/go.mod b/go.mod index 10dfe834..2ee143b4 100644 --- a/go.mod +++ b/go.mod @@ -1,8 +1,6 @@ module github.com/jfrog/froggit-go -go 1.21 - -toolchain go1.21.11 +go 1.20 require ( github.com/gfleury/go-bitbucket-v1 v0.0.0-20230825095122-9bc1711434ab diff --git a/go.sum b/go.sum index 618aff70..ae77b3d6 100644 --- a/go.sum +++ b/go.sum @@ -614,13 +614,11 @@ github.com/ajstarks/svgo v0.0.0-20180226025133-644b8db467af/go.mod h1:K08gAheRH3 github.com/ajstarks/svgo v0.0.0-20211024235047-1546f124cd8b/go.mod h1:1KcenG0jGWcpt8ov532z81sp/kMMUG485J2InIOyADM= github.com/andybalholm/brotli v1.0.4/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8= -github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4= github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apache/arrow/go/v10 v10.0.1/go.mod h1:YvhnlEePVnBS4+0z3fhPfUy7W1Ikj0Ih0vcRo/gZ1M0= github.com/apache/arrow/go/v11 v11.0.0/go.mod h1:Eg5OsL5H+e299f7u5ssuXsuHQVEGC4xei5aX110hRiI= github.com/apache/thrift v0.16.0/go.mod h1:PHK3hniurgQaNMZYaCLEqXKsYK8upmhPbmdP2FXSqgU= 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/boombuler/barcode v1.0.0/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/boombuler/barcode v1.0.1/go.mod h1:paBWMcWSl3LHKBqUq+rly7CNSldXjb2rDl3JlRe0mD8= github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0= @@ -659,7 +657,6 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3ebgob9U8Nd0kOddGdZWjyMGR8Wziv+TBNwSE= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a h1:mATvB/9r/3gvcejNsXKSkQ6lcIaNec2nyfOdlTBR2lU= -github.com/elazarl/goproxy v0.0.0-20230808193330-2592e75ae04a/go.mod h1:Ro8st/ElPeALwNFlcTpWmkr6IoMFfkjXAvTHpevnDsM= github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc= github.com/emirpasic/gods v1.18.1/go.mod h1:8tpGGwCnJ5H4r6BWwaV6OrWmMoPhUl5jm/FMNAnJvWQ= github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= @@ -683,7 +680,6 @@ github.com/gfleury/go-bitbucket-v1 v0.0.0-20230825095122-9bc1711434ab h1:+7KwW/y github.com/gfleury/go-bitbucket-v1 v0.0.0-20230825095122-9bc1711434ab/go.mod h1:IqOZzks2wlWCIai0esXnZPdPwxF2yOz0HcCYw5I4pCg= github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04= 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-fonts/dejavu v0.1.0/go.mod h1:4Wt4I4OU2Nq9asgDCteaAaWZOV24E+0/Pwo0gppep4g= github.com/go-fonts/latin-modern v0.2.0/go.mod h1:rQVLdDMK+mK1xscDwsqM5J8U2jrRa3T0ecnM9pNujks= github.com/go-fonts/liberation v0.1.1/go.mod h1:K6qoJYypsmfVjWg8KOVDQhLc8UDgIK2HYqyqAO9z7GY= @@ -694,7 +690,6 @@ github.com/go-git/gcfg v1.5.1-0.20230307220236-3a3c6141e376/go.mod h1:an3vInlBmS github.com/go-git/go-billy/v5 v5.5.0 h1:yEY4yhzCDuMGSv83oGxiBotRzhwhNr8VZyphhiu+mTU= github.com/go-git/go-billy/v5 v5.5.0/go.mod h1:hmexnoNsr2SJU1Ju67OaNz5ASJY3+sHgFRpCtpDCKow= github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMje31YglSBqCdIqdhKBW8lokaMrL3uTkpGYlE2OOT4= -github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= github.com/go-git/go-git/v5 v5.11.0 h1:XIZc1p+8YzypNr34itUfSvYJcv+eYdTnTvOZ2vD3cA4= github.com/go-git/go-git/v5 v5.11.0/go.mod h1:6GFcX2P3NM7FPBfpePbpLd21XxsgdAt+lKqXmCUiUCY= github.com/go-gl/glfw v0.0.0-20190409004039-e6da0acd62b1/go.mod h1:vR7hzQXu2zJy9AVAgeJqvqgH9Q5CA+iKCZ2gyEVpxRU= @@ -763,7 +758,6 @@ github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8 github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= -github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github/v56 v56.0.0 h1:TysL7dMa/r7wsQi44BjqlwaHvwlFlqkK8CtBWCX3gb4= github.com/google/go-github/v56 v56.0.0/go.mod h1:D8cdcX98YWJvi7TLo7zM4/h8ZTx6u6fwGEkCdisopo0= github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8= @@ -856,7 +850,6 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= -github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= @@ -878,7 +871,6 @@ github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= github.com/onsi/gomega v1.27.10 h1:naR28SdDFlqrG6kScpT8VWpu1xWY5nJRCF3XaYyBjhI= -github.com/onsi/gomega v1.27.10/go.mod h1:RsS8tutOdbdgzbPtzzATp12yT7kM5I5aElG3evPbQ0M= github.com/phpdave11/gofpdf v1.4.2/go.mod h1:zpO6xFn9yxo3YLyMvW8HcKWVdbNqgIfOOp2dXMnm1mY= github.com/phpdave11/gofpdi v1.0.12/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= github.com/phpdave11/gofpdi v1.0.13/go.mod h1:vBmVV0Do6hSBHC8uKUQ71JGW+ZGQq74llk/7bXwjDoI= @@ -902,7 +894,6 @@ github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFR github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTEfhy4qGm1nDQc= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= -github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/ruudk/golang-pdf417 v0.0.0-20181029194003-1af4ab5afa58/go.mod h1:6lfFZQK844Gfx8o5WFuvpxWRwnSoipWe/p622j1v06w= github.com/ruudk/golang-pdf417 v0.0.0-20201230142125-a7e3863a1245/go.mod h1:pQAZKsJ8yyVxGRWYNEm9oFB8ieLgKFnamEyDmSA0BRk= github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= @@ -1142,7 +1133,6 @@ golang.org/x/sync v0.0.0-20220929204114-8fcdb60fdcc0/go.mod h1:RxMgew5VJxzue5/jJ golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.2.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= -golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190312061237-fead79001313/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= From 12f37b1c6b2233561395d3aa9b1bba8adbc6ddeb Mon Sep 17 00:00:00 2001 From: gail Date: Sun, 23 Jun 2024 17:28:13 +0300 Subject: [PATCH 07/14] fix tests. --- vcsclient/bitbucketserver_test.go | 3 ++- vcsclient/github_test.go | 3 ++- vcsclient/gitlab_test.go | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index 3db5559c..b1e7a57a 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -398,12 +398,13 @@ func TestBitbucketServer_GetCommitsWithQueryOptions(t *testing.T) { client, serverUrl, cleanUp := createServerWithUrlAndClientReturningStatus(t, vcsutils.BitbucketServer, false, response, - fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/commits?limit=30&limit=30&since=2021-01-01T00%%3A00%%3A00Z&start=1", owner, repo1), + fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/commits?limit=30&limit=30&since=2021-01-01T00%%3A00%%3A00Z&start=1&until=2024-01-01T00%%3A00%%3A00Z", owner, repo1), http.StatusOK, createBitbucketServerHandler) defer cleanUp() options := GitCommitsQueryOptions{ Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + Until: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), ListOptions: ListOptions{ Page: 1, PerPage: 30, diff --git a/vcsclient/github_test.go b/vcsclient/github_test.go index bad6c8eb..1b930b48 100644 --- a/vcsclient/github_test.go +++ b/vcsclient/github_test.go @@ -399,11 +399,12 @@ func TestGitHubClient_GetCommitsWithQueryOptions(t *testing.T) { assert.NoError(t, err) client, cleanUp := createServerAndClient(t, vcsutils.GitHub, false, response, - fmt.Sprintf("/repos/%s/%s/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z", owner, repo1), createGitHubHandler) + fmt.Sprintf("/repos/%s/%s/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z&until=2024-01-01T00%%3A00%%3A00Z", owner, repo1), createGitHubHandler) defer cleanUp() options := GitCommitsQueryOptions{ Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + Until: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), ListOptions: ListOptions{ Page: 1, PerPage: 30, diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index 9b05f1d8..064c2710 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -400,12 +400,13 @@ func TestGitLabClient_GetCommitsWithQueryOptions(t *testing.T) { assert.NoError(t, err) client, cleanUp := createServerAndClient(t, vcsutils.GitLab, false, response, - fmt.Sprintf("/api/v4/projects/%s/repository/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z", + fmt.Sprintf("/api/v4/projects/%s/repository/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z&until=2024-01-01T00%%3A00%%3A00Z", url.PathEscape(owner+"/"+repo1)), createGitLabHandler) defer cleanUp() options := GitCommitsQueryOptions{ Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + Until: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), ListOptions: ListOptions{ Page: 1, PerPage: 30, From 5b4412202153cde36ae516bf134d3e306662415e Mon Sep 17 00:00:00 2001 From: gail Date: Tue, 25 Jun 2024 17:08:06 +0300 Subject: [PATCH 08/14] remove until parameter. --- vcsclient/bitbucketserver.go | 37 ++++++++++++-- vcsclient/bitbucketserver_test.go | 83 +++++++++++++++++++++++++++++-- vcsclient/github.go | 3 +- vcsclient/github_test.go | 6 +-- vcsclient/gitlab.go | 4 +- vcsclient/gitlab_test.go | 8 +-- vcsclient/vcsclient.go | 2 - 7 files changed, 125 insertions(+), 18 deletions(-) diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index a6c4bdd9..86deff2c 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -564,7 +564,38 @@ func (client *BitbucketServerClient) GetCommitsWithQueryOptions(ctx context.Cont if err != nil { return nil, err } - return client.getCommitsWithQueryOptions(ctx, owner, repository, convertToBitbucketOptionsMap(listOptions)) + commits, err := client.getCommitsWithQueryOptions(ctx, owner, repository, convertToBitbucketOptionsMap(listOptions)) + if err != nil { + return nil, err + } + return getCommitsInDateRate(commits, listOptions), nil +} + +// Bitbucket doesn't support filtering by date, so we need to filter the commits by date ourselves. +func getCommitsInDateRate(commits []CommitInfo, options GitCommitsQueryOptions) []CommitInfo { + commitsNumber := len(commits) + if commitsNumber == 0 { + return commits + } + + firstCommit := time.Unix(0, commits[0].Timestamp*int64(time.Millisecond)).UTC() + lastCommit := time.Unix(0, commits[commitsNumber-1].Timestamp*int64(time.Millisecond)).UTC() + + // If all commits are in the range return all. + if lastCommit.After(options.Since) || lastCommit.Equal(options.Since) { + return commits + } + // If the first commit is older than the "since" timestamp, all commits are out of range, return an empty list. + if firstCommit.Before(options.Since) { + return []CommitInfo{} + } + // Find the first commit that is older than the "since" timestamp. + for i, commit := range commits { + if time.Unix(0, commit.Timestamp*int64(time.Millisecond)).UTC().Before(options.Since) { + return commits[:i] + } + } + return []CommitInfo{} } func (client *BitbucketServerClient) getCommitsWithQueryOptions(ctx context.Context, owner, repository string, options map[string]interface{}) ([]CommitInfo, error) { @@ -589,9 +620,7 @@ func (client *BitbucketServerClient) getCommitsWithQueryOptions(ctx context.Cont func convertToBitbucketOptionsMap(listOptions GitCommitsQueryOptions) map[string]interface{} { return map[string]interface{}{ "limit": listOptions.PerPage, - "since": listOptions.Since.Format(time.RFC3339), - "until": listOptions.Until.Format(time.RFC3339), - "start": listOptions.Page, + "start": (listOptions.Page - 1) * listOptions.PerPage, } } diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index b1e7a57a..3fa64990 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -10,6 +10,7 @@ import ( "net/http" "os" "path/filepath" + "reflect" "strconv" "strings" "testing" @@ -395,16 +396,15 @@ func TestBitbucketServer_GetCommitsWithQueryOptions(t *testing.T) { ctx := context.Background() response, err := os.ReadFile(filepath.Join("testdata", "bitbucketserver", "commit_list_response.json")) assert.NoError(t, err) - + nowStr := time.Now().UTC().Format(time.RFC3339) client, serverUrl, cleanUp := createServerWithUrlAndClientReturningStatus(t, vcsutils.BitbucketServer, false, response, - fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/commits?limit=30&limit=30&since=2021-01-01T00%%3A00%%3A00Z&start=1&until=2024-01-01T00%%3A00%%3A00Z", owner, repo1), + fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/commits?limit=30&limit=30&since=2021-01-01T00%%3A00%%3A00Z&start=1&until=%s", owner, repo1, nowStr), http.StatusOK, createBitbucketServerHandler) defer cleanUp() options := GitCommitsQueryOptions{ Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), - Until: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), ListOptions: ListOptions{ Page: 1, PerPage: 30, @@ -932,3 +932,80 @@ func createBadBitbucketServerClient(t *testing.T) VcsClient { assert.NoError(t, err) return client } + +func TestGetCommitsInDateRate(t *testing.T) { + tests := []struct { + name string + commits []CommitInfo + options GitCommitsQueryOptions + expected []CommitInfo + }{ + { + name: "All commits within range", + commits: []CommitInfo{ + {Timestamp: 1717396600000}, // Mon, 03 Jun 2024 09:56:40 GMT (Within range) + {Timestamp: 1717396500000}, // Mon, 03 Jun 2024 09:55:00 GMT (Within range) + {Timestamp: 1717396400000}, // Mon, 03 Jun 2024 09:53:20 GMT (Within range) + }, + options: GitCommitsQueryOptions{ + Since: time.Unix(1717396300, 0), // Mon, 03 Jun 2024 09:51:40 GMT (Set since timestamp in seconds) + }, + expected: []CommitInfo{ + {Timestamp: 1717396600000}, + {Timestamp: 1717396500000}, + {Timestamp: 1717396400000}, + }, + }, + { + name: "All commits within range or equal", + commits: []CommitInfo{ + {Timestamp: 1717396600000}, // Mon, 03 Jun 2024 09:56:40 GMT (Within range) + {Timestamp: 1717396500000}, // Mon, 03 Jun 2024 09:55:00 GMT (Within range) + {Timestamp: 1717396400000}, // Mon, 03 Jun 2024 09:53:20 GMT (Within range) + }, + options: GitCommitsQueryOptions{ + Since: time.Unix(1717396400, 0), // Mon, 03 Jun 2024 09:53:20 GMT (Set since timestamp in seconds) + }, + expected: []CommitInfo{ + {Timestamp: 1717396600000}, + {Timestamp: 1717396500000}, + {Timestamp: 1717396400000}, + }, + }, + { + name: "No commits within range", + commits: []CommitInfo{ + {Timestamp: 1717396500000}, // Mon, 03 Jun 2024 09:55:00 GMT (Older than range) + {Timestamp: 1717396400000}, // Mon, 03 Jun 2024 09:53:20 GMT (Older than range) + }, + options: GitCommitsQueryOptions{ + Since: time.Unix(1717396600, 0), // Mon, 03 Jun 2024 09:56:40 GMT (Set since timestamp in seconds) + }, + expected: []CommitInfo{}, + }, + { + name: "Partial commits within range", + commits: []CommitInfo{ + {Timestamp: 1717396600000}, // Mon, 03 Jun 2024 09:56:40 GMT (Within range) + {Timestamp: 1717396500000}, // Mon, 03 Jun 2024 09:55:00 GMT (Within range) + {Timestamp: 1717396400000}, // Mon, 03 Jun 2024 09:53:20 GMT (Older than range) + }, + options: GitCommitsQueryOptions{ + Since: time.Unix(1717396500, 0), // Mon, 03 Jun 2024 09:55:00 GMT (Set since timestamp in seconds) + }, + expected: []CommitInfo{ + {Timestamp: 1717396600000}, + {Timestamp: 1717396500000}, + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := getCommitsInDateRate(tt.commits, tt.options) + if !reflect.DeepEqual(result, tt.expected) { + t.Errorf("Test case %s failed: expected %v, got %v", tt.name, tt.expected, result) + } + }) + } +} diff --git a/vcsclient/github.go b/vcsclient/github.go index ce109324..9c13c3ea 100644 --- a/vcsclient/github.go +++ b/vcsclient/github.go @@ -19,6 +19,7 @@ import ( "sort" "strconv" "strings" + "time" ) const ( @@ -724,7 +725,7 @@ func (client *GitHubClient) GetCommitsWithQueryOptions(ctx context.Context, owne func convertToGitHubCommitsListOptions(listOptions GitCommitsQueryOptions) *github.CommitsListOptions { return &github.CommitsListOptions{ Since: listOptions.Since, - Until: listOptions.Until, + Until: time.Now(), ListOptions: github.ListOptions{ Page: listOptions.Page, PerPage: listOptions.PerPage, diff --git a/vcsclient/github_test.go b/vcsclient/github_test.go index 1b930b48..c036b5cd 100644 --- a/vcsclient/github_test.go +++ b/vcsclient/github_test.go @@ -397,14 +397,14 @@ func TestGitHubClient_GetCommitsWithQueryOptions(t *testing.T) { ctx := context.Background() response, err := os.ReadFile(filepath.Join("testdata", "github", "commit_list_response.json")) assert.NoError(t, err) - + // TODO fix test + nowStr := time.Now().UTC().Format(time.RFC3339) client, cleanUp := createServerAndClient(t, vcsutils.GitHub, false, response, - fmt.Sprintf("/repos/%s/%s/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z&until=2024-01-01T00%%3A00%%3A00Z", owner, repo1), createGitHubHandler) + fmt.Sprintf("/repos/%s/%s/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z&until=%s", owner, repo1, nowStr), createGitHubHandler) defer cleanUp() options := GitCommitsQueryOptions{ Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), - Until: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), ListOptions: ListOptions{ Page: 1, PerPage: 30, diff --git a/vcsclient/gitlab.go b/vcsclient/gitlab.go index ff771c8d..9be47037 100644 --- a/vcsclient/gitlab.go +++ b/vcsclient/gitlab.go @@ -13,6 +13,7 @@ import ( "sort" "strconv" "strings" + "time" ) // GitLabClient API version 4 @@ -548,13 +549,14 @@ func (client *GitLabClient) GetCommitsWithQueryOptions(ctx context.Context, owne } func convertToListCommitsOptions(options GitCommitsQueryOptions) *gitlab.ListCommitsOptions { + t := time.Now() return &gitlab.ListCommitsOptions{ ListOptions: gitlab.ListOptions{ Page: options.Page, PerPage: options.PerPage, }, Since: &options.Since, - Until: &options.Until, + Until: &t, } } diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index 064c2710..62368a8e 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -398,15 +398,15 @@ func TestGitLabClient_GetCommitsWithQueryOptions(t *testing.T) { ctx := context.Background() response, err := os.ReadFile(filepath.Join("testdata", "gitlab", "commit_list_response.json")) assert.NoError(t, err) - + // TODO fix test + nowStr := time.Now().UTC().Format(time.RFC3339) client, cleanUp := createServerAndClient(t, vcsutils.GitLab, false, response, - fmt.Sprintf("/api/v4/projects/%s/repository/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z&until=2024-01-01T00%%3A00%%3A00Z", - url.PathEscape(owner+"/"+repo1)), createGitLabHandler) + fmt.Sprintf("/api/v4/projects/%s/repository/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z&until=%s", + url.PathEscape(owner+"/"+repo1), nowStr), createGitLabHandler) defer cleanUp() options := GitCommitsQueryOptions{ Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), - Until: time.Date(2024, 1, 1, 0, 0, 0, 0, time.UTC), ListOptions: ListOptions{ Page: 1, PerPage: 30, diff --git a/vcsclient/vcsclient.go b/vcsclient/vcsclient.go index 6ddba6f3..7830d50b 100644 --- a/vcsclient/vcsclient.go +++ b/vcsclient/vcsclient.go @@ -409,8 +409,6 @@ type LabelInfo struct { type GitCommitsQueryOptions struct { // Since when should Commits be included in the response. Since time.Time - // Until when should Commits be included in the response. - Until time.Time ListOptions } From 8e096b4a63c700ed15bae71d2a22cd23592e60c8 Mon Sep 17 00:00:00 2001 From: gail Date: Wed, 26 Jun 2024 15:23:07 +0300 Subject: [PATCH 09/14] Fix tests. --- vcsclient/bitbucketserver_test.go | 7 +++---- vcsclient/github_test.go | 22 ++++++++++++++++++---- vcsclient/gitlab_test.go | 21 +++++++++++++++++---- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index 3fa64990..e6fe12f7 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -396,15 +396,14 @@ func TestBitbucketServer_GetCommitsWithQueryOptions(t *testing.T) { ctx := context.Background() response, err := os.ReadFile(filepath.Join("testdata", "bitbucketserver", "commit_list_response.json")) assert.NoError(t, err) - nowStr := time.Now().UTC().Format(time.RFC3339) client, serverUrl, cleanUp := createServerWithUrlAndClientReturningStatus(t, vcsutils.BitbucketServer, false, response, - fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/commits?limit=30&limit=30&since=2021-01-01T00%%3A00%%3A00Z&start=1&until=%s", owner, repo1, nowStr), + fmt.Sprintf("/rest/api/1.0/projects/%s/repos/%s/commits?limit=30&limit=30&start=0", owner, repo1), http.StatusOK, createBitbucketServerHandler) defer cleanUp() options := GitCommitsQueryOptions{ - Since: time.Date(2021, 1, 1, 0, 0, 0, 0, time.UTC), + Since: time.Date(2017, 1, 1, 0, 0, 0, 0, time.UTC), ListOptions: ListOptions{ Page: 1, PerPage: 30, @@ -437,7 +436,7 @@ func TestBitbucketServer_GetCommitsWithQueryOptions(t *testing.T) { AuthorEmail: "marly@example.com", }, result[1]) - _, err = createBadBitbucketServerClient(t).GetCommits(ctx, owner, repo1, "master") + _, err = createBadBitbucketServerClient(t).GetCommitsWithQueryOptions(ctx, owner, repo1, options) assert.Error(t, err) } diff --git a/vcsclient/github_test.go b/vcsclient/github_test.go index c036b5cd..8f5749e4 100644 --- a/vcsclient/github_test.go +++ b/vcsclient/github_test.go @@ -397,10 +397,8 @@ func TestGitHubClient_GetCommitsWithQueryOptions(t *testing.T) { ctx := context.Background() response, err := os.ReadFile(filepath.Join("testdata", "github", "commit_list_response.json")) assert.NoError(t, err) - // TODO fix test - nowStr := time.Now().UTC().Format(time.RFC3339) client, cleanUp := createServerAndClient(t, vcsutils.GitHub, false, response, - fmt.Sprintf("/repos/%s/%s/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z&until=%s", owner, repo1, nowStr), createGitHubHandler) + fmt.Sprintf("/repos/%s/%s/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z&until=", owner, repo1), createGitHubHandlerForUnknownUrl) defer cleanUp() options := GitCommitsQueryOptions{ @@ -434,7 +432,7 @@ func TestGitHubClient_GetCommitsWithQueryOptions(t *testing.T) { AuthorEmail: "vinci@github.com", }, result[1]) - _, err = createBadGitHubClient(t).GetCommits(ctx, owner, repo1, "master") + _, err = createBadGitHubClient(t).GetCommitsWithQueryOptions(ctx, owner, repo1, options) assert.Error(t, err) } @@ -1054,6 +1052,22 @@ func createGitHubHandler(t *testing.T, expectedURI string, response []byte, expe } } +// Similar to createGitHubHandler but without checking if the expectedURI is equal to the request URI, only if it contained in the request URI. +func createGitHubHandlerForUnknownUrl(t *testing.T, expectedURI string, response []byte, expectedStatusCode int) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + assert.Contains(t, r.RequestURI, expectedURI) + assert.Equal(t, "Bearer "+token, r.Header.Get("Authorization")) + if strings.Contains(r.RequestURI, "tarball") { + w.Header().Add("Location", string(response)) + w.WriteHeader(expectedStatusCode) + return + } + w.WriteHeader(expectedStatusCode) + _, err := w.Write(response) + assert.NoError(t, err) + } +} + func createGitHubHandlerWithoutExpectedURI(t *testing.T, _ string, response []byte, expectedStatusCode int) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { assert.Equal(t, "Bearer "+token, r.Header.Get("Authorization")) diff --git a/vcsclient/gitlab_test.go b/vcsclient/gitlab_test.go index 62368a8e..2084f4da 100644 --- a/vcsclient/gitlab_test.go +++ b/vcsclient/gitlab_test.go @@ -398,11 +398,9 @@ func TestGitLabClient_GetCommitsWithQueryOptions(t *testing.T) { ctx := context.Background() response, err := os.ReadFile(filepath.Join("testdata", "gitlab", "commit_list_response.json")) assert.NoError(t, err) - // TODO fix test - nowStr := time.Now().UTC().Format(time.RFC3339) client, cleanUp := createServerAndClient(t, vcsutils.GitLab, false, response, - fmt.Sprintf("/api/v4/projects/%s/repository/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z&until=%s", - url.PathEscape(owner+"/"+repo1), nowStr), createGitLabHandler) + fmt.Sprintf("/api/v4/projects/%s/repository/commits?page=1&per_page=30&since=2021-01-01T00%%3A00%%3A00Z&until=", + url.PathEscape(owner+"/"+repo1)), createGitLabHandlerForUnknownUrl) defer cleanUp() options := GitCommitsQueryOptions{ @@ -756,6 +754,21 @@ func createGitLabHandler(t *testing.T, expectedURI string, response []byte, expe } } +// Similar to createGitLabHandler but without checking if the expectedURI is equal to the request URI, only if it contained in the request URI. +func createGitLabHandlerForUnknownUrl(t *testing.T, expectedURI string, response []byte, expectedStatusCode int) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + if r.RequestURI == "/api/v4/" { + w.WriteHeader(http.StatusOK) + return + } + w.WriteHeader(expectedStatusCode) + _, err := w.Write(response) + assert.NoError(t, err) + assert.Contains(t, r.RequestURI, expectedURI) + assert.Equal(t, token, r.Header.Get("Private-Token")) + } +} + func createGitLabHandlerWithoutExpectedURI(t *testing.T, _ string, response []byte, expectedStatusCode int) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { if r.RequestURI == "/api/v4/" { From 1836c1d0d4b6c12f9cad80bbb8aa48cbb3bd5088 Mon Sep 17 00:00:00 2001 From: gail Date: Wed, 26 Jun 2024 16:39:07 +0300 Subject: [PATCH 10/14] Review fixes. --- vcsclient/azurerepos.go | 3 +++ vcsclient/bitbucketcommon.go | 3 --- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/vcsclient/azurerepos.go b/vcsclient/azurerepos.go index 593b4688..d65dd9fe 100644 --- a/vcsclient/azurerepos.go +++ b/vcsclient/azurerepos.go @@ -20,11 +20,14 @@ import ( ) const ( + notSupportedOnAzure = "currently not supported on Azure" defaultAzureBaseUrl = "https://dev.azure.com/" azurePullRequestDetailsSizeLimit = 4000 azurePullRequestCommentSizeLimit = 150000 ) +var errAzureGetCommitsWithOptionsNotSupported = fmt.Errorf("get commits with options is %s", notSupportedOnAzure) + // Azure Devops API version 6 type AzureReposClient struct { vcsInfo VcsInfo diff --git a/vcsclient/bitbucketcommon.go b/vcsclient/bitbucketcommon.go index f86b2f5f..db15f6db 100644 --- a/vcsclient/bitbucketcommon.go +++ b/vcsclient/bitbucketcommon.go @@ -9,7 +9,6 @@ import ( const ( notSupportedOnBitbucket = "currently not supported on Bitbucket" - notSupportedOnAzure = "currently not supported on Azure" bitbucketPrContentSizeLimit = 32768 ) @@ -23,8 +22,6 @@ var ( errBitbucketListPullRequestReviewCommentsNotSupported = fmt.Errorf("list pull request review comments is %s", notSupportedOnBitbucket) errBitbucketAddPullRequestReviewCommentsNotSupported = fmt.Errorf("add pull request review comment is %s", notSupportedOnBitbucket) errBitbucketDeletePullRequestComment = fmt.Errorf("delete pull request comment is %s", notSupportedOnBitbucket) - - errAzureGetCommitsWithOptionsNotSupported = fmt.Errorf("get commits with options is %s", notSupportedOnAzure) ) type BitbucketCommitInfo struct { From 76f54670ec73287ba19798d23e6498a144a921b3 Mon Sep 17 00:00:00 2001 From: gail Date: Wed, 26 Jun 2024 17:47:50 +0300 Subject: [PATCH 11/14] Yahavi's fix. --- vcsclient/bitbucketserver_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index e6fe12f7..2db06d50 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -10,7 +10,6 @@ import ( "net/http" "os" "path/filepath" - "reflect" "strconv" "strings" "testing" @@ -1002,7 +1001,7 @@ func TestGetCommitsInDateRate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := getCommitsInDateRate(tt.commits, tt.options) - if !reflect.DeepEqual(result, tt.expected) { + if !assert.ElementsMatch(t, result, tt.expected) { t.Errorf("Test case %s failed: expected %v, got %v", tt.name, tt.expected, result) } }) From 39555a1e69832c3faa1cbba12d70e0b6777732f2 Mon Sep 17 00:00:00 2001 From: gail Date: Thu, 27 Jun 2024 10:42:08 +0300 Subject: [PATCH 12/14] Yahavi's fix 2. --- vcsclient/bitbucketserver_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index 2db06d50..cc5b7bf8 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -1001,9 +1001,7 @@ func TestGetCommitsInDateRate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := getCommitsInDateRate(tt.commits, tt.options) - if !assert.ElementsMatch(t, result, tt.expected) { - t.Errorf("Test case %s failed: expected %v, got %v", tt.name, tt.expected, result) - } + assert.ElementsMatch(t, result, tt.expected, fmt.Sprintf("Test case %s failed: expected %v, got %v", tt.name, tt.expected, result)) }) } } From d10e1a766add43d78170f360bffc4d8fec9fcbd2 Mon Sep 17 00:00:00 2001 From: gail Date: Sun, 30 Jun 2024 11:47:22 +0300 Subject: [PATCH 13/14] Fix bitbucket timestamp. --- vcsclient/bitbucketserver.go | 15 ++++++------ vcsclient/bitbucketserver_test.go | 40 +++++++++++++++---------------- 2 files changed, 28 insertions(+), 27 deletions(-) diff --git a/vcsclient/bitbucketserver.go b/vcsclient/bitbucketserver.go index 86deff2c..47a11c4b 100644 --- a/vcsclient/bitbucketserver.go +++ b/vcsclient/bitbucketserver.go @@ -578,8 +578,8 @@ func getCommitsInDateRate(commits []CommitInfo, options GitCommitsQueryOptions) return commits } - firstCommit := time.Unix(0, commits[0].Timestamp*int64(time.Millisecond)).UTC() - lastCommit := time.Unix(0, commits[commitsNumber-1].Timestamp*int64(time.Millisecond)).UTC() + firstCommit := time.Unix(commits[0].Timestamp, 0).UTC() + lastCommit := time.Unix(commits[commitsNumber-1].Timestamp, 0).UTC() // If all commits are in the range return all. if lastCommit.After(options.Since) || lastCommit.Equal(options.Since) { @@ -591,7 +591,7 @@ func getCommitsInDateRate(commits []CommitInfo, options GitCommitsQueryOptions) } // Find the first commit that is older than the "since" timestamp. for i, commit := range commits { - if time.Unix(0, commit.Timestamp*int64(time.Millisecond)).UTC().Before(options.Since) { + if time.Unix(commit.Timestamp, 0).UTC().Before(options.Since) { return commits[:i] } } @@ -820,10 +820,11 @@ func (client *BitbucketServerClient) mapBitbucketServerCommitToCommitInfo(commit AuthorName: commit.Author.Name, CommitterName: commit.Committer.Name, Url: url, - Timestamp: commit.CommitterTimestamp, - Message: commit.Message, - ParentHashes: parents, - AuthorEmail: commit.Author.EmailAddress, + // Convert from bitbucket millisecond timestamp to CommitInfo seconds timestamp. + Timestamp: commit.CommitterTimestamp / 1000, + Message: commit.Message, + ParentHashes: parents, + AuthorEmail: commit.Author.EmailAddress, } } diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index cc5b7bf8..3fbedbae 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -941,40 +941,40 @@ func TestGetCommitsInDateRate(t *testing.T) { { name: "All commits within range", commits: []CommitInfo{ - {Timestamp: 1717396600000}, // Mon, 03 Jun 2024 09:56:40 GMT (Within range) - {Timestamp: 1717396500000}, // Mon, 03 Jun 2024 09:55:00 GMT (Within range) - {Timestamp: 1717396400000}, // Mon, 03 Jun 2024 09:53:20 GMT (Within range) + {Timestamp: 1717396600}, // Mon, 03 Jun 2024 09:56:40 GMT (Within range) + {Timestamp: 1717396500}, // Mon, 03 Jun 2024 09:55:00 GMT (Within range) + {Timestamp: 1717396400}, // Mon, 03 Jun 2024 09:53:20 GMT (Within range) }, options: GitCommitsQueryOptions{ Since: time.Unix(1717396300, 0), // Mon, 03 Jun 2024 09:51:40 GMT (Set since timestamp in seconds) }, expected: []CommitInfo{ - {Timestamp: 1717396600000}, - {Timestamp: 1717396500000}, - {Timestamp: 1717396400000}, + {Timestamp: 1717396600}, + {Timestamp: 1717396500}, + {Timestamp: 1717396400}, }, }, { name: "All commits within range or equal", commits: []CommitInfo{ - {Timestamp: 1717396600000}, // Mon, 03 Jun 2024 09:56:40 GMT (Within range) - {Timestamp: 1717396500000}, // Mon, 03 Jun 2024 09:55:00 GMT (Within range) - {Timestamp: 1717396400000}, // Mon, 03 Jun 2024 09:53:20 GMT (Within range) + {Timestamp: 1717396600}, // Mon, 03 Jun 2024 09:56:40 GMT (Within range) + {Timestamp: 1717396500}, // Mon, 03 Jun 2024 09:55:00 GMT (Within range) + {Timestamp: 1717396400}, // Mon, 03 Jun 2024 09:53:20 GMT (Within range) }, options: GitCommitsQueryOptions{ Since: time.Unix(1717396400, 0), // Mon, 03 Jun 2024 09:53:20 GMT (Set since timestamp in seconds) }, expected: []CommitInfo{ - {Timestamp: 1717396600000}, - {Timestamp: 1717396500000}, - {Timestamp: 1717396400000}, + {Timestamp: 1717396600}, + {Timestamp: 1717396500}, + {Timestamp: 1717396400}, }, }, { name: "No commits within range", commits: []CommitInfo{ - {Timestamp: 1717396500000}, // Mon, 03 Jun 2024 09:55:00 GMT (Older than range) - {Timestamp: 1717396400000}, // Mon, 03 Jun 2024 09:53:20 GMT (Older than range) + {Timestamp: 1717396500}, // Mon, 03 Jun 2024 09:55:00 GMT (Older than range) + {Timestamp: 1717396400}, // Mon, 03 Jun 2024 09:53:20 GMT (Older than range) }, options: GitCommitsQueryOptions{ Since: time.Unix(1717396600, 0), // Mon, 03 Jun 2024 09:56:40 GMT (Set since timestamp in seconds) @@ -984,16 +984,16 @@ func TestGetCommitsInDateRate(t *testing.T) { { name: "Partial commits within range", commits: []CommitInfo{ - {Timestamp: 1717396600000}, // Mon, 03 Jun 2024 09:56:40 GMT (Within range) - {Timestamp: 1717396500000}, // Mon, 03 Jun 2024 09:55:00 GMT (Within range) - {Timestamp: 1717396400000}, // Mon, 03 Jun 2024 09:53:20 GMT (Older than range) + {Timestamp: 1717396600}, // Mon, 03 Jun 2024 09:56:40 GMT (Within range) + {Timestamp: 1717396500}, // Mon, 03 Jun 2024 09:55:00 GMT (Within range) + {Timestamp: 1717396400}, // Mon, 03 Jun 2024 09:53:20 GMT (Older than range) }, options: GitCommitsQueryOptions{ Since: time.Unix(1717396500, 0), // Mon, 03 Jun 2024 09:55:00 GMT (Set since timestamp in seconds) }, expected: []CommitInfo{ - {Timestamp: 1717396600000}, - {Timestamp: 1717396500000}, + {Timestamp: 1717396600}, + {Timestamp: 1717396500}, }, }, } @@ -1001,7 +1001,7 @@ func TestGetCommitsInDateRate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { result := getCommitsInDateRate(tt.commits, tt.options) - assert.ElementsMatch(t, result, tt.expected, fmt.Sprintf("Test case %s failed: expected %v, got %v", tt.name, tt.expected, result)) + assert.ElementsMatch(t, result, tt.expected) }) } } From 419bd4353e094965dc0e0cf83cb8fa44e1161087 Mon Sep 17 00:00:00 2001 From: gail Date: Sun, 30 Jun 2024 11:57:23 +0300 Subject: [PATCH 14/14] Fix bitbucket timestamp. --- vcsclient/bitbucketserver_test.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/vcsclient/bitbucketserver_test.go b/vcsclient/bitbucketserver_test.go index 3fbedbae..281543f5 100644 --- a/vcsclient/bitbucketserver_test.go +++ b/vcsclient/bitbucketserver_test.go @@ -340,7 +340,7 @@ func TestBitbucketServer_GetLatestCommit(t *testing.T) { AuthorName: "charlie", CommitterName: "mark", Url: expectedUrl, - Timestamp: 1548720847610, + Timestamp: 1548720847, Message: "More work on feature 1", ParentHashes: []string{"abcdef0123abcdef4567abcdef8987abcdef6543", "qwerty0123abcdef4567abcdef8987abcdef6543"}, AuthorEmail: "charlie@example.com", @@ -371,7 +371,7 @@ func TestBitbucketServer_GetCommits(t *testing.T) { AuthorName: "charlie", CommitterName: "mark", Url: expectedUrl, - Timestamp: 1548720847610, + Timestamp: 1548720847, Message: "More work on feature 1", ParentHashes: []string{"abcdef0123abcdef4567abcdef8987abcdef6543", "qwerty0123abcdef4567abcdef8987abcdef6543"}, AuthorEmail: "charlie@example.com", @@ -381,7 +381,7 @@ func TestBitbucketServer_GetCommits(t *testing.T) { AuthorName: "marly", CommitterName: "marly", Url: expectedUrl, - Timestamp: 1548720847610, + Timestamp: 1548720847, Message: "More work on feature 2", ParentHashes: []string{"abcdef0123abcdef4567abcdef8987abcdef6543", "qwerty0123abcdef4567abcdef8987abcdef6543"}, AuthorEmail: "marly@example.com", @@ -419,7 +419,7 @@ func TestBitbucketServer_GetCommitsWithQueryOptions(t *testing.T) { AuthorName: "charlie", CommitterName: "mark", Url: expectedUrl, - Timestamp: 1548720847610, + Timestamp: 1548720847, Message: "More work on feature 1", ParentHashes: []string{"abcdef0123abcdef4567abcdef8987abcdef6543", "qwerty0123abcdef4567abcdef8987abcdef6543"}, AuthorEmail: "charlie@example.com", @@ -429,7 +429,7 @@ func TestBitbucketServer_GetCommitsWithQueryOptions(t *testing.T) { AuthorName: "marly", CommitterName: "marly", Url: expectedUrl, - Timestamp: 1548720847610, + Timestamp: 1548720847, Message: "More work on feature 2", ParentHashes: []string{"abcdef0123abcdef4567abcdef8987abcdef6543", "qwerty0123abcdef4567abcdef8987abcdef6543"}, AuthorEmail: "marly@example.com", @@ -651,7 +651,7 @@ func TestBitbucketServer_GetCommitBySha(t *testing.T) { AuthorName: "charlie", CommitterName: "mark", Url: expectedUrl, - Timestamp: 1636089306104, + Timestamp: 1636089306, Message: "WIP on feature 1", ParentHashes: []string{"bbcdef0123abcdef4567abcdef8987abcdef6543"}, AuthorEmail: "charlie@example.com",