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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/git"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/markup"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/modules/util"
  14. "github.com/Unknwon/com"
  15. )
  16. // ToEmail convert models.EmailAddress to api.Email
  17. func ToEmail(email *models.EmailAddress) *api.Email {
  18. return &api.Email{
  19. Email: email.Email,
  20. Verified: email.IsActivated,
  21. Primary: email.IsPrimary,
  22. }
  23. }
  24. // ToBranch convert a git.Commit and git.Branch to an api.Branch
  25. func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit) *api.Branch {
  26. return &api.Branch{
  27. Name: b.Name,
  28. Commit: ToCommit(repo, c),
  29. }
  30. }
  31. // ToTag convert a git.Tag to an api.Tag
  32. func ToTag(repo *models.Repository, t *git.Tag) *api.Tag {
  33. return &api.Tag{
  34. Name: t.Name,
  35. ID: t.ID.String(),
  36. Commit: ToCommitMeta(repo, t),
  37. ZipballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".zip"),
  38. TarballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".tar.gz"),
  39. }
  40. }
  41. // ToCommit convert a git.Commit to api.PayloadCommit
  42. func ToCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit {
  43. authorUsername := ""
  44. if author, err := models.GetUserByEmail(c.Author.Email); err == nil {
  45. authorUsername = author.Name
  46. } else if !models.IsErrUserNotExist(err) {
  47. log.Error("GetUserByEmail: %v", err)
  48. }
  49. committerUsername := ""
  50. if committer, err := models.GetUserByEmail(c.Committer.Email); err == nil {
  51. committerUsername = committer.Name
  52. } else if !models.IsErrUserNotExist(err) {
  53. log.Error("GetUserByEmail: %v", err)
  54. }
  55. return &api.PayloadCommit{
  56. ID: c.ID.String(),
  57. Message: c.Message(),
  58. URL: util.URLJoin(repo.HTMLURL(), "commit", c.ID.String()),
  59. Author: &api.PayloadUser{
  60. Name: c.Author.Name,
  61. Email: c.Author.Email,
  62. UserName: authorUsername,
  63. },
  64. Committer: &api.PayloadUser{
  65. Name: c.Committer.Name,
  66. Email: c.Committer.Email,
  67. UserName: committerUsername,
  68. },
  69. Timestamp: c.Author.When,
  70. Verification: ToVerification(c),
  71. }
  72. }
  73. // ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification
  74. func ToVerification(c *git.Commit) *api.PayloadCommitVerification {
  75. verif := models.ParseCommitWithSignature(c)
  76. var signature, payload string
  77. if c.Signature != nil {
  78. signature = c.Signature.Signature
  79. payload = c.Signature.Payload
  80. }
  81. return &api.PayloadCommitVerification{
  82. Verified: verif.Verified,
  83. Reason: verif.Reason,
  84. Signature: signature,
  85. Payload: payload,
  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. Visibility: org.Visibility.String(),
  198. }
  199. }
  200. // ToTeam convert models.Team to api.Team
  201. func ToTeam(team *models.Team) *api.Team {
  202. return &api.Team{
  203. ID: team.ID,
  204. Name: team.Name,
  205. Description: team.Description,
  206. Permission: team.Authorize.String(),
  207. Units: team.GetUnitNames(),
  208. }
  209. }
  210. // ToUser convert models.User to api.User
  211. func ToUser(user *models.User, signed, authed bool) *api.User {
  212. result := &api.User{
  213. UserName: user.Name,
  214. AvatarURL: user.AvatarLink(),
  215. FullName: markup.Sanitize(user.FullName),
  216. Created: user.CreatedUnix.AsTime(),
  217. }
  218. // hide primary email if API caller isn't user itself or an admin
  219. if !signed {
  220. result.Email = ""
  221. } else if user.KeepEmailPrivate && !authed {
  222. result.Email = user.GetEmail()
  223. } else { // only user himself and admin could visit these information
  224. result.ID = user.ID
  225. result.Email = user.Email
  226. result.IsAdmin = user.IsAdmin
  227. result.LastLogin = user.LastLoginUnix.AsTime()
  228. }
  229. return result
  230. }
  231. // ToAnnotatedTag convert git.Tag to api.AnnotatedTag
  232. func ToAnnotatedTag(repo *models.Repository, t *git.Tag, c *git.Commit) *api.AnnotatedTag {
  233. return &api.AnnotatedTag{
  234. Tag: t.Name,
  235. SHA: t.ID.String(),
  236. Object: ToAnnotatedTagObject(repo, c),
  237. Message: t.Message,
  238. URL: util.URLJoin(repo.APIURL(), "git/tags", t.ID.String()),
  239. Tagger: ToCommitUser(t.Tagger),
  240. Verification: ToVerification(c),
  241. }
  242. }
  243. // ToAnnotatedTagObject convert a git.Commit to an api.AnnotatedTagObject
  244. func ToAnnotatedTagObject(repo *models.Repository, commit *git.Commit) *api.AnnotatedTagObject {
  245. return &api.AnnotatedTagObject{
  246. SHA: commit.ID.String(),
  247. Type: string(git.ObjectCommit),
  248. URL: util.URLJoin(repo.APIURL(), "git/commits", commit.ID.String()),
  249. }
  250. }
  251. // ToCommitUser convert a git.Signature to an api.CommitUser
  252. func ToCommitUser(sig *git.Signature) *api.CommitUser {
  253. return &api.CommitUser{
  254. Identity: api.Identity{
  255. Name: sig.Name,
  256. Email: sig.Email,
  257. },
  258. Date: sig.When.UTC().Format(time.RFC3339),
  259. }
  260. }
  261. // ToCommitMeta convert a git.Tag to an api.CommitMeta
  262. func ToCommitMeta(repo *models.Repository, tag *git.Tag) *api.CommitMeta {
  263. return &api.CommitMeta{
  264. SHA: tag.Object.String(),
  265. // TODO: Add the /commits API endpoint and use it here (https://developer.github.com/v3/repos/commits/#get-a-single-commit)
  266. URL: util.URLJoin(repo.APIURL(), "git/commits", tag.ID.String()),
  267. }
  268. }