convert.go 4.9 KB

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