Skip to content

Commit c0ad4e7

Browse files
authored
Merge pull request #343 from drone/ritek/PIPE-29374
feat: [PIPE-29374]: make author/committer fields optional for GitHub App signed commits
2 parents 15eef26 + 7cf2ec0 commit c0ad4e7

File tree

2 files changed

+133
-18
lines changed

2 files changed

+133
-18
lines changed

scm/driver/github/content.go

Lines changed: 30 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,18 @@ func (s *contentService) Create(ctx context.Context, repo, path string, params *
3737
Message: params.Message,
3838
Branch: params.Branch,
3939
Content: params.Data,
40-
Committer: commitAuthor{
40+
}
41+
42+
// Omit author/committer fields for GitHub App signed commits (empty signature)
43+
if params.Signature.Name != "" && params.Signature.Email != "" {
44+
in.Committer = &commitAuthor{
4145
Name: params.Signature.Name,
4246
Email: params.Signature.Email,
43-
},
44-
Author: commitAuthor{
47+
}
48+
in.Author = &commitAuthor{
4549
Name: params.Signature.Name,
4650
Email: params.Signature.Email,
47-
},
51+
}
4852
}
4953

5054
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
@@ -59,14 +63,18 @@ func (s *contentService) Update(ctx context.Context, repo, path string, params *
5963
Content: params.Data,
6064
// NB the sha passed to github rest api is the blob sha, not the commit sha
6165
Sha: params.BlobID,
62-
Committer: commitAuthor{
66+
}
67+
68+
// Omit author/committer fields for GitHub App signed commits (empty signature)
69+
if params.Signature.Name != "" && params.Signature.Email != "" {
70+
in.Committer = &commitAuthor{
6371
Name: params.Signature.Name,
6472
Email: params.Signature.Email,
65-
},
66-
Author: commitAuthor{
73+
}
74+
in.Author = &commitAuthor{
6775
Name: params.Signature.Name,
6876
Email: params.Signature.Email,
69-
},
77+
}
7078
}
7179
res, err := s.client.do(ctx, "PUT", endpoint, in, nil)
7280
return res, err
@@ -79,14 +87,18 @@ func (s *contentService) Delete(ctx context.Context, repo, path string, params *
7987
Branch: params.Branch,
8088
// NB the sha passed to github rest api is the blob sha, not the commit sha
8189
Sha: params.BlobID,
82-
Committer: commitAuthor{
90+
}
91+
92+
// Omit author/committer fields for GitHub App signed commits (empty signature)
93+
if params.Signature.Name != "" && params.Signature.Email != "" {
94+
in.Committer = &commitAuthor{
8395
Name: params.Signature.Name,
8496
Email: params.Signature.Email,
85-
},
86-
Author: commitAuthor{
97+
}
98+
in.Author = &commitAuthor{
8799
Name: params.Signature.Name,
88100
Email: params.Signature.Email,
89-
},
101+
}
90102
}
91103
res, err := s.client.do(ctx, "DELETE", endpoint, in, nil)
92104
return res, err
@@ -108,12 +120,12 @@ type content struct {
108120
}
109121

110122
type contentCreateUpdate struct {
111-
Branch string `json:"branch"`
112-
Message string `json:"message"`
113-
Content []byte `json:"content"`
114-
Sha string `json:"sha"`
115-
Author commitAuthor `json:"author"`
116-
Committer commitAuthor `json:"committer"`
123+
Branch string `json:"branch"`
124+
Message string `json:"message"`
125+
Content []byte `json:"content"`
126+
Sha string `json:"sha"`
127+
Author *commitAuthor `json:"author,omitempty"`
128+
Committer *commitAuthor `json:"committer,omitempty"`
117129
}
118130

119131
type commitAuthor struct {

scm/driver/github/content_test.go

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,109 @@ func TestContentDelete(t *testing.T) {
224224
}
225225
}
226226

227+
func TestContentCreateWithEmptySignature(t *testing.T) {
228+
defer gock.Off()
229+
230+
gock.New("https://api.github.com").
231+
Put("/repos/octocat/hello-world/contents/test/hello").
232+
Reply(201).
233+
Type("application/json").
234+
SetHeaders(mockHeaders).
235+
File("testdata/content_create.json")
236+
237+
params := &scm.ContentParams{
238+
Message: "my commit message",
239+
Data: []byte("bXkgbmV3IGZpbGUgY29udGVudHM="),
240+
Signature: scm.Signature{}, // Empty signature for GitHub App signed commits
241+
}
242+
243+
client := NewDefault()
244+
res, err := client.Contents.Create(
245+
context.Background(),
246+
"octocat/hello-world",
247+
"test/hello",
248+
params,
249+
)
250+
251+
if err != nil {
252+
t.Error(err)
253+
return
254+
}
255+
256+
if res.Status != 201 {
257+
t.Errorf("Unexpected Results")
258+
}
259+
}
260+
261+
func TestContentUpdateWithEmptySignature(t *testing.T) {
262+
defer gock.Off()
263+
264+
gock.New("https://api.github.com").
265+
Put("/repos/octocat/hello-world/contents/test/hello").
266+
Reply(200).
267+
Type("application/json").
268+
SetHeaders(mockHeaders).
269+
File("testdata/content_update.json")
270+
271+
params := &scm.ContentParams{
272+
Message: "a new commit message",
273+
Data: []byte("bXkgdXBkYXRlZCBmaWxlIGNvbnRlbnRz"),
274+
BlobID: "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
275+
Signature: scm.Signature{}, // Empty signature for GitHub App signed commits
276+
}
277+
278+
client := NewDefault()
279+
res, err := client.Contents.Update(
280+
context.Background(),
281+
"octocat/hello-world",
282+
"test/hello",
283+
params,
284+
)
285+
286+
if err != nil {
287+
t.Error(err)
288+
return
289+
}
290+
291+
if res.Status != 200 {
292+
t.Errorf("Unexpected Results")
293+
}
294+
}
295+
296+
func TestContentDeleteWithEmptySignature(t *testing.T) {
297+
defer gock.Off()
298+
299+
gock.New("https://api.github.com").
300+
Delete("/repos/octocat/hello-world/contents/test/hello").
301+
Reply(200).
302+
Type("application/json").
303+
SetHeaders(mockHeaders).
304+
File("testdata/content_delete.json")
305+
306+
params := &scm.ContentParams{
307+
Message: "a new commit message",
308+
BlobID: "95b966ae1c166bd92f8ae7d1c313e738c731dfc3",
309+
Signature: scm.Signature{}, // Empty signature for GitHub App signed commits
310+
}
311+
312+
client := NewDefault()
313+
res, err := client.Contents.Delete(
314+
context.Background(),
315+
"octocat/hello-world",
316+
"test/hello",
317+
params,
318+
)
319+
320+
if err != nil {
321+
t.Error(err)
322+
return
323+
}
324+
325+
if res.Status != 200 {
326+
t.Errorf("Unexpected Results")
327+
}
328+
}
329+
227330
func TestContentList(t *testing.T) {
228331
defer gock.Off()
229332

0 commit comments

Comments
 (0)