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

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