gitlab_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. package gitlab
  2. import (
  3. "flag"
  4. "testing"
  5. "go-common/app/tool/saga/conf"
  6. . "github.com/smartystreets/goconvey/convey"
  7. )
  8. var (
  9. g *Gitlab
  10. )
  11. func init() {
  12. var (
  13. err error
  14. )
  15. g = New("http://gitlab.bilibili.co/api/v4", "z3nN4s4BVX5oNYXKbEPL")
  16. flag.Set("conf", "/Users/bilibili/go/src/go-common/app/tool/saga/cmd/saga-test.toml")
  17. if err = conf.Init(); err != nil {
  18. panic(err)
  19. }
  20. }
  21. // Test Function HostToken
  22. func TestHostToken(t *testing.T) {
  23. Convey("Test HostToken", t, func() {
  24. var (
  25. host string
  26. token string
  27. err error
  28. )
  29. host, token, err = g.HostToken()
  30. So(err, ShouldBeNil)
  31. So(host, ShouldEqual, "gitlab.bilibili.co")
  32. So(token, ShouldEqual, "z3nN4s4BVX5oNYXKbEPL")
  33. })
  34. }
  35. // TODO AutoPush
  36. // Test MR Create/Close
  37. // Test MRNote Create/Update/Delete
  38. //func TestCreateMRNote(t *testing.T) {
  39. // Convey("Test CreateMRNote", t, func() {
  40. // var (
  41. // err error
  42. // noteID int
  43. //
  44. // mr *gitlab.MergeRequest
  45. // //res *gitlab.Response
  46. //
  47. // // MR CreateMergeRequestOptions Info
  48. // title = "test"
  49. // description = "test"
  50. // sourceBranch = "chy-saga-test"
  51. // targetBranch = "saga-test-1"
  52. // assigneeID = 15
  53. // projectID = 23
  54. //
  55. // //MR Info
  56. // state string
  57. // status string
  58. // )
  59. //
  60. // // Create CreateMergeRequestOptions Instance
  61. // opt := new(gitlab.CreateMergeRequestOptions)
  62. // opt.Title = &title
  63. // opt.Description = &description
  64. // opt.SourceBranch = &sourceBranch
  65. // opt.TargetBranch = &targetBranch
  66. // opt.AssigneeID = &assigneeID
  67. // opt.TargetProjectID = &projectID
  68. //
  69. // // Create MergeRequest
  70. // mr, _, err = g.client.MergeRequests.CreateMergeRequest(35, opt)
  71. // So(err, ShouldBeNil)
  72. //
  73. // // Query MR Status
  74. // state = mr.State
  75. // status = mr.MergeStatus
  76. // So(state, ShouldEqual, "opened")
  77. // So(status, ShouldEqual, "cannot_be_merged")
  78. //
  79. // // Create MRNote
  80. // noteID, err = g.CreateMRNote(projectID, mr.IID, "CreateMRNote: PASS!")
  81. // So(err, ShouldBeNil)
  82. // So(noteID, ShouldNotBeNil)
  83. //
  84. // // Update MRNote
  85. // err = g.UpdateMRNote(projectID, mr.IID, noteID, "CreateMRNote: PASS!\nUpdateMRNote: PASS!")
  86. // So(err, ShouldBeNil)
  87. //
  88. // // Delete MRNote
  89. // err = g.DeleteMRNote(projectID, mr.IID, noteID)
  90. // So(err, ShouldBeNil)
  91. //
  92. // // Accept MR
  93. // err = g.AcceptMR(projectID, mr.IID, "Accept MR: PASS!")
  94. // So(err, ShouldBeNil)
  95. //
  96. // //Close MR
  97. // //err = g.CloseMR(projectID, mr.IID)
  98. // //So(err, ShouldBeNil)
  99. //
  100. // // Report
  101. // noteID, err = g.CreateMRNote(projectID, mr.IID, "- MR Create Successfully!\n"+
  102. // "- MRNote Create Successfully\n"+
  103. // "- MRNote Update Successfully\n"+
  104. // "- MRNote Delete Successfully\n"+
  105. // "- MR Accept Successfully\n"+
  106. // "- MR Close Successfully")
  107. // So(err, ShouldBeNil)
  108. // })
  109. //}
  110. func TestProjectID(t *testing.T) {
  111. Convey("Test ProjectID", t, func() {
  112. var (
  113. projID int
  114. err error
  115. )
  116. projID, err = g.ProjectID("git@gitlab.bilibili.co:platform/go-common.git")
  117. So(err, ShouldBeNil)
  118. So(projID, ShouldEqual, 23)
  119. })
  120. }
  121. func TestCommitDiff(t *testing.T) {
  122. Convey("Test CommitDiff", t, func() {
  123. var (
  124. err error
  125. files []string
  126. )
  127. files, err = g.CommitDiff(23, "f5c9bfa037771b7f8179db7a245a25695c90be9b")
  128. So(err, ShouldBeNil)
  129. So(files[0], ShouldEqual, ".gitlab-ci.yml")
  130. })
  131. }