From d1cd29c41ffec074584a2f3bd63dfe08e9b15510 Mon Sep 17 00:00:00 2001 From: Rafael Lukas Maers Date: Tue, 14 Dec 2021 21:29:47 +0100 Subject: [PATCH 1/6] Add basic authentication support --- bitbucket/bitbucket.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/bitbucket/bitbucket.go b/bitbucket/bitbucket.go index f8bca3a..d72fbfa 100644 --- a/bitbucket/bitbucket.go +++ b/bitbucket/bitbucket.go @@ -28,6 +28,10 @@ type Client struct { // User agent used when communicating with the Bitbucket Server API. UserAgent string + // Basic authentication credentials + Username string + Password string + common service // Base URL for API requests. @@ -135,6 +139,10 @@ func (c *Client) NewRequest(ctx context.Context, method, urlStr string, body int return nil, err } + if c.Username != "" { + req.SetBasicAuth(c.Username, c.Password) + } + if body != nil { req.Header.Set("Content-Type", "application/json") } From 5cb13a703e685e3bb4ef3afdb171b20534b86dc9 Mon Sep 17 00:00:00 2001 From: Rafael Lukas Maers Date: Tue, 14 Dec 2021 21:50:43 +0100 Subject: [PATCH 2/6] Add repository creation support --- bitbucket/repos.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/bitbucket/repos.go b/bitbucket/repos.go index e365992..4eaa55c 100644 --- a/bitbucket/repos.go +++ b/bitbucket/repos.go @@ -87,6 +87,28 @@ type ListRepositoriesOptions struct { ListOptions } +// Create repository in a project. To create a personal repository the projectKey +// should be ~ then user slug (e.g., ~suhaib). +// +// Bitbucket Server API doc: https://docs.atlassian.com/bitbucket-server/rest/7.0.1/bitbucket-rest.html#idp168 +func (s *RepositoriesService) Create(ctx context.Context, projectKey, repoName string) (*Repository, *Response, error) { + u := fmt.Sprintf("projects/%s/repos", projectKey) + b := map[string]string{"name": repoName} + + req, err := s.client.NewRequest(ctx, "POST", u, b) + if err != nil { + return nil, nil, err + } + + repo := new(Repository) + resp, err := s.client.Do(req, &repo) + if err != nil { + return nil, resp, err + } + + return repo, resp, nil +} + // List retrieves a page of repositories based on the options that control the search. // // Bitbucket Server API doc: https://docs.atlassian.com/bitbucket-server/rest/7.0.1/bitbucket-rest.html#idp393 From a0e1512edf5aca1de61962575151e84b6647c589 Mon Sep 17 00:00:00 2001 From: Rafael Lukas Maers Date: Tue, 14 Dec 2021 21:51:02 +0100 Subject: [PATCH 3/6] Add repository forking support --- bitbucket/repos.go | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/bitbucket/repos.go b/bitbucket/repos.go index 4eaa55c..0dbc729 100644 --- a/bitbucket/repos.go +++ b/bitbucket/repos.go @@ -109,6 +109,31 @@ func (s *RepositoriesService) Create(ctx context.Context, projectKey, repoName s return repo, resp, nil } +// Fork repository in a project. To fork a personal repository the projectKey +// should be ~ then user slug (e.g., ~suhaib). +// +// Bitbucket Server API doc: https://docs.atlassian.com/bitbucket-server/rest/7.0.1/bitbucket-rest.html#idp174 +func (s *RepositoriesService) Fork(ctx context.Context, projectKey, repoSlug, repoName string) (*Repository, *Response, error) { + u := fmt.Sprintf("projects/%s/repos/%s", projectKey, repoSlug) + b := map[string]interface{}{ + "name": repoName, + "project": map[string]interface{}{"key": projectKey}, + } + + req, err := s.client.NewRequest(ctx, "POST", u, b) + if err != nil { + return nil, nil, err + } + + repo := new(Repository) + resp, err := s.client.Do(req, &repo) + if err != nil { + return nil, resp, err + } + + return repo, resp, nil +} + // List retrieves a page of repositories based on the options that control the search. // // Bitbucket Server API doc: https://docs.atlassian.com/bitbucket-server/rest/7.0.1/bitbucket-rest.html#idp393 From ff542032bb412d0274e3238214e69311e4c14c01 Mon Sep 17 00:00:00 2001 From: Rafael Lukas Maers Date: Tue, 14 Dec 2021 21:52:16 +0100 Subject: [PATCH 4/6] Add repository update support --- bitbucket/repos.go | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/bitbucket/repos.go b/bitbucket/repos.go index 0dbc729..05fec47 100644 --- a/bitbucket/repos.go +++ b/bitbucket/repos.go @@ -263,3 +263,24 @@ func (s *RepositoriesService) GetDefaultBranch(ctx context.Context, projectKey, return b, resp, nil } + +// Update repository in a project. To update a personal repository the projectKey +// should be ~ then user slug (e.g., ~suhaib). +// +// Bitbucket Server API doc: https://docs.atlassian.com/bitbucket-server/rest/7.0.1/bitbucket-rest.html#idp173 +func (s *RepositoriesService) Update(ctx context.Context, projectKey, repoSlug string, repo *Repository) (*Repository, *Response, error) { + u := fmt.Sprintf("projects/%s/repos/%s", projectKey, repoSlug) + + req, err := s.client.NewRequest(ctx, "POST", u, repo) + if err != nil { + return nil, nil, err + } + + respRepo := new(Repository) + resp, err := s.client.Do(req, &respRepo) + if err != nil { + return nil, resp, err + } + + return respRepo, resp, nil +} From eddee752c5be96b1222a270b4798524e9938f435 Mon Sep 17 00:00:00 2001 From: Rafael Lukas Maers Date: Tue, 14 Dec 2021 21:52:40 +0100 Subject: [PATCH 5/6] Add repository permission support --- bitbucket/permissions.go | 27 ++++++++++++ bitbucket/repos.go | 88 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) create mode 100644 bitbucket/permissions.go diff --git a/bitbucket/permissions.go b/bitbucket/permissions.go new file mode 100644 index 0000000..295fac6 --- /dev/null +++ b/bitbucket/permissions.go @@ -0,0 +1,27 @@ +package bitbucket + +import ( + "fmt" +) + +type Group struct { + Name string `json:"name,omitempty"` +} + +type GroupPermission struct { + Group *Group `json:"group,omitempty"` + Permission string `json:"permission,omitempty"` +} + +func (p *GroupPermission) String() string { + return fmt.Sprintf("", p.Group.Name, p.Permission) +} + +type UserPermission struct { + Permission string `json:"permission,omitempty"` + User *User `json:"user,omitempty"` +} + +func (p *UserPermission) String() string { + return fmt.Sprintf("", p.User.Name, p.Permission) +} diff --git a/bitbucket/repos.go b/bitbucket/repos.go index 05fec47..7a217fd 100644 --- a/bitbucket/repos.go +++ b/bitbucket/repos.go @@ -264,6 +264,94 @@ func (s *RepositoriesService) GetDefaultBranch(ctx context.Context, projectKey, return b, resp, nil } +// Get group permissions for a repository in a project. To get the group permissions for a personal repository the projectKey +// should be ~ then user slug (e.g., ~suhaib). +// +// Bitbucket Server API doc: https://docs.atlassian.com/bitbucket-server/rest/7.0.1/bitbucket-rest.html#idp270 +func (s *RepositoriesService) GetGroupPermissions(ctx context.Context, projectKey, repoSlug string) ([]*GroupPermission, *Response, error) { + u := fmt.Sprintf("projects/%s/repos/%s/permissions/groups", projectKey, repoSlug) + + req, err := s.client.NewRequest(ctx, "GET", u, nil) + if err != nil { + return nil, nil, err + } + + var groupPerms []*GroupPermission + page := &pagedResponse{ + Values: &groupPerms, + } + resp, err := s.client.Do(req, page) + if err != nil { + return nil, resp, err + } + + return groupPerms, resp, nil +} + +// Get user permissions for a repository in a project. To get the user permissions for a personal repository the projectKey +// should be ~ then user slug (e.g., ~suhaib). +// +// Bitbucket Server API doc: https://docs.atlassian.com/bitbucket-server/rest/7.0.1/bitbucket-rest.html#idp276 +func (s *RepositoriesService) GetUserPermissions(ctx context.Context, projectKey, repoSlug string) ([]*UserPermission, *Response, error) { + u := fmt.Sprintf("projects/%s/repos/%s/permissions/users", projectKey, repoSlug) + + req, err := s.client.NewRequest(ctx, "GET", u, nil) + if err != nil { + return nil, nil, err + } + + var userPerms []*UserPermission + page := &pagedResponse{ + Values: &userPerms, + } + resp, err := s.client.Do(req, page) + if err != nil { + return nil, resp, err + } + + return userPerms, resp, nil +} + +// Set group permissions for a repository in a project. To set the group permissions for a personal repository the projectKey +// should be ~ then user slug (e.g., ~suhaib). +// +// Bitbucket Server API doc: https://docs.atlassian.com/bitbucket-server/rest/7.0.1/bitbucket-rest.html#idp269 +func (s *RepositoriesService) SetGroupPermission(ctx context.Context, projectKey, repoSlug string, gPerm *GroupPermission) (*Response, error) { + u := fmt.Sprintf("projects/%s/repos/%s/permissions/groups?permission=%s&name=%s", projectKey, repoSlug, gPerm.Permission, gPerm.Group.Name) + + req, err := s.client.NewRequest(ctx, "PUT", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + +// Set user permissions for a repository in a project. To set the user permissions for a personal repository the projectKey +// should be ~ then user slug (e.g., ~suhaib). +// +// Bitbucket Server API doc: https://docs.atlassian.com/bitbucket-server/rest/7.0.1/bitbucket-rest.html#idp275 +func (s *RepositoriesService) SetUserPermission(ctx context.Context, projectKey, repoSlug string, uPerm *UserPermission) (*Response, error) { + u := fmt.Sprintf("projects/%s/repos/%s/permissions/users?permission=%s&name=%s", projectKey, repoSlug, uPerm.Permission, uPerm.User.Name) + + req, err := s.client.NewRequest(ctx, "PUT", u, nil) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + // Update repository in a project. To update a personal repository the projectKey // should be ~ then user slug (e.g., ~suhaib). // From 8241bebe2c6eef121c1ca28d7e52cdccad02628e Mon Sep 17 00:00:00 2001 From: Rafael Lukas Maers Date: Mon, 20 Dec 2021 20:45:13 +0100 Subject: [PATCH 6/6] Add repository label support --- bitbucket/repos.go | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/bitbucket/repos.go b/bitbucket/repos.go index 7a217fd..ad4a079 100644 --- a/bitbucket/repos.go +++ b/bitbucket/repos.go @@ -87,6 +87,26 @@ type ListRepositoriesOptions struct { ListOptions } +// Add a label to a repository. +// +// Bitbucket Server API doc: https://docs.atlassian.com/bitbucket-server/rest/7.0.1/bitbucket-rest.html#idp254 +func (s *RepositoriesService) AddLabel(ctx context.Context, projectKey, repoSlug, label string) (*Response, error) { + u := fmt.Sprintf("projects/%s/repos/%s/labels", projectKey, repoSlug) + b := map[string]string{"name": label} + + req, err := s.client.NewRequest(ctx, "POST", u, b) + if err != nil { + return nil, err + } + + resp, err := s.client.Do(req, nil) + if err != nil { + return resp, err + } + + return resp, nil +} + // Create repository in a project. To create a personal repository the projectKey // should be ~ then user slug (e.g., ~suhaib). //