go.go 3.7 KB

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