Skip to content

Commit

Permalink
Align /deploy_keys API to get instance-wide deploy keys
Browse files Browse the repository at this point in the history
Apparently, the response here has changed from GitLab v13 to GitLab v14.

see https://docs.gitlab.com/ee/api/deploy_keys.html
  • Loading branch information
timofurrer committed Feb 14, 2022
1 parent 60dfa37 commit 4b179f4
Show file tree
Hide file tree
Showing 3 changed files with 160 additions and 29 deletions.
45 changes: 42 additions & 3 deletions deploy_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,56 @@ func (k DeployKey) String() string {
return Stringify(k)
}

// InstaceDeployKey represents a GitLab deploy key with the associated projects with it has write access to.
type InstanceDeployKey struct {
ID int `json:"id"`
Title string `json:"title"`
CreatedAt time.Time `json:"created_at"`
Key string `json:"key"`
Fingerprint string `json:"fingerprint"`
ProjectsWithWriteAccess []*InstanceDeployKeyProject `json:"projects_with_write_access"`
}

func (k InstanceDeployKey) String() string {
return Stringify(k)
}

// InstanceDeployKeyProject represents a Project to which an InstanceDeployKey has write access to.
type InstanceDeployKeyProject struct {
ID int `json:"id"`
Description string `json:"description"`
Name string `json:"name"`
NameWithNamespace string `json:"name_with_namespace"`
Path string `json:"path"`
PathWithNamespace string `json:"path_with_namespace"`
CreatedAt time.Time `json:"created_at"`
}

func (k InstanceDeployKeyProject) String() string {
return Stringify(k)
}

// ListProjectDeployKeysOptions represents the available ListAllDeployKeys()
// options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/deploy_keys.html#list-all-deploy-keys
type ListInstanceDeployKeysOptions struct {
ListOptions
Public *bool `url:"public,omitempty" json:"public,omitempty"`
}

