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

convert.go 3.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. api "code.gitea.io/sdk/gitea"
  9. "code.gitea.io/git"
  10. "code.gitea.io/gitea/models"
  11. )
  12. // ToEmail convert models.EmailAddress to api.Email
  13. func ToEmail(email *models.EmailAddress) *api.Email {
  14. return &api.Email{
  15. Email: email.Email,
  16. Verified: email.IsActivated,
  17. Primary: email.IsPrimary,
  18. }
  19. }
  20. // ToBranch convert a commit and branch to an api.Branch
  21. func ToBranch(b *models.Branch, c *git.Commit) *api.Branch {
  22. return &api.Branch{
  23. Name: b.Name,
  24. Commit: ToCommit(c),
  25. }
  26. }
  27. // ToCommit convert a commit to api.PayloadCommit
  28. func ToCommit(c *git.Commit) *api.PayloadCommit {
  29. authorUsername := ""
  30. author, err := models.GetUserByEmail(c.Author.Email)
  31. if err == nil {
  32. authorUsername = author.Name
  33. }
  34. committerUsername := ""
  35. committer, err := models.GetUserByEmail(c.Committer.Email)
  36. if err == nil {
  37. committerUsername = committer.Name
  38. }
  39. return &api.PayloadCommit{
  40. ID: c.ID.String(),
  41. Message: c.Message(),
  42. URL: "Not implemented",
  43. Author: &api.PayloadUser{
  44. Name: c.Author.Name,
  45. Email: c.Author.Email,
  46. UserName: authorUsername,
  47. },
  48. Committer: &api.PayloadUser{
  49. Name: c.Committer.Name,
  50. Email: c.Committer.Email,
  51. UserName: committerUsername,
  52. },
  53. Timestamp: c.Author.When,
  54. }
  55. }
  56. // ToPublicKey convert models.PublicKey to api.PublicKey
  57. func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
  58. return &api.PublicKey{
  59. ID: key.ID,
  60. Key: key.Content,
  61. URL: apiLink + com.ToStr(key.ID),
  62. Title: key.Name,
  63. Created: key.Created,
  64. }
  65. }
  66. // ToHook convert models.Webhook to api.Hook
  67. func ToHook(repoLink string, w *models.Webhook) *api.Hook {
  68. config := map[string]string{
  69. "url": w.URL,
  70. "content_type": w.ContentType.Name(),
  71. }
  72. if w.HookTaskType == models.SLACK {
  73. s := w.GetSlackHook()
  74. config["channel"] = s.Channel
  75. config["username"] = s.Username
  76. config["icon_url"] = s.IconURL
  77. config["color"] = s.Color
  78. }
  79. return &api.Hook{
  80. ID: w.ID,
  81. Type: w.HookTaskType.Name(),
  82. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  83. Active: w.IsActive,
  84. Config: config,
  85. Events: w.EventsArray(),
  86. Updated: w.Updated,
  87. Created: w.Created,
  88. }
  89. }
  90. // ToDeployKey convert models.DeployKey to api.DeployKey
  91. func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
  92. return &api.DeployKey{
  93. ID: key.ID,
  94. Key: key.Content,
  95. URL: apiLink + com.ToStr(key.ID),
  96. Title: key.Name,
  97. Created: key.Created,
  98. ReadOnly: true, // All deploy keys are read-only.
  99. }
  100. }
  101. // ToOrganization convert models.User to api.Organization
  102. func ToOrganization(org *models.User) *api.Organization {
  103. return &api.Organization{
  104. ID: org.ID,
  105. AvatarUrl: org.AvatarLink(),
  106. UserName: org.Name,
  107. FullName: org.FullName,
  108. Description: org.Description,
  109. Website: org.Website,
  110. Location: org.Location,
  111. }
  112. }
  113. // ToTeam convert models.Team to api.Team
  114. func ToTeam(team *models.Team) *api.Team {
  115. return &api.Team{
  116. ID: team.ID,
  117. Name: team.Name,
  118. Description: team.Description,
  119. Permission: team.Authorize.String(),
  120. }
  121. }