-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathmain.go
77 lines (62 loc) · 2.37 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2022 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.
// The actionpermissions command utilizes go-github as a cli tool for
// changing GitHub Actions related permission settings for a repository.
package main
import (
"context"
"flag"
"fmt"
"log"
"os"
"github.com/google/go-github/v71/github"
)
var (
name = flag.String("name", "", "repo to change Actions permissions.")
owner = flag.String("owner", "", "owner of targeted repo.")
)
func main() {
flag.Parse()
token := os.Getenv("GITHUB_AUTH_TOKEN")
if token == "" {
log.Fatal("Unauthorized: No token present")
}
if *name == "" {
log.Fatal("No name: repo name must be given")
}
if *owner == "" {
log.Fatal("No owner: owner of repo must be given")
}
ctx := context.Background()
client := github.NewClient(nil).WithAuthToken(token)
actionsPermissionsRepository, _, err := client.Repositories.GetActionsPermissions(ctx, *owner, *name)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String())
actionsPermissionsRepository = &github.ActionsPermissionsRepository{Enabled: github.Ptr(true), AllowedActions: github.Ptr("selected")}
_, _, err = client.Repositories.EditActionsPermissions(ctx, *owner, *name, *actionsPermissionsRepository)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String())
actionsAllowed, _, err := client.Repositories.GetActionsAllowed(ctx, *owner, *name)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Current ActionsAllowed %s\n", actionsAllowed.String())
actionsAllowed = &github.ActionsAllowed{GithubOwnedAllowed: github.Ptr(true), VerifiedAllowed: github.Ptr(false), PatternsAllowed: []string{"a/b"}}
_, _, err = client.Repositories.EditActionsAllowed(ctx, *owner, *name, *actionsAllowed)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Current ActionsAllowed %s\n", actionsAllowed.String())
actionsPermissionsRepository = &github.ActionsPermissionsRepository{Enabled: github.Ptr(true), AllowedActions: github.Ptr("all")}
_, _, err = client.Repositories.EditActionsPermissions(ctx, *owner, *name, *actionsPermissionsRepository)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String())
}