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 4.9 kB

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