Skip to content

Commit 93166f4

Browse files
authored
Support OIDC subject claim customization templates for actions (#2615)
Fixes: #2614.
1 parent 8ec1e49 commit 93166f4

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed

github/actions_oidc.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2023 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
)
12+
13+
// OIDCSubjectClaimCustomTemplate represents an OIDC subject claim customization template.
14+
type OIDCSubjectClaimCustomTemplate struct {
15+
UseDefault *bool `json:"use_default,omitempty"`
16+
IncludeClaimKeys []string `json:"include_claim_keys"`
17+
}
18+
19+
// GetOrgOIDCSubjectClaimCustomTemplate gets the subject claim customization template for an organization.
20+
//
21+
// GitHub API docs: https://docs.github.com/en/rest/actions/oidc#get-the-customization-template-for-an-oidc-subject-claim-for-an-organization
22+
func (s *ActionsService) GetOrgOIDCSubjectClaimCustomTemplate(ctx context.Context, org string) (*OIDCSubjectClaimCustomTemplate, *Response, error) {
23+
u := fmt.Sprintf("orgs/%v/actions/oidc/customization/sub", org)
24+
return s.getOIDCSubjectClaimCustomTemplate(ctx, u)
25+
}
26+
27+
// GetRepoOIDCSubjectClaimCustomTemplate gets the subject claim customization template for a repository.
28+
//
29+
// GitHub API docs: https://docs.github.com/en/rest/actions/oidc#get-the-customization-template-for-an-oidc-subject-claim-for-a-repository
30+
func (s *ActionsService) GetRepoOIDCSubjectClaimCustomTemplate(ctx context.Context, owner, repo string) (*OIDCSubjectClaimCustomTemplate, *Response, error) {
31+
u := fmt.Sprintf("repos/%v/%v/actions/oidc/customization/sub", owner, repo)
32+
return s.getOIDCSubjectClaimCustomTemplate(ctx, u)
33+
}
34+
35+
func (s *ActionsService) getOIDCSubjectClaimCustomTemplate(ctx context.Context, url string) (*OIDCSubjectClaimCustomTemplate, *Response, error) {
36+
req, err := s.client.NewRequest("GET", url, nil)
37+
if err != nil {
38+
return nil, nil, err
39+
}
40+
41+
tmpl := new(OIDCSubjectClaimCustomTemplate)
42+
resp, err := s.client.Do(ctx, req, tmpl)
43+
if err != nil {
44+
return nil, resp, err
45+
}
46+
47+
return tmpl, resp, nil
48+
}
49+
50+
// SetOrgOIDCSubjectClaimCustomTemplate sets the subject claim customization for an organization.
51+
//
52+
// GitHub API docs: https://docs.github.com/en/rest/actions/oidc#set-the-customization-template-for-an-oidc-subject-claim-for-an-organization
53+
func (s *ActionsService) SetOrgOIDCSubjectClaimCustomTemplate(ctx context.Context, org string, template *OIDCSubjectClaimCustomTemplate) (*Response, error) {
54+
u := fmt.Sprintf("orgs/%v/actions/oidc/customization/sub", org)
55+
return s.setOIDCSubjectClaimCustomTemplate(ctx, u, template)
56+
}
57+
58+
// SetRepoOIDCSubjectClaimCustomTemplate sets the subject claim customization for a repository.
59+
//
60+
// GitHub API docs: https://docs.github.com/en/rest/actions/oidc#set-the-customization-template-for-an-oidc-subject-claim-for-a-repository
61+
func (s *ActionsService) SetRepoOIDCSubjectClaimCustomTemplate(ctx context.Context, owner, repo string, template *OIDCSubjectClaimCustomTemplate) (*Response, error) {
62+
u := fmt.Sprintf("repos/%v/%v/actions/oidc/customization/sub", owner, repo)
63+
return s.setOIDCSubjectClaimCustomTemplate(ctx, u, template)
64+
}
65+
66+
func (s *ActionsService) setOIDCSubjectClaimCustomTemplate(ctx context.Context, url string, template *OIDCSubjectClaimCustomTemplate) (*Response, error) {
67+
req, err := s.client.NewRequest("PUT", url, template)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
return s.client.Do(ctx, req, nil)
73+
}

