repo_handler.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. package api
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "net/url"
  8. "github.com/go-chi/chi"
  9. "github.com/google/go-github/v32/github"
  10. )
  11. // Repo represents a GitHub or Gitab repository
  12. type Repo struct {
  13. FullName string
  14. Kind string
  15. }
  16. // DirectoryItem represents a file or subfolder in a repository
  17. type DirectoryItem struct {
  18. Path string
  19. Type string
  20. }
  21. // HandleListRepos retrieves a list of repo names
  22. func (app *App) HandleListRepos(w http.ResponseWriter, r *http.Request) {
  23. client := github.NewClient(nil)
  24. // list all organizations for specified user
  25. // TODO: fix hardcoded user/org
  26. repos, _, err := client.Repositories.List(context.Background(), "porter-dev", nil)
  27. if err != nil {
  28. fmt.Println(err)
  29. return
  30. }
  31. res := []Repo{}
  32. for i := range repos {
  33. r := Repo{}
  34. r.FullName = *repos[i].FullName
  35. r.Kind = "github"
  36. res = append(res, r)
  37. }
  38. json.NewEncoder(w).Encode(res)
  39. }
  40. // HandleGetBranches retrieves a list of branch names for a specified repo
  41. func (app *App) HandleGetBranches(w http.ResponseWriter, r *http.Request) {
  42. name := chi.URLParam(r, "name")
  43. client := github.NewClient(nil)
  44. // List all branches for a specified repo
  45. // TODO: fix hardcoded user/org
  46. branches, _, err := client.Repositories.ListBranches(context.Background(), "porter-dev", name, nil)
  47. if err != nil {
  48. fmt.Println(err)
  49. return
  50. }
  51. res := []string{}
  52. for i := range branches {
  53. b := *branches[i].Name
  54. res = append(res, b)
  55. }
  56. json.NewEncoder(w).Encode(res)
  57. }
  58. // HandleGetBranchContents retrieves the contents of a specific branch and subdirectory
  59. func (app *App) HandleGetBranchContents(w http.ResponseWriter, r *http.Request) {
  60. queryParams, err := url.ParseQuery(r.URL.RawQuery)
  61. if err != nil {
  62. app.handleErrorFormDecoding(err, ErrReleaseDecode, w)
  63. return
  64. }
  65. name := chi.URLParam(r, "name")
  66. branch := chi.URLParam(r, "branch")
  67. client := github.NewClient(nil)
  68. // TODO: fix hardcoded user/org
  69. repoContentOptions := github.RepositoryContentGetOptions{}
  70. repoContentOptions.Ref = branch
  71. _, directoryContents, _, err := client.Repositories.GetContents(context.Background(), "porter-dev", name, queryParams["dir"][0], &repoContentOptions)
  72. if err != nil {
  73. fmt.Println(err)
  74. return
  75. }
  76. res := []DirectoryItem{}
  77. for i := range directoryContents {
  78. d := DirectoryItem{}
  79. d.Path = *directoryContents[i].Path
  80. d.Type = *directoryContents[i].Type
  81. res = append(res, d)
  82. }
  83. // Ret2: recursively traverse all dirs to create config bundle (case on type == dir)
  84. // https://api.github.com/repos/porter-dev/porter/contents?ref=frontend-graph
  85. fmt.Println(res)
  86. json.NewEncoder(w).Encode(res)
  87. }