Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support OIDC subject claim customization templates for actions #2615

Merged
merged 3 commits into from Jan 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
73 changes: 73 additions & 0 deletions github/actions_oidc.go
@@ -0,0 +1,73 @@
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"context"
"fmt"
)

// OIDCSubjectClaimCustomizationTemplate represents an OIDC subject claim customization template
F21 marked this conversation as resolved.
Show resolved Hide resolved
type OIDCSubjectClaimCustomizationTemplate struct {
UseDefault *bool `json:"use_default,omitempty"`
IncludeClaimKeys []string `json:"include_claim_keys"`
}

// GetOrganizationOIDCSubjectClaimCustomizationTemplate gets the subject claim customization template for an organization.
gmlewis marked this conversation as resolved.
Show resolved Hide resolved
//
// GitHub API docs: https://docs.github.com/en/rest/actions/oidc#get-the-customization-template-for-an-oidc-subject-claim-for-an-organization
func (s *ActionsService) GetOrganizationOIDCSubjectClaimCustomizationTemplate(ctx context.Context, org string) (*OIDCSubjectClaimCustomizationTemplate, *Response, error) {
u := fmt.Sprintf("orgs/%v/actions/oidc/customization/sub", org)
return s.getOIDCSubjectClaimCustomizationTemplate(ctx, u)
}

// GetRepositoryOIDCSubjectClaimCustomizationTemplate gets the subject claim customization template for a repository.
F21 marked this conversation as resolved.
Show resolved Hide resolved
//
// GitHub API docs: https://docs.github.com/en/rest/actions/oidc#get-the-customization-template-for-an-oidc-subject-claim-for-a-repository
func (s *ActionsService) GetRepositoryOIDCSubjectClaimCustomizationTemplate(ctx context.Context, owner, repo string) (*OIDCSubjectClaimCustomizationTemplate, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/oidc/customization/sub", owner, repo)
return s.getOIDCSubjectClaimCustomizationTemplate(ctx, u)
}

func (s *ActionsService) getOIDCSubjectClaimCustomizationTemplate(ctx context.Context, url string) (*OIDCSubjectClaimCustomizationTemplate, *Response, error) {
req, err := s.client.NewRequest("GET", url, nil)
if err != nil {
return nil, nil, err
}

oidcSubjectClaimCustomizationTemplate := new(OIDCSubjectClaimCustomizationTemplate)
F21 marked this conversation as resolved.
Show resolved Hide resolved
resp, err := s.client.Do(ctx, req, oidcSubjectClaimCustomizationTemplate)
if err != nil {
return nil, resp, err
}

return oidcSubjectClaimCustomizationTemplate, resp, nil
}

// SetOrganizationOIDCSubjectClaimCustomizationTemplate sets the subject claim customization for an organization.
F21 marked this conversation as resolved.
Show resolved Hide resolved
//
// GitHub API docs: https://docs.github.com/en/rest/actions/oidc#set-the-customization-template-for-an-oidc-subject-claim-for-an-organization
func (s *ActionsService) SetOrganizationOIDCSubjectClaimCustomizationTemplate(ctx context.Context, org string, template *OIDCSubjectClaimCustomizationTemplate) (*Response, error) {
u := fmt.Sprintf("orgs/%v/actions/oidc/customization/sub", org)
return s.setOIDCSubjectClaimCustomizationTemplate(ctx, u, template)
}

// SetRepositoryOIDCSubjectClaimCustomizationTemplate sets the subject claim customization for a repository.
F21 marked this conversation as resolved.
Show resolved Hide resolved
//
// GitHub API docs: https://docs.github.com/en/rest/actions/oidc#set-the-customization-template-for-an-oidc-subject-claim-for-a-repository
func (s *ActionsService) SetRepositoryOIDCSubjectClaimCustomizationTemplate(ctx context.Context, owner, repo string, template *OIDCSubjectClaimCustomizationTemplate) (*Response, error) {
u := fmt.Sprintf("repos/%v/%v/actions/oidc/customization/sub", owner, repo)
return s.setOIDCSubjectClaimCustomizationTemplate(ctx, u, template)
}

func (s *ActionsService) setOIDCSubjectClaimCustomizationTemplate(ctx context.Context, url string, template *OIDCSubjectClaimCustomizationTemplate) (*Response, error) {
req, err := s.client.NewRequest("PUT", url, template)
if err != nil {
return nil, err
}

return s.client.Do(ctx, req, nil)
}
150 changes: 150 additions & 0 deletions github/actions_oidc_test.go
@@ -0,0 +1,150 @@
// Copyright 2023 The go-github AUTHORS. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package github

