Skip to content

Commit 379c28d

Browse files

File tree

5 files changed

+192
-7
lines changed

5 files changed

+192
-7
lines changed

bitbucket.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -527,3 +527,11 @@ type RepositoryDeploymentVariableDeleteOptions struct {
527527
Environment *Environment `json:"environment"`
528528
Uuid string `json:"uuid"`
529529
}
530+
531+
type DeployKeyOptions struct {
532+
Owner string `json:"owner"`
533+
RepoSlug string `json:"repo_slug"`
534+
Id int `json:"id"`
535+
Label string `json:"label"`
536+
Key string `json:"key"`
537+
}

client.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
package bitbucket
22

33
import (
4+
"bytes"
45
"encoding/json"
56
"fmt"
7+
"io"
68
"log"
7-
9+
"mime/multipart"
810
"net/http"
911
"net/url"
12+
"os"
1013
"strconv"
1114
"strings"
1215

13-
"bytes"
14-
"io"
15-
"mime/multipart"
16-
"os"
17-
1816
"golang.org/x/net/context"
1917
"golang.org/x/oauth2"
2018
"golang.org/x/oauth2/bitbucket"
@@ -151,6 +149,7 @@ func injectClient(a *auth) *Client {
151149
BranchRestrictions: &BranchRestrictions{c: c},
152150
Webhooks: &Webhooks{c: c},
153151
Downloads: &Downloads{c: c},
152+
DeployKeys: &DeployKeys{c: c},
154153
}
155154
c.Users = &Users{c: c}
156155
c.User = &User{c: c}

deploykeys.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package bitbucket
2+
3+
import (
4+
"encoding/json"
5+
"os"
6+
7+
"github.com/k0kubun/pp"
8+
"github.com/mitchellh/mapstructure"
9+
)
10+
11+
type DeployKeys struct {
12+
c *Client
13+
}
14+
15+
type DeployKey struct {
16+
Id int `json:"id"`
17+
Label string `json:"label"`
18+
Key string `json:"key"`
19+
}
20+
21+
func decodeDeployKey(response interface{}) (*DeployKey, error) {
22+
respMap := response.(map[string]interface{})
23+
24+
if respMap["type"] == "error" {
25+
return nil, DecodeError(respMap)
26+
}
27+
28+
var deployKey = new(DeployKey)
29+
err := mapstructure.Decode(respMap, deployKey)
30+
if err != nil {
31+
return nil, err
32+
}
33+
34+
return deployKey, nil
35+
}
36+
37+
func buildDeployKeysBody(opt *DeployKeyOptions) string {
38+
body := map[string]interface{}{}
39+
body["label"] = opt.Label
40+
body["key"] = opt.Key
41+
42+
data, err := json.Marshal(body)
43+
if err != nil {
44+
_, _ = pp.Println(err)
45+
os.Exit(9)
46+
}
47+
48+
return string(data)
49+
}
50+
51+
func (dk *DeployKeys) Create(opt *DeployKeyOptions) (*DeployKey, error) {
52+
data := buildDeployKeysBody(opt)
53+
urlStr := dk.c.requestUrl("/repositories/%s/%s/deploy-keys", opt.Owner, opt.RepoSlug)
54+
response, err := dk.c.execute("POST", urlStr, data)
55+
if err != nil {
56+
return nil, err
57+
}
58+
59+
return decodeDeployKey(response)
60+
}
61+
62+
func (dk *DeployKeys) Get(opt *DeployKeyOptions) (*DeployKey, error) {
63+
urlStr := dk.c.requestUrl("/repositories/%s/%s/deploy-keys/%d", opt.Owner, opt.RepoSlug, opt.Id)
64+
response, err := dk.c.execute("GET", urlStr, "")
65+
if err != nil {
66+
return nil, err
67+
}
68+
69+
return decodeDeployKey(response)
70+
}
71+
72+
func (dk *DeployKeys) Delete(opt *DeployKeyOptions) (interface{}, error) {
73+
urlStr := dk.c.requestUrl("/repositories/%s/%s/deploy-keys/%d", opt.Owner, opt.RepoSlug, opt.Id)
74+
return dk.c.execute("DELETE", urlStr, "")
75+
}

repositories.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package bitbucket
22

33
import (
44
"errors"
5-
"fmt"
5+
"fmt"
66

77
"github.com/mitchellh/mapstructure"
88
)
@@ -20,6 +20,7 @@ type Repositories struct {
2020
BranchRestrictions *BranchRestrictions
2121
Webhooks *Webhooks
2222
Downloads *Downloads
23+
DeployKeys *DeployKeys
2324
repositories
2425
}
2526

tests/deploykeys_test.go

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package tests
2+
3+
import (
4+
"os"
5+
"testing"
6+
7+
_ "github.com/k0kubun/pp"
8+
9+
"github.com/ktrysmt/go-bitbucket"
10+
)
11+
12+
func TestDeployKey(t *testing.T) {
13+
user := os.Getenv("BITBUCKET_TEST_USERNAME")
14+
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
15+
owner := os.Getenv("BITBUCKET_TEST_OWNER")
16+
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")
17+
18+
if user == "" {
19+
t.Error("BITBUCKET_TEST_USERNAME is empty.")
20+
}
21+
if pass == "" {
22+
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
23+
}
24+
if owner == "" {
25+
t.Error("BITBUCKET_TEST_OWNER is empty.")
26+
}
27+
if repo == "" {
28+
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
29+
}
30+
31+
c := bitbucket.NewBasicAuth(user, pass)
32+
33+
var deployKeyResourceId int
34+
35+
label := "go-bb-test"
36+
key := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAK/b1cHHDr/TEV1JGQl+WjCwStKG6Bhrv0rFpEsYlyTBm1fzN0VOJJYn4ZOPCPJwqse6fGbXntEs+BbXiptR+++HycVgl65TMR0b5ul5AgwrVdZdT7qjCOCgaSV74/9xlHDK8oqgGnfA7ZoBBU+qpVyaloSjBdJfLtPY/xqj4yHnXKYzrtn/uFc4Kp9Tb7PUg9Io3qohSTGJGVHnsVblq/rToJG7L5xIo0OxK0SJSQ5vuId93ZuFZrCNMXj8JDHZeSEtjJzpRCBEXHxpOPhAcbm4MzULgkFHhAVgp4JbkrT99/wpvZ7r9AdkTg7HGqL3rlaDrEcWfL7Lu6TnhBdq5"
37+
38+
t.Run("create", func(t *testing.T) {
39+
opt := &bitbucket.DeployKeyOptions{
40+
Owner: owner,
41+
RepoSlug: repo,
42+
Label: label,
43+
Key: key,
44+
}
45+
46+
deployKey, err := c.Repositories.DeployKeys.Create(opt)
47+
if err != nil {
48+
t.Error(err)
49+
}
50+
51+
if deployKey == nil {
52+
t.Error("The Deploy Key could not be created.")
53+
}
54+
55+
if deployKey.Label != label {
56+
t.Error("The Deploy Key `label` attribute does not match the expected value.")
57+
}
58+
if deployKey.Key != key {
59+
t.Error("The Deploy Key `key` attribute does not match the expected value.")
60+
}
61+
62+
deployKeyResourceId = deployKey.Id
63+
})
64+
65+
t.Run("get", func(t *testing.T) {
66+
opt := &bitbucket.DeployKeyOptions{
67+
Owner: owner,
68+
RepoSlug: repo,
69+
Id: deployKeyResourceId,
70+
}
71+
deployKey, err := c.Repositories.DeployKeys.Get(opt)
72+
if err != nil {
73+
t.Error(err)
74+
}
75+
76+
if deployKey == nil {
77+
t.Error("The Deploy Key could not be retrieved.")
78+
}
79+
80+
if deployKey.Id != deployKeyResourceId {
81+
t.Error("The Deploy Key `label` attribute does not match the expected value.")
82+
}
83+
if deployKey.Label != label {
84+
t.Error("The Deploy Key `label` attribute does not match the expected value.")
85+
}
86+
if deployKey.Key != key {
87+
t.Error("The Deploy Key `key` attribute does not match the expected value.")
88+
}
89+
})
90+
91+
t.Run("delete", func(t *testing.T) {
92+
opt := &bitbucket.DeployKeyOptions{
93+
Owner: owner,
94+
RepoSlug: repo,
95+
Id: deployKeyResourceId,
96+
}
97+
_, err := c.Repositories.DeployKeys.Delete(opt)
98+
if err != nil {
99+
t.Error(err)
100+
}
101+
})
102+
}

0 commit comments

Comments
 (0)