Skip to content

Commit 3feff68

Browse files
authored
Add comment field to Deploy Key response data structure (#169)
When you add a deploy key that contains a comment, the Bitbucket API splits out the comment and returns it in a separate `comment` field in the API. For example, `key = "ssh-rsa AAAA...=== [email protected]"` API response ``` key = "ssh-rsa AAAA...===" comment = "[email protected]" ```
1 parent ddab5c9 commit 3feff68

File tree

2 files changed

+104
-3
lines changed

2 files changed

+104
-3
lines changed

deploykeys.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ type DeployKeys struct {
1313
}
1414

1515
type DeployKey struct {
16-
Id int `json:"id"`
17-
Label string `json:"label"`
18-
Key string `json:"key"`
16+
Id int `json:"id"`
17+
Label string `json:"label"`
18+
Key string `json:"key"`
19+
Comment string `json:"comment"`
1920
}
2021

2122
func decodeDeployKey(response interface{}) (*DeployKey, error) {

tests/deploykeys_test.go

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package tests
22

33
import (
4+
"fmt"
45
"os"
56
"testing"
67

@@ -100,3 +101,102 @@ func TestDeployKey(t *testing.T) {
100101
}
101102
})
102103
}
104+
105+
func TestDeployKeyWithComment(t *testing.T) {
106+
user := os.Getenv("BITBUCKET_TEST_USERNAME")
107+
pass := os.Getenv("BITBUCKET_TEST_PASSWORD")
108+
owner := os.Getenv("BITBUCKET_TEST_OWNER")
109+
repo := os.Getenv("BITBUCKET_TEST_REPOSLUG")
110+
111+
if user == "" {
112+
t.Error("BITBUCKET_TEST_USERNAME is empty.")
113+
}
114+
if pass == "" {
115+
t.Error("BITBUCKET_TEST_PASSWORD is empty.")
116+
}
117+
if owner == "" {
118+
t.Error("BITBUCKET_TEST_OWNER is empty.")
119+
}
120+
if repo == "" {
121+
t.Error("BITBUCKET_TEST_REPOSLUG is empty.")
122+
}
123+
124+
c := bitbucket.NewBasicAuth(user, pass)
125+
126+
var deployKeyResourceId int
127+
128+
label := "go-bb-test"
129+
key := "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDAK/b1cHHDr/TEV1JGQl+WjCwStKG6Bhrv0rFpEsYlyTBm1fzN0VOJJYn4ZOPCPJwqse6fGbXntEs+BbXiptR+++HycVgl65TMR0b5ul5AgwrVdZdT7qjCOCgaSV74/9xlHDK8oqgGnfA7ZoBBU+qpVyaloSjBdJfLtPY/xqj4yHnXKYzrtn/uFc4Kp9Tb7PUg9Io3qohSTGJGVHnsVblq/rToJG7L5xIo0OxK0SJSQ5vuId93ZuFZrCNMXj8JDHZeSEtjJzpRCBEXHxpOPhAcbm4MzULgkFHhAVgp4JbkrT99/wpvZ7r9AdkTg7HGqL3rlaDrEcWfL7Lu6TnhBdq5"
130+
comment := "[email protected]"
131+
132+
t.Run("create", func(t *testing.T) {
133+
opt := &bitbucket.DeployKeyOptions{
134+
Owner: owner,
135+
RepoSlug: repo,
136+
Label: label,
137+
Key: fmt.Sprintf("%s %s", key, comment),
138+
}
139+
140+
deployKey, err := c.Repositories.DeployKeys.Create(opt)
141+
if err != nil {
142+
t.Error(err)
143+
}
144+
145+
if deployKey == nil {
146+
t.Error("The Deploy Key could not be created.")
147+
}
148+
149+
if deployKey.Label != label {
150+
t.Error("The Deploy Key `label` attribute does not match the expected value.")
151+
}
152+
if deployKey.Key != key {
153+
t.Error("The Deploy Key `key` attribute does not match the expected value.")
154+
}
155+
if deployKey.Comment != comment {
156+
t.Error("The Deploy Key `comment` attribute does not match the expected value.")
157+
}
158+
159+
deployKeyResourceId = deployKey.Id
160+
})
161+
162+
t.Run("get", func(t *testing.T) {
163+
opt := &bitbucket.DeployKeyOptions{
164+
Owner: owner,
165+
RepoSlug: repo,
166+
Id: deployKeyResourceId,
167+
}
168+
deployKey, err := c.Repositories.DeployKeys.Get(opt)
169+
if err != nil {
170+
t.Error(err)
171+
}
172+
173+
if deployKey == nil {
174+
t.Error("The Deploy Key could not be retrieved.")
175+
}
176+
177+
if deployKey.Id != deployKeyResourceId {
178+
t.Error("The Deploy Key `label` attribute does not match the expected value.")
179+
}
180+
if deployKey.Label != label {
181+
t.Error("The Deploy Key `label` attribute does not match the expected value.")
182+
}
183+
if deployKey.Key != key {
184+
t.Error("The Deploy Key `key` attribute does not match the expected value.")
185+
}
186+
if deployKey.Comment != comment {
187+
t.Error("The Deploy Key `comment` attribute does not match the expected value.")
188+
}
189+
})
190+
191+
t.Run("delete", func(t *testing.T) {
192+
opt := &bitbucket.DeployKeyOptions{
193+
Owner: owner,
194+
RepoSlug: repo,
195+
Id: deployKeyResourceId,
196+
}
197+
_, err := c.Repositories.DeployKeys.Delete(opt)
198+
if err != nil {
199+
t.Error(err)
200+
}
201+
})
202+
}

0 commit comments

Comments
 (0)