|
|
@@ -6,6 +6,9 @@ import (
|
|
|
"fmt"
|
|
|
"net/http"
|
|
|
"net/url"
|
|
|
+ "strconv"
|
|
|
+
|
|
|
+ "golang.org/x/oauth2"
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
"github.com/google/go-github/v32/github"
|
|
|
@@ -25,49 +28,75 @@ type DirectoryItem struct {
|
|
|
|
|
|
// HandleListRepos retrieves a list of repo names
|
|
|
func (app *App) HandleListRepos(w http.ResponseWriter, r *http.Request) {
|
|
|
- client := github.NewClient(nil)
|
|
|
+ tok, err := app.githubTokenFromRequest(r)
|
|
|
|
|
|
- // list all organizations for specified user
|
|
|
- // TODO: fix hardcoded user/org
|
|
|
- repos, _, err := client.Repositories.List(context.Background(), "porter-dev", nil)
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ app.handleErrorInternal(err, w)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- res := []Repo{}
|
|
|
- for i := range repos {
|
|
|
- r := Repo{}
|
|
|
- r.FullName = *repos[i].FullName
|
|
|
- r.Kind = "github"
|
|
|
- res = append(res, r)
|
|
|
+ res := make([]Repo, 0)
|
|
|
+
|
|
|
+ client := github.NewClient(app.GithubConfig.Client(oauth2.NoContext, tok))
|
|
|
+
|
|
|
+ // list all repositories for specified user
|
|
|
+ repos, _, err := client.Repositories.List(context.Background(), "", nil)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ app.handleErrorInternal(err, w)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // TODO -- check if repo has already been appended -- there may be duplicates
|
|
|
+ for _, repo := range repos {
|
|
|
+ res = append(res, Repo{
|
|
|
+ FullName: repo.GetFullName(),
|
|
|
+ Kind: "github",
|
|
|
+ })
|
|
|
}
|
|
|
+
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
}
|
|
|
|
|
|
// HandleGetBranches retrieves a list of branch names for a specified repo
|
|
|
func (app *App) HandleGetBranches(w http.ResponseWriter, r *http.Request) {
|
|
|
+ tok, err := app.githubTokenFromRequest(r)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ app.handleErrorInternal(err, w)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
name := chi.URLParam(r, "name")
|
|
|
- client := github.NewClient(nil)
|
|
|
+
|
|
|
+ client := github.NewClient(app.GithubConfig.Client(oauth2.NoContext, tok))
|
|
|
|
|
|
// List all branches for a specified repo
|
|
|
- // TODO: fix hardcoded user/org
|
|
|
- branches, _, err := client.Repositories.ListBranches(context.Background(), "porter-dev", name, nil)
|
|
|
+ branches, _, err := client.Repositories.ListBranches(context.Background(), "", name, nil)
|
|
|
if err != nil {
|
|
|
fmt.Println(err)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
res := []string{}
|
|
|
- for i := range branches {
|
|
|
- b := *branches[i].Name
|
|
|
- res = append(res, b)
|
|
|
+ for _, b := range branches {
|
|
|
+ res = append(res, b.GetName())
|
|
|
}
|
|
|
+
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
}
|
|
|
|
|
|
// HandleGetBranchContents retrieves the contents of a specific branch and subdirectory
|
|
|
func (app *App) HandleGetBranchContents(w http.ResponseWriter, r *http.Request) {
|
|
|
+ tok, err := app.githubTokenFromRequest(r)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ app.handleErrorInternal(err, w)
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ client := github.NewClient(app.GithubConfig.Client(oauth2.NoContext, tok))
|
|
|
+
|
|
|
queryParams, err := url.ParseQuery(r.URL.RawQuery)
|
|
|
if err != nil {
|
|
|
app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
|
|
|
@@ -76,14 +105,12 @@ func (app *App) HandleGetBranchContents(w http.ResponseWriter, r *http.Request)
|
|
|
|
|
|
name := chi.URLParam(r, "name")
|
|
|
branch := chi.URLParam(r, "branch")
|
|
|
- client := github.NewClient(nil)
|
|
|
|
|
|
- // TODO: fix hardcoded user/org
|
|
|
repoContentOptions := github.RepositoryContentGetOptions{}
|
|
|
repoContentOptions.Ref = branch
|
|
|
- _, directoryContents, _, err := client.Repositories.GetContents(context.Background(), "porter-dev", name, queryParams["dir"][0], &repoContentOptions)
|
|
|
+ _, directoryContents, _, err := client.Repositories.GetContents(context.Background(), "", name, queryParams["dir"][0], &repoContentOptions)
|
|
|
if err != nil {
|
|
|
- fmt.Println(err)
|
|
|
+ app.handleErrorInternal(err, w)
|
|
|
return
|
|
|
}
|
|
|
|
|
|
@@ -97,6 +124,52 @@ func (app *App) HandleGetBranchContents(w http.ResponseWriter, r *http.Request)
|
|
|
|
|
|
// Ret2: recursively traverse all dirs to create config bundle (case on type == dir)
|
|
|
// https://api.github.com/repos/porter-dev/porter/contents?ref=frontend-graph
|
|
|
- fmt.Println(res)
|
|
|
+ // fmt.Println(res)
|
|
|
json.NewEncoder(w).Encode(res)
|
|
|
}
|
|
|
+
|
|
|
+func (app *App) githubTokenFromRequest(
|
|
|
+ r *http.Request,
|
|
|
+) (*oauth2.Token, error) {
|
|
|
+ // read project id
|
|
|
+ projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
|
|
|
+
|
|
|
+ if err != nil || projID == 0 {
|
|
|
+ return nil, fmt.Errorf("could not read project id")
|
|
|
+ }
|
|
|
+
|
|
|
+ // read user id
|
|
|
+ session, err := app.store.Get(r, app.cookieName)
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, fmt.Errorf("could not read user id")
|
|
|
+ }
|
|
|
+
|
|
|
+ userID, ok := session.Values["user_id"].(uint)
|
|
|
+
|
|
|
+ if !ok {
|
|
|
+ return nil, fmt.Errorf("could not read user id")
|
|
|
+ }
|
|
|
+
|
|
|
+ // query for repo client
|
|
|
+ repoClients, err := app.repo.RepoClient.ListRepoClientsByProjectID(uint(projID))
|
|
|
+
|
|
|
+ if err != nil {
|
|
|
+ return nil, err
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, rc := range repoClients {
|
|
|
+ // find the RepoClient that matches the user id in the request
|
|
|
+ if rc.UserID == userID {
|
|
|
+ // TODO -- refresh token is irrelevant at the moment, because the access token
|
|
|
+ // doesn't expire.
|
|
|
+ return &oauth2.Token{
|
|
|
+ AccessToken: rc.AccessToken,
|
|
|
+ RefreshToken: rc.RefreshToken,
|
|
|
+ TokenType: "Bearer",
|
|
|
+ }, nil
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return nil, fmt.Errorf("could not find matching token")
|
|
|
+}
|