Repository: xanzy/go-gitlab
Pull request #646 (2019-06-27 12:05:05)

Add support for getting a commit's GPG signature.
Based on Gitlab's commit reference, https://docs.gitlab.com/ce/api/commits.html#get-gpg-signature-of-a-commit.


Patch:
diff --git a/commits.go b/commits.go
index 85af094..4bf067e 100644
--- a/commits.go
+++ b/commits.go
@@ -145,7 +145,7 @@ type GetCommitRefsOptions struct {
 //
 // GitLab API docs:
 // https://docs.gitlab.com/ce/api/commits.html#get-references-a-commit-is-pushed-to
-func (s *CommitsService) GetCommitRefs(pid interface{}, sha string, opt *GetCommitRefsOptions, options ...OptionFunc) ([]CommitRef, *Response, error) {
+func (s *CommitsService) GetCommitRefs(pid interface{}, sha string, opt *GetCommitRefsOptions, options ...OptionFunc) ([]*CommitRef, *Response, error) {
 	project, err := parseID(pid)
 	if err != nil {
 		return nil, nil, err
@@ -157,7 +157,7 @@ func (s *CommitsService) GetCommitRefs(pid interface{}, sha string, opt *GetComm
 		return nil, nil, err
 	}
 
-	var cs []CommitRef
+	var cs []*CommitRef
 	resp, err := s.client.Do(req, &cs)
 	if err != nil {
 		return nil, resp, err
@@ -218,7 +218,7 @@ func (s *CommitsService) CreateCommit(pid interface{}, opt *CreateCommitOptions,
 		return nil, nil, err
 	}
 
-	var c *Commit
+	c := new(Commit)
 	resp, err := s.client.Do(req, &c)
 	if err != nil {
 		return nil, resp, err
@@ -451,7 +451,7 @@ func (s *CommitsService) SetCommitStatus(pid interface{}, sha string, opt *SetCo
 		return nil, nil, err
 	}
 
-	var cs *CommitStatus
+	cs := new(CommitStatus)
 	resp, err := s.client.Do(req, &cs)
 	if err != nil {
 		return nil, resp, err
@@ -507,7 +507,7 @@ func (s *CommitsService) CherryPickCommit(pid interface{}, sha string, opt *Cher
 		return nil, nil, err
 	}
 
-	var c *Commit
+	c := new(Commit)
 	resp, err := s.client.Do(req, &c)
 	if err != nil {
 		return nil, resp, err
@@ -538,7 +538,7 @@ func (s *CommitsService) RevertCommit(pid interface{}, sha string, opt *RevertCo
 		return nil, nil, err
 	}
 
-	var c *Commit
+	c := new(Commit)
 	resp, err := s.client.Do(req, &c)
 	if err != nil {
 		return nil, resp, err
@@ -546,3 +546,40 @@ func (s *CommitsService) RevertCommit(pid interface{}, sha string, opt *RevertCo
 
 	return c, resp, err
 }
+
+// GPGSignature represents a Gitlab commit's GPG Signature.
+//
+// GitLab API docs:
+// https://docs.gitlab.com/ee/api/commits.html#get-gpg-signature-of-a-commit
+type GPGSignature struct {
+	KeyID              int    `json:"gpg_key_id"`
+	KeyPrimaryKeyID    string `json:"gpg_key_primary_keyid"`
+	KeyUserName        string `json:"gpg_key_user_name"`
+	KeyUserEmail       string `json:"gpg_key_user_email"`
+	VerificationStatus string `json:"verification_status"`
+	KeySubkeyID        int    `json:"gpg_key_subkey_id"`
+}
+
+// GetGPGSiganature gets a GPG signature of a commit.
+//
+// GitLab API docs: https://docs.gitlab.com/ee/api/commits.html#get-gpg-signature-of-a-commit
+func (s *CommitsService) GetGPGSiganature(pid interface{}, sha string, options ...OptionFunc) (*GPGSignature, *Response, error) {
+	project, err := parseID(pid)
+	if err != nil {
+		return nil, nil, err
+	}
+	u := fmt.Sprintf("projects/%s/repository/commits/%s/signature", pathEscape(project), sha)
+
+	req, err := s.client.NewRequest("GET", u, nil, options)
+	if err != nil {
+		return nil, nil, err
+	}
+
+	sig := new(GPGSignature)
+	resp, err := s.client.Do(req, &sig)
+	if err != nil {
+		return nil, resp, err
+	}
+
+	return sig, resp, err
+}


Test patch:
diff --git a/commits_test.go b/commits_test.go
index a407df0..3800b37 100644
--- a/commits_test.go
+++ b/commits_test.go
@@ -174,3 +174,29 @@ func TestRevertCommit_WithOptions(t *testing.T) {
 
 	assert.Equal(t, want, commit)
 }
+
+func TestGetGPGSignature(t *testing.T) {
+	mux, server, client := setup()
+	defer teardown(server)
+
+	mux.HandleFunc("/api/v4/projects/1/repository/commits/b0b3a907f41409829b307a28b82fdbd552ee5a27/signature", func(w http.ResponseWriter, r *http.Request) {
+		testMethod(t, r, "GET")
+		mustWriteHTTPResponse(t, w, "testdata/get_signature.json")
+	})
+
+	sig, resp, err := client.Commits.GetGPGSiganature("1", "b0b3a907f41409829b307a28b82fdbd552ee5a27", nil)
+	if err != nil {
+		t.Fatalf("Commits.GetGPGSignature returned error: %v, response: %v", err, resp)
+	}
+
+	want := &GPGSignature{
+		KeyID:              7977,
+		KeyPrimaryKeyID:    "627C5F589F467F17",
+		KeyUserName:        "Dmitriy Zaporozhets",
+		KeyUserEmail:       "dmitriy.zaporozhets@gmail.com",
+		VerificationStatus: "verified",
+		KeySubkeyID:        0,
+	}
+
+	assert.Equal(t, want, sig)
+}
diff --git a/testdata/get_commit.json b/testdata/get_commit.json
index 126c7dc..79be3c4 100644
--- a/testdata/get_commit.json
+++ b/testdata/get_commit.json
@@ -1,27 +1,27 @@
 {
-  "id": "6104942438c14ec7bd21c6cd5bd995272b3faff6",
-  "short_id": "6104942438c",
-  "title": "Sanitize for network graph",
-  "author_name": "randx",
-  "author_email": "dmitriy.zaporozhets@gmail.com",
-  "committer_name": "Dmitriy",
-  "committer_email": "dmitriy.zaporozhets@gmail.com",
-  "message": "Sanitize for network graph",
-  "parent_ids": [
-    "ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"
-  ],
-  "last_pipeline" : {
-    "id": 8,
-    "ref": "master",
-    "sha": "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
-    "status": "created",
-    "web_url":"https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416"
-  },
-  "stats": {
-    "additions": 15,
-    "deletions": 10,
-    "total": 25
-  },
-  "status": "running",
-  "project_id":13083
+    "id": "6104942438c14ec7bd21c6cd5bd995272b3faff6",
+    "short_id": "6104942438c",
+    "title": "Sanitize for network graph",
+    "author_name": "randx",
+    "author_email": "dmitriy.zaporozhets@gmail.com",
+    "committer_name": "Dmitriy",
+    "committer_email": "dmitriy.zaporozhets@gmail.com",
+    "message": "Sanitize for network graph",
+    "parent_ids": [
+        "ae1d9fb46aa2b07ee9836d49862ec4e2c46fbbba"
+    ],
+    "last_pipeline": {
+        "id": 8,
+        "ref": "master",
+        "sha": "2dc6aa325a317eda67812f05600bdf0fcdc70ab0",
+        "status": "created",
+        "web_url": "https://gitlab.com/gitlab-org/gitlab-ce/pipelines/54268416"
+    },
+    "stats": {
+        "additions": 15,
+        "deletions": 10,
+        "total": 25
+    },
+    "status": "running",
+    "project_id": 13083
 }
diff --git a/testdata/get_signature.json b/testdata/get_signature.json
new file mode 100644
index 0000000..ea47169
--- /dev/null
+++ b/testdata/get_signature.json
@@ -0,0 +1,8 @@
+{
+    "gpg_key_id": 7977,
+    "gpg_key_primary_keyid": "627C5F589F467F17",
+    "gpg_key_user_name": "Dmitriy Zaporozhets",
+    "gpg_key_user_email": "dmitriy.zaporozhets@gmail.com",
+    "verification_status": "verified",
+    "gpg_key_subkey_id": null
+}