From 4ccf8cfdb1149cb8735074c097923529a4cd66f5 Mon Sep 17 00:00:00 2001 From: Michalis Zampetakis Date: Thu, 3 Aug 2023 00:55:49 +0300 Subject: [PATCH 1/2] Add BranchHead tests --- server/forge/bitbucket/bitbucket_test.go | 12 ++++++++++ server/forge/bitbucket/fixtures/handler.go | 26 ++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/server/forge/bitbucket/bitbucket_test.go b/server/forge/bitbucket/bitbucket_test.go index a3d3277fdc1..170cfa07e01 100644 --- a/server/forge/bitbucket/bitbucket_test.go +++ b/server/forge/bitbucket/bitbucket_test.go @@ -178,6 +178,18 @@ func Test_bitbucket(t *testing.T) { }) }) + g.Describe("When requesting repo branch HEAD", func() { + g.It("Should return the details", func() { + branchHead, err := c.BranchHead(ctx, fakeUser, fakeRepo, "branch_name") + g.Assert(err).IsNil() + g.Assert(branchHead).Equal("branch_head_name") + }) + g.It("Should handle not found errors", func() { + _, err := c.BranchHead(ctx, fakeUser, fakeRepo, "branch_not_found") + g.Assert(err).IsNotNil() + }) + }) + g.Describe("When requesting repo directory contents", func() { g.It("Should return the details", func() { files, err := c.Dir(ctx, fakeUser, fakeRepo, fakePipeline, "/dir") diff --git a/server/forge/bitbucket/fixtures/handler.go b/server/forge/bitbucket/fixtures/handler.go index 90847e821d0..d55e2b88113 100644 --- a/server/forge/bitbucket/fixtures/handler.go +++ b/server/forge/bitbucket/fixtures/handler.go @@ -38,6 +38,7 @@ func Handler() http.Handler { e.GET("/2.0/repositories/:owner", getUserRepos) e.GET("/2.0/user/", getUser) e.GET("/2.0/user/permissions/repositories", getPermissions) + e.GET("/2.0/repositories/:owner/:name/commits/:commit", getBranchHead) return e } @@ -119,6 +120,15 @@ func getRepoFile(c *gin.Context) { } } +func getBranchHead(c *gin.Context) { + switch c.Param("commit") { + case "branch_name": + c.String(200, branchCommitsPayload) + default: + c.String(404, "") + } +} + func createRepoStatus(c *gin.Context) { switch c.Param("name") { case "repo_not_found": @@ -248,6 +258,22 @@ const repoDirPayload = ` } ` +const branchCommitsPayload = ` +{ + "values": [ + { + "hash": "branch_head_name" + }, + { + "hash": "random1" + }, + { + "hash": "random2" + } + ] +} +` + const userPayload = ` { "username": "superman", From 5216354cc80aaf6f42f12ad4c074eecd5879dd94 Mon Sep 17 00:00:00 2001 From: Michalis Zampetakis Date: Thu, 3 Aug 2023 01:26:08 +0300 Subject: [PATCH 2/2] Add PullRequests test --- server/forge/bitbucket/bitbucket_test.go | 18 ++++++++++++++ server/forge/bitbucket/fixtures/handler.go | 29 +++++++++++++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/server/forge/bitbucket/bitbucket_test.go b/server/forge/bitbucket/bitbucket_test.go index 170cfa07e01..b616e0db8a4 100644 --- a/server/forge/bitbucket/bitbucket_test.go +++ b/server/forge/bitbucket/bitbucket_test.go @@ -190,6 +190,24 @@ func Test_bitbucket(t *testing.T) { }) }) + g.Describe("When requesting repo pull requests", func() { + listOpts := model.ListOptions{ + All: false, + Page: 1, + PerPage: 10, + } + g.It("Should return the details", func() { + repoPRs, err := c.PullRequests(ctx, fakeUser, fakeRepo, &listOpts) + g.Assert(err).IsNil() + g.Assert(repoPRs[0].Title).Equal("PRs title") + g.Assert(repoPRs[0].Index).Equal(int64(123)) + }) + g.It("Should handle not found errors", func() { + _, err := c.PullRequests(ctx, fakeUser, fakeRepoNotFound, &listOpts) + g.Assert(err).IsNotNil() + }) + }) + g.Describe("When requesting repo directory contents", func() { g.It("Should return the details", func() { files, err := c.Dir(ctx, fakeUser, fakeRepo, fakePipeline, "/dir") diff --git a/server/forge/bitbucket/fixtures/handler.go b/server/forge/bitbucket/fixtures/handler.go index d55e2b88113..b200bb219d7 100644 --- a/server/forge/bitbucket/fixtures/handler.go +++ b/server/forge/bitbucket/fixtures/handler.go @@ -39,7 +39,7 @@ func Handler() http.Handler { e.GET("/2.0/user/", getUser) e.GET("/2.0/user/permissions/repositories", getPermissions) e.GET("/2.0/repositories/:owner/:name/commits/:commit", getBranchHead) - + e.GET("/2.0/repositories/:owner/:name/pullrequests", getPullRequests) return e } @@ -129,6 +129,15 @@ func getBranchHead(c *gin.Context) { } } +func getPullRequests(c *gin.Context) { + switch c.Param("name") { + case "repo_name": + c.String(200, pullRequestsPayload) + default: + c.String(404, "") + } +} + func createRepoStatus(c *gin.Context) { switch c.Param("name") { case "repo_not_found": @@ -274,6 +283,24 @@ const branchCommitsPayload = ` } ` +const pullRequestsPayload = ` +{ + "values": [ + { + "id": 123, + "title": "PRs title" + }, + { + "id": 456, + "title": "Another PRs title" + } + ], + "pagelen": 10, + "size": 2, + "page": 1 +} +` + const userPayload = ` { "username": "superman",