Created
March 7, 2023 18:35
-
-
Save alexellis/5e6977b577d78503a4dd843fb73fbcbd to your computer and use it in GitHub Desktop.
Detail GitHub App Installations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"fmt" | |
"os" | |
"reflect" | |
"time" | |
"github.com/golang-jwt/jwt/v5" | |
"github.com/google/go-github/v50/github" | |
"golang.org/x/oauth2" | |
) | |
func main() { | |
var installationID int | |
var keyPath string | |
flag.IntVar(&installationID, "id", 0, "GitHub App Installation ID") | |
flag.StringVar(&keyPath, "key", "", "GitHub App Private Key") | |
flag.Parse() | |
data, err := os.ReadFile(keyPath) | |
if err != nil { | |
panic(err) | |
} | |
key, err := jwt.ParseRSAPrivateKeyFromPEM(data) | |
if err != nil { | |
panic(err) | |
} | |
token := jwt.NewWithClaims(jwt.SigningMethodRS256, jwt.MapClaims{ | |
"iat": time.Now().Unix(), | |
"exp": time.Now().Add(time.Minute * 10).Unix(), | |
"iss": installationID, | |
}) | |
signed, err := token.SignedString(key) | |
if err != nil { | |
panic(err) | |
} | |
c := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(&oauth2.Token{AccessToken: signed})) | |
ghClient := github.NewClient(c) | |
installations, _, err := ghClient.Apps.ListInstallations(context.Background(), &github.ListOptions{}) | |
if err != nil { | |
panic(err) | |
} | |
for _, installation := range installations { | |
if installation.SuspendedAt == nil { | |
fmt.Printf("Installation ID: %d - Account: %s \n", installation.GetID(), installation.GetAccount().GetLogin()) | |
fmt.Printf("- Repositories: %s\n", installation.GetRepositorySelection()) | |
fmt.Printf("- Permissions:\n") | |
p := installation.GetPermissions() | |
val := reflect.ValueOf(p).Elem() | |
for i := 0; i < val.NumField(); i++ { | |
rv := reflect.ValueOf(p) | |
field := val.Type().Field(i) | |
value := reflect.Indirect(rv).FieldByName(field.Name) | |
if !value.IsNil() { | |
switch value.Kind() { | |
case reflect.String: | |
b := value.String() | |
fmt.Printf(" - %s=%s\n", field.Name, b) | |
case reflect.Ptr: | |
b := value.Elem().String() | |
fmt.Printf(" - %s=%s\n", field.Name, b) | |
} | |
} | |
} | |
} | |
} | |
fmt.Printf("Suspended Installations\n") | |
for _, installation := range installations { | |
if installation.SuspendedAt != nil { | |
fmt.Printf("Installation ID: %d - Account: %s - Suspended at: %s by: %s\n", installation.GetID(), | |
installation.GetAccount().GetLogin(), | |
installation.GetSuspendedAt().Format(time.RFC3339), | |
installation.GetSuspendedBy().GetLogin()) | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment