github.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. package github
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "net/http"
  6. "os"
  7. "strings"
  8. )
  9. // branch contains the information of a repository branch
  10. type branch struct {
  11. Name string `json:"name"`
  12. Commit struct {
  13. SHA string `json:"sha"`
  14. URL string `json:"url"`
  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. var savedInfo infos
  26. var configfile string
  27. // Updated returns if database has been updated
  28. var Updated bool
  29. func init() {
  30. Updated = false
  31. configfile = os.Getenv("HOME") + "/.config/yay/yay_vcs.json"
  32. if _, err := os.Stat(configfile); os.IsNotExist(err) {
  33. _ = os.MkdirAll(os.Getenv("HOME")+"/.config/yay", 0755)
  34. return
  35. }
  36. file, err := os.Open(configfile)
  37. if err != nil {
  38. fmt.Println("error:", err)
  39. return
  40. }
  41. decoder := json.NewDecoder(file)
  42. err = decoder.Decode(&savedInfo)
  43. if err != nil {
  44. fmt.Println("error:", err)
  45. }
  46. }
  47. // ParseSource returns owner and repo from source
  48. func ParseSource(source string) (owner string, repo string) {
  49. if !(strings.Contains(source, "git://") ||
  50. strings.Contains(source, ".git") ||
  51. strings.Contains(source, "git+https://")) {
  52. return
  53. }
  54. split := strings.Split(source, "github.com/")
  55. if len(split) > 1 {
  56. secondSplit := strings.Split(split[1], "/")
  57. if len(secondSplit) > 1 {
  58. owner = secondSplit[0]
  59. thirdSplit := strings.Split(secondSplit[1], ".git")
  60. if len(thirdSplit) > 0 {
  61. repo = thirdSplit[0]
  62. }
  63. }
  64. }
  65. return
  66. }
  67. func (info *Info) needsUpdate() bool {
  68. var newRepo branches
  69. r, err := http.Get(info.URL)
  70. if err != nil {
  71. fmt.Println(err)
  72. return false
  73. }
  74. defer r.Body.Close()
  75. err = json.NewDecoder(r.Body).Decode(&newRepo)
  76. if err != nil {
  77. fmt.Println(err)
  78. return false
  79. }
  80. for _, e := range newRepo {
  81. if e.Name == "master" {
  82. if e.Commit.SHA != info.SHA {
  83. return true
  84. } else {
  85. return false
  86. }
  87. }
  88. }
  89. return false
  90. }
  91. func CheckUpdates() (toUpdate []string) {
  92. for _, e := range savedInfo {
  93. if e.needsUpdate() {
  94. toUpdate = append(toUpdate, e.Package)
  95. }
  96. }
  97. return
  98. }
  99. func inStore(url string) *Info {
  100. for i, e := range savedInfo {
  101. if url == e.URL {
  102. return &savedInfo[i]
  103. }
  104. }
  105. return nil
  106. }
  107. // BranchInfo updates saved information
  108. func BranchInfo(pkgname string, owner string, repo string) (err error) {
  109. Updated = true
  110. var newRepo branches
  111. url := "https://api.github.com/repos/" + owner + "/" + repo + "/branches"
  112. r, err := http.Get(url)
  113. if err != nil {
  114. return
  115. }
  116. defer r.Body.Close()
  117. _ = json.NewDecoder(r.Body).Decode(&newRepo)
  118. packinfo := inStore(url)
  119. for _, e := range newRepo {
  120. if e.Name == "master" {
  121. if packinfo != nil {
  122. packinfo.Package = pkgname
  123. packinfo.URL = url
  124. packinfo.SHA = e.Commit.SHA
  125. } else {
  126. savedInfo = append(savedInfo, Info{Package: pkgname, URL: url, SHA: e.Commit.SHA})
  127. }
  128. }
  129. }
  130. return
  131. }
  132. func SaveBranchInfo() error {
  133. marshalledinfo, _ := json.Marshal(savedInfo)
  134. in, err := os.OpenFile(configfile, os.O_RDWR|os.O_CREATE, 0755)
  135. if err != nil {
  136. return err
  137. }
  138. defer in.Close()
  139. _, err = in.Write(marshalledinfo)
  140. if err != nil {
  141. return err
  142. }
  143. err = in.Sync()
  144. return err
  145. }