Skip to content
This repository was archived by the owner on Dec 10, 2024. It is now read-only.

Commit e5d92f6

Browse files
authored
Merge pull request #2054 from heidiberry/project-deploy-key-expires-at
Add expires_at field when creating project deploy keys
2 parents e53b7a1 + 75ad5f4 commit e5d92f6

File tree

2 files changed

+58
-6
lines changed

2 files changed

+58
-6
lines changed

deploy_keys.go

+7-5
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ type ProjectDeployKey struct {
6767
Key string `json:"key"`
6868
CreatedAt *time.Time `json:"created_at"`
6969
CanPush bool `json:"can_push"`
70+
ExpiresAt *time.Time `json:"expires_at"`
7071
}
7172

7273
func (k ProjectDeployKey) String() string {
@@ -162,19 +163,20 @@ func (s *DeployKeysService) GetDeployKey(pid interface{}, deployKey int, options
162163
// AddDeployKeyOptions represents the available ADDDeployKey() options.
163164
//
164165
// GitLab API docs:
165-
// https://docs.gitlab.com/ee/api/deploy_keys.html#add-deploy-key
166+
// https://docs.gitlab.com/ee/api/deploy_keys.html#add-deploy-key-for-a-project
166167
type AddDeployKeyOptions struct {
167-
Title *string `url:"title,omitempty" json:"title,omitempty"`
168-
Key *string `url:"key,omitempty" json:"key,omitempty"`
169-
CanPush *bool `url:"can_push,omitempty" json:"can_push,omitempty"`
168+
Key *string `url:"key,omitempty" json:"key,omitempty"`
169+
Title *string `url:"title,omitempty" json:"title,omitempty"`
170+
CanPush *bool `url:"can_push,omitempty" json:"can_push,omitempty"`
171+
ExpiresAt *time.Time `url:"expires_at,omitempty" json:"expires_at,omitempty"`
170172
}
171173

172174
// AddDeployKey creates a new deploy key for a project. If deploy key already
173175
// exists in another project - it will be joined to project but only if
174176
// original one was is accessible by same user.
175177
//
176178
// GitLab API docs:
177-
// https://docs.gitlab.com/ee/api/deploy_keys.html#add-deploy-key
179+
// https://docs.gitlab.com/ee/api/deploy_keys.html#add-deploy-key-for-a-project
178180
func (s *DeployKeysService) AddDeployKey(pid interface{}, opt *AddDeployKeyOptions, options ...RequestOptionFunc) (*ProjectDeployKey, *Response, error) {
179181
project, err := parseID(pid)
180182
if err != nil {

deploy_keys_test.go

+51-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ func TestAddDeployKey(t *testing.T) {
225225
"id" : 12,
226226
"title" : "My deploy key",
227227
"can_push": true,
228-
"created_at" : "2015-08-29T12:44:31.550Z"
228+
"created_at" : "2015-08-29T12:44:31.550Z",
229+
"expires_at": null
229230
}`)
230231
})
231232

@@ -256,6 +257,55 @@ func TestAddDeployKey(t *testing.T) {
256257
}
257258
}
258259

260+
func TestAddDeployKey_withExpiresAt(t *testing.T) {
261+
mux, client := setup(t)
262+
263+
mux.HandleFunc("/api/v4/projects/5/deploy_keys", func(w http.ResponseWriter, r *http.Request) {
264+
testMethod(t, r, http.MethodPost)
265+
fmt.Fprintf(w, `{
266+
"key" : "ssh-rsa AAAA...",
267+
"id" : 12,
268+
"title" : "My deploy key",
269+
"can_push": true,
270+
"created_at" : "2015-08-29T12:44:31.550Z",
271+
"expires_at": "2999-03-01T00:00:00.000Z"
272+
}`)
273+
})
274+
275+
expiresAt, err := time.Parse(timeLayout, "2999-03-01T00:00:00.000Z")
276+
if err != nil {
277+
t.Errorf("DeployKeys.AddDeployKey returned an error while parsing time: %v", err)
278+
}
279+
280+
opt := &AddDeployKeyOptions{
281+
Key: Ptr("ssh-rsa AAAA..."),
282+
Title: Ptr("My deploy key"),
283+
CanPush: Ptr(true),
284+
ExpiresAt: &expiresAt,
285+
}
286+
deployKey, _, err := client.DeployKeys.AddDeployKey(5, opt)
287+
if err != nil {
288+
t.Errorf("DeployKey.AddDeployKey returned error: %v", err)
289+
}
290+
291+
createdAt, err := time.Parse(timeLayout, "2015-08-29T12:44:31.550Z")
292+
if err != nil {
293+
t.Errorf("DeployKeys.AddDeployKey returned an error while parsing time: %v", err)
294+
}
295+
296+
want := &ProjectDeployKey{
297+
Title: "My deploy key",
298+
ID: 12,
299+
Key: "ssh-rsa AAAA...",
300+
CreatedAt: &createdAt,
301+
CanPush: true,
302+
ExpiresAt: &expiresAt,
303+
}
304+
if !reflect.DeepEqual(want, deployKey) {
305+
t.Errorf("DeployKeys.AddDeployKey returned %+v, want %+v", deployKey, want)
306+
}
307+
}
308+
259309
func TestDeleteDeployKey(t *testing.T) {
260310
mux, client := setup(t)
261311

0 commit comments

Comments
 (0)