MCPcopy Index your code
hub / github.com/apache/devlake / main

Function main

backend/plugins/github/token/cmd/test_refresh/main.go:53–140  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

51}
52
53func main() {
54 appID := os.Getenv("GITHUB_APP_ID")
55 if appID == "" {
56 fatal("GITHUB_APP_ID env var is required")
57 }
58
59 pemData := os.Getenv("GITHUB_APP_PEM")
60 if pemData == "" {
61 pemFile := os.Getenv("GITHUB_APP_PEM_FILE")
62 if pemFile == "" {
63 fatal("Set GITHUB_APP_PEM (contents) or GITHUB_APP_PEM_FILE (path)")
64 }
65 data, err := os.ReadFile(pemFile)
66 if err != nil {
67 fatal("failed to read PEM file: %v", err)
68 }
69 pemData = string(data)
70 }
71
72 // Step 1: Create a JWT signed with the app's private key
73 fmt.Println("=== Step 1: Creating JWT from App ID and private key ===")
74 jwtToken, err := createJWT(appID, pemData)
75 if err != nil {
76 fatal("failed to create JWT: %v", err)
77 }
78 fmt.Printf("JWT created (first 20 chars): %s...\n\n", jwtToken[:20])
79
80 // Step 2: List installations
81 fmt.Println("=== Step 2: Listing installations for this app ===")
82 installations, err := listInstallations(jwtToken)
83 if err != nil {
84 fatal("failed to list installations: %v", err)
85 }
86 if len(installations) == 0 {
87 fatal("no installations found for this app")
88 }
89 for _, inst := range installations {
90 fmt.Printf(" Installation ID: %d Account: %s\n", inst.ID, inst.Account.Login)
91 }
92 fmt.Println()
93
94 // Step 3: Get an installation token for the first installation
95 inst := installations[0]
96 fmt.Printf("=== Step 3: Minting installation token for %s (ID: %d) ===\n", inst.Account.Login, inst.ID)
97 token1, err := getInstallationToken(jwtToken, inst.ID)
98 if err != nil {
99 fatal("failed to get installation token: %v", err)
100 }
101 fmt.Printf("Token 1: %s... Expires: %s\n\n", token1.Token[:10], token1.ExpiresAt.Format(time.RFC3339))
102
103 // Step 4: Make an API call with the token to verify it works
104 fmt.Println("=== Step 4: Verifying token works (GET /installation/repositories) ===")
105 err = verifyToken(token1.Token)
106 if err != nil {
107 fatal("token verification failed: %v", err)
108 }
109 fmt.Printf("Token is valid and working.\n\n")
110

Callers

nothing calls this directly

Calls 7

fatalFunction · 0.85
createJWTFunction · 0.85
listInstallationsFunction · 0.85
getInstallationTokenFunction · 0.85
verifyTokenFunction · 0.85
FormatMethod · 0.80
PrintfMethod · 0.65

Tested by

no test coverage detected