service_test.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. package service
  2. import (
  3. "flag"
  4. "testing"
  5. "go-common/app/tool/saga/conf"
  6. "go-common/app/tool/saga/model"
  7. . "github.com/smartystreets/goconvey/convey"
  8. )
  9. var (
  10. s *Service
  11. repoTest = &model.RepoInfo{Group: "changhengyuan", Name: "test-saga", Branch: "master"}
  12. GitlabHookCommentTest = []byte(`{
  13. "object_kind":"note",
  14. "event_type":"note",
  15. "user":{
  16. "name":"changhengyuan",
  17. "username":"changhengyuan",
  18. "avatar_url":"https://www.gravatar.com/avatar/d3218d34473c6fb4d18a770f14e59a89?s=80\u0026d=identicon"
  19. },
  20. "project_id":35,
  21. "project":{
  22. "id":35,
  23. "name":"test-saga",
  24. "description":"",
  25. "web_url":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  26. "avatar_url":null,
  27. "git_ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  28. "git_http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git",
  29. "namespace":"changhengyuan",
  30. "visibility_level":20,
  31. "path_with_namespace":"changhengyuan/test-saga",
  32. "default_branch":"master",
  33. "ci_config_path":null,
  34. "homepage":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  35. "url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  36. "ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  37. "http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git"},
  38. "object_attributes":{
  39. "id":3040,
  40. "note":"test",
  41. "noteable_type":"MergeRequest",
  42. "author_id":15,
  43. "created_at":"2018-09-26 06:55:13 UTC",
  44. "updated_at":"2018-09-26 06:55:13 UTC",
  45. "project_id":35,
  46. "attachment":null,
  47. "line_code":null,
  48. "commit_id":"",
  49. "noteable_id":390,
  50. "system":false,
  51. "st_diff":null,
  52. "updated_by_id":null,
  53. "type":null,
  54. "position":null,
  55. "original_position":null,
  56. "resolved_at":null,
  57. "resolved_by_id":null,
  58. "discussion_id":"450c34e4c0f9e925bdc6a24c2ae4920d7a394ebc",
  59. "change_position":null,
  60. "resolved_by_push":null,
  61. "url":"http://gitlab.bilibili.co/changhengyuan/test-saga/merge_requests/52#note_3040"
  62. },
  63. "repository":{
  64. "name":"test-saga",
  65. "url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  66. "description":"",
  67. "homepage":"http://gitlab.bilibili.co/changhengyuan/test-saga"
  68. },
  69. "merge_request":{
  70. "assignee_id":null,
  71. "author_id":15,
  72. "created_at":"2018-09-26 06:41:55 UTC",
  73. "description":"",
  74. "head_pipeline_id":4510,
  75. "id":390,
  76. "iid":52,
  77. "last_edited_at":null,
  78. "last_edited_by_id":null,
  79. "merge_commit_sha":null,
  80. "merge_error":null,
  81. "merge_params":{
  82. "force_remove_source_branch":"0"
  83. },
  84. "merge_status":"cannot_be_merged",
  85. "merge_user_id":null,
  86. "merge_when_pipeline_succeeds":false,
  87. "milestone_id":null,
  88. "source_branch":"test-branch",
  89. "source_project_id":35,
  90. "state":"opened",
  91. "target_branch":"master",
  92. "target_project_id":35,
  93. "time_estimate":0,
  94. "title":"Test branch",
  95. "updated_at":"2018-09-26 06:54:33 UTC",
  96. "updated_by_id":null,
  97. "url":"http://gitlab.bilibili.co/changhengyuan/test-saga/merge_requests/52",
  98. "source":{
  99. "id":35,
  100. "name":"test-saga",
  101. "description":"",
  102. "web_url":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  103. "avatar_url":null,
  104. "git_ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  105. "git_http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git",
  106. "namespace":"changhengyuan",
  107. "visibility_level":20,
  108. "path_with_namespace":"changhengyuan/test-saga",
  109. "default_branch":"master",
  110. "ci_config_path":null,
  111. "homepage":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  112. "url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  113. "ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  114. "http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git"
  115. },
  116. "target":{
  117. "id":35,
  118. "name":"test-saga",
  119. "description":"",
  120. "web_url":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  121. "avatar_url":null,
  122. "git_ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  123. "git_http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git",
  124. "namespace":"changhengyuan",
  125. "visibility_level":20,
  126. "path_with_namespace":"changhengyuan/test-saga",
  127. "default_branch":"master",
  128. "ci_config_path":null,
  129. "homepage":"http://gitlab.bilibili.co/changhengyuan/test-saga",
  130. "url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  131. "ssh_url":"git@gitlab.bilibili.co:changhengyuan/test-saga.git",
  132. "http_url":"http://gitlab.bilibili.co/changhengyuan/test-saga.git"
  133. },
  134. "last_commit":{
  135. "id":"51e9c3ba2ceac496dbaf55f0db564ab6a15e20eb",
  136. "message":"add CONTRIBUTORS.md\n",
  137. "timestamp":"2018-09-17T18:02:13+08:00",
  138. "url":"http://gitlab.bilibili.co/changhengyuan/test-saga/commit/51e9c3ba2ceac496dbaf55f0db564ab6a15e20eb",
  139. "author":{
  140. "name":"哔哩哔哩",
  141. "email":"bilibili@bilibilideMac-mini.local"
  142. }
  143. },
  144. "work_in_progress":false,
  145. "total_time_spent":0,
  146. "human_total_time_spent":null,
  147. "human_time_estimate":null}}`)
  148. )
  149. func init() {
  150. var err error
  151. flag.Set("conf", "../cmd/saga-test.toml")
  152. if err = conf.Init(); err != nil {
  153. panic(err)
  154. }
  155. }
  156. func TestContributor(t *testing.T) {
  157. var (
  158. err error
  159. repo = &model.RepoInfo{
  160. Group: "platform",
  161. Name: "go-common",
  162. Branch: "master",
  163. }
  164. )
  165. Convey("TEST BuildContributor", t, func() {
  166. s = New()
  167. err = s.cmd.BuildContributor(repo)
  168. So(err, ShouldBeNil)
  169. })
  170. }
  171. func TestMergeRequest(t *testing.T) {
  172. var (
  173. err error
  174. ok bool
  175. repo *model.Repo
  176. url = "git@gitlab.bilibili.co:platform/go-common.git"
  177. projID = 23
  178. mrIID = 130
  179. sourceBranch = "ci/test-7"
  180. targetBranch = "master"
  181. )
  182. Convey("TEST ParseContributor", t, func() {
  183. s = New()
  184. ok, repo = s.findRepo(url)
  185. So(ok, ShouldBeTrue)
  186. err = s.cmd.UpdateContributor(projID, mrIID, sourceBranch, targetBranch, repo.Config.AuthBranches)
  187. So(err, ShouldBeNil)
  188. })
  189. }