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 6.5KB

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