From 80d5b9c95a09fd3ae86348fee604ded68fa996a0 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Tue, 31 Jan 2023 12:38:08 +0100 Subject: [PATCH 1/6] new(github): added support for actions variables. Signed-off-by: Federico Di Pierro --- github/actions_variables.go | 293 ++++++++++++++ github/actions_variables_test.go | 659 +++++++++++++++++++++++++++++++ 2 files changed, 952 insertions(+) create mode 100644 github/actions_variables.go create mode 100644 github/actions_variables_test.go diff --git a/github/actions_variables.go b/github/actions_variables.go new file mode 100644 index 00000000000..db9c5b2a7ed --- /dev/null +++ b/github/actions_variables.go @@ -0,0 +1,293 @@ +// 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" +) + +// Variable represents a repository action variable. +type Variable struct { + Name string `json:"name"` + Value string `json:"value"` + CreatedAt Timestamp `json:"created_at"` + UpdatedAt Timestamp `json:"updated_at"` + Visibility string `json:"visibility,omitempty"` + // Used by ListOrgVariables and GetOrgVariables + SelectedRepositoriesURL string `json:"selected_repositories_url,omitempty"` + // Used by UpdateOrgVariable and CreateOrgVariable + SelectedRepositoryIDs SelectedRepoIDs `json:"selected_repository_ids,omitempty"` +} + +// Variables represents one item from the ListVariables response. +type Variables struct { + TotalCount int `json:"total_count"` + Variables []*Variable `json:"variables"` +} + +func (s *ActionsService) listVariables(ctx context.Context, url string, opts *ListOptions) (*Variables, *Response, error) { + u, err := addOptions(url, opts) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + variables := new(Variables) + resp, err := s.client.Do(ctx, req, &variables) + if err != nil { + return nil, resp, err + } + + return variables, resp, nil +} + +// ListRepoVariables lists all variables available in a repository. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#list-repository-variables +func (s *ActionsService) ListRepoVariables(ctx context.Context, owner, repo string, opts *ListOptions) (*Variables, *Response, error) { + url := fmt.Sprintf("repos/%v/%v/actions/variables", owner, repo) + return s.listVariables(ctx, url, opts) +} + +// ListOrgVariables lists all variables available in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#list-organization-variables +func (s *ActionsService) ListOrgVariables(ctx context.Context, org string, opts *ListOptions) (*Variables, *Response, error) { + url := fmt.Sprintf("orgs/%v/actions/variables", org) + return s.listVariables(ctx, url, opts) +} + +// ListEnvVariables lists all variables available in an environment. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#list-environment-variables +func (s *ActionsService) ListEnvVariables(ctx context.Context, repoID int, env string, opts *ListOptions) (*Variables, *Response, error) { + url := fmt.Sprintf("repositories/%v/environments/%v/variables", repoID, env) + return s.listVariables(ctx, url, opts) +} + +func (s *ActionsService) getVariable(ctx context.Context, url string) (*Variable, *Response, error) { + req, err := s.client.NewRequest("GET", url, nil) + if err != nil { + return nil, nil, err + } + + variable := new(Variable) + resp, err := s.client.Do(ctx, req, variable) + if err != nil { + return nil, resp, err + } + + return variable, resp, nil +} + +// GetRepoVariable gets a single repository variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#get-a-repository-variable +func (s *ActionsService) GetRepoVariable(ctx context.Context, owner, repo, name string) (*Variable, *Response, error) { + url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, name) + return s.getVariable(ctx, url) +} + +// GetOrgVariable gets a single organization variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#get-an-organization-variable +func (s *ActionsService) GetOrgVariable(ctx context.Context, org, name string) (*Variable, *Response, error) { + url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, name) + return s.getVariable(ctx, url) +} + +// GetEnvVariable gets a single environment variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#get-an-environment-variable +func (s *ActionsService) GetEnvVariable(ctx context.Context, repoID int, env, variableName string) (*Variable, *Response, error) { + url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variableName) + return s.getVariable(ctx, url) +} + +func (s *ActionsService) postVariable(ctx context.Context, url string, variable *Variable) (*Response, error) { + req, err := s.client.NewRequest("POST", url, variable) + if err != nil { + return nil, err + } + return s.client.Do(ctx, req, nil) +} + +// CreateRepoVariable creates a repository variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-a-repository-variable +func (s *ActionsService) CreateRepoVariable(ctx context.Context, owner, repo string, variable *Variable) (*Response, error) { + url := fmt.Sprintf("repos/%v/%v/actions/variables", owner, repo) + return s.postVariable(ctx, url, variable) +} + +// CreateOrgVariable creates an organization variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-an-organization-variable +func (s *ActionsService) CreateOrgVariable(ctx context.Context, org string, variable *Variable) (*Response, error) { + url := fmt.Sprintf("orgs/%v/actions/variables", org) + return s.postVariable(ctx, url, variable) +} + +// CreateEnvVariable creates an environment variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-an-environment-variable +func (s *ActionsService) CreateEnvVariable(ctx context.Context, repoID int, env string, variable *Variable) (*Response, error) { + url := fmt.Sprintf("repositories/%v/environments/%v/variables", repoID, env) + return s.postVariable(ctx, url, variable) +} + +func (s *ActionsService) patchVariable(ctx context.Context, url string, variable *Variable) (*Response, error) { + req, err := s.client.NewRequest("PATCH", url, variable) + if err != nil { + return nil, err + } + return s.client.Do(ctx, req, nil) +} + +// UpdateRepoVariable updates a repository variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#update-a-repository-variable +func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo string, variable *Variable) (*Response, error) { + url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, variable.Name) + return s.patchVariable(ctx, url, variable) +} + +// UpdateOrgVariable updates an organization variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#update-an-organization-variable +func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, variable *Variable) (*Response, error) { + url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, variable.Name) + return s.patchVariable(ctx, url, variable) +} + +// UpdateEnvVariable updates an environment variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-an-environment-variable +func (s *ActionsService) UpdateEnvVariable(ctx context.Context, repoID int, env string, variable *Variable) (*Response, error) { + url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variable.Name) + return s.patchVariable(ctx, url, variable) +} + +func (s *ActionsService) deleteVariable(ctx context.Context, url string) (*Response, error) { + req, err := s.client.NewRequest("DELETE", url, nil) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// DeleteRepoVariable deletes a variable in a repository. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#delete-a-repository-variable +func (s *ActionsService) DeleteRepoVariable(ctx context.Context, owner, repo, name string) (*Response, error) { + url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, name) + return s.deleteVariable(ctx, url) +} + +// DeleteOrgVariable deletes a variable in an organization. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#delete-an-organization-variable +func (s *ActionsService) DeleteOrgVariable(ctx context.Context, org, name string) (*Response, error) { + url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, name) + return s.deleteVariable(ctx, url) +} + +// DeleteEnvVariable deletes a variable in an environment. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#delete-an-environment-variable +func (s *ActionsService) DeleteEnvVariable(ctx context.Context, repoID int, env, variableName string) (*Response, error) { + url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variableName) + return s.deleteVariable(ctx, url) +} + +func (s *ActionsService) listSelectedReposForVariable(ctx context.Context, url string, opts *ListOptions) (*SelectedReposList, *Response, error) { + u, err := addOptions(url, opts) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + result := new(SelectedReposList) + resp, err := s.client.Do(ctx, req, result) + if err != nil { + return nil, resp, err + } + + return result, resp, nil +} + +// ListSelectedReposForOrgVariable lists all repositories that have access to a variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#list-selected-repositories-for-an-organization-variable +func (s *ActionsService) ListSelectedReposForOrgVariable(ctx context.Context, org, name string, opts *ListOptions) (*SelectedReposList, *Response, error) { + url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories", org, name) + return s.listSelectedReposForVariable(ctx, url, opts) +} + +func (s *ActionsService) setSelectedReposForVariable(ctx context.Context, url string, ids SelectedRepoIDs) (*Response, error) { + type repoIDs struct { + SelectedIDs SelectedRepoIDs `json:"selected_repository_ids"` + } + + req, err := s.client.NewRequest("PUT", url, repoIDs{SelectedIDs: ids}) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// SetSelectedReposForOrgVariable sets the repositories that have access to a variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#set-selected-repositories-for-an-organization-variable +func (s *ActionsService) SetSelectedReposForOrgVariable(ctx context.Context, org, name string, ids SelectedRepoIDs) (*Response, error) { + url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories", org, name) + return s.setSelectedReposForVariable(ctx, url, ids) +} + +func (s *ActionsService) addSelectedRepoToVariable(ctx context.Context, url string) (*Response, error) { + req, err := s.client.NewRequest("PUT", url, nil) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// AddSelectedRepoToOrgVariable adds a repository to an organization variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#add-selected-repository-to-an-organization-variable +func (s *ActionsService) AddSelectedRepoToOrgVariable(ctx context.Context, org, name string, repo *Repository) (*Response, error) { + url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories/%v", org, name, *repo.ID) + return s.addSelectedRepoToVariable(ctx, url) +} + +func (s *ActionsService) removeSelectedRepoFromVariable(ctx context.Context, url string) (*Response, error) { + req, err := s.client.NewRequest("DELETE", url, nil) + if err != nil { + return nil, err + } + + return s.client.Do(ctx, req, nil) +} + +// RemoveSelectedRepoFromOrgVariable removes a repository from an organization variable. +// +// GitHub API docs: https://docs.github.com/en/rest/actions/variables#remove-selected-repository-from-an-organization-variable +func (s *ActionsService) RemoveSelectedRepoFromOrgVariable(ctx context.Context, org, name string, repo *Repository) (*Response, error) { + url := fmt.Sprintf("orgs/%v/actions/variables/%v/repositories/%v", org, name, *repo.ID) + return s.removeSelectedRepoFromVariable(ctx, url) +} diff --git a/github/actions_variables_test.go b/github/actions_variables_test.go new file mode 100644 index 00000000000..61ed297e29d --- /dev/null +++ b/github/actions_variables_test.go @@ -0,0 +1,659 @@ +// 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" + "time" + + "github.com/google/go-cmp/cmp" +) + +func TestActionsService_ListRepoVariables(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/variables", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"per_page": "2", "page": "2"}) + fmt.Fprint(w, `{"total_count":4,"variables":[{"name":"A","value":"AA","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","value":"BB","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) + }) + + opts := &ListOptions{Page: 2, PerPage: 2} + ctx := context.Background() + variables, _, err := client.Actions.ListRepoVariables(ctx, "o", "r", opts) + if err != nil { + t.Errorf("Actions.ListRepoVariables returned error: %v", err) + } + + want := &Variables{ + TotalCount: 4, + Variables: []*Variable{ + {Name: "A", Value: "AA", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, + {Name: "B", Value: "BB", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, + }, + } + if !cmp.Equal(variables, want) { + t.Errorf("Actions.ListRepoVariables returned %+v, want %+v", variables, want) + } + + const methodName = "ListRepoVariables" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.ListRepoVariables(ctx, "\n", "\n", opts) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.ListRepoVariables(ctx, "o", "r", opts) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_GetRepoVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"name":"NAME","value":"VALUE","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`) + }) + + ctx := context.Background() + variable, _, err := client.Actions.GetRepoVariable(ctx, "o", "r", "NAME") + if err != nil { + t.Errorf("Actions.GetRepoVariable returned error: %v", err) + } + + want := &Variable{ + Name: "NAME", + Value: "VALUE", + CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, + UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, + } + if !cmp.Equal(variable, want) { + t.Errorf("Actions.GetRepoVariable returned %+v, want %+v", variable, want) + } + + const methodName = "GetRepoVariable" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetRepoVariable(ctx, "\n", "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetRepoVariable(ctx, "o", "r", "NAME") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_CreateRepoVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/variables", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + testHeader(t, r, "Content-Type", "application/json") + testBody(t, r, `{"name":"NAME","value":"VALUE","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"}`+"\n") + w.WriteHeader(http.StatusCreated) + }) + + input := &Variable{ + Name: "NAME", + Value: "VALUE", + } + ctx := context.Background() + _, err := client.Actions.CreateRepoVariable(ctx, "o", "r", input) + if err != nil { + t.Errorf("Actions.CreateRepoVariable returned error: %v", err) + } + + const methodName = "CreateRepoVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.CreateRepoVariable(ctx, "\n", "\n", input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.CreateRepoVariable(ctx, "o", "r", input) + }) +} + +func TestActionsService_UpdateRepoVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PATCH") + testHeader(t, r, "Content-Type", "application/json") + testBody(t, r, `{"name":"NAME","value":"VALUE","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"}`+"\n") + w.WriteHeader(http.StatusNoContent) + }) + + input := &Variable{ + Name: "NAME", + Value: "VALUE", + } + ctx := context.Background() + _, err := client.Actions.UpdateRepoVariable(ctx, "o", "r", input) + if err != nil { + t.Errorf("Actions.UpdateRepoVariable returned error: %v", err) + } + + const methodName = "UpdateRepoVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.UpdateRepoVariable(ctx, "\n", "\n", input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.UpdateRepoVariable(ctx, "o", "r", input) + }) +} + +func TestActionsService_DeleteRepoVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + ctx := context.Background() + _, err := client.Actions.DeleteRepoVariable(ctx, "o", "r", "NAME") + if err != nil { + t.Errorf("Actions.( returned error: %v", err) + } + + const methodName = "(" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.DeleteRepoVariable(ctx, "\n", "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.DeleteRepoVariable(ctx, "o", "r", "NAME") + }) +} + +func TestActionsService_ListOrgVariables(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/variables", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"per_page": "2", "page": "2"}) + fmt.Fprint(w, `{"total_count":3,"variables":[{"name":"A","value":"AA","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"private"},{"name":"B","value":"BB","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"all"},{"name":"C","value":"CC","created_at":"2019-08-10T14:59:22Z","updated_at":"2020-01-10T14:59:22Z","visibility":"selected","selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"}]}`) + }) + + opts := &ListOptions{Page: 2, PerPage: 2} + ctx := context.Background() + variables, _, err := client.Actions.ListOrgVariables(ctx, "o", opts) + if err != nil { + t.Errorf("Actions.ListOrgVariables returned error: %v", err) + } + + want := &Variables{ + TotalCount: 3, + Variables: []*Variable{ + {Name: "A", Value: "AA", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "private"}, + {Name: "B", Value: "BB", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "all"}, + {Name: "C", Value: "CC", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "selected", SelectedRepositoriesURL: "https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"}, + }, + } + if !cmp.Equal(variables, want) { + t.Errorf("Actions.ListOrgVariables returned %+v, want %+v", variables, want) + } + + const methodName = "ListOrgVariables" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.ListOrgVariables(ctx, "\n", opts) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.ListOrgVariables(ctx, "o", opts) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_GetOrgVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"name":"NAME","value":"VALUE","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z","visibility":"selected","selected_repositories_url":"https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"}`) + }) + + ctx := context.Background() + variable, _, err := client.Actions.GetOrgVariable(ctx, "o", "NAME") + if err != nil { + t.Errorf("Actions.GetOrgVariable returned error: %v", err) + } + + want := &Variable{ + Name: "NAME", + Value: "VALUE", + CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, + UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, + Visibility: "selected", + SelectedRepositoriesURL: "https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories", + } + if !cmp.Equal(variable, want) { + t.Errorf("Actions.GetOrgVariable returned %+v, want %+v", variable, want) + } + + const methodName = "GetOrgVariable" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetOrgVariable(ctx, "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetOrgVariable(ctx, "o", "NAME") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_CreateOrgVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/variables", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + testHeader(t, r, "Content-Type", "application/json") + testBody(t, r, `{"name":"NAME","value":"VALUE","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") + w.WriteHeader(http.StatusCreated) + }) + + input := &Variable{ + Name: "NAME", + Value: "VALUE", + Visibility: "selected", + SelectedRepositoryIDs: SelectedRepoIDs{1296269, 1269280}, + } + ctx := context.Background() + _, err := client.Actions.CreateOrgVariable(ctx, "o", input) + if err != nil { + t.Errorf("Actions.CreateOrgVariable returned error: %v", err) + } + + const methodName = "CreateOrgVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.CreateOrgVariable(ctx, "\n", input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.CreateOrgVariable(ctx, "o", input) + }) +} + +func TestActionsService_UpdateOrgVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PATCH") + testHeader(t, r, "Content-Type", "application/json") + testBody(t, r, `{"name":"NAME","value":"VALUE","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") + w.WriteHeader(http.StatusNoContent) + }) + + input := &Variable{ + Name: "NAME", + Value: "VALUE", + Visibility: "selected", + SelectedRepositoryIDs: SelectedRepoIDs{1296269, 1269280}, + } + ctx := context.Background() + _, err := client.Actions.UpdateOrgVariable(ctx, "o", input) + if err != nil { + t.Errorf("Actions.UpdateOrgVariable returned error: %v", err) + } + + const methodName = "UpdateOrgVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.UpdateOrgVariable(ctx, "\n", input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.UpdateOrgVariable(ctx, "o", input) + }) +} + +func TestActionsService_ListSelectedReposForOrgVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprintf(w, `{"total_count":1,"repositories":[{"id":1}]}`) + }) + + opts := &ListOptions{Page: 2, PerPage: 2} + ctx := context.Background() + repos, _, err := client.Actions.ListSelectedReposForOrgVariable(ctx, "o", "NAME", opts) + if err != nil { + t.Errorf("Actions.( returned error: %v", err) + } + + want := &SelectedReposList{ + TotalCount: Int(1), + Repositories: []*Repository{ + {ID: Int64(1)}, + }, + } + if !cmp.Equal(repos, want) { + t.Errorf("Actions.( returned %+v, want %+v", repos, want) + } + + const methodName = "ListSelectedReposForOrgVariable" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.ListSelectedReposForOrgVariable(ctx, "\n", "\n", opts) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.ListSelectedReposForOrgVariable(ctx, "o", "NAME", opts) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_SetSelectedReposForOrgSVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + testHeader(t, r, "Content-Type", "application/json") + testBody(t, r, `{"selected_repository_ids":[64780797]}`+"\n") + }) + + ctx := context.Background() + _, err := client.Actions.SetSelectedReposForOrgVariable(ctx, "o", "NAME", SelectedRepoIDs{64780797}) + if err != nil { + t.Errorf("Actions.( returned error: %v", err) + } + + const methodName = "SetSelectedReposForOrgVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.SetSelectedReposForOrgVariable(ctx, "\n", "\n", SelectedRepoIDs{64780797}) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.SetSelectedReposForOrgVariable(ctx, "o", "NAME", SelectedRepoIDs{64780797}) + }) +} + +func TestActionsService_AddSelectedRepoToOrgVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories/1234", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PUT") + }) + + repo := &Repository{ID: Int64(1234)} + ctx := context.Background() + _, err := client.Actions.AddSelectedRepoToOrgVariable(ctx, "o", "NAME", repo) + if err != nil { + t.Errorf("Actions.AddSelectedRepoToOrgVariable returned error: %v", err) + } + + const methodName = "AddSelectedRepoToOrgVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.AddSelectedRepoToOrgVariable(ctx, "\n", "\n", repo) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.AddSelectedRepoToOrgVariable(ctx, "o", "NAME", repo) + }) +} + +func TestActionsService_RemoveSelectedRepoFromOrgVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/variables/NAME/repositories/1234", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + repo := &Repository{ID: Int64(1234)} + ctx := context.Background() + _, err := client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "o", "NAME", repo) + if err != nil { + t.Errorf("Actions.RemoveSelectedRepoFromOrgVariable returned error: %v", err) + } + + const methodName = "RemoveSelectedRepoFromOrgVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "\n", "\n", repo) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.RemoveSelectedRepoFromOrgVariable(ctx, "o", "NAME", repo) + }) +} + +func TestActionsService_DeleteOrgVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + ctx := context.Background() + _, err := client.Actions.DeleteOrgVariable(ctx, "o", "NAME") + if err != nil { + t.Errorf("Actions.DeleteOrgVariable returned error: %v", err) + } + + const methodName = "DeleteOrgVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.DeleteOrgVariable(ctx, "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.DeleteOrgVariable(ctx, "o", "NAME") + }) +} + +func TestActionsService_ListEnvVariables(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repositories/1/environments/e/variables", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"per_page": "2", "page": "2"}) + fmt.Fprint(w, `{"total_count":4,"variables":[{"name":"A","value":"AA","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"},{"name":"B","value":"BB","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}]}`) + }) + + opts := &ListOptions{Page: 2, PerPage: 2} + ctx := context.Background() + variables, _, err := client.Actions.ListEnvVariables(ctx, 1, "e", opts) + if err != nil { + t.Errorf("Actions.ListEnvVariables returned error: %v", err) + } + + want := &Variables{ + TotalCount: 4, + Variables: []*Variable{ + {Name: "A", Value: "AA", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, + {Name: "B", Value: "BB", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, + }, + } + if !cmp.Equal(variables, want) { + t.Errorf("Actions.ListEnvVariables returned %+v, want %+v", variables, want) + } + + const methodName = "ListEnvVariables" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.ListEnvVariables(ctx, 0.0, "\n", opts) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.ListEnvVariables(ctx, 1, "e", opts) + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_GetEnvVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repositories/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"name":"variable","value":"VAR","created_at":"2019-01-02T15:04:05Z","updated_at":"2020-01-02T15:04:05Z"}`) + }) + + ctx := context.Background() + variable, _, err := client.Actions.GetEnvVariable(ctx, 1, "e", "variable") + if err != nil { + t.Errorf("Actions.GetEnvVariable returned error: %v", err) + } + + want := &Variable{ + Name: "variable", + Value: "VAR", + CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, + UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, + } + if !cmp.Equal(variable, want) { + t.Errorf("Actions.GetEnvVariable returned %+v, want %+v", variable, want) + } + + const methodName = "GetEnvVariable" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Actions.GetEnvVariable(ctx, 0.0, "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Actions.GetEnvVariable(ctx, 1, "e", "variable") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} + +func TestActionsService_CreateEnvVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repositories/1/environments/e/variables", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "POST") + testHeader(t, r, "Content-Type", "application/json") + testBody(t, r, `{"name":"variable","value":"VAR","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"}`+"\n") + w.WriteHeader(http.StatusCreated) + }) + + input := &Variable{ + Name: "variable", + Value: "VAR", + } + ctx := context.Background() + _, err := client.Actions.CreateEnvVariable(ctx, 1, "e", input) + if err != nil { + t.Errorf("Actions.CreateEnvVariable returned error: %v", err) + } + + const methodName = "CreateEnvVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.CreateEnvVariable(ctx, 0.0, "\n", input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.CreateEnvVariable(ctx, 1, "e", input) + }) +} + +func TestActionsService_UpdateEnvVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repositories/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "PATCH") + testHeader(t, r, "Content-Type", "application/json") + testBody(t, r, `{"name":"variable","value":"VAR","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"}`+"\n") + w.WriteHeader(http.StatusNoContent) + }) + + input := &Variable{ + Name: "variable", + Value: "VAR", + } + ctx := context.Background() + _, err := client.Actions.UpdateEnvVariable(ctx, 1, "e", input) + if err != nil { + t.Errorf("Actions.UpdateEnvVariable returned error: %v", err) + } + + const methodName = "UpdateEnvVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.UpdateEnvVariable(ctx, 0.0, "\n", input) + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.UpdateEnvVariable(ctx, 1, "e", input) + }) +} + +func TestActionsService_DeleteEnvVariable(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/repositories/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "DELETE") + }) + + ctx := context.Background() + _, err := client.Actions.DeleteEnvVariable(ctx, 1, "e", "variable") + if err != nil { + t.Errorf("Actions.DeleteEnvVariable returned error: %v", err) + } + + const methodName = "DeleteEnvVariable" + testBadOptions(t, methodName, func() (err error) { + _, err = client.Actions.DeleteEnvVariable(ctx, 0.0, "\n", "\n") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + return client.Actions.DeleteEnvVariable(ctx, 1, "r", "variable") + }) +} From 9a5baf7b6462c2b460d7c380f8c7b27380640f46 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Tue, 31 Jan 2023 13:53:04 +0100 Subject: [PATCH 2/6] update(github/actions_variables_test): fixed typo. Co-authored-by: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> --- github/actions_variables_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/actions_variables_test.go b/github/actions_variables_test.go index 61ed297e29d..5bc4c7c77bb 100644 --- a/github/actions_variables_test.go +++ b/github/actions_variables_test.go @@ -176,7 +176,7 @@ func TestActionsService_DeleteRepoVariable(t *testing.T) { t.Errorf("Actions.( returned error: %v", err) } - const methodName = "(" + const methodName = "DeleteRepoVariable" testBadOptions(t, methodName, func() (err error) { _, err = client.Actions.DeleteRepoVariable(ctx, "\n", "\n", "\n") return err From 15e2eabfefe79307d5bf2de011f957a2ff470750 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Tue, 31 Jan 2023 14:23:27 +0100 Subject: [PATCH 3/6] chore(github): renamed `Variable` and `Variables` to `ActionVariable` and `ActionVariables`. Signed-off-by: Federico Di Pierro --- github/actions_variables.go | 48 ++++++++++++++++---------------- github/actions_variables_test.go | 30 ++++++++++---------- 2 files changed, 39 insertions(+), 39 deletions(-) diff --git a/github/actions_variables.go b/github/actions_variables.go index db9c5b2a7ed..75b2b62e95e 100644 --- a/github/actions_variables.go +++ b/github/actions_variables.go @@ -10,8 +10,8 @@ import ( "fmt" ) -// Variable represents a repository action variable. -type Variable struct { +// ActionVariable represents a repository action variable. +type ActionVariable struct { Name string `json:"name"` Value string `json:"value"` CreatedAt Timestamp `json:"created_at"` @@ -23,13 +23,13 @@ type Variable struct { SelectedRepositoryIDs SelectedRepoIDs `json:"selected_repository_ids,omitempty"` } -// Variables represents one item from the ListVariables response. -type Variables struct { - TotalCount int `json:"total_count"` - Variables []*Variable `json:"variables"` +// ActionsVariables represents one item from the ListVariables response. +type ActionsVariables struct { + TotalCount int `json:"total_count"` + Variables []*ActionVariable `json:"variables"` } -func (s *ActionsService) listVariables(ctx context.Context, url string, opts *ListOptions) (*Variables, *Response, error) { +func (s *ActionsService) listVariables(ctx context.Context, url string, opts *ListOptions) (*ActionsVariables, *Response, error) { u, err := addOptions(url, opts) if err != nil { return nil, nil, err @@ -40,7 +40,7 @@ func (s *ActionsService) listVariables(ctx context.Context, url string, opts *Li return nil, nil, err } - variables := new(Variables) + variables := new(ActionsVariables) resp, err := s.client.Do(ctx, req, &variables) if err != nil { return nil, resp, err @@ -52,7 +52,7 @@ func (s *ActionsService) listVariables(ctx context.Context, url string, opts *Li // ListRepoVariables lists all variables available in a repository. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#list-repository-variables -func (s *ActionsService) ListRepoVariables(ctx context.Context, owner, repo string, opts *ListOptions) (*Variables, *Response, error) { +func (s *ActionsService) ListRepoVariables(ctx context.Context, owner, repo string, opts *ListOptions) (*ActionsVariables, *Response, error) { url := fmt.Sprintf("repos/%v/%v/actions/variables", owner, repo) return s.listVariables(ctx, url, opts) } @@ -60,7 +60,7 @@ func (s *ActionsService) ListRepoVariables(ctx context.Context, owner, repo stri // ListOrgVariables lists all variables available in an organization. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#list-organization-variables -func (s *ActionsService) ListOrgVariables(ctx context.Context, org string, opts *ListOptions) (*Variables, *Response, error) { +func (s *ActionsService) ListOrgVariables(ctx context.Context, org string, opts *ListOptions) (*ActionsVariables, *Response, error) { url := fmt.Sprintf("orgs/%v/actions/variables", org) return s.listVariables(ctx, url, opts) } @@ -68,18 +68,18 @@ func (s *ActionsService) ListOrgVariables(ctx context.Context, org string, opts // ListEnvVariables lists all variables available in an environment. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#list-environment-variables -func (s *ActionsService) ListEnvVariables(ctx context.Context, repoID int, env string, opts *ListOptions) (*Variables, *Response, error) { +func (s *ActionsService) ListEnvVariables(ctx context.Context, repoID int, env string, opts *ListOptions) (*ActionsVariables, *Response, error) { url := fmt.Sprintf("repositories/%v/environments/%v/variables", repoID, env) return s.listVariables(ctx, url, opts) } -func (s *ActionsService) getVariable(ctx context.Context, url string) (*Variable, *Response, error) { +func (s *ActionsService) getVariable(ctx context.Context, url string) (*ActionVariable, *Response, error) { req, err := s.client.NewRequest("GET", url, nil) if err != nil { return nil, nil, err } - variable := new(Variable) + variable := new(ActionVariable) resp, err := s.client.Do(ctx, req, variable) if err != nil { return nil, resp, err @@ -91,7 +91,7 @@ func (s *ActionsService) getVariable(ctx context.Context, url string) (*Variable // GetRepoVariable gets a single repository variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#get-a-repository-variable -func (s *ActionsService) GetRepoVariable(ctx context.Context, owner, repo, name string) (*Variable, *Response, error) { +func (s *ActionsService) GetRepoVariable(ctx context.Context, owner, repo, name string) (*ActionVariable, *Response, error) { url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, name) return s.getVariable(ctx, url) } @@ -99,7 +99,7 @@ func (s *ActionsService) GetRepoVariable(ctx context.Context, owner, repo, name // GetOrgVariable gets a single organization variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#get-an-organization-variable -func (s *ActionsService) GetOrgVariable(ctx context.Context, org, name string) (*Variable, *Response, error) { +func (s *ActionsService) GetOrgVariable(ctx context.Context, org, name string) (*ActionVariable, *Response, error) { url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, name) return s.getVariable(ctx, url) } @@ -107,12 +107,12 @@ func (s *ActionsService) GetOrgVariable(ctx context.Context, org, name string) ( // GetEnvVariable gets a single environment variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#get-an-environment-variable -func (s *ActionsService) GetEnvVariable(ctx context.Context, repoID int, env, variableName string) (*Variable, *Response, error) { +func (s *ActionsService) GetEnvVariable(ctx context.Context, repoID int, env, variableName string) (*ActionVariable, *Response, error) { url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variableName) return s.getVariable(ctx, url) } -func (s *ActionsService) postVariable(ctx context.Context, url string, variable *Variable) (*Response, error) { +func (s *ActionsService) postVariable(ctx context.Context, url string, variable *ActionVariable) (*Response, error) { req, err := s.client.NewRequest("POST", url, variable) if err != nil { return nil, err @@ -123,7 +123,7 @@ func (s *ActionsService) postVariable(ctx context.Context, url string, variable // CreateRepoVariable creates a repository variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-a-repository-variable -func (s *ActionsService) CreateRepoVariable(ctx context.Context, owner, repo string, variable *Variable) (*Response, error) { +func (s *ActionsService) CreateRepoVariable(ctx context.Context, owner, repo string, variable *ActionVariable) (*Response, error) { url := fmt.Sprintf("repos/%v/%v/actions/variables", owner, repo) return s.postVariable(ctx, url, variable) } @@ -131,7 +131,7 @@ func (s *ActionsService) CreateRepoVariable(ctx context.Context, owner, repo str // CreateOrgVariable creates an organization variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-an-organization-variable -func (s *ActionsService) CreateOrgVariable(ctx context.Context, org string, variable *Variable) (*Response, error) { +func (s *ActionsService) CreateOrgVariable(ctx context.Context, org string, variable *ActionVariable) (*Response, error) { url := fmt.Sprintf("orgs/%v/actions/variables", org) return s.postVariable(ctx, url, variable) } @@ -139,12 +139,12 @@ func (s *ActionsService) CreateOrgVariable(ctx context.Context, org string, vari // CreateEnvVariable creates an environment variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-an-environment-variable -func (s *ActionsService) CreateEnvVariable(ctx context.Context, repoID int, env string, variable *Variable) (*Response, error) { +func (s *ActionsService) CreateEnvVariable(ctx context.Context, repoID int, env string, variable *ActionVariable) (*Response, error) { url := fmt.Sprintf("repositories/%v/environments/%v/variables", repoID, env) return s.postVariable(ctx, url, variable) } -func (s *ActionsService) patchVariable(ctx context.Context, url string, variable *Variable) (*Response, error) { +func (s *ActionsService) patchVariable(ctx context.Context, url string, variable *ActionVariable) (*Response, error) { req, err := s.client.NewRequest("PATCH", url, variable) if err != nil { return nil, err @@ -155,7 +155,7 @@ func (s *ActionsService) patchVariable(ctx context.Context, url string, variable // UpdateRepoVariable updates a repository variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#update-a-repository-variable -func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo string, variable *Variable) (*Response, error) { +func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo string, variable *ActionVariable) (*Response, error) { url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, variable.Name) return s.patchVariable(ctx, url, variable) } @@ -163,7 +163,7 @@ func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo str // UpdateOrgVariable updates an organization variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#update-an-organization-variable -func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, variable *Variable) (*Response, error) { +func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, variable *ActionVariable) (*Response, error) { url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, variable.Name) return s.patchVariable(ctx, url, variable) } @@ -171,7 +171,7 @@ func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, vari // UpdateEnvVariable updates an environment variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-an-environment-variable -func (s *ActionsService) UpdateEnvVariable(ctx context.Context, repoID int, env string, variable *Variable) (*Response, error) { +func (s *ActionsService) UpdateEnvVariable(ctx context.Context, repoID int, env string, variable *ActionVariable) (*Response, error) { url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variable.Name) return s.patchVariable(ctx, url, variable) } diff --git a/github/actions_variables_test.go b/github/actions_variables_test.go index 5bc4c7c77bb..82ef5f2c49b 100644 --- a/github/actions_variables_test.go +++ b/github/actions_variables_test.go @@ -32,9 +32,9 @@ func TestActionsService_ListRepoVariables(t *testing.T) { t.Errorf("Actions.ListRepoVariables returned error: %v", err) } - want := &Variables{ + want := &ActionsVariables{ TotalCount: 4, - Variables: []*Variable{ + Variables: []*ActionVariable{ {Name: "A", Value: "AA", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, {Name: "B", Value: "BB", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, }, @@ -73,7 +73,7 @@ func TestActionsService_GetRepoVariable(t *testing.T) { t.Errorf("Actions.GetRepoVariable returned error: %v", err) } - want := &Variable{ + want := &ActionVariable{ Name: "NAME", Value: "VALUE", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, @@ -109,7 +109,7 @@ func TestActionsService_CreateRepoVariable(t *testing.T) { w.WriteHeader(http.StatusCreated) }) - input := &Variable{ + input := &ActionVariable{ Name: "NAME", Value: "VALUE", } @@ -141,7 +141,7 @@ func TestActionsService_UpdateRepoVariable(t *testing.T) { w.WriteHeader(http.StatusNoContent) }) - input := &Variable{ + input := &ActionVariable{ Name: "NAME", Value: "VALUE", } @@ -204,9 +204,9 @@ func TestActionsService_ListOrgVariables(t *testing.T) { t.Errorf("Actions.ListOrgVariables returned error: %v", err) } - want := &Variables{ + want := &ActionsVariables{ TotalCount: 3, - Variables: []*Variable{ + Variables: []*ActionVariable{ {Name: "A", Value: "AA", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "private"}, {Name: "B", Value: "BB", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "all"}, {Name: "C", Value: "CC", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "selected", SelectedRepositoriesURL: "https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"}, @@ -246,7 +246,7 @@ func TestActionsService_GetOrgVariable(t *testing.T) { t.Errorf("Actions.GetOrgVariable returned error: %v", err) } - want := &Variable{ + want := &ActionVariable{ Name: "NAME", Value: "VALUE", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, @@ -284,7 +284,7 @@ func TestActionsService_CreateOrgVariable(t *testing.T) { w.WriteHeader(http.StatusCreated) }) - input := &Variable{ + input := &ActionVariable{ Name: "NAME", Value: "VALUE", Visibility: "selected", @@ -318,7 +318,7 @@ func TestActionsService_UpdateOrgVariable(t *testing.T) { w.WriteHeader(http.StatusNoContent) }) - input := &Variable{ + input := &ActionVariable{ Name: "NAME", Value: "VALUE", Visibility: "selected", @@ -503,9 +503,9 @@ func TestActionsService_ListEnvVariables(t *testing.T) { t.Errorf("Actions.ListEnvVariables returned error: %v", err) } - want := &Variables{ + want := &ActionsVariables{ TotalCount: 4, - Variables: []*Variable{ + Variables: []*ActionVariable{ {Name: "A", Value: "AA", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, {Name: "B", Value: "BB", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, }, @@ -544,7 +544,7 @@ func TestActionsService_GetEnvVariable(t *testing.T) { t.Errorf("Actions.GetEnvVariable returned error: %v", err) } - want := &Variable{ + want := &ActionVariable{ Name: "variable", Value: "VAR", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, @@ -580,7 +580,7 @@ func TestActionsService_CreateEnvVariable(t *testing.T) { w.WriteHeader(http.StatusCreated) }) - input := &Variable{ + input := &ActionVariable{ Name: "variable", Value: "VAR", } @@ -612,7 +612,7 @@ func TestActionsService_UpdateEnvVariable(t *testing.T) { w.WriteHeader(http.StatusNoContent) }) - input := &Variable{ + input := &ActionVariable{ Name: "variable", Value: "VAR", } From 75b621a053c76e99218d1ada335a92f84452dc50 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Tue, 31 Jan 2023 14:30:01 +0100 Subject: [PATCH 4/6] chore(github): made multiple `ActionsVariable` fields optional. Signed-off-by: Federico Di Pierro --- github/actions_variables.go | 14 ++++----- github/actions_variables_test.go | 50 ++++++++++++++++---------------- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/github/actions_variables.go b/github/actions_variables.go index 75b2b62e95e..456eca99864 100644 --- a/github/actions_variables.go +++ b/github/actions_variables.go @@ -12,15 +12,15 @@ import ( // ActionVariable represents a repository action variable. type ActionVariable struct { - Name string `json:"name"` - Value string `json:"value"` - CreatedAt Timestamp `json:"created_at"` - UpdatedAt Timestamp `json:"updated_at"` - Visibility string `json:"visibility,omitempty"` + Name string `json:"name"` + Value string `json:"value"` + CreatedAt *Timestamp `json:"created_at,omitempty"` + UpdatedAt *Timestamp `json:"updated_at,omitempty"` + Visibility *string `json:"visibility,omitempty"` // Used by ListOrgVariables and GetOrgVariables - SelectedRepositoriesURL string `json:"selected_repositories_url,omitempty"` + SelectedRepositoriesURL *string `json:"selected_repositories_url,omitempty"` // Used by UpdateOrgVariable and CreateOrgVariable - SelectedRepositoryIDs SelectedRepoIDs `json:"selected_repository_ids,omitempty"` + SelectedRepositoryIDs *SelectedRepoIDs `json:"selected_repository_ids,omitempty"` } // ActionsVariables represents one item from the ListVariables response. diff --git a/github/actions_variables_test.go b/github/actions_variables_test.go index 82ef5f2c49b..7b92d3842ec 100644 --- a/github/actions_variables_test.go +++ b/github/actions_variables_test.go @@ -35,8 +35,8 @@ func TestActionsService_ListRepoVariables(t *testing.T) { want := &ActionsVariables{ TotalCount: 4, Variables: []*ActionVariable{ - {Name: "A", Value: "AA", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, - {Name: "B", Value: "BB", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, + {Name: "A", Value: "AA", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, + {Name: "B", Value: "BB", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, }, } if !cmp.Equal(variables, want) { @@ -76,8 +76,8 @@ func TestActionsService_GetRepoVariable(t *testing.T) { want := &ActionVariable{ Name: "NAME", Value: "VALUE", - CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, - UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, + CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, + UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, } if !cmp.Equal(variable, want) { t.Errorf("Actions.GetRepoVariable returned %+v, want %+v", variable, want) @@ -105,7 +105,7 @@ func TestActionsService_CreateRepoVariable(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"NAME","value":"VALUE","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"}`+"\n") + testBody(t, r, `{"name":"NAME","value":"VALUE"}`+"\n") w.WriteHeader(http.StatusCreated) }) @@ -137,7 +137,7 @@ func TestActionsService_UpdateRepoVariable(t *testing.T) { mux.HandleFunc("/repos/o/r/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"NAME","value":"VALUE","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"}`+"\n") + testBody(t, r, `{"name":"NAME","value":"VALUE"}`+"\n") w.WriteHeader(http.StatusNoContent) }) @@ -207,9 +207,9 @@ func TestActionsService_ListOrgVariables(t *testing.T) { want := &ActionsVariables{ TotalCount: 3, Variables: []*ActionVariable{ - {Name: "A", Value: "AA", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "private"}, - {Name: "B", Value: "BB", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "all"}, - {Name: "C", Value: "CC", CreatedAt: Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: "selected", SelectedRepositoriesURL: "https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"}, + {Name: "A", Value: "AA", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: String("private")}, + {Name: "B", Value: "BB", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: String("all")}, + {Name: "C", Value: "CC", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: String("selected"), SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories")}, }, } if !cmp.Equal(variables, want) { @@ -249,10 +249,10 @@ func TestActionsService_GetOrgVariable(t *testing.T) { want := &ActionVariable{ Name: "NAME", Value: "VALUE", - CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, - UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, - Visibility: "selected", - SelectedRepositoriesURL: "https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories", + CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, + UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, + Visibility: String("selected"), + SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories"), } if !cmp.Equal(variable, want) { t.Errorf("Actions.GetOrgVariable returned %+v, want %+v", variable, want) @@ -280,15 +280,15 @@ func TestActionsService_CreateOrgVariable(t *testing.T) { mux.HandleFunc("/orgs/o/actions/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"NAME","value":"VALUE","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") + testBody(t, r, `{"name":"NAME","value":"VALUE","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") w.WriteHeader(http.StatusCreated) }) input := &ActionVariable{ Name: "NAME", Value: "VALUE", - Visibility: "selected", - SelectedRepositoryIDs: SelectedRepoIDs{1296269, 1269280}, + Visibility: String("selected"), + SelectedRepositoryIDs: &SelectedRepoIDs{1296269, 1269280}, } ctx := context.Background() _, err := client.Actions.CreateOrgVariable(ctx, "o", input) @@ -314,15 +314,15 @@ func TestActionsService_UpdateOrgVariable(t *testing.T) { mux.HandleFunc("/orgs/o/actions/variables/NAME", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"NAME","value":"VALUE","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") + testBody(t, r, `{"name":"NAME","value":"VALUE","visibility":"selected","selected_repository_ids":[1296269,1269280]}`+"\n") w.WriteHeader(http.StatusNoContent) }) input := &ActionVariable{ Name: "NAME", Value: "VALUE", - Visibility: "selected", - SelectedRepositoryIDs: SelectedRepoIDs{1296269, 1269280}, + Visibility: String("selected"), + SelectedRepositoryIDs: &SelectedRepoIDs{1296269, 1269280}, } ctx := context.Background() _, err := client.Actions.UpdateOrgVariable(ctx, "o", input) @@ -506,8 +506,8 @@ func TestActionsService_ListEnvVariables(t *testing.T) { want := &ActionsVariables{ TotalCount: 4, Variables: []*ActionVariable{ - {Name: "A", Value: "AA", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, - {Name: "B", Value: "BB", CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, + {Name: "A", Value: "AA", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, + {Name: "B", Value: "BB", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, }, } if !cmp.Equal(variables, want) { @@ -547,8 +547,8 @@ func TestActionsService_GetEnvVariable(t *testing.T) { want := &ActionVariable{ Name: "variable", Value: "VAR", - CreatedAt: Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, - UpdatedAt: Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, + CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, + UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}, } if !cmp.Equal(variable, want) { t.Errorf("Actions.GetEnvVariable returned %+v, want %+v", variable, want) @@ -576,7 +576,7 @@ func TestActionsService_CreateEnvVariable(t *testing.T) { mux.HandleFunc("/repositories/1/environments/e/variables", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "POST") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"variable","value":"VAR","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"}`+"\n") + testBody(t, r, `{"name":"variable","value":"VAR"}`+"\n") w.WriteHeader(http.StatusCreated) }) @@ -608,7 +608,7 @@ func TestActionsService_UpdateEnvVariable(t *testing.T) { mux.HandleFunc("/repositories/1/environments/e/variables/variable", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, "PATCH") testHeader(t, r, "Content-Type", "application/json") - testBody(t, r, `{"name":"variable","value":"VAR","created_at":"0001-01-01T00:00:00Z","updated_at":"0001-01-01T00:00:00Z"}`+"\n") + testBody(t, r, `{"name":"variable","value":"VAR"}`+"\n") w.WriteHeader(http.StatusNoContent) }) From 59098a1a4cc3c086b074b52605ff5cd704728a94 Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Tue, 31 Jan 2023 14:31:36 +0100 Subject: [PATCH 5/6] chore(github): renamed `ActionVariable` to `ActionsVariables`. Signed-off-by: Federico Di Pierro --- github/actions_variables.go | 34 ++++++++++++++++---------------- github/actions_variables_test.go | 24 +++++++++++----------- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/github/actions_variables.go b/github/actions_variables.go index 456eca99864..29445edd042 100644 --- a/github/actions_variables.go +++ b/github/actions_variables.go @@ -10,8 +10,8 @@ import ( "fmt" ) -// ActionVariable represents a repository action variable. -type ActionVariable struct { +// ActionsVariable represents a repository action variable. +type ActionsVariable struct { Name string `json:"name"` Value string `json:"value"` CreatedAt *Timestamp `json:"created_at,omitempty"` @@ -25,8 +25,8 @@ type ActionVariable struct { // ActionsVariables represents one item from the ListVariables response. type ActionsVariables struct { - TotalCount int `json:"total_count"` - Variables []*ActionVariable `json:"variables"` + TotalCount int `json:"total_count"` + Variables []*ActionsVariable `json:"variables"` } func (s *ActionsService) listVariables(ctx context.Context, url string, opts *ListOptions) (*ActionsVariables, *Response, error) { @@ -73,13 +73,13 @@ func (s *ActionsService) ListEnvVariables(ctx context.Context, repoID int, env s return s.listVariables(ctx, url, opts) } -func (s *ActionsService) getVariable(ctx context.Context, url string) (*ActionVariable, *Response, error) { +func (s *ActionsService) getVariable(ctx context.Context, url string) (*ActionsVariable, *Response, error) { req, err := s.client.NewRequest("GET", url, nil) if err != nil { return nil, nil, err } - variable := new(ActionVariable) + variable := new(ActionsVariable) resp, err := s.client.Do(ctx, req, variable) if err != nil { return nil, resp, err @@ -91,7 +91,7 @@ func (s *ActionsService) getVariable(ctx context.Context, url string) (*ActionVa // GetRepoVariable gets a single repository variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#get-a-repository-variable -func (s *ActionsService) GetRepoVariable(ctx context.Context, owner, repo, name string) (*ActionVariable, *Response, error) { +func (s *ActionsService) GetRepoVariable(ctx context.Context, owner, repo, name string) (*ActionsVariable, *Response, error) { url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, name) return s.getVariable(ctx, url) } @@ -99,7 +99,7 @@ func (s *ActionsService) GetRepoVariable(ctx context.Context, owner, repo, name // GetOrgVariable gets a single organization variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#get-an-organization-variable -func (s *ActionsService) GetOrgVariable(ctx context.Context, org, name string) (*ActionVariable, *Response, error) { +func (s *ActionsService) GetOrgVariable(ctx context.Context, org, name string) (*ActionsVariable, *Response, error) { url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, name) return s.getVariable(ctx, url) } @@ -107,12 +107,12 @@ func (s *ActionsService) GetOrgVariable(ctx context.Context, org, name string) ( // GetEnvVariable gets a single environment variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#get-an-environment-variable -func (s *ActionsService) GetEnvVariable(ctx context.Context, repoID int, env, variableName string) (*ActionVariable, *Response, error) { +func (s *ActionsService) GetEnvVariable(ctx context.Context, repoID int, env, variableName string) (*ActionsVariable, *Response, error) { url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variableName) return s.getVariable(ctx, url) } -func (s *ActionsService) postVariable(ctx context.Context, url string, variable *ActionVariable) (*Response, error) { +func (s *ActionsService) postVariable(ctx context.Context, url string, variable *ActionsVariable) (*Response, error) { req, err := s.client.NewRequest("POST", url, variable) if err != nil { return nil, err @@ -123,7 +123,7 @@ func (s *ActionsService) postVariable(ctx context.Context, url string, variable // CreateRepoVariable creates a repository variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-a-repository-variable -func (s *ActionsService) CreateRepoVariable(ctx context.Context, owner, repo string, variable *ActionVariable) (*Response, error) { +func (s *ActionsService) CreateRepoVariable(ctx context.Context, owner, repo string, variable *ActionsVariable) (*Response, error) { url := fmt.Sprintf("repos/%v/%v/actions/variables", owner, repo) return s.postVariable(ctx, url, variable) } @@ -131,7 +131,7 @@ func (s *ActionsService) CreateRepoVariable(ctx context.Context, owner, repo str // CreateOrgVariable creates an organization variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-an-organization-variable -func (s *ActionsService) CreateOrgVariable(ctx context.Context, org string, variable *ActionVariable) (*Response, error) { +func (s *ActionsService) CreateOrgVariable(ctx context.Context, org string, variable *ActionsVariable) (*Response, error) { url := fmt.Sprintf("orgs/%v/actions/variables", org) return s.postVariable(ctx, url, variable) } @@ -139,12 +139,12 @@ func (s *ActionsService) CreateOrgVariable(ctx context.Context, org string, vari // CreateEnvVariable creates an environment variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-an-environment-variable -func (s *ActionsService) CreateEnvVariable(ctx context.Context, repoID int, env string, variable *ActionVariable) (*Response, error) { +func (s *ActionsService) CreateEnvVariable(ctx context.Context, repoID int, env string, variable *ActionsVariable) (*Response, error) { url := fmt.Sprintf("repositories/%v/environments/%v/variables", repoID, env) return s.postVariable(ctx, url, variable) } -func (s *ActionsService) patchVariable(ctx context.Context, url string, variable *ActionVariable) (*Response, error) { +func (s *ActionsService) patchVariable(ctx context.Context, url string, variable *ActionsVariable) (*Response, error) { req, err := s.client.NewRequest("PATCH", url, variable) if err != nil { return nil, err @@ -155,7 +155,7 @@ func (s *ActionsService) patchVariable(ctx context.Context, url string, variable // UpdateRepoVariable updates a repository variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#update-a-repository-variable -func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo string, variable *ActionVariable) (*Response, error) { +func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo string, variable *ActionsVariable) (*Response, error) { url := fmt.Sprintf("repos/%v/%v/actions/variables/%v", owner, repo, variable.Name) return s.patchVariable(ctx, url, variable) } @@ -163,7 +163,7 @@ func (s *ActionsService) UpdateRepoVariable(ctx context.Context, owner, repo str // UpdateOrgVariable updates an organization variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#update-an-organization-variable -func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, variable *ActionVariable) (*Response, error) { +func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, variable *ActionsVariable) (*Response, error) { url := fmt.Sprintf("orgs/%v/actions/variables/%v", org, variable.Name) return s.patchVariable(ctx, url, variable) } @@ -171,7 +171,7 @@ func (s *ActionsService) UpdateOrgVariable(ctx context.Context, org string, vari // UpdateEnvVariable updates an environment variable. // // GitHub API docs: https://docs.github.com/en/rest/actions/variables#create-an-environment-variable -func (s *ActionsService) UpdateEnvVariable(ctx context.Context, repoID int, env string, variable *ActionVariable) (*Response, error) { +func (s *ActionsService) UpdateEnvVariable(ctx context.Context, repoID int, env string, variable *ActionsVariable) (*Response, error) { url := fmt.Sprintf("repositories/%v/environments/%v/variables/%v", repoID, env, variable.Name) return s.patchVariable(ctx, url, variable) } diff --git a/github/actions_variables_test.go b/github/actions_variables_test.go index 7b92d3842ec..646da92b1e8 100644 --- a/github/actions_variables_test.go +++ b/github/actions_variables_test.go @@ -34,7 +34,7 @@ func TestActionsService_ListRepoVariables(t *testing.T) { want := &ActionsVariables{ TotalCount: 4, - Variables: []*ActionVariable{ + Variables: []*ActionsVariable{ {Name: "A", Value: "AA", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, {Name: "B", Value: "BB", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, }, @@ -73,7 +73,7 @@ func TestActionsService_GetRepoVariable(t *testing.T) { t.Errorf("Actions.GetRepoVariable returned error: %v", err) } - want := &ActionVariable{ + want := &ActionsVariable{ Name: "NAME", Value: "VALUE", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, @@ -109,7 +109,7 @@ func TestActionsService_CreateRepoVariable(t *testing.T) { w.WriteHeader(http.StatusCreated) }) - input := &ActionVariable{ + input := &ActionsVariable{ Name: "NAME", Value: "VALUE", } @@ -141,7 +141,7 @@ func TestActionsService_UpdateRepoVariable(t *testing.T) { w.WriteHeader(http.StatusNoContent) }) - input := &ActionVariable{ + input := &ActionsVariable{ Name: "NAME", Value: "VALUE", } @@ -206,7 +206,7 @@ func TestActionsService_ListOrgVariables(t *testing.T) { want := &ActionsVariables{ TotalCount: 3, - Variables: []*ActionVariable{ + Variables: []*ActionsVariable{ {Name: "A", Value: "AA", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: String("private")}, {Name: "B", Value: "BB", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: String("all")}, {Name: "C", Value: "CC", CreatedAt: &Timestamp{time.Date(2019, time.August, 10, 14, 59, 22, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 10, 14, 59, 22, 0, time.UTC)}, Visibility: String("selected"), SelectedRepositoriesURL: String("https://api.github.com/orgs/octo-org/actions/variables/VAR/repositories")}, @@ -246,7 +246,7 @@ func TestActionsService_GetOrgVariable(t *testing.T) { t.Errorf("Actions.GetOrgVariable returned error: %v", err) } - want := &ActionVariable{ + want := &ActionsVariable{ Name: "NAME", Value: "VALUE", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, @@ -284,7 +284,7 @@ func TestActionsService_CreateOrgVariable(t *testing.T) { w.WriteHeader(http.StatusCreated) }) - input := &ActionVariable{ + input := &ActionsVariable{ Name: "NAME", Value: "VALUE", Visibility: String("selected"), @@ -318,7 +318,7 @@ func TestActionsService_UpdateOrgVariable(t *testing.T) { w.WriteHeader(http.StatusNoContent) }) - input := &ActionVariable{ + input := &ActionsVariable{ Name: "NAME", Value: "VALUE", Visibility: String("selected"), @@ -505,7 +505,7 @@ func TestActionsService_ListEnvVariables(t *testing.T) { want := &ActionsVariables{ TotalCount: 4, - Variables: []*ActionVariable{ + Variables: []*ActionsVariable{ {Name: "A", Value: "AA", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, {Name: "B", Value: "BB", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, UpdatedAt: &Timestamp{time.Date(2020, time.January, 02, 15, 04, 05, 0, time.UTC)}}, }, @@ -544,7 +544,7 @@ func TestActionsService_GetEnvVariable(t *testing.T) { t.Errorf("Actions.GetEnvVariable returned error: %v", err) } - want := &ActionVariable{ + want := &ActionsVariable{ Name: "variable", Value: "VAR", CreatedAt: &Timestamp{time.Date(2019, time.January, 02, 15, 04, 05, 0, time.UTC)}, @@ -580,7 +580,7 @@ func TestActionsService_CreateEnvVariable(t *testing.T) { w.WriteHeader(http.StatusCreated) }) - input := &ActionVariable{ + input := &ActionsVariable{ Name: "variable", Value: "VAR", } @@ -612,7 +612,7 @@ func TestActionsService_UpdateEnvVariable(t *testing.T) { w.WriteHeader(http.StatusNoContent) }) - input := &ActionVariable{ + input := &ActionsVariable{ Name: "variable", Value: "VAR", } From 329f749f05e0bc209273979614c12031f6d5bf1c Mon Sep 17 00:00:00 2001 From: Federico Di Pierro Date: Tue, 31 Jan 2023 14:32:04 +0100 Subject: [PATCH 6/6] chore(github): run `go generate`. Signed-off-by: Federico Di Pierro --- github/github-accessors.go | 40 ++++++++++++++++++++++++++++ github/github-accessors_test.go | 47 +++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/github/github-accessors.go b/github/github-accessors.go index e2d0d7f8357..3110d8132d8 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -174,6 +174,46 @@ func (a *ActionsPermissionsRepository) GetSelectedActionsURL() string { return *a.SelectedActionsURL } +// GetCreatedAt returns the CreatedAt field if it's non-nil, zero value otherwise. +func (a *ActionsVariable) GetCreatedAt() Timestamp { + if a == nil || a.CreatedAt == nil { + return Timestamp{} + } + return *a.CreatedAt +} + +// GetSelectedRepositoriesURL returns the SelectedRepositoriesURL field if it's non-nil, zero value otherwise. +func (a *ActionsVariable) GetSelectedRepositoriesURL() string { + if a == nil || a.SelectedRepositoriesURL == nil { + return "" + } + return *a.SelectedRepositoriesURL +} + +// GetSelectedRepositoryIDs returns the SelectedRepositoryIDs field. +func (a *ActionsVariable) GetSelectedRepositoryIDs() *SelectedRepoIDs { + if a == nil { + return nil + } + return a.SelectedRepositoryIDs +} + +// GetUpdatedAt returns the UpdatedAt field if it's non-nil, zero value otherwise. +func (a *ActionsVariable) GetUpdatedAt() Timestamp { + if a == nil || a.UpdatedAt == nil { + return Timestamp{} + } + return *a.UpdatedAt +} + +// GetVisibility returns the Visibility field if it's non-nil, zero value otherwise. +func (a *ActionsVariable) GetVisibility() string { + if a == nil || a.Visibility == nil { + return "" + } + return *a.Visibility +} + // GetFrom returns the From field if it's non-nil, zero value otherwise. func (a *AdminEnforcedChanges) GetFrom() bool { if a == nil || a.From == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 24e0a758264..9457df769ea 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -215,6 +215,53 @@ func TestActionsPermissionsRepository_GetSelectedActionsURL(tt *testing.T) { a.GetSelectedActionsURL() } +func TestActionsVariable_GetCreatedAt(tt *testing.T) { + var zeroValue Timestamp + a := &ActionsVariable{CreatedAt: &zeroValue} + a.GetCreatedAt() + a = &ActionsVariable{} + a.GetCreatedAt() + a = nil + a.GetCreatedAt() +} + +func TestActionsVariable_GetSelectedRepositoriesURL(tt *testing.T) { + var zeroValue string + a := &ActionsVariable{SelectedRepositoriesURL: &zeroValue} + a.GetSelectedRepositoriesURL() + a = &ActionsVariable{} + a.GetSelectedRepositoriesURL() + a = nil + a.GetSelectedRepositoriesURL() +} + +func TestActionsVariable_GetSelectedRepositoryIDs(tt *testing.T) { + a := &ActionsVariable{} + a.GetSelectedRepositoryIDs() + a = nil + a.GetSelectedRepositoryIDs() +} + +func TestActionsVariable_GetUpdatedAt(tt *testing.T) { + var zeroValue Timestamp + a := &ActionsVariable{UpdatedAt: &zeroValue} + a.GetUpdatedAt() + a = &ActionsVariable{} + a.GetUpdatedAt() + a = nil + a.GetUpdatedAt() +} + +func TestActionsVariable_GetVisibility(tt *testing.T) { + var zeroValue string + a := &ActionsVariable{Visibility: &zeroValue} + a.GetVisibility() + a = &ActionsVariable{} + a.GetVisibility() + a = nil + a.GetVisibility() +} + func TestAdminEnforcedChanges_GetFrom(tt *testing.T) { var zeroValue bool a := &AdminEnforcedChanges{From: &zeroValue}