Skip to content

Commit

Permalink
Add repo list endpoint to Bitbucket client
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelsauter committed Mar 21, 2022
1 parent 740e1ac commit bf17cc0
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 0 deletions.
34 changes: 34 additions & 0 deletions pkg/bitbucket/repos.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,47 @@ type Repo struct {
} `json:"links"`
}

type RepoPage struct {
Size int `json:"size"`
Limit int `json:"limit"`
IsLastPage bool `json:"isLastPage"`
Values []Repo `json:"values"`
Start int `json:"start"`
}

type RepoCreatePayload struct {
SCMID string `json:"scmId"`
Name string `json:"name"`
Forkable bool `json:"forkable"`
DefaultBranch string `json:"defaultBranch"`
}

type RepoClientInterface interface {
RepoList(projectKey string) (*RepoPage, error)
RepoCreate(projectKey string, payload RepoCreatePayload) (*Repo, error)
}

// RepoList retrieves repositories from the project corresponding to the supplied projectKey.
// The authenticated user must have REPO_READ permission for the context repository to call this resource.
// https://docs.atlassian.com/bitbucket-server/rest/7.13.0/bitbucket-rest.html#idp175
func (c *Client) RepoList(projectKey string) (*RepoPage, error) {

urlPath := fmt.Sprintf(
"/rest/api/1.0/projects/%s/repos",
projectKey,
)
_, response, err := c.get(urlPath)
if err != nil {
return nil, err
}
var repoPage RepoPage
err = json.Unmarshal(response, &repoPage)
if err != nil {
return nil, err
}
return &repoPage, nil
}

// RepoCreate creates a new repository. Requires an existing project in which this repository will be created. The only parameters which will be used are name and scmId.
// The authenticated user must have PROJECT_ADMIN permission for the context project to call this resource.
// https://docs.atlassian.com/bitbucket-server/rest/7.13.0/bitbucket-rest.html#idp174
Expand Down
19 changes: 19 additions & 0 deletions pkg/bitbucket/repos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,22 @@ func TestRepoCreate(t *testing.T) {
t.Fatalf("got %s, want %s", r.Slug, "my-repo")
}
}

func TestRepoList(t *testing.T) {
srv, cleanup := testserver.NewTestServer(t)
defer cleanup()
bitbucketClient := testClient(srv.Server.URL)

srv.EnqueueResponse(
t, "/rest/api/1.0/projects/PRJ/repos",
200, "bitbucket/repo-list.json",
)

l, err := bitbucketClient.RepoList("PRJ")
if err != nil {
t.Fatal(err)
}
if l.Size != 1 {
t.Fatalf("got %d, want %d", l.Size, 1)
}
}
52 changes: 52 additions & 0 deletions test/testdata/fixtures/bitbucket/repo-list.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"size": 1,
"limit": 25,
"isLastPage": true,
"values": [
{
"slug": "my-repo",
"id": 1,
"name": "My repo",
"description": "My repo description",
"hierarchyId": "e3c939f9ef4a7fae272e",
"scmId": "git",
"state": "AVAILABLE",
"statusMessage": "Available",
"forkable": true,
"project": {
"key": "PRJ",
"id": 1,
"name": "My Cool Project",
"description": "The description for my cool project.",
"public": true,
"type": "NORMAL",
"links": {
"self": [
{
"href": "http://link/to/project"
}
]
}
},
"public": true,
"links": {
"clone": [
{
"href": "ssh://git@<baseURL>/PRJ/my-repo.git",
"name": "ssh"
},
{
"href": "https://<baseURL>/scm/PRJ/my-repo.git",
"name": "http"
}
],
"self": [
{
"href": "http://link/to/repository"
}
]
}
}
],
"start": 0
}

0 comments on commit bf17cc0

Please sign in to comment.