Skip to content

Commit 0a1564a

Browse files
committed
feat: add GITHUB_TOKEN as replacement for GitHub App
1 parent 1ff7e79 commit 0a1564a

File tree

1 file changed

+42
-29
lines changed

1 file changed

+42
-29
lines changed

main.go

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,16 @@ func main() {
2727
logger := log.Default()
2828
logger.SetFlags(log.Ltime | log.Ldate | log.LUTC)
2929

30-
appTransport, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, conf.GHAppID, conf.GHAppInstallationID, conf.GHAppKeyPath)
31-
appClient := github.NewClient(&http.Client{Transport: appTransport})
30+
var appClient *github.Client
31+
if conf.GHToken != "" {
32+
appClient = github.NewTokenClient(context.Background(), conf.GHToken)
33+
} else {
34+
appTransport, err := ghinstallation.NewKeyFromFile(http.DefaultTransport, conf.GHAppID, conf.GHAppInstallationID, conf.GHAppKeyPath)
35+
if err != nil {
36+
log.Fatalf("Error creating GitHub App transport: %v", err)
37+
}
38+
appClient = github.NewClient(&http.Client{Transport: appTransport})
39+
}
3240

3341
http.HandleFunc("/link", handlePanicHTTP(handleLink(conf.GHOauthID)))
3442
http.HandleFunc("/authorize", handlePanicHTTP(handleAuthorize(conf.GHOauthID, conf.GHOAuthSecret, appClient)))
@@ -41,43 +49,46 @@ func main() {
4149
func getConfigFromEnv() (Config, error) {
4250
_ = godotenv.Load()
4351

44-
ghOAuthID, ok := os.LookupEnv("GITHUB_OAUTH_ID")
52+
config := Config{}
53+
54+
var ok bool
55+
56+
config.GHOauthID, ok = os.LookupEnv("GITHUB_OAUTH_ID")
4557
if !ok {
4658
return Config{}, errors.New("GITHUB_OAUTH_ID is required")
4759
}
48-
ghOAuthSecret, ok := os.LookupEnv("GITHUB_OAUTH_SECRET")
60+
config.GHOAuthSecret, ok = os.LookupEnv("GITHUB_OAUTH_SECRET")
4961
if !ok {
5062
return Config{}, errors.New("GITHUB_OAUTH_SECRET is required")
5163
}
52-
ghAppIDStr, ok := os.LookupEnv("GITHUB_APP_ID")
53-
if !ok {
54-
return Config{}, errors.New("GITHUB_APP_ID is required")
55-
}
56-
ghAppID, err := strconv.ParseInt(ghAppIDStr, 0, 64)
57-
if err != nil {
58-
return Config{}, errors.New("GITHUB_APP_ID is not a valid int64")
59-
}
60-
ghInstallationIDStr, ok := os.LookupEnv("GITHUB_INSTALLATION_ID")
61-
if !ok {
62-
return Config{}, errors.New("GITHUB_INSTALLATION_ID is required")
63-
}
64-
ghAppInstallationID, err := strconv.ParseInt(ghInstallationIDStr, 0, 64)
65-
if err != nil {
66-
return Config{}, errors.New("GITHUB_INSTALLATION_ID is not a valid int64")
67-
}
6864

69-
ghAppKeyPath, ok := os.LookupEnv("GITHUB_APP_KEY_PATH")
65+
config.GHToken, ok = os.LookupEnv("GITHUB_TOKEN")
7066
if !ok {
71-
return Config{}, errors.New("GITHUB_APP_KEY_PATH is required")
67+
var err error
68+
ghAppIDStr, ok := os.LookupEnv("GITHUB_APP_ID")
69+
if !ok {
70+
return Config{}, errors.New("GITHUB_APP_ID is required")
71+
}
72+
config.GHAppID, err = strconv.ParseInt(ghAppIDStr, 0, 64)
73+
if err != nil {
74+
return Config{}, errors.New("GITHUB_APP_ID is not a valid int64")
75+
}
76+
ghInstallationIDStr, ok := os.LookupEnv("GITHUB_INSTALLATION_ID")
77+
if !ok {
78+
return Config{}, errors.New("GITHUB_INSTALLATION_ID is required")
79+
}
80+
config.GHAppInstallationID, err = strconv.ParseInt(ghInstallationIDStr, 0, 64)
81+
if err != nil {
82+
return Config{}, errors.New("GITHUB_INSTALLATION_ID is not a valid int64")
83+
}
84+
85+
config.GHAppKeyPath, ok = os.LookupEnv("GITHUB_APP_KEY_PATH")
86+
if !ok {
87+
return Config{}, errors.New("GITHUB_APP_KEY_PATH is required")
88+
}
7289
}
7390

74-
return Config{
75-
GHOauthID: ghOAuthID,
76-
GHOAuthSecret: ghOAuthSecret,
77-
GHAppID: ghAppID,
78-
GHAppInstallationID: ghAppInstallationID,
79-
GHAppKeyPath: ghAppKeyPath,
80-
}, nil
91+
return config, nil
8192
}
8293

8394
type Config struct {
@@ -87,6 +98,8 @@ type Config struct {
8798
GHAppID int64
8899
GHAppInstallationID int64
89100
GHAppKeyPath string
101+
102+
GHToken string
90103
}
91104

92105
func handlePanicHTTP(handlerFunc http.HandlerFunc) http.HandlerFunc {

0 commit comments

Comments
 (0)