@@ -2,16 +2,12 @@ package github
22
33import (
44 "encoding/json"
5- "errors"
65 "fmt"
76 "net/http"
8- "time"
97
108 "github.com/corecollectives/mist/api/handlers"
119 "github.com/corecollectives/mist/api/middleware"
1210 "github.com/corecollectives/mist/github"
13- "github.com/corecollectives/mist/models"
14- "gorm.io/gorm"
1511)
1612
1713type RepoListResponse struct {
@@ -26,40 +22,45 @@ func GetRepositories(w http.ResponseWriter, r *http.Request) {
2622 return
2723 }
2824
29- installationID , err := models .GetInstallationID (int (userData .ID ))
30- if errors .Is (err , gorm .ErrRecordNotFound ) {
31- handlers .SendResponse (w , http .StatusNotFound , false , nil , "no installation found for user" , "No installation found" )
32- return
33- }
34- if err != nil {
35- handlers .SendResponse (w , http .StatusInternalServerError , false , nil , "database error" , err .Error ())
36- return
37- }
38-
39- token , tokenExpires , appID , err := models . GetInstallationToken ( installationID )
25+ // installationID, err := models.GetInstallationID(int(userData.ID))
26+ // if errors.Is(err, gorm.ErrRecordNotFound) {
27+ // handlers.SendResponse(w, http.StatusNotFound, false, nil, "no installation found for user", "No installation found")
28+ // return
29+ // }
30+ // if err != nil {
31+ // handlers.SendResponse(w, http.StatusInternalServerError, false, nil, "database error", err.Error())
32+ // return
33+ // }
34+
35+ token , _ , err := github . GetGitHubAccessToken ( int ( userData . ID ) )
4036 if err != nil {
41- handlers .SendResponse (w , http .StatusInternalServerError , false , nil , "failed to fetch installation info" , err .Error ())
42- return
37+ handlers .SendResponse (w , http .StatusInternalServerError , false , nil , "failed to fetch GH installation info" , err .Error ())
4338 }
4439
45- expiry , _ := time .Parse (time .RFC3339 , tokenExpires )
46- if time .Now ().After (expiry ) {
47- appJWT , err := github .GenerateGithubJwt (appID )
48- if err != nil {
49- handlers .SendResponse (w , http .StatusInternalServerError , false , nil , "failed to generate app jwt" , err .Error ())
50- return
51- }
52-
53- newToken , newExpiry , err := regenerateInstallationToken (appJWT , installationID )
54- if err != nil {
55- handlers .SendResponse (w , http .StatusInternalServerError , false , nil , "failed to refresh token" , err .Error ())
56- return
57- }
58-
59- _ = models .UpdateInstallationToken (int64 (installationID ), newToken , newExpiry )
60-
61- token = newToken
62- }
40+ // token, tokenExpires, appID, err := models.GetInstallationToken(installationID)
41+ // if err != nil {
42+ // handlers.SendResponse(w, http.StatusInternalServerError, false, nil, "failed to fetch installation info", err.Error())
43+ // return
44+ // }
45+ //
46+ // expiry, _ := time.Parse(time.RFC3339, tokenExpires)
47+ // if time.Now().After(expiry) {
48+ // appJWT, err := github.GenerateGithubJwt(appID)
49+ // if err != nil {
50+ // handlers.SendResponse(w, http.StatusInternalServerError, false, nil, "failed to generate app jwt", err.Error())
51+ // return
52+ // }
53+ //
54+ // newToken, newExpiry, err := regenerateInstallationToken(appJWT, installationID)
55+ // if err != nil {
56+ // handlers.SendResponse(w, http.StatusInternalServerError, false, nil, "failed to refresh token", err.Error())
57+ // return
58+ // }
59+ //
60+ // _ = models.UpdateInstallationToken(int64(installationID), newToken, newExpiry)
61+ //
62+ // token = newToken
63+ // }
6364
6465 allRepos := []any {}
6566 page := 1
@@ -99,32 +100,3 @@ func GetRepositories(w http.ResponseWriter, r *http.Request) {
99100 w .Header ().Set ("Content-Type" , "application/json" )
100101 json .NewEncoder (w ).Encode (allRepos )
101102}
102-
103- func regenerateInstallationToken (appJWT string , installationID int64 ) (string , time.Time , error ) {
104- url := fmt .Sprintf ("https://api.github.com/app/installations/%d/access_tokens" , installationID )
105-
106- req , _ := http .NewRequest ("POST" , url , nil )
107- req .Header .Set ("Authorization" , "Bearer " + appJWT )
108- req .Header .Set ("Accept" , "application/vnd.github+json" )
109-
110- resp , err := http .DefaultClient .Do (req )
111- if err != nil {
112- return "" , time.Time {}, err
113- }
114- defer resp .Body .Close ()
115-
116- if resp .StatusCode != http .StatusCreated {
117- return "" , time.Time {}, fmt .Errorf ("failed to create token, status %d" , resp .StatusCode )
118- }
119-
120- var tokenResp struct {
121- Token string `json:"token"`
122- ExpiresAt time.Time `json:"expires_at"`
123- }
124-
125- if err := json .NewDecoder (resp .Body ).Decode (& tokenResp ); err != nil {
126- return "" , time.Time {}, err
127- }
128-
129- return tokenResp .Token , tokenResp .ExpiresAt , nil
130- }
0 commit comments