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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/util"
  13. )
  14. // ToEmail convert models.EmailAddress to api.Email
  15. func ToEmail(email *models.EmailAddress) *api.Email {
  16. return &api.Email{
  17. Email: email.Email,
  18. Verified: email.IsActivated,
  19. Primary: email.IsPrimary,
  20. }
  21. }
  22. // ToBranch convert a commit and branch to an api.Branch
  23. func ToBranch(repo *models.Repository, b *models.Branch, c *git.Commit) *api.Branch {
  24. return &api.Branch{
  25. Name: b.Name,
  26. Commit: ToCommit(repo, c),
  27. }
  28. }
  29. // ToCommit convert a commit to api.PayloadCommit
  30. func ToCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit {
  31. authorUsername := ""
  32. if author, err := models.GetUserByEmail(c.Author.Email); err == nil {
  33. authorUsername = author.Name
  34. } else if !models.IsErrUserNotExist(err) {
  35. log.Error(4, "GetUserByEmail: %v", err)
  36. }
  37. committerUsername := ""
  38. if committer, err := models.GetUserByEmail(c.Committer.Email); err == nil {
  39. committerUsername = committer.Name
  40. } else if !models.IsErrUserNotExist(err) {
  41. log.Error(4, "GetUserByEmail: %v", err)
  42. }
  43. verif := models.ParseCommitWithSignature(c)
  44. var signature, payload string
  45. if c.Signature != nil {
  46. signature = c.Signature.Signature
  47. payload = c.Signature.Payload
  48. }
  49. return &api.PayloadCommit{
  50. ID: c.ID.String(),
  51. Message: c.Message(),
  52. URL: util.URLJoin(repo.Link(), "commit", c.ID.String()),
  53. Author: &api.PayloadUser{
  54. Name: c.Author.Name,
  55. Email: c.Author.Email,
  56. UserName: authorUsername,
  57. },
  58. Committer: &api.PayloadUser{
  59. Name: c.Committer.Name,
  60. Email: c.Committer.Email,
  61. UserName: committerUsername,
  62. },
  63. Timestamp: c.Author.When,
  64. Verification: &api.PayloadCommitVerification{
  65. Verified: verif.Verified,
  66. Reason: verif.Reason,
  67. Signature: signature,
  68. Payload: payload,
  69. },
  70. }
  71. }
  72. // ToPublicKey convert models.PublicKey to api.PublicKey
  73. func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
  74. return &api.PublicKey{
  75. ID: key.ID,
  76. Key: key.Content,
  77. URL: apiLink + com.ToStr(key.ID),
  78. Title: key.Name,
  79. Fingerprint: key.Fingerprint,
  80. Created: key.CreatedUnix.AsTime(),
  81. }
  82. }
  83. // ToGPGKey converts models.GPGKey to api.GPGKey
  84. func ToGPGKey(key *models.GPGKey) *api.GPGKey {
  85. subkeys := make([]*api.GPGKey, len(key.SubsKey))
  86. for id, k := range key.SubsKey {
  87. subkeys[id] = &api.GPGKey{
  88. ID: k.ID,
  89. PrimaryKeyID: k.PrimaryKeyID,
  90. KeyID: k.KeyID,
  91. PublicKey: k.Content,
  92. Created: k.CreatedUnix.AsTime(),
  93. Expires: k.ExpiredUnix.AsTime(),
  94. CanSign: k.CanSign,
  95. CanEncryptComms: k.CanEncryptComms,
  96. CanEncryptStorage: k.CanEncryptStorage,
  97. CanCertify: k.CanSign,
  98. }
  99. }
  100. emails := make([]*api.GPGKeyEmail, len(key.Emails))
  101. for i, e := range key.Emails {
  102. emails[i] = ToGPGKeyEmail(e)
  103. }
  104. return &api.GPGKey{
  105. ID: key.ID,
  106. PrimaryKeyID: key.PrimaryKeyID,
  107. KeyID: key.KeyID,
  108. PublicKey: key.Content,
  109. Created: key.CreatedUnix.AsTime(),
  110. Expires: key.ExpiredUnix.AsTime(),
  111. Emails: emails,
  112. SubsKey: subkeys,
  113. CanSign: key.CanSign,
  114. CanEncryptComms: key.CanEncryptComms,
  115. CanEncryptStorage: key.CanEncryptStorage,
  116. CanCertify: key.CanSign,
  117. }
  118. }
  119. // ToGPGKeyEmail convert models.EmailAddress to api.GPGKeyEmail
  120. func ToGPGKeyEmail(email *models.EmailAddress) *api.GPGKeyEmail {
  121. return &api.GPGKeyEmail{
  122. Email: email.Email,
  123. Verified: email.IsActivated,
  124. }
  125. }
  126. // ToHook convert models.Webhook to api.Hook
  127. func ToHook(repoLink string, w *models.Webhook) *api.Hook {
  128. config := map[string]string{
  129. "url": w.URL,
  130. "content_type": w.ContentType.Name(),
  131. }
  132. if w.HookTaskType == models.SLACK {
  133. s := w.GetSlackHook()
  134. config["channel"] = s.Channel
  135. config["username"] = s.Username
  136. config["icon_url"] = s.IconURL
  137. config["color"] = s.Color
  138. }
  139. return &api.Hook{
  140. ID: w.ID,
  141. Type: w.HookTaskType.Name(),
  142. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  143. Active: w.IsActive,
  144. Config: config,
  145. Events: w.EventsArray(),
  146. Updated: w.UpdatedUnix.AsTime(),
  147. Created: w.CreatedUnix.AsTime(),
  148. }
  149. }
  150. // ToDeployKey convert models.DeployKey to api.DeployKey
  151. func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
  152. return &api.DeployKey{
  153. ID: key.ID,
  154. KeyID: key.KeyID,
  155. Key: key.Content,
  156. Fingerprint: key.Fingerprint,
  157. URL: apiLink + com.ToStr(key.ID),
  158. Title: key.Name,
  159. Created: key.CreatedUnix.AsTime(),
  160. ReadOnly: key.Mode == models.AccessModeRead, // All deploy keys are read-only.
  161. }
  162. }
  163. // ToOrganization convert models.User to api.Organization
  164. func ToOrganization(org *models.User) *api.Organization {
  165. return &api.Organization{
  166. ID: org.ID,
  167. AvatarURL: org.AvatarLink(),
  168. UserName: org.Name,
  169. FullName: org.FullName,
  170. Description: org.Description,
  171. Website: org.Website,
  172. Location: org.Location,
  173. }
  174. }
  175. // ToTeam convert models.Team to api.Team
  176. func ToTeam(team *models.Team) *api.Team {
  177. return &api.Team{
  178. ID: team.ID,
  179. Name: team.Name,
  180. Description: team.Description,
  181. Permission: team.Authorize.String(),
  182. }
  183. }