convert.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. Created: repo.Created,
  50. Updated: repo.Updated,
  51. Permissions: permission,
  52. }
  53. }
  54. func ToBranch(b *models.Branch, c *git.Commit) *api.Branch {
  55. return &api.Branch{
  56. Name: b.Name,
  57. Commit: ToCommit(c),
  58. }
  59. }
  60. func ToCommit(c *git.Commit) *api.PayloadCommit {
  61. authorUsername := ""
  62. author, err := models.GetUserByEmail(c.Author.Email)
  63. if err == nil {
  64. authorUsername = author.Name
  65. }
  66. committerUsername := ""
  67. committer, err := models.GetUserByEmail(c.Committer.Email)
  68. if err == nil {
  69. committerUsername = committer.Name
  70. }
  71. return &api.PayloadCommit{
  72. ID: c.ID.String(),
  73. Message: c.Message(),
  74. URL: "Not implemented",
  75. Author: &api.PayloadAuthor{
  76. Name: c.Author.Name,
  77. Email: c.Author.Email,
  78. UserName: authorUsername,
  79. },
  80. Committer: &api.PayloadCommitter{
  81. Name: c.Committer.Name,
  82. Email: c.Committer.Email,
  83. UserName: committerUsername,
  84. },
  85. Timestamp: c.Author.When,
  86. }
  87. }
  88. func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
  89. return &api.PublicKey{
  90. ID: key.ID,
  91. Key: key.Content,
  92. URL: apiLink + com.ToStr(key.ID),
  93. Title: key.Name,
  94. Created: key.Created,
  95. }
  96. }
  97. func ToHook(repoLink string, w *models.Webhook) *api.Hook {
  98. config := map[string]string{
  99. "url": w.URL,
  100. "content_type": w.ContentType.Name(),
  101. }
  102. if w.HookTaskType == models.SLACK {
  103. s := w.GetSlackHook()
  104. config["channel"] = s.Channel
  105. config["username"] = s.Username
  106. config["icon_url"] = s.IconURL
  107. config["color"] = s.Color
  108. }
  109. return &api.Hook{
  110. ID: w.ID,
  111. Type: w.HookTaskType.Name(),
  112. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  113. Active: w.IsActive,
  114. Config: config,
  115. Events: w.EventsArray(),
  116. Updated: w.Updated,
  117. Created: w.Created,
  118. }
  119. }
  120. func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
  121. return &api.DeployKey{
  122. ID: key.ID,
  123. Key: key.Content,
  124. URL: apiLink + com.ToStr(key.ID),
  125. Title: key.Name,
  126. Created: key.Created,
  127. ReadOnly: true, // All deploy keys are read-only.
  128. }
  129. }
  130. func ToLabel(label *models.Label) *api.Label {
  131. return &api.Label{
  132. ID: label.ID,
  133. Name: label.Name,
  134. Color: label.Color,
  135. }
  136. }
  137. func ToMilestone(milestone *models.Milestone) *api.Milestone {
  138. if milestone == nil {
  139. return nil
  140. }
  141. apiMilestone := &api.Milestone{
  142. ID: milestone.ID,
  143. State: milestone.State(),
  144. Title: milestone.Name,
  145. Description: milestone.Content,
  146. OpenIssues: milestone.NumOpenIssues,
  147. ClosedIssues: milestone.NumClosedIssues,
  148. }
  149. if milestone.IsClosed {
  150. apiMilestone.Closed = &milestone.ClosedDate
  151. }
  152. if milestone.Deadline.Year() < 9999 {
  153. apiMilestone.Deadline = &milestone.Deadline
  154. }
  155. return apiMilestone
  156. }
  157. func ToIssue(issue *models.Issue) *api.Issue {
  158. apiLabels := make([]*api.Label, len(issue.Labels))
  159. for i := range issue.Labels {
  160. apiLabels[i] = ToLabel(issue.Labels[i])
  161. }
  162. apiIssue := &api.Issue{
  163. ID: issue.ID,
  164. Index: issue.Index,
  165. State: issue.State(),
  166. Title: issue.Name,
  167. Body: issue.Content,
  168. User: ToUser(issue.Poster),
  169. Labels: apiLabels,
  170. Assignee: ToUser(issue.Assignee),
  171. Milestone: ToMilestone(issue.Milestone),
  172. Comments: issue.NumComments,
  173. Created: issue.Created,
  174. Updated: issue.Updated,
  175. }
  176. if issue.IsPull {
  177. if err := issue.GetPullRequest(); err != nil {
  178. log.Error(4, "GetPullRequest", err)
  179. } else {
  180. apiIssue.PullRequest = &api.PullRequestMeta{
  181. HasMerged: issue.PullRequest.HasMerged,
  182. }
  183. if issue.PullRequest.HasMerged {
  184. apiIssue.PullRequest.Merged = &issue.PullRequest.Merged
  185. }
  186. }
  187. }
  188. return apiIssue
  189. }
  190. func ToOrganization(org *models.User) *api.Organization {
  191. return &api.Organization{
  192. ID: org.ID,
  193. AvatarUrl: org.AvatarLink(),
  194. UserName: org.Name,
  195. FullName: org.FullName,
  196. Description: org.Description,
  197. Website: org.Website,
  198. Location: org.Location,
  199. }
  200. }
  201. func ToTeam(team *models.Team) *api.Team {
  202. return &api.Team{
  203. ID: team.ID,
  204. Name: team.Name,
  205. Description: team.Description,
  206. Permission: team.Authorize.String(),
  207. }
  208. }