vcs.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. package main
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io/ioutil"
  6. "net/http"
  7. "os"
  8. "strings"
  9. )
  10. // branch contains the information of a repository branch
  11. type branch struct {
  12. Name string `json:"name"`
  13. Commit struct {
  14. SHA string `json:"sha"`
  15. } `json:"commit"`
  16. }
  17. type branches []branch
  18. // Info contains the last commit sha of a repo
  19. type Info struct {
  20. Package string `json:"pkgname"`
  21. URL string `json:"url"`
  22. SHA string `json:"sha"`
  23. }
  24. type infos []Info
  25. // Repo contains information about the repository
  26. type repo struct {
  27. Name string `json:"name"`
  28. FullName string `json:"full_name"`
  29. DefaultBranch string `json:"default_branch"`
  30. }
  31. // createDevelDB forces yay to create a DB of the existing development packages
  32. func createDevelDB() error {
  33. _, _, _, remoteNames, err := filterPackages()
  34. if err != nil {
  35. return err
  36. }
  37. config.NoConfirm = true
  38. arguments := makeArguments()
  39. arguments.addArg("gendb")
  40. arguments.addTarget(remoteNames...)
  41. err = install(arguments)
  42. return err
  43. }
  44. // parseSource returns owner and repo from source
  45. func parseSource(source string) (owner string, repo string) {
  46. if !(strings.Contains(source, "git://") ||
  47. strings.Contains(source, ".git") ||
  48. strings.Contains(source, "git+https://")) {
  49. return
  50. }
  51. split := strings.Split(source, "github.com/")
  52. if len(split) > 1 {
  53. secondSplit := strings.Split(split[1], "/")
  54. if len(secondSplit) > 1 {
  55. owner = secondSplit[0]
  56. thirdSplit := strings.Split(secondSplit[1], ".git")
  57. if len(thirdSplit) > 0 {
  58. repo = thirdSplit[0]
  59. }
  60. }
  61. }
  62. return
  63. }
  64. func (info *Info) needsUpdate() bool {
  65. var newRepo repo
  66. var newBranches branches
  67. if strings.HasSuffix(info.URL, "/branches") {
  68. info.URL = info.URL[:len(info.URL)-9]
  69. }
  70. infoResp, infoErr := http.Get(info.URL)
  71. if infoErr != nil {
  72. fmt.Println(infoErr)
  73. return false
  74. }
  75. defer infoResp.Body.Close()
  76. infoBody, _ := ioutil.ReadAll(infoResp.Body)
  77. var err = json.Unmarshal(infoBody, &newRepo)
  78. if err != nil {
  79. fmt.Printf("Cannot update '%v'\nError: %v\nStatus code: %v\nBody: %v\n",
  80. info.Package, err, infoResp.StatusCode, string(infoBody))
  81. return false
  82. }
  83. defaultBranch := newRepo.DefaultBranch
  84. branchesURL := info.URL + "/branches"
  85. branchResp, branchErr := http.Get(branchesURL)
  86. if branchErr != nil {
  87. fmt.Println(branchErr)
  88. return false
  89. }
  90. defer branchResp.Body.Close()
  91. branchBody, _ := ioutil.ReadAll(branchResp.Body)
  92. err = json.Unmarshal(branchBody, &newBranches)
  93. if err != nil {
  94. fmt.Printf("Cannot update '%v'\nError: %v\nStatus code: %v\nBody: %v\n",
  95. info.Package, err, branchResp.StatusCode, string(branchBody))
  96. return false
  97. }
  98. for _, e := range newBranches {
  99. if e.Name == defaultBranch {
  100. return e.Commit.SHA != info.SHA
  101. }
  102. }
  103. return false
  104. }
  105. func inStore(pkgName string) *Info {
  106. for i, e := range savedInfo {
  107. if pkgName == e.Package {
  108. return &savedInfo[i]
  109. }
  110. }
  111. return nil
  112. }
  113. // branchInfo updates saved information
  114. func branchInfo(pkgName string, owner string, repoName string) (err error) {
  115. updated = true
  116. var newRepo repo
  117. var newBranches branches
  118. url := "https://api.github.com/repos/" + owner + "/" + repoName
  119. repoResp, err := http.Get(url)
  120. if err != nil {
  121. return
  122. }
  123. defer repoResp.Body.Close()
  124. _ = json.NewDecoder(repoResp.Body).Decode(&newRepo)
  125. defaultBranch := newRepo.DefaultBranch
  126. branchesURL := url + "/branches"
  127. branchResp, err := http.Get(branchesURL)
  128. if err != nil {
  129. return
  130. }
  131. defer branchResp.Body.Close()
  132. _ = json.NewDecoder(branchResp.Body).Decode(&newBranches)
  133. packinfo := inStore(pkgName)
  134. for _, e := range newBranches {
  135. if e.Name == defaultBranch {
  136. if packinfo != nil {
  137. packinfo.Package = pkgName
  138. packinfo.URL = url
  139. packinfo.SHA = e.Commit.SHA
  140. } else {
  141. savedInfo = append(savedInfo, Info{Package: pkgName, URL: url, SHA: e.Commit.SHA})
  142. }
  143. }
  144. }
  145. return
  146. }
  147. func saveVCSInfo() error {
  148. marshalledinfo, err := json.MarshalIndent(savedInfo, "", "\t")
  149. if err != nil || string(marshalledinfo) == "null" {
  150. return err
  151. }
  152. in, err := os.OpenFile(vcsFile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  153. if err != nil {
  154. return err
  155. }
  156. defer in.Close()
  157. _, err = in.Write(marshalledinfo)
  158. if err != nil {
  159. return err
  160. }
  161. err = in.Sync()
  162. return err
  163. }