You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
4.7 KiB

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