Skip to content

Commit da81e17

Browse files
authored
fix: Enable submitting empty allowlist for actions permissions patterns (#4371)
1 parent 90a1849 commit da81e17

2 files changed

Lines changed: 112 additions & 3 deletions

File tree

github/actions_permissions_orgs.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,12 @@ type ActionsEnabledOnOrgRepos struct {
3434
//
3535
// GitHub API docs: https://docs.github.com/rest/actions/permissions?apiVersion=2022-11-28
3636
type ActionsAllowed struct {
37-
GithubOwnedAllowed *bool `json:"github_owned_allowed,omitempty"`
38-
VerifiedAllowed *bool `json:"verified_allowed,omitempty"`
39-
PatternsAllowed []string `json:"patterns_allowed,omitempty"`
37+
GithubOwnedAllowed *bool `json:"github_owned_allowed,omitempty"`
38+
VerifiedAllowed *bool `json:"verified_allowed,omitempty"`
39+
// PatternsAllowed uses `omitzero` to allow an empty array to be sent in the request body in order to
40+
// configure the organization to allow no 3rd-party actions. It needs to still omit a `nil` value so
41+
// that it's possible to patch the other values without changing the allowed actions.
42+
PatternsAllowed []string `json:"patterns_allowed,omitzero"`
4043
}
4144

4245
func (a ActionsAllowed) String() string {

github/actions_permissions_orgs_test.go

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,40 @@ func TestActionsService_GetActionsAllowed(t *testing.T) {
252252
})
253253
}
254254

255+
func TestActionsService_GetActionsAllowed_emptyPatterns(t *testing.T) {
256+
t.Parallel()
257+
client, mux, _ := setup(t)
258+
259+
mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
260+
testMethod(t, r, "GET")
261+
fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":[]}`)
262+
})
263+
264+
ctx := t.Context()
265+
org, _, err := client.Actions.GetActionsAllowed(ctx, "o")
266+
if err != nil {
267+
t.Errorf("Actions.GetActionsAllowed returned error: %v", err)
268+
}
269+
want := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: []string{}}
270+
if !cmp.Equal(org, want) {
271+
t.Errorf("Actions.GetActionsAllowed returned %+v, want %+v", org, want)
272+
}
273+
274+
const methodName = "GetActionsAllowed"
275+
testBadOptions(t, methodName, func() (err error) {
276+
_, _, err = client.Actions.GetActionsAllowed(ctx, "\n")
277+
return err
278+
})
279+
280+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
281+
got, resp, err := client.Actions.GetActionsAllowed(ctx, "o")
282+
if got != nil {
283+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
284+
}
285+
return resp, err
286+
})
287+
}
288+
255289
func TestActionsService_UpdateActionsAllowed(t *testing.T) {
256290
t.Parallel()
257291
client, mux, _ := setup(t)
@@ -290,6 +324,78 @@ func TestActionsService_UpdateActionsAllowed(t *testing.T) {
290324
})
291325
}
292326

327+
func TestActionsService_UpdateActionsAllowed_emptyPatterns(t *testing.T) {
328+
t.Parallel()
329+
client, mux, _ := setup(t)
330+
331+
input := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: []string{}}
332+
333+
mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
334+
testMethod(t, r, "PUT")
335+
testJSONBody(t, r, input)
336+
fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":[]}`)
337+
})
338+
339+
ctx := t.Context()
340+
org, _, err := client.Actions.UpdateActionsAllowed(ctx, "o", *input)
341+
if err != nil {
342+
t.Errorf("Actions.UpdateActionsAllowed returned error: %v", err)
343+
}
344+
345+
want := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: []string{}}
346+
if !cmp.Equal(org, want) {
347+
t.Errorf("Actions.UpdateActionsAllowed returned %+v, want %+v", org, want)
348+
}
349+
}
350+
351+
func TestActionsService_UpdateActionsAllowed_nilPatterns(t *testing.T) {
352+
t.Parallel()
353+
client, mux, _ := setup(t)
354+
355+
input := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: nil}
356+
357+
mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
358+
testMethod(t, r, "PUT")
359+
testJSONBody(t, r, input)
360+
fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`)
361+
})
362+
363+
ctx := t.Context()
364+
org, _, err := client.Actions.UpdateActionsAllowed(ctx, "o", *input)
365+
if err != nil {
366+
t.Errorf("Actions.UpdateActionsAllowed returned error: %v", err)
367+
}
368+
369+
want := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: []string{"a/b"}}
370+
if !cmp.Equal(org, want) {
371+
t.Errorf("Actions.UpdateActionsAllowed returned %+v, want %+v", org, want)
372+
}
373+
}
374+
375+
func TestActionsService_UpdateActionsAllowed_omittedPatterns(t *testing.T) {
376+
t.Parallel()
377+
client, mux, _ := setup(t)
378+
379+
input := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false)}
380+
381+
mux.HandleFunc("/orgs/o/actions/permissions/selected-actions", func(w http.ResponseWriter, r *http.Request) {
382+
testMethod(t, r, "PUT")
383+
testJSONBody(t, r, input)
384+
fmt.Fprint(w, `{"github_owned_allowed":true, "verified_allowed":false, "patterns_allowed":["a/b"]}`)
385+
})
386+
387+
ctx := t.Context()
388+
org, _, err := client.Actions.UpdateActionsAllowed(ctx, "o", *input)
389+
if err != nil {
390+
t.Errorf("Actions.UpdateActionsAllowed returned error: %v", err)
391+
}
392+
393+
want := &ActionsAllowed{GithubOwnedAllowed: Ptr(true), VerifiedAllowed: Ptr(false), PatternsAllowed: []string{"a/b"}}
394+
if !cmp.Equal(org, want) {
395+
t.Errorf("Actions.UpdateActionsAllowed returned %+v, want %+v", org, want)
396+
}
397+
}
398+
293399
func TestActionsService_GetDefaultWorkflowPermissionsInOrganization(t *testing.T) {
294400
t.Parallel()
295401
client, mux, _ := setup(t)

0 commit comments

Comments
 (0)