File: main.go

package info (click to toggle)
golang-github-google-go-github 60.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,700 kB
  • sloc: sh: 111; makefile: 5
file content (77 lines) | stat: -rw-r--r-- 2,434 bytes parent folder | download
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/v60/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.Bool(true), AllowedActions: github.String("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.Bool(true), VerifiedAllowed: github.Bool(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.Bool(true), AllowedActions: github.String("all")}
	_, _, err = client.Repositories.EditActionsPermissions(ctx, *owner, *name, *actionsPermissionsRepository)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Printf("Current ActionsPermissions %s\n", actionsPermissionsRepository.String())
}