// ListAllDeployKeys gets a list of all deploy keys
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/deploy_keys.html#list-all-deploy-keys
func (s *DeployKeysService) ListAllDeployKeys(options ...RequestOptionFunc) ([]*DeployKey, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "deploy_keys", nil, options)
func (s *DeployKeysService) ListAllDeployKeys(opt *ListInstanceDeployKeysOptions, options ...RequestOptionFunc) ([]*InstanceDeployKey, *Response, error) {
req, err := s.client.NewRequest(http.MethodGet, "deploy_keys", opt, options)
if err != nil {
return nil, nil, err
}

var ks []*DeployKey
var ks []*InstanceDeployKey
resp, err := s.client.Do(req, &ks)
if err != nil {
return nil, resp, err
Expand Down
93 changes: 67 additions & 26 deletions deploy_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,40 @@ func TestListAllDeployKeys(t *testing.T) {
mux.HandleFunc("/api/v4/deploy_keys", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprintf(w, `[
{
"id": 1,
"title": "Public key",
"key": "ssh-rsa AAAA...",
"fingerprint": "7f:72:08:7d:0e:47:48:ec:37:79:b2:76:68:b5:87:65",
"created_at": "2013-10-02T10:12:29Z",
"projects_with_write_access": [
{
"id": 1,
"title": "Public key",
"key": "ssh-rsa AAAA...",
"created_at": "2013-10-02T10:12:29Z"
"id": 73,
"description": null,
"name": "project2",
"name_with_namespace": "Sidney Jones / project2",
"path": "project2",
"path_with_namespace": "sidney_jones/project2",
"created_at": "2021-10-25T18:33:17.550Z"
},
{
"id": 3,
"title": "Another Public key",
"key": "ssh-rsa AAAA...",
"created_at": "2013-10-02T11:12:29Z"
"id": 74,
"description": null,
"name": "project3",
"name_with_namespace": "Sidney Jones / project3",
"path": "project3",
"path_with_namespace": "sidney_jones/project3",
"created_at": "2021-10-25T18:33:17.666Z"
}
]
},
{
"id": 3,
"title": "Another Public key",
"key": "ssh-rsa AAAA...",
"fingerprint": "64:d3:73:d4:83:70:ab:41:96:68:d5:3d:a5:b0:34:ea",
"created_at": "2013-10-02T11:12:29Z",
"projects_with_write_access": []
}
]`)
})
Expand All @@ -51,28 +74,46 @@ func TestListAllDeployKeys(t *testing.T) {
t.Errorf("DeployKeys.ListAllDeployKeys returned error: %v", err)
}

createdAt, err := time.Parse(timeLayout, "2013-10-02T10:12:29Z")
if err != nil {
t.Errorf("DeployKeys.ListAllDeployKeys returned an error while parsing time: %v", err)
}

createdAt2, err := time.Parse(timeLayout, "2013-10-02T11:12:29Z")
if err != nil {
t.Errorf("DeployKeys.ListAllDeployKeys returned an error while parsing time: %v", err)
}
createdAtKey1, _ := time.Parse(timeLayout, "2013-10-02T10:12:29Z")
createdAtKey1Enable1, _ := time.Parse(timeLayout, "2021-10-25T18:33:17.550Z")
createdAtKey1Enable2, _ := time.Parse(timeLayout, "2021-10-25T18:33:17.666Z")
createdAtKey2, _ := time.Parse(timeLayout, "2013-10-02T11:12:29Z")

want := []*DeployKey{
want := []*InstanceDeployKey{
{
ID: 1,
Title: "Public key",
Key: "ssh-rsa AAAA...",
CreatedAt: &createdAt,
ID: 1,
Title: "Public key",
Key: "ssh-rsa AAAA...",
CreatedAt: createdAtKey1,
Fingerprint: "7f:72:08:7d:0e:47:48:ec:37:79:b2:76:68:b5:87:65",
ProjectsWithWriteAccess: []*InstanceDeployKeyProject{
{
ID: 73,
Description: "",
Name: "project2",
NameWithNamespace: "Sidney Jones / project2",
Path: "project2",
PathWithNamespace: "sidney_jones/project2",
CreatedAt: createdAtKey1Enable1,
},
{
ID: 74,
Description: "",
Name: "project3",
NameWithNamespace: "Sidney Jones / project3",
Path: "project3",
PathWithNamespace: "sidney_jones/project3",
CreatedAt: createdAtKey1Enable2,
},
},
},
{
ID: 3,
Title: "Another Public key",
Key: "ssh-rsa AAAA...",
CreatedAt: &createdAt2,
ID: 3,
Title: "Another Public key",
Key: "ssh-rsa AAAA...",
Fingerprint: "64:d3:73:d4:83:70:ab:41:96:68:d5:3d:a5:b0:34:ea",
CreatedAt: createdAtKey2,
ProjectsWithWriteAccess: []*InstanceDeployKeyProject{},
},
}
if !reflect.DeepEqual(want, deployKeys) {
Expand Down
51 changes: 51 additions & 0 deletions examples/deploy_keys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
//
// Copyright 2022, Timo Furrer <tuxtimo@gmail.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

package main

import (
"log"

"github.com/xanzy/go-gitlab"
)

func labelExample() {
git, err := gitlab.NewClient("yourtokengoeshere")
if err != nil {
log.Fatal(err)
}

// Create new label
l := &gitlab.CreateLabelOptions{
Name: gitlab.String("My Label"),
Color: gitlab.String("#11FF22"),
}
label, _, err := git.Labels.CreateLabel("myname/myproject", l)
if err != nil {
log.Fatal(err)
}
log.Printf("Created label: %s\nWith color: %s\n", label.Name, label.Color)

// List all labels
labels, _, err := git.Labels.ListLabels("myname/myproject", nil)
if err != nil {
log.Fatal(err)
}

for _, label := range labels {
log.Printf("Found label: %s", label.Name)
}
}

0 comments on commit 4b179f4

Please sign in to comment.