convert.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. // Copyright 2015 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package convert
  5. import (
  6. "fmt"
  7. "github.com/Unknwon/com"
  8. "github.com/gogits/git-module"
  9. api "github.com/gogits/go-gogs-client"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/log"
  12. "github.com/gogits/gogs/modules/setting"
  13. )
  14. func ToUser(u *models.User) *api.User {
  15. if u == nil {
  16. return nil
  17. }
  18. return &api.User{
  19. ID: u.Id,
  20. UserName: u.Name,
  21. FullName: u.FullName,
  22. Email: u.Email,
  23. AvatarUrl: u.AvatarLink(),
  24. }
  25. }
  26. func ToEmail(email *models.EmailAddress) *api.Email {
  27. return &api.Email{
  28. Email: email.Email,
  29. Verified: email.IsActivated,
  30. Primary: email.IsPrimary,
  31. }
  32. }
  33. func ToRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository {
  34. cl := repo.CloneLink()
  35. return &api.Repository{
  36. ID: repo.ID,
  37. Owner: ToUser(owner),
  38. FullName: owner.Name + "/" + repo.Name,
  39. Description: repo.Description,
  40. Private: repo.IsPrivate,
  41. Fork: repo.IsFork,
  42. HtmlUrl: setting.AppUrl + owner.Name + "/" + repo.Name,
  43. CloneUrl: cl.HTTPS,
  44. SshUrl: cl.SSH,
  45. OpenIssues: repo.NumOpenIssues,
  46. Stars: repo.NumStars,
  47. Forks: repo.NumForks,
  48. Watchers: repo.NumWatches,
  49. Permissions: permission,
  50. }
  51. }
  52. func ToBranch(b *models.Branch, c *git.Commit) *api.Branch {
  53. return &api.Branch{
  54. Name: b.Name,
  55. Commit: ToCommit(c),
  56. }
  57. }
  58. func ToCommit(c *git.Commit) *api.PayloadCommit {
  59. return &api.PayloadCommit{
  60. ID: c.ID.String(),
  61. Message: c.Message(),
  62. URL: "Not implemented",
  63. Author: &api.PayloadAuthor{
  64. Name: c.Committer.Name,
  65. Email: c.Committer.Email,
  66. /* UserName: c.Committer.UserName, */
  67. },
  68. }
  69. }
  70. func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
  71. return &api.PublicKey{
  72. ID: key.ID,
  73. Key: key.Content,
  74. URL: apiLink + com.ToStr(key.ID),
  75. Title: key.Name,
  76. Created: key.Created,
  77. }
  78. }
  79. func ToHook(repoLink string, w *models.Webhook) *api.Hook {
  80. config := map[string]string{
  81. "url": w.URL,
  82. "content_type": w.ContentType.Name(),
  83. }
  84. if w.HookTaskType == models.SLACK {
  85. s := w.GetSlackHook()
  86. config["channel"] = s.Channel
  87. config["username"] = s.Username
  88. config["icon_url"] = s.IconURL
  89. config["color"] = s.Color
  90. }
  91. return &api.Hook{
  92. ID: w.ID,
  93. Type: w.HookTaskType.Name(),
  94. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  95. Active: w.IsActive,
  96. Config: config,
  97. Events: w.EventsArray(),
  98. Updated: w.Updated,
  99. Created: w.Created,
  100. }
  101. }
  102. func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
  103. return &api.DeployKey{
  104. ID: key.ID,
  105. Key: key.Content,
  106. URL: apiLink + com.ToStr(key.ID),
  107. Title: key.Name,
  108. Created: key.Created,
  109. ReadOnly: true, // All deploy keys are read-only.
  110. }
  111. }
  112. func ToLabel(label *models.Label) *api.Label {
  113. return &api.Label{
  114. Name: label.Name,
  115. Color: label.Color,
  116. }
  117. }
  118. func ToMilestone(milestone *models.Milestone) *api.Milestone {
  119. if milestone == nil {
  120. return nil
  121. }
  122. apiMilestone := &api.Milestone{
  123. ID: milestone.ID,
  124. State: milestone.State(),
  125. Title: milestone.Name,
  126. Description: milestone.Content,
  127. OpenIssues: milestone.NumOpenIssues,
  128. ClosedIssues: milestone.NumClosedIssues,
  129. }
  130. if milestone.IsClosed {
  131. apiMilestone.Closed = &milestone.ClosedDate
  132. }
  133. if milestone.Deadline.Year() < 9999 {
  134. apiMilestone.Deadline = &milestone.Deadline
  135. }
  136. return apiMilestone
  137. }
  138. func ToIssue(issue *models.Issue) *api.Issue {
  139. apiLabels := make([]*api.Label, len(issue.Labels))
  140. for i := range issue.Labels {
  141. apiLabels[i] = ToLabel(issue.Labels[i])
  142. }
  143. apiIssue := &api.Issue{
  144. ID: issue.ID,
  145. Index: issue.Index,
  146. State: issue.State(),
  147. Title: issue.Name,
  148. Body: issue.Content,
  149. User: ToUser(issue.Poster),
  150. Labels: apiLabels,
  151. Assignee: ToUser(issue.Assignee),
  152. Milestone: ToMilestone(issue.Milestone),
  153. Comments: issue.NumComments,
  154. Created: issue.Created,
  155. Updated: issue.Updated,
  156. }
  157. if issue.IsPull {
  158. if err := issue.GetPullRequest(); err != nil {
  159. log.Error(4, "GetPullRequest", err)
  160. } else {
  161. apiIssue.PullRequest = &api.PullRequestMeta{
  162. HasMerged: issue.PullRequest.HasMerged,
  163. }
  164. if issue.PullRequest.HasMerged {
  165. apiIssue.PullRequest.Merged = &issue.PullRequest.Merged
  166. }
  167. }
  168. }
  169. return apiIssue
  170. }
  171. func ToOrganization(org *models.User) *api.Organization {
  172. return &api.Organization{
  173. ID: org.Id,
  174. AvatarUrl: org.AvatarLink(),
  175. UserName: org.Name,
  176. FullName: org.FullName,
  177. Description: org.Description,
  178. Website: org.Website,
  179. Location: org.Location,
  180. }
  181. }
  182. func ToTeam(team *models.Team) *api.Team {
  183. return &api.Team{
  184. ID: team.ID,
  185. Name: team.Name,
  186. Description: team.Description,
  187. Permission: team.Authorize.String(),
  188. }
  189. }