Skip to content

Commit 1bb88f3

Browse files
committed
Merge branch 'master' into stringify-tests
2 parents c235e4a + 6cd5d41 commit 1bb88f3

16 files changed

+509
-117
lines changed

example/actionpermissions/main.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
// Copyright 2022 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+
// The actionpermissions command utilizes go-github as a cli tool for
7+
// changing GitHub Actions related permission settings for a repository.
8+
package main
9+
10+
import (
11+
"context"
12+
"flag"
13+
"fmt"
14+
"log"
15+
"os"
16+
17+
"github.com/google/go-github/v43/github"
18+
"golang.org/x/oauth2"
19+
)
20+
21+
var (
22+
name = flag.String("name", "", "repo to change Actions permissions.")
23+
owner = flag.String("owner", "", "owner of targeted repo.")
24+
)
25+
26+
func main() {
27+
flag.Parse()
28+
token := os.Getenv("GITHUB_AUTH_TOKEN")
29+
if token == "" {
30+
log.Fatal("Unauthorized: No token present")
31+
}
32+
if *name == "" {
33+
log.Fatal("No name: repo name must be given")
34+
}
35+
if *owner == "" {
36+
log.Fatal("No owner: owner of repo must be given")
37+
}
38+
ctx := context.Background()
39+
ts := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})
40+
tc := oauth2.NewClient(ctx, ts)
41+
client := github.NewClient(tc)
42+
43+
actionsPermissionsRepository, _, err := client.Repositories.GetActionsPermissions(ctx, *owner, *name)
44+
if err != nil {
45+
log.Fatal(err)
46+
}
47+
48+
fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String())
49+
50+
actionsPermissionsRepository = &github.ActionsPermissionsRepository{Enabled: github.Bool(true), AllowedActions: github.String("selected")}
51+
_, _, err = client.Repositories.EditActionsPermissions(ctx, *owner, *name, *actionsPermissionsRepository)
52+
if err != nil {
53+
log.Fatal(err)
54+
}
55+
56+
fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String())
57+
58+
actionsAllowed, _, err := client.Repositories.GetActionsAllowed(ctx, *owner, *name)
59+
if err != nil {
60+
log.Fatal(err)
61+
}
62+
63+
fmt.Printf("Current ActionsAllowed %s\n", actionsAllowed.String())
64+
65+
actionsAllowed = &github.ActionsAllowed{GithubOwnedAllowed: github.Bool(true), VerifiedAllowed: github.Bool(false), PatternsAllowed: []string{"a/b"}}
66+
_, _, err = client.Repositories.EditActionsAllowed(ctx, *owner, *name, *actionsAllowed)
67+
if err != nil {
68+
log.Fatal(err)
69+
}
70+
71+
fmt.Printf("Current ActionsAllowed %s\n", actionsAllowed.String())
72+
73+
actionsPermissionsRepository = &github.ActionsPermissionsRepository{Enabled: github.Bool(true), AllowedActions: github.String("all")}
74+
_, _, err = client.Repositories.EditActionsPermissions(ctx, *owner, *name, *actionsPermissionsRepository)
75+
if err != nil {
76+
log.Fatal(err)
77+
}
78+
79+
fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String())
80+
}

