go.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. package buildpacks
  2. import (
  3. "sync"
  4. "github.com/google/go-github/v41/github"
  5. "github.com/xanzy/go-gitlab"
  6. )
  7. type goRuntime struct {
  8. wg sync.WaitGroup
  9. }
  10. func NewGoRuntime() Runtime {
  11. return &goRuntime{}
  12. }
  13. func (runtime *goRuntime) detectModGithub(results chan struct {
  14. string
  15. bool
  16. }, directoryContent []*github.RepositoryContent,
  17. ) {
  18. goModFound := false
  19. for i := 0; i < len(directoryContent); i++ {
  20. name := directoryContent[i].GetName()
  21. if name == "go.mod" {
  22. goModFound = true
  23. break
  24. }
  25. }
  26. if goModFound {
  27. results <- struct {
  28. string
  29. bool
  30. }{mod, true}
  31. }
  32. runtime.wg.Done()
  33. }
  34. func (runtime *goRuntime) detectModGitlab(results chan struct {
  35. string
  36. bool
  37. }, tree []*gitlab.TreeNode,
  38. ) {
  39. goModFound := false
  40. for i := 0; i < len(tree); i++ {
  41. name := tree[i].Name
  42. if name == "go.mod" {
  43. goModFound = true
  44. break
  45. }
  46. }
  47. if goModFound {
  48. results <- struct {
  49. string
  50. bool
  51. }{mod, true}
  52. }
  53. runtime.wg.Done()
  54. }
  55. func (runtime *goRuntime) detectDepGithub(results chan struct {
  56. string
  57. bool
  58. }, directoryContent []*github.RepositoryContent,
  59. ) {
  60. gopkgFound := false
  61. vendorFound := false
  62. for i := 0; i < len(directoryContent); i++ {
  63. name := directoryContent[i].GetName()
  64. if name == "Gopkg.toml" {
  65. gopkgFound = true
  66. } else if name == "vendor" && directoryContent[i].GetType() == "dir" {
  67. vendorFound = true
  68. }
  69. if gopkgFound && vendorFound {
  70. break
  71. }
  72. }
  73. if gopkgFound && vendorFound {
  74. results <- struct {
  75. string
  76. bool
  77. }{dep, true}
  78. }
  79. runtime.wg.Done()
  80. }
  81. func (runtime *goRuntime) detectDepGitlab(results chan struct {
  82. string
  83. bool
  84. }, tree []*gitlab.TreeNode,
  85. ) {
  86. gopkgFound := false
  87. vendorFound := false
  88. for i := 0; i < len(tree); i++ {
  89. name := tree[i].Name
  90. if name == "Gopkg.toml" {
  91. gopkgFound = true
  92. } else if name == "vendor" && tree[i].Type == "tree" {
  93. vendorFound = true
  94. }
  95. if gopkgFound && vendorFound {
  96. break
  97. }
  98. }
  99. if gopkgFound && vendorFound {
  100. results <- struct {
  101. string
  102. bool
  103. }{dep, true}
  104. }
  105. runtime.wg.Done()
  106. }
  107. func (runtime *goRuntime) DetectGithub(
  108. client *github.Client,
  109. directoryContent []*github.RepositoryContent,
  110. owner, name, path string,
  111. repoContentOptions github.RepositoryContentGetOptions,
  112. paketo, heroku *BuilderInfo,
  113. ) error {
  114. results := make(chan struct {
  115. string
  116. bool
  117. }, 2)
  118. runtime.wg.Add(2)
  119. go runtime.detectModGithub(results, directoryContent)
  120. go runtime.detectDepGithub(results, directoryContent)
  121. runtime.wg.Wait()
  122. close(results)
  123. paketoBuildpackInfo := BuildpackInfo{
  124. Name: "Go",
  125. Buildpack: "gcr.io/paketo-buildpacks/go",
  126. }
  127. herokuBuildpackInfo := BuildpackInfo{
  128. Name: "Go",
  129. Buildpack: "heroku/go",
  130. }
  131. if len(results) == 0 {
  132. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  133. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  134. return nil
  135. }
  136. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  137. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  138. return nil
  139. }
  140. func (runtime *goRuntime) DetectGitlab(
  141. client *gitlab.Client,
  142. tree []*gitlab.TreeNode,
  143. repoPath, path, ref string,
  144. paketo, heroku *BuilderInfo,
  145. ) error {
  146. results := make(chan struct {
  147. string
  148. bool
  149. }, 2)
  150. runtime.wg.Add(2)
  151. go runtime.detectModGitlab(results, tree)
  152. go runtime.detectDepGitlab(results, tree)
  153. runtime.wg.Wait()
  154. close(results)
  155. paketoBuildpackInfo := BuildpackInfo{
  156. Name: "Go",
  157. Buildpack: "gcr.io/paketo-buildpacks/go",
  158. }
  159. herokuBuildpackInfo := BuildpackInfo{
  160. Name: "Go",
  161. Buildpack: "heroku/go",
  162. }
  163. if len(results) == 0 {
  164. paketo.Others = append(paketo.Others, paketoBuildpackInfo)
  165. heroku.Others = append(heroku.Others, herokuBuildpackInfo)
  166. return nil
  167. }
  168. paketo.Detected = append(paketo.Detected, paketoBuildpackInfo)
  169. heroku.Detected = append(heroku.Detected, herokuBuildpackInfo)
  170. return nil
  171. }