Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

convert.go 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. verif := models.ParseCommitWithSignature(c)
  40. var signature, payload string
  41. if c.Signature != nil {
  42. signature = c.Signature.Signature
  43. payload = c.Signature.Payload
  44. }
  45. return &api.PayloadCommit{
  46. ID: c.ID.String(),
  47. Message: c.Message(),
  48. URL: "Not implemented",
  49. Author: &api.PayloadUser{
  50. Name: c.Author.Name,
  51. Email: c.Author.Email,
  52. UserName: authorUsername,
  53. },
  54. Committer: &api.PayloadUser{
  55. Name: c.Committer.Name,
  56. Email: c.Committer.Email,
  57. UserName: committerUsername,
  58. },
  59. Timestamp: c.Author.When,
  60. Verification: &api.PayloadCommitVerification{
  61. Verified: verif.Verified,
  62. Reason: verif.Reason,
  63. Signature: signature,
  64. Payload: payload,
  65. },
  66. }
  67. }
  68. // ToPublicKey convert models.PublicKey to api.PublicKey
  69. func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
  70. return &api.PublicKey{
  71. ID: key.ID,
  72. Key: key.Content,
  73. URL: apiLink + com.ToStr(key.ID),
  74. Title: key.Name,
  75. Fingerprint: key.Fingerprint,
  76. Created: key.Created,
  77. }
  78. }
  79. // ToGPGKey converts models.GPGKey to api.GPGKey
  80. func ToGPGKey(key *models.GPGKey) *api.GPGKey {
  81. subkeys := make([]*api.GPGKey, len(key.SubsKey))
  82. for id, k := range key.SubsKey {
  83. subkeys[id] = &api.GPGKey{
  84. ID: k.ID,
  85. PrimaryKeyID: k.PrimaryKeyID,
  86. KeyID: k.KeyID,
  87. PublicKey: k.Content,
  88. Created: k.Created,
  89. Expires: k.Expired,
  90. CanSign: k.CanSign,
  91. CanEncryptComms: k.CanEncryptComms,
  92. CanEncryptStorage: k.CanEncryptStorage,
  93. CanCertify: k.CanSign,
  94. }
  95. }
  96. emails := make([]*api.GPGKeyEmail, len(key.Emails))
  97. for i, e := range key.Emails {
  98. emails[i] = ToGPGKeyEmail(e)
  99. }
  100. return &api.GPGKey{
  101. ID: key.ID,
  102. PrimaryKeyID: key.PrimaryKeyID,
  103. KeyID: key.KeyID,
  104. PublicKey: key.Content,
  105. Created: key.Created,
  106. Expires: key.Expired,
  107. Emails: emails,
  108. SubsKey: subkeys,
  109. CanSign: key.CanSign,
  110. CanEncryptComms: key.CanEncryptComms,
  111. CanEncryptStorage: key.CanEncryptStorage,
  112. CanCertify: key.CanSign,
  113. }
  114. }
  115. // ToGPGKeyEmail convert models.EmailAddress to api.GPGKeyEmail
  116. func ToGPGKeyEmail(email *models.EmailAddress) *api.GPGKeyEmail {
  117. return &api.GPGKeyEmail{
  118. Email: email.Email,
  119. Verified: email.IsActivated,
  120. }
  121. }
  122. // ToHook convert models.Webhook to api.Hook
  123. func ToHook(repoLink string, w *models.Webhook) *api.Hook {
  124. config := map[string]string{
  125. "url": w.URL,
  126. "content_type": w.ContentType.Name(),
  127. }
  128. if w.HookTaskType == models.SLACK {
  129. s := w.GetSlackHook()
  130. config["channel"] = s.Channel
  131. config["username"] = s.Username
  132. config["icon_url"] = s.IconURL
  133. config["color"] = s.Color
  134. }
  135. return &api.Hook{
  136. ID: w.ID,
  137. Type: w.HookTaskType.Name(),
  138. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  139. Active: w.IsActive,
  140. Config: config,
  141. Events: w.EventsArray(),
  142. Updated: w.Updated,
  143. Created: w.Created,
  144. }
  145. }
  146. // ToDeployKey convert models.DeployKey to api.DeployKey
  147. func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
  148. return &api.DeployKey{
  149. ID: key.ID,
  150. Key: key.Content,
  151. URL: apiLink + com.ToStr(key.ID),
  152. Title: key.Name,
  153. Created: key.Created,
  154. ReadOnly: true, // All deploy keys are read-only.
  155. }
  156. }
  157. // ToOrganization convert models.User to api.Organization
  158. func ToOrganization(org *models.User) *api.Organization {
  159. return &api.Organization{
  160. ID: org.ID,
  161. AvatarURL: org.AvatarLink(),
  162. UserName: org.Name,
  163. FullName: org.FullName,
  164. Description: org.Description,
  165. Website: org.Website,
  166. Location: org.Location,
  167. }
  168. }
  169. // ToTeam convert models.Team to api.Team
  170. func ToTeam(team *models.Team) *api.Team {
  171. return &api.Team{
  172. ID: team.ID,
  173. Name: team.Name,
  174. Description: team.Description,
  175. Permission: team.Authorize.String(),
  176. }
  177. }