import (
"context"
"fmt"
"net/http"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestActionsService_GetOrganizationOIDCSubjectClaimCustomizationTemplate(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"include_claim_keys":["repo","context"]}`)
})

ctx := context.Background()
template, _, err := client.Actions.GetOrganizationOIDCSubjectClaimCustomizationTemplate(ctx, "o")
if err != nil {
t.Errorf("Actions.GetOrganizationOIDCSubjectClaimCustomizationTemplate returned error: %v", err)
}

want := &OIDCSubjectClaimCustomizationTemplate{IncludeClaimKeys: []string{"repo", "context"}}
if !cmp.Equal(template, want) {
t.Errorf("Actions.GetOrganizationOIDCSubjectClaimCustomizationTemplate returned %+v, want %+v", template, want)
}

const methodName = "GetOrganizationOIDCSubjectClaimCustomizationTemplate"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Actions.GetOrganizationOIDCSubjectClaimCustomizationTemplate(ctx, "\n")
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Actions.GetOrganizationOIDCSubjectClaimCustomizationTemplate(ctx, "o")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestActionsService_GetRepositoryOIDCSubjectClaimCustomizationTemplate(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"use_default":false,"include_claim_keys":["repo","context"]}`)
})

ctx := context.Background()
template, _, err := client.Actions.GetRepositoryOIDCSubjectClaimCustomizationTemplate(ctx, "o", "r")
if err != nil {
t.Errorf("Actions.GetRepositoryOIDCSubjectClaimCustomizationTemplate returned error: %v", err)
}

want := &OIDCSubjectClaimCustomizationTemplate{UseDefault: Bool(false), IncludeClaimKeys: []string{"repo", "context"}}
if !cmp.Equal(template, want) {
t.Errorf("Actions.GetOrganizationOIDCSubjectClaimCustomizationTemplate returned %+v, want %+v", template, want)
}

const methodName = "GetRepositoryOIDCSubjectClaimCustomizationTemplate"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Actions.GetRepositoryOIDCSubjectClaimCustomizationTemplate(ctx, "\n", "\n")
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Actions.GetRepositoryOIDCSubjectClaimCustomizationTemplate(ctx, "o", "r")
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestActionsService_SetOrganizationOIDCSubjectClaimCustomizationTemplate(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, `{"include_claim_keys":["repo","context"]}`+"\n")
w.WriteHeader(http.StatusCreated)
})

input := &OIDCSubjectClaimCustomizationTemplate{
IncludeClaimKeys: []string{"repo", "context"},
}
ctx := context.Background()
_, err := client.Actions.SetOrganizationOIDCSubjectClaimCustomizationTemplate(ctx, "o", input)
if err != nil {
t.Errorf("Actions.SetOrganizationOIDCSubjectClaimCustomizationTemplate returned error: %v", err)
}

const methodName = "SetOrganizationOIDCSubjectClaimCustomizationTemplate"

testBadOptions(t, methodName, func() (err error) {
_, err = client.Actions.SetOrganizationOIDCSubjectClaimCustomizationTemplate(ctx, "\n", input)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Actions.SetOrganizationOIDCSubjectClaimCustomizationTemplate(ctx, "o", input)
})
}

func TestActionsService_SetRepositoryOIDCSubjectClaimCustomizationTemplate(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/actions/oidc/customization/sub", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
testHeader(t, r, "Content-Type", "application/json")
testBody(t, r, `{"use_default":false,"include_claim_keys":["repo","context"]}`+"\n")
w.WriteHeader(http.StatusCreated)
})

input := &OIDCSubjectClaimCustomizationTemplate{
UseDefault: Bool(false),
IncludeClaimKeys: []string{"repo", "context"},
}
ctx := context.Background()
_, err := client.Actions.SetRepositoryOIDCSubjectClaimCustomizationTemplate(ctx, "o", "r", input)
if err != nil {
t.Errorf("Actions.SetRepositoryOIDCSubjectClaimCustomizationTemplate returned error: %v", err)
}

const methodName = "SetRepositoryOIDCSubjectClaimCustomizationTemplate"

testBadOptions(t, methodName, func() (err error) {
_, err = client.Actions.SetRepositoryOIDCSubjectClaimCustomizationTemplate(ctx, "\n", "\n", input)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
return client.Actions.SetRepositoryOIDCSubjectClaimCustomizationTemplate(ctx, "o", "r", input)
})
}
8 changes: 8 additions & 0 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.