Skip to content

Commit

Permalink
show the installations
Browse files Browse the repository at this point in the history
  • Loading branch information
nabeken committed May 3, 2023
1 parent 7d02e40 commit e0d4bcf
Showing 1 changed file with 51 additions and 5 deletions.
56 changes: 51 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import (
"time"

"github.com/bradleyfalzon/ghinstallation"
"github.com/google/go-github/v52/github"
"github.com/k0kubun/pp/v3"
)

var (
Expand All @@ -27,6 +29,7 @@ func main() {
instID := flag.Int64("inst-id", 0, "Installation ID")
export := flag.Bool("export", false, "show token as 'export GITHUB_TOKEN=...'")
showVersion := flag.Bool("version", false, "show version info")
showInsts := flag.Bool("show-insts", false, "show all of the installations for the app")

origUsage := flag.Usage
flag.Usage = func() {
Expand All @@ -45,17 +48,29 @@ func main() {
os.Exit(0)
}

key := os.Getenv("GITHUB_PRIV_KEY")
if key == "" {
log.Fatal("Please populate GITHUB_PRIV_KEY environment variable with the private key for the App")
}

if *showInsts {
if *appID == 0 {
fmt.Fprintf(os.Stderr, "App ID is required to show the installations for the app.\n\n")
flag.Usage()
os.Exit(1)
}

showInstallations(*appID, []byte(key))

return
}

if *appID == 0 || *instID == 0 {
fmt.Fprintf(os.Stderr, "App ID and Installation ID are required.\n\n")
flag.Usage()
os.Exit(1)
}

key := os.Getenv("GITHUB_PRIV_KEY")
if key == "" {
log.Fatal("Please populate GITHUB_PRIV_KEY environment variable with the private key for the App")
}

// Wrap the shared transport for use with the app ID 1 authenticating with installation ID 99.
itr, err := ghinstallation.New(http.DefaultTransport, *appID, *instID, []byte(key))
if err != nil {
Expand All @@ -80,3 +95,34 @@ func main() {
func showExport(token string) {
fmt.Printf("export GITHUB_TOKEN=%s\n", token)
}

func showInstallations(appID int64, key []byte) {
atr, err := ghinstallation.NewAppsTransport(http.DefaultTransport, appID, key)
if err != nil {
log.Fatal(err)
}

client := github.NewClient(&http.Client{Transport: atr})

opts := &github.ListOptions{
PerPage: 10,
}

ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()

for {
inst, resp, err := client.Apps.ListInstallations(ctx, opts)
if err != nil {
log.Fatal(err)
}

pp.Println(inst)

if resp.NextPage == 0 {
break
}

opts.Page = resp.NextPage
}
}

0 comments on commit e0d4bcf

Please sign in to comment.