github/actions_oidc_test.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
1+
// Copyright 2023 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"net/http"
12+
"testing"
13+
14+
"github.com/google/go-cmp/cmp"
15+
)
16+
17+
func TestActionsService_GetOrgOIDCSubjectClaimCustomTemplate(t *testing.T) {
18+
client, mux, _, teardown := setup()
19+
defer teardown()
20+
21+
mux.HandleFunc("/orgs/o/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, "GET")
23+
fmt.Fprint(w, `{"include_claim_keys":["repo","context"]}`)
24+
})
25+
26+
ctx := context.Background()
27+
template, _, err := client.Actions.GetOrgOIDCSubjectClaimCustomTemplate(ctx, "o")
28+
if err != nil {
29+
t.Errorf("Actions.GetOrgOIDCSubjectClaimCustomTemplate returned error: %v", err)
30+
}
31+
32+
want := &OIDCSubjectClaimCustomTemplate{IncludeClaimKeys: []string{"repo", "context"}}
33+
if !cmp.Equal(template, want) {
34+
t.Errorf("Actions.GetOrgOIDCSubjectClaimCustomTemplate returned %+v, want %+v", template, want)
35+
}
36+
37+
const methodName = "GetOrgOIDCSubjectClaimCustomTemplate"
38+
testBadOptions(t, methodName, func() (err error) {
39+
_, _, err = client.Actions.GetOrgOIDCSubjectClaimCustomTemplate(ctx, "\n")
40+
return err
41+
})
42+
43+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
44+
got, resp, err := client.Actions.GetOrgOIDCSubjectClaimCustomTemplate(ctx, "o")
45+
if got != nil {
46+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
47+
}
48+
return resp, err
49+
})
50+
}
51+
52+
func TestActionsService_GetRepoOIDCSubjectClaimCustomTemplate(t *testing.T) {
53+
client, mux, _, teardown := setup()
54+
defer teardown()
55+
56+
mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
57+
testMethod(t, r, "GET")
58+
fmt.Fprint(w, `{"use_default":false,"include_claim_keys":["repo","context"]}`)
59+
})
60+
61+
ctx := context.Background()
62+
template, _, err := client.Actions.GetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r")
63+
if err != nil {
64+
t.Errorf("Actions.GetRepoOIDCSubjectClaimCustomTemplate returned error: %v", err)
65+
}
66+
67+
want := &OIDCSubjectClaimCustomTemplate{UseDefault: Bool(false), IncludeClaimKeys: []string{"repo", "context"}}
68+
if !cmp.Equal(template, want) {
69+
t.Errorf("Actions.GetOrgOIDCSubjectClaimCustomTemplate returned %+v, want %+v", template, want)
70+
}
71+
72+
const methodName = "GetRepoOIDCSubjectClaimCustomTemplate"
73+
testBadOptions(t, methodName, func() (err error) {
74+
_, _, err = client.Actions.GetRepoOIDCSubjectClaimCustomTemplate(ctx, "\n", "\n")
75+
return err
76+
})
77+
78+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
79+
got, resp, err := client.Actions.GetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r")
80+
if got != nil {
81+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
82+
}
83+
return resp, err
84+
})
85+
}
86+
87+
func TestActionsService_SetOrgOIDCSubjectClaimCustomTemplate(t *testing.T) {
88+
client, mux, _, teardown := setup()
89+
defer teardown()
90+
91+
mux.HandleFunc("/orgs/o/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
92+
testMethod(t, r, "PUT")
93+
testHeader(t, r, "Content-Type", "application/json")
94+
testBody(t, r, `{"include_claim_keys":["repo","context"]}`+"\n")
95+
w.WriteHeader(http.StatusCreated)
96+
})
97+
98+
input := &OIDCSubjectClaimCustomTemplate{
99+
IncludeClaimKeys: []string{"repo", "context"},
100+
}
101+
ctx := context.Background()
102+
_, err := client.Actions.SetOrgOIDCSubjectClaimCustomTemplate(ctx, "o", input)
103+
if err != nil {
104+
t.Errorf("Actions.SetOrgOIDCSubjectClaimCustomTemplate returned error: %v", err)
105+
}
106+
107+
const methodName = "SetOrgOIDCSubjectClaimCustomTemplate"
108+
109+
testBadOptions(t, methodName, func() (err error) {
110+
_, err = client.Actions.SetOrgOIDCSubjectClaimCustomTemplate(ctx, "\n", input)
111+
return err
112+
})
113+
114+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
115+
return client.Actions.SetOrgOIDCSubjectClaimCustomTemplate(ctx, "o", input)
116+
})
117+
}
118+
119+
func TestActionsService_SetRepoOIDCSubjectClaimCustomTemplate(t *testing.T) {
120+
client, mux, _, teardown := setup()
121+
defer teardown()
122+
123+
mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
124+
testMethod(t, r, "PUT")
125+
testHeader(t, r, "Content-Type", "application/json")
126+
testBody(t, r, `{"use_default":false,"include_claim_keys":["repo","context"]}`+"\n")
127+
w.WriteHeader(http.StatusCreated)
128+
})
129+
130+
input := &OIDCSubjectClaimCustomTemplate{
131+
UseDefault: Bool(false),
132+
IncludeClaimKeys: []string{"repo", "context"},
133+
}
134+
ctx := context.Background()
135+
_, err := client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
136+
if err != nil {
137+
t.Errorf("Actions.SetRepoOIDCSubjectClaimCustomTemplate returned error: %v", err)
138+
}
139+
140+
const methodName = "SetRepoOIDCSubjectClaimCustomTemplate"
141+
142+
testBadOptions(t, methodName, func() (err error) {
143+
_, err = client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "\n", "\n", input)
144+
return err
145+
})
146+
147+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
148+
return client.Actions.SetRepoOIDCSubjectClaimCustomTemplate(ctx, "o", "r", input)
149+
})
150+
}

github/github-accessors.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)