gitlab.go 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. package gitlab
  2. import (
  3. "context"
  4. "time"
  5. "github.com/pkg/errors"
  6. "github.com/xanzy/go-gitlab"
  7. )
  8. const (
  9. _defaultPerPage = 20
  10. _maxPerPage = 100
  11. )
  12. // Gitlab def
  13. type Gitlab struct {
  14. url string
  15. token string
  16. client *gitlab.Client
  17. }
  18. // New new gitlab structure
  19. func New(url string, token string) (g *Gitlab) {
  20. g = &Gitlab{
  21. url: url,
  22. token: token,
  23. client: gitlab.NewClient(nil, token),
  24. }
  25. g.client.SetBaseURL(url)
  26. return
  27. }
  28. // ListProjects list all project
  29. func (g *Gitlab) ListProjects(page int) (projects []*gitlab.Project, err error) {
  30. opt := &gitlab.ListProjectsOptions{}
  31. opt.ListOptions.Page = page
  32. opt.ListOptions.PerPage = _defaultPerPage
  33. if projects, _, err = g.client.Projects.ListProjects(opt); err != nil {
  34. err = errors.Wrapf(err, "ListProjects err(%+v)", err)
  35. return
  36. }
  37. return
  38. }
  39. // ListProjectCommit ...
  40. func (g *Gitlab) ListProjectCommit(projectID, page int, since, until *time.Time) (commits []*gitlab.Commit, resp *gitlab.Response, err error) {
  41. var (
  42. opt = &gitlab.ListCommitsOptions{}
  43. all = true
  44. )
  45. opt.Page = page
  46. opt.PerPage = _maxPerPage
  47. opt.All = &all
  48. if since != nil {
  49. opt.Since = since
  50. }
  51. if until != nil {
  52. opt.Until = until
  53. }
  54. if commits, resp, err = g.client.Commits.ListCommits(projectID, opt); err != nil {
  55. err = errors.Wrapf(err, "ListCommits projectId(%d), err(%+v)", projectID, err)
  56. return
  57. }
  58. return
  59. }
  60. // ListProjectMergeRequests list all MR
  61. func (g *Gitlab) ListProjectMergeRequests(projectID int, since, until *time.Time, page int) (mrs []*gitlab.MergeRequest, resp *gitlab.Response, err error) {
  62. opt := &gitlab.ListProjectMergeRequestsOptions{}
  63. opt.UpdatedAfter = since
  64. opt.UpdatedBefore = until
  65. if page != -1 {
  66. opt.PerPage = _defaultPerPage
  67. opt.Page = page
  68. }
  69. if mrs, resp, err = g.client.MergeRequests.ListProjectMergeRequests(projectID, opt); err != nil {
  70. err = errors.Wrapf(err, "ListProjectMergeRequests projectId(%d), err(%+v)", projectID, err)
  71. return
  72. }
  73. return
  74. }
  75. // ListProjectPipelines list all pipelines
  76. func (g *Gitlab) ListProjectPipelines(page, projectID int, status gitlab.BuildStateValue) (pipelineList gitlab.PipelineList, resp *gitlab.Response, err error) {
  77. opt := &gitlab.ListProjectPipelinesOptions{}
  78. opt.ListOptions.Page = page
  79. if status != "" {
  80. opt.Status = gitlab.BuildState(status)
  81. }
  82. if pipelineList, resp, err = g.client.Pipelines.ListProjectPipelines(projectID, opt); err != nil {
  83. err = errors.Wrapf(err, "ListProjectPipelines projectId(%d), err(%+v)", projectID, err)
  84. return
  85. }
  86. return
  87. }
  88. // ListPipelineJobs list all pipeline jobs
  89. func (g *Gitlab) ListPipelineJobs(opt *gitlab.ListJobsOptions, projectID, pipelineID int) (jobList []*gitlab.Job, resp *gitlab.Response, err error) {
  90. if jobList, resp, err = g.client.Jobs.ListPipelineJobs(projectID, pipelineID, opt); err != nil {
  91. err = errors.Wrapf(err, "ListPipelineJobs projectId(%d), pipelineId(%d), err(%+v)", projectID, pipelineID, err)
  92. return
  93. }
  94. return
  95. }
  96. // GetPipeline get a pipeline info
  97. func (g *Gitlab) GetPipeline(projectID, pipelineID int) (pipeline *gitlab.Pipeline, resp *gitlab.Response, err error) {
  98. if pipeline, resp, err = g.client.Pipelines.GetPipeline(projectID, pipelineID); err != nil {
  99. err = errors.Wrapf(err, "GetPipeline projectId(%d), err(%+v)", projectID, err)
  100. return
  101. }
  102. return
  103. }
  104. // ListProjectBranch ...
  105. func (g *Gitlab) ListProjectBranch(projectID, page int) (branches []*gitlab.Branch, resp *gitlab.Response, err error) {
  106. var opt = &gitlab.ListBranchesOptions{Page: page, PerPage: 20}
  107. if branches, resp, err = g.client.Branches.ListBranches(projectID, opt); err != nil {
  108. err = errors.Wrapf(err, "ListBranches projectId(%d), err(%+v)", projectID, err)
  109. return
  110. }
  111. return
  112. }
  113. // ListMRNotes get notes of the MR
  114. func (g *Gitlab) ListMRNotes(c context.Context, projectID, mrID, page int) (notes []*gitlab.Note, resp *gitlab.Response, err error) {
  115. var opt = &gitlab.ListMergeRequestNotesOptions{Page: page, PerPage: 20}
  116. if notes, resp, err = g.client.Notes.ListMergeRequestNotes(projectID, mrID, opt); err != nil {
  117. err = errors.Wrapf(err, "ListMergeRequestNotes projectId(%d), mrId(%d), err(%+v)", projectID, mrID, err)
  118. return
  119. }
  120. return
  121. }
  122. // MergeBase 获取两个分支最近的合并commit
  123. func (g *Gitlab) MergeBase(c context.Context, projectID int, refs []string) (commit *gitlab.Commit, resp *gitlab.Response, err error) {
  124. var opt = &gitlab.MergeBaseOptions{Ref: refs}
  125. if commit, resp, err = g.client.Repositories.MergeBase(projectID, opt); err != nil {
  126. err = errors.Wrapf(err, "MergeBase projectId(%d), refs(%v), err(%+v)", projectID, refs, err)
  127. return
  128. }
  129. return
  130. }
  131. // ListMRAwardEmoji ...
  132. func (g *Gitlab) ListMRAwardEmoji(projectID, mrIID, page int) (emojis []*gitlab.AwardEmoji, resp *gitlab.Response, err error) {
  133. var opt = &gitlab.ListAwardEmojiOptions{Page: page, PerPage: _defaultPerPage}
  134. if emojis, resp, err = g.client.AwardEmoji.ListMergeRequestAwardEmoji(projectID, mrIID, opt); err != nil {
  135. err = errors.Wrapf(err, "ListMergeRequestAwardEmoji projectId(%d), mrIID(%d), err(%+v)", projectID, mrIID, err)
  136. return
  137. }
  138. return
  139. }
  140. //ListMRDiscussions ...
  141. func (g *Gitlab) ListMRDiscussions(projectID, mrIID, page int) (result []*gitlab.Discussion, resp *gitlab.Response, err error) {
  142. var opt = &gitlab.ListMergeRequestDiscussionsOptions{Page: page, PerPage: _defaultPerPage}
  143. if result, resp, err = g.client.Discussions.ListMergeRequestDiscussions(projectID, mrIID, opt); err != nil {
  144. err = errors.Wrapf(err, "ListMergeRequestDiscussions projectId(%d), mrIID(%d), err(%+v)", projectID, mrIID, err)
  145. return
  146. }
  147. return
  148. }
  149. // GetMergeRequestDiff ...
  150. func (g *Gitlab) GetMergeRequestDiff(projectID, mrID int) (mr *gitlab.MergeRequest, resp *gitlab.Response, err error) {
  151. if mr, resp, err = g.client.MergeRequests.GetMergeRequestChanges(projectID, mrID); err != nil {
  152. err = errors.Wrapf(err, "GetMergeRequestChanges projectId(%d), err(%+v)", projectID, err)
  153. return
  154. }
  155. return
  156. }
  157. // ListProjectJobs get jobs of the project
  158. func (g *Gitlab) ListProjectJobs(projID, page int) (jobs []gitlab.Job, resp *gitlab.Response, err error) {
  159. var opt = &gitlab.ListJobsOptions{}
  160. opt.Page = page
  161. // perPage max is 100
  162. opt.PerPage = _maxPerPage
  163. if jobs, resp, err = g.client.Jobs.ListProjectJobs(projID, opt); err != nil {
  164. err = errors.Wrapf(err, "ListProjectJobs projectId(%d), err(%+v)", projID, err)
  165. return
  166. }
  167. return
  168. }
  169. // ListProjectMembers ...
  170. func (g *Gitlab) ListProjectMembers(projectID, page int) (members []*gitlab.ProjectMember, resp *gitlab.Response, err error) {
  171. opt := &gitlab.ListProjectMembersOptions{}
  172. opt.Page = page
  173. opt.PerPage = _defaultPerPage
  174. if members, resp, err = g.client.ProjectMembers.ListProjectMembers(projectID, opt); err != nil {
  175. err = errors.Wrapf(err, "ListProjectMembers projectId(%d), err(%+v)", projectID, err)
  176. return
  177. }
  178. return
  179. }
  180. // ListProjectIssues ...
  181. func (g *Gitlab) ListProjectIssues(projID, page int, since, until *time.Time) (issues []*gitlab.Issue, resp *gitlab.Response, err error) {
  182. var opt = &gitlab.ListProjectIssuesOptions{}
  183. opt.Page = page
  184. // perPage max is 100
  185. opt.PerPage = _maxPerPage
  186. if since != nil {
  187. opt.CreatedBefore = until
  188. }
  189. if until != nil {
  190. opt.CreatedAfter = since
  191. }
  192. if issues, resp, err = g.client.Issues.ListProjectIssues(projID, opt); err != nil {
  193. err = errors.Wrapf(err, "ListProjectIssues projectId(%d), err(%+v)", projID, err)
  194. return
  195. }
  196. return
  197. }
  198. // ListProjectRunners ...
  199. func (g *Gitlab) ListProjectRunners(projID, page int) (result []*gitlab.Runner, resp *gitlab.Response, err error) {
  200. var opt = &gitlab.ListProjectRunnersOptions{}
  201. opt.ListOptions.Page = page
  202. opt.PerPage = _defaultPerPage
  203. if result, resp, err = g.client.Runners.ListProjectRunners(projID, opt); err != nil {
  204. err = errors.Wrapf(err, "ListProjectRunners projectId(%d), err(%+v)", projID, err)
  205. return
  206. }
  207. return
  208. }