github/actions_artifacts.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -110,45 +110,20 @@ func (s *ActionsService) GetArtifact(ctx context.Context, owner, repo string, ar
110110
func (s *ActionsService) DownloadArtifact(ctx context.Context, owner, repo string, artifactID int64, followRedirects bool) (*url.URL, *Response, error) {
111111
u := fmt.Sprintf("repos/%v/%v/actions/artifacts/%v/zip", owner, repo, artifactID)
112112

113-
resp, err := s.getDownloadArtifactFromURL(ctx, u, followRedirects)
113+
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
114114
if err != nil {
115115
return nil, nil, err
116116
}
117+
defer resp.Body.Close()
117118

118119
if resp.StatusCode != http.StatusFound {
119120
return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
120121
}
122+
121123
parsedURL, err := url.Parse(resp.Header.Get("Location"))
122124
return parsedURL, newResponse(resp), nil
123125
}
124126

125-
func (s *ActionsService) getDownloadArtifactFromURL(ctx context.Context, u string, followRedirects bool) (*http.Response, error) {
126-
req, err := s.client.NewRequest("GET", u, nil)
127-
if err != nil {
128-
return nil, err
129-
}
130-
131-
var resp *http.Response
132-
// Use http.DefaultTransport if no custom Transport is configured
133-
req = withContext(ctx, req)
134-
if s.client.client.Transport == nil {
135-
resp, err = http.DefaultTransport.RoundTrip(req)
136-
} else {
137-
resp, err = s.client.client.Transport.RoundTrip(req)
138-
}
139-
if err != nil {
140-
return nil, err
141-
}
142-
resp.Body.Close()
143-
144-
// If redirect response is returned, follow it
145-
if followRedirects && resp.StatusCode == http.StatusMovedPermanently {
146-
u = resp.Header.Get("Location")
147-
resp, err = s.getDownloadArtifactFromURL(ctx, u, false)
148-
}
149-
return resp, err
150-
}
151-
152127
// DeleteArtifact deletes a workflow run artifact.
153128
//
154129
// GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/actions/#delete-an-artifact

github/actions_workflow_jobs.go

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -114,41 +114,16 @@ func (s *ActionsService) GetWorkflowJobByID(ctx context.Context, owner, repo str
114114
func (s *ActionsService) GetWorkflowJobLogs(ctx context.Context, owner, repo string, jobID int64, followRedirects bool) (*url.URL, *Response, error) {
115115
u := fmt.Sprintf("repos/%v/%v/actions/jobs/%v/logs", owner, repo, jobID)
116116

117-
resp, err := s.getWorkflowLogsFromURL(ctx, u, followRedirects)
117+
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
118118
if err != nil {
119119
return nil, nil, err
120120
}
121+
defer resp.Body.Close()
121122

122123
if resp.StatusCode != http.StatusFound {
123124
return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
124125
}
126+
125127
parsedURL, err := url.Parse(resp.Header.Get("Location"))
126128
return parsedURL, newResponse(resp), err
127129
}
128-
129-
func (s *ActionsService) getWorkflowLogsFromURL(ctx context.Context, u string, followRedirects bool) (*http.Response, error) {
130-
req, err := s.client.NewRequest("GET", u, nil)
131-
if err != nil {
132-
return nil, err
133-
}
134-
135-
var resp *http.Response
136-
// Use http.DefaultTransport if no custom Transport is configured
137-
req = withContext(ctx, req)
138-
if s.client.client.Transport == nil {
139-
resp, err = http.DefaultTransport.RoundTrip(req)
140-
} else {
141-
resp, err = s.client.client.Transport.RoundTrip(req)
142-
}
143-
if err != nil {
144-
return nil, err
145-
}
146-
resp.Body.Close()
147-
148-
// If redirect response is returned, follow it
149-
if followRedirects && resp.StatusCode == http.StatusMovedPermanently {
150-
u = resp.Header.Get("Location")
151-
resp, err = s.getWorkflowLogsFromURL(ctx, u, false)
152-
}
153-
return resp, err
154-
}

github/actions_workflow_runs.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,14 +231,16 @@ func (s *ActionsService) CancelWorkflowRunByID(ctx context.Context, owner, repo
231231
func (s *ActionsService) GetWorkflowRunLogs(ctx context.Context, owner, repo string, runID int64, followRedirects bool) (*url.URL, *Response, error) {
232232
u := fmt.Sprintf("repos/%v/%v/actions/runs/%v/logs", owner, repo, runID)
233233

234-
resp, err := s.getWorkflowLogsFromURL(ctx, u, followRedirects)
234+
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
235235
if err != nil {
236236
return nil, nil, err
237237
}
238+
defer resp.Body.Close()
238239

239240
if resp.StatusCode != http.StatusFound {
240241
return nil, newResponse(resp), fmt.Errorf("unexpected status code: %s", resp.Status)
241242
}
243+
242244
parsedURL, err := url.Parse(resp.Header.Get("Location"))
243245
return parsedURL, newResponse(resp), err
244246
}

github/github-accessors.go

Lines changed: 24 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: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-stringify_test.go

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

github/github.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,6 +1279,35 @@ func formatRateReset(d time.Duration) string {
12791279
return fmt.Sprintf("[rate reset in %v]", timeString)
12801280
}
12811281

1282+
// When using roundTripWithOptionalFollowRedirect, note that it
1283+
// is the responsibility of the caller to close the response body.
1284+
func (c *Client) roundTripWithOptionalFollowRedirect(ctx context.Context, u string, followRedirects bool) (*http.Response, error) {
1285+
req, err := c.NewRequest("GET", u, nil)
1286+
if err != nil {
1287+
return nil, err
1288+
}
1289+
1290+
var resp *http.Response
1291+
// Use http.DefaultTransport if no custom Transport is configured
1292+
req = withContext(ctx, req)
1293+
if c.client.Transport == nil {
1294+
resp, err = http.DefaultTransport.RoundTrip(req)
1295+
} else {
1296+
resp, err = c.client.Transport.RoundTrip(req)
1297+
}
1298+
if err != nil {
1299+
return nil, err
1300+
}
1301+
1302+
// If redirect response is returned, follow it
1303+
if followRedirects && resp.StatusCode == http.StatusMovedPermanently {
1304+
resp.Body.Close()
1305+
u = resp.Header.Get("Location")
1306+
resp, err = c.roundTripWithOptionalFollowRedirect(ctx, u, false)
1307+
}
1308+
return resp, err
1309+
}
1310+
12821311
// Bool is a helper routine that allocates a new bool value
12831312
// to store v and returns a pointer to it.
12841313
func Bool(v bool) *bool { return &v }

github/orgs_actions_allowed.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import (
1010
"fmt"
1111
)
1212

13-
// ActionsAllowed represents selected actions that are allowed in an organization.
13+
// ActionsAllowed represents selected actions that are allowed.
1414
//
15-
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#get-allowed-actions-for-an-organization
15+
// GitHub API docs: https://docs.github.com/en/rest/reference/actions#set-allowed-actions-and-workflows-for-an-organization--parameters
1616
type ActionsAllowed struct {
1717
GithubOwnedAllowed *bool `json:"github_owned_allowed,omitempty"`
1818
VerifiedAllowed *bool `json:"verified_allowed,omitempty"`

github/repos.go

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,7 +1061,7 @@ func (s *RepositoriesService) ListBranches(ctx context.Context, owner string, re
10611061
func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch string, followRedirects bool) (*Branch, *Response, error) {
10621062
u := fmt.Sprintf("repos/%v/%v/branches/%v", owner, repo, branch)
10631063

1064-
resp, err := s.getBranchFromURL(ctx, u, followRedirects)
1064+
resp, err := s.client.roundTripWithOptionalFollowRedirect(ctx, u, followRedirects)
10651065
if err != nil {
10661066
return nil, nil, err
10671067
}
@@ -1076,33 +1076,6 @@ func (s *RepositoriesService) GetBranch(ctx context.Context, owner, repo, branch
10761076
return b, newResponse(resp), err
10771077
}
10781078

1079-
func (s *RepositoriesService) getBranchFromURL(ctx context.Context, u string, followRedirects bool) (*http.Response, error) {
1080-
req, err := s.client.NewRequest("GET", u, nil)
1081-
if err != nil {
1082-
return nil, err
1083-
}
1084-
1085-
var resp *http.Response
1086-
// Use http.DefaultTransport if no custom Transport is configured
1087-
req = withContext(ctx, req)
1088-
if s.client.client.Transport == nil {
1089-
resp, err = http.DefaultTransport.RoundTrip(req)
1090-
} else {
1091-
resp, err = s.client.client.Transport.RoundTrip(req)
1092-
}
1093-
if err != nil {
1094-
return nil, err
1095-
}
1096-
1097-
// If redirect response is returned, follow it
1098-
if followRedirects && resp.StatusCode == http.StatusMovedPermanently {
1099-
resp.Body.Close()
1100-
u = resp.Header.Get("Location")
1101-
resp, err = s.getBranchFromURL(ctx, u, false)
1102-
}
1103-
return resp, err
1104-
}
1105-
11061079
// renameBranchRequest represents a request to rename a branch.
11071080
type renameBranchRequest struct {
11081081
NewName string `json:"new_name"`

0 commit comments

Comments
 (0)