Debian Codegraph @

example/tagprotection/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 tagprotection command demonstrates the functionality that
7 // prompts the user for GitHub owner, repo, tag protection pattern and token,
8 // then creates a new tag protection if the user entered a pattern at the prompt.
9 // Otherwise, it will just list all existing tag protections.
10 package main
11
12 import (
13 "bufio"
14 "context"
15 "encoding/json"
16 "fmt"
17 "log"
18 "os"
19 "strings"
20
21 "github.com/google/go-github/v60/github"
22 "golang.org/x/term"
23 )
24
25 func main() {
26 // read github owner, repo, token from standard input
27 r := bufio.NewReader(os.Stdin)
28 fmt.Print("GitHub Org/User name: ")
29 owner, _ := r.ReadString('\n')
30 owner = strings.TrimSpace(owner)
31
32 fmt.Print("GitHub repo name: ")
33 repo, _ := r.ReadString('\n')
34 repo = strings.TrimSpace(repo)
35
36 fmt.Print("Tag pattern(leave blank to not create new tag protection): ")
37 pattern, _ := r.ReadString('\n')
38 pattern = strings.TrimSpace(pattern)
39
40 fmt.Print("GitHub Token: ")
41 byteToken, _ := term.ReadPassword(int(os.Stdin.Fd()))
42 println()
43 token := string(byteToken)
44
45 ctx := context.Background()
46 client := github.NewClient(nil).WithAuthToken(token)
47
48 // create new tag protection
49 if pattern != "" {
50 tagProtection, _, err := client.Repositories.CreateTagProtection(ctx, owner, repo, pattern)
51 if err != nil {
52 log.Fatalf("Error: %v\n", err)
53 }
54 println()
55 fmt.Printf("New tag protection created in github.com/%v/%v\n", owner, repo)
56 tp, _ := json.Marshal(tagProtection)
57 fmt.Println(string(tp))
58 }
59
60 // list all tag protection
61 println()
62 fmt.Printf("List all tag protection in github.com/%v/%v\n", owner, repo)
63 tagProtections, _, err := client.Repositories.ListTagProtection(ctx, owner, repo)
64 if err != nil {
65 log.Fatalf("Error: %v\n", err)
66 }
67 results, _ := json.Marshal(tagProtections)
68 fmt.Println(string(results))
69 }