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.

convert.go 5.2KB

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