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

Add expires_at field when creating project deploy keys #2054

Merged
merged 2 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions deploy_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type ProjectDeployKey struct {
Key string `json:"key"`
CreatedAt *time.Time `json:"created_at"`
CanPush bool `json:"can_push"`
ExpiresAt *time.Time `json:"expires_at"`
}

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

// AddDeployKey creates a new deploy key for a project. If deploy key already
// exists in another project - it will be joined to project but only if
// original one was is accessible by same user.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/deploy_keys.html#add-deploy-key
// https://docs.gitlab.com/ee/api/deploy_keys.html#add-deploy-key-for-a-project
func (s *DeployKeysService) AddDeployKey(pid interface{}, opt *AddDeployKeyOptions, options ...RequestOptionFunc) (*ProjectDeployKey, *Response, error) {
project, err := parseID(pid)
if err != nil {
Expand Down
52 changes: 51 additions & 1 deletion deploy_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ func TestAddDeployKey(t *testing.T) {
"id" : 12,
"title" : "My deploy key",
"can_push": true,
"created_at" : "2015-08-29T12:44:31.550Z"
"created_at" : "2015-08-29T12:44:31.550Z",
"expires_at": null
}`)
})

Expand Down Expand Up @@ -256,6 +257,55 @@ func TestAddDeployKey(t *testing.T) {
}
}

func TestAddDeployKey_withExpiresAt(t *testing.T) {
mux, client := setup(t)

mux.HandleFunc("/api/v4/projects/5/deploy_keys", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprintf(w, `{
"key" : "ssh-rsa AAAA...",
"id" : 12,
"title" : "My deploy key",
"can_push": true,
"created_at" : "2015-08-29T12:44:31.550Z",
"expires_at": "2999-03-01T00:00:00.000Z"
}`)
})

expiresAt, err := time.Parse(timeLayout, "2999-03-01T00:00:00.000Z")
if err != nil {
t.Errorf("DeployKeys.AddDeployKey returned an error while parsing time: %v", err)
}

opt := &AddDeployKeyOptions{
Key: Ptr("ssh-rsa AAAA..."),
Title: Ptr("My deploy key"),
CanPush: Ptr(true),
ExpiresAt: &expiresAt,
}
deployKey, _, err := client.DeployKeys.AddDeployKey(5, opt)
if err != nil {
t.Errorf("DeployKey.AddDeployKey returned error: %v", err)
}

createdAt, err := time.Parse(timeLayout, "2015-08-29T12:44:31.550Z")
if err != nil {
t.Errorf("DeployKeys.AddDeployKey returned an error while parsing time: %v", err)
}

want := &ProjectDeployKey{
Title: "My deploy key",
ID: 12,
Key: "ssh-rsa AAAA...",
CreatedAt: &createdAt,
CanPush: true,
ExpiresAt: &expiresAt,
}
if !reflect.DeepEqual(want, deployKey) {
t.Errorf("DeployKeys.AddDeployKey returned %+v, want %+v", deployKey, want)
}
}

func TestDeleteDeployKey(t *testing.T) {
mux, client := setup(t)

Expand Down