Debian Codegraph @

example/actionpermissions/main.go

raw
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/v60/github"
18 )
19
20 var (
21 name = flag.String("name", "", "repo to change Actions permissions.")
22 owner = flag.String("owner", "", "owner of targeted repo.")
23 )
24
25 func main() {
26 flag.Parse()
27 token := os.Getenv("GITHUB_AUTH_TOKEN")
28 if token == "" {
29 log.Fatal("Unauthorized: No token present")
30 }
31 if *name == "" {
32 log.Fatal("No name: repo name must be given")
33 }
34 if *owner == "" {
35 log.Fatal("No owner: owner of repo must be given")
36 }
37 ctx := context.Background()
38 client := github.NewClient(nil).WithAuthToken(token)
39
40 actionsPermissionsRepository, _, err := client.Repositories.GetActionsPermissions(ctx, *owner, *name)
41 if err != nil {
42 log.Fatal(err)
43 }
44
45 fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String())
46
47 actionsPermissionsRepository = &github.ActionsPermissionsRepository{Enabled: github.Bool(true), AllowedActions: github.String("selected")}
48 _, _, err = client.Repositories.EditActionsPermissions(ctx, *owner, *name, *actionsPermissionsRepository)
49 if err != nil {
50 log.Fatal(err)
51 }
52
53 fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String())
54
55 actionsAllowed, _, err := client.Repositories.GetActionsAllowed(ctx, *owner, *name)
56 if err != nil {
57 log.Fatal(err)
58 }
59
60 fmt.Printf("Current ActionsAllowed %s\n", actionsAllowed.String())
61
62 actionsAllowed = &github.ActionsAllowed{GithubOwnedAllowed: github.Bool(true), VerifiedAllowed: github.Bool(false), PatternsAllowed: []string{"a/b"}}
63 _, _, err = client.Repositories.EditActionsAllowed(ctx, *owner, *name, *actionsAllowed)
64 if err != nil {
65 log.Fatal(err)
66 }
67
68 fmt.Printf("Current ActionsAllowed %s\n", actionsAllowed.String())
69
70 actionsPermissionsRepository = &github.ActionsPermissionsRepository{Enabled: github.Bool(true), AllowedActions: github.String("all")}
71 _, _, err = client.Repositories.EditActionsPermissions(ctx, *owner, *name, *actionsPermissionsRepository)
72 if err != nil {
73 log.Fatal(err)
74 }
75
76 fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String())
77 }