repo_handler.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. package api
  2. // import (
  3. // "context"
  4. // "encoding/json"
  5. // "fmt"
  6. // "net/http"
  7. // "net/url"
  8. // "strconv"
  9. // "golang.org/x/oauth2"
  10. // "github.com/go-chi/chi"
  11. // "github.com/google/go-github/v32/github"
  12. // )
  13. // // Repo represents a GitHub or Gitab repository
  14. // type Repo struct {
  15. // FullName string
  16. // Kind string
  17. // }
  18. // // DirectoryItem represents a file or subfolder in a repository
  19. // type DirectoryItem struct {
  20. // Path string
  21. // Type string
  22. // }
  23. // // HandleListRepos retrieves a list of repo names
  24. // func (app *App) HandleListRepos(w http.ResponseWriter, r *http.Request) {
  25. // tok, err := app.githubTokenFromRequest(r)
  26. // if err != nil {
  27. // app.handleErrorInternal(err, w)
  28. // return
  29. // }
  30. // res := make([]Repo, 0)
  31. // client := github.NewClient(app.GithubConfig.Client(oauth2.NoContext, tok))
  32. // // list all repositories for specified user
  33. // repos, _, err := client.Repositories.List(context.Background(), "", nil)
  34. // if err != nil {
  35. // app.handleErrorInternal(err, w)
  36. // return
  37. // }
  38. // // TODO -- check if repo has already been appended -- there may be duplicates
  39. // for _, repo := range repos {
  40. // res = append(res, Repo{
  41. // FullName: repo.GetFullName(),
  42. // Kind: "github",
  43. // })
  44. // }
  45. // json.NewEncoder(w).Encode(res)
  46. // }
  47. // // HandleGetBranches retrieves a list of branch names for a specified repo
  48. // func (app *App) HandleGetBranches(w http.ResponseWriter, r *http.Request) {
  49. // tok, err := app.githubTokenFromRequest(r)
  50. // if err != nil {
  51. // app.handleErrorInternal(err, w)
  52. // return
  53. // }
  54. // name := chi.URLParam(r, "name")
  55. // client := github.NewClient(app.GithubConfig.Client(oauth2.NoContext, tok))
  56. // // List all branches for a specified repo
  57. // branches, _, err := client.Repositories.ListBranches(context.Background(), "", name, nil)
  58. // if err != nil {
  59. // fmt.Println(err)
  60. // return
  61. // }
  62. // res := []string{}
  63. // for _, b := range branches {
  64. // res = append(res, b.GetName())
  65. // }
  66. // json.NewEncoder(w).Encode(res)
  67. // }
  68. // // HandleGetBranchContents retrieves the contents of a specific branch and subdirectory
  69. // func (app *App) HandleGetBranchContents(w http.ResponseWriter, r *http.Request) {
  70. // tok, err := app.githubTokenFromRequest(r)
  71. // if err != nil {
  72. // app.handleErrorInternal(err, w)
  73. // return
  74. // }
  75. // client := github.NewClient(app.GithubConfig.Client(oauth2.NoContext, tok))
  76. // queryParams, err := url.ParseQuery(r.URL.RawQuery)
  77. // if err != nil {
  78. // app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  79. // return
  80. // }
  81. // name := chi.URLParam(r, "name")
  82. // branch := chi.URLParam(r, "branch")
  83. // repoContentOptions := github.RepositoryContentGetOptions{}
  84. // repoContentOptions.Ref = branch
  85. // _, directoryContents, _, err := client.Repositories.GetContents(context.Background(), "", name, queryParams["dir"][0], &repoContentOptions)
  86. // if err != nil {
  87. // app.handleErrorInternal(err, w)
  88. // return
  89. // }
  90. // res := []DirectoryItem{}
  91. // for i := range directoryContents {
  92. // d := DirectoryItem{}
  93. // d.Path = *directoryContents[i].Path
  94. // d.Type = *directoryContents[i].Type
  95. // res = append(res, d)
  96. // }
  97. // // Ret2: recursively traverse all dirs to create config bundle (case on type == dir)
  98. // // https://api.github.com/repos/porter-dev/porter/contents?ref=frontend-graph
  99. // // fmt.Println(res)
  100. // json.NewEncoder(w).Encode(res)
  101. // }
  102. // func (app *App) githubTokenFromRequest(
  103. // r *http.Request,
  104. // ) (*oauth2.Token, error) {
  105. // // read project id
  106. // projID, err := strconv.ParseUint(chi.URLParam(r, "project_id"), 0, 64)
  107. // if err != nil || projID == 0 {
  108. // return nil, fmt.Errorf("could not read project id")
  109. // }
  110. // // read user id
  111. // session, err := app.store.Get(r, app.cookieName)
  112. // if err != nil {
  113. // return nil, fmt.Errorf("could not read user id")
  114. // }
  115. // userID, ok := session.Values["user_id"].(uint)
  116. // if !ok {
  117. // return nil, fmt.Errorf("could not read user id")
  118. // }
  119. // // query for repo client
  120. // gitRepos, err := app.repo.GitRepo.ListGitReposByProjectID(uint(projID))
  121. // if err != nil {
  122. // return nil, err
  123. // }
  124. // for _, rc := range repoClients {
  125. // // find the RepoClient that matches the user id in the request
  126. // if rc.UserID == userID {
  127. // // TODO -- refresh token is irrelevant at the moment, because the access token
  128. // // doesn't expire.
  129. // return &oauth2.Token{
  130. // AccessToken: string(rc.AccessToken),
  131. // RefreshToken: string(rc.RefreshToken),
  132. // TokenType: "Bearer",
  133. // }, nil
  134. // }
  135. // }
  136. // return nil, fmt.Errorf("could not find matching token")
  137. // }