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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. "code.gitea.io/gitea/modules/structs"
  13. api "code.gitea.io/gitea/modules/structs"
  14. "code.gitea.io/gitea/modules/util"
  15. "github.com/unknwon/com"
  16. )
  17. // ToEmail convert models.EmailAddress to api.Email
  18. func ToEmail(email *models.EmailAddress) *api.Email {
  19. return &api.Email{
  20. Email: email.Email,
  21. Verified: email.IsActivated,
  22. Primary: email.IsPrimary,
  23. }
  24. }
  25. // ToBranch convert a git.Commit and git.Branch to an api.Branch
  26. func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit) *api.Branch {
  27. return &api.Branch{
  28. Name: b.Name,
  29. Commit: ToCommit(repo, c),
  30. }
  31. }
  32. // ToTag convert a git.Tag to an api.Tag
  33. func ToTag(repo *models.Repository, t *git.Tag) *api.Tag {
  34. return &api.Tag{
  35. Name: t.Name,
  36. ID: t.ID.String(),
  37. Commit: ToCommitMeta(repo, t),
  38. ZipballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".zip"),
  39. TarballURL: util.URLJoin(repo.HTMLURL(), "archive", t.Name+".tar.gz"),
  40. }
  41. }
  42. // ToCommit convert a git.Commit to api.PayloadCommit
  43. func ToCommit(repo *models.Repository, c *git.Commit) *api.PayloadCommit {
  44. authorUsername := ""
  45. if author, err := models.GetUserByEmail(c.Author.Email); err == nil {
  46. authorUsername = author.Name
  47. } else if !models.IsErrUserNotExist(err) {
  48. log.Error("GetUserByEmail: %v", err)
  49. }
  50. committerUsername := ""
  51. if committer, err := models.GetUserByEmail(c.Committer.Email); err == nil {
  52. committerUsername = committer.Name
  53. } else if !models.IsErrUserNotExist(err) {
  54. log.Error("GetUserByEmail: %v", err)
  55. }
  56. return &api.PayloadCommit{
  57. ID: c.ID.String(),
  58. Message: c.Message(),
  59. URL: util.URLJoin(repo.HTMLURL(), "commit", c.ID.String()),
  60. Author: &api.PayloadUser{
  61. Name: c.Author.Name,
  62. Email: c.Author.Email,
  63. UserName: authorUsername,
  64. },
  65. Committer: &api.PayloadUser{
  66. Name: c.Committer.Name,
  67. Email: c.Committer.Email,
  68. UserName: committerUsername,
  69. },
  70. Timestamp: c.Author.When,
  71. Verification: ToVerification(c),
  72. }
  73. }
  74. // ToVerification convert a git.Commit.Signature to an api.PayloadCommitVerification
  75. func ToVerification(c *git.Commit) *api.PayloadCommitVerification {
  76. verif := models.ParseCommitWithSignature(c)
  77. commitVerification := &api.PayloadCommitVerification{
  78. Verified: verif.Verified,
  79. Reason: verif.Reason,
  80. }
  81. if c.Signature != nil {
  82. commitVerification.Signature = c.Signature.Signature
  83. commitVerification.Payload = c.Signature.Payload
  84. }
  85. if verif.SigningUser != nil {
  86. commitVerification.Signer = &structs.PayloadUser{
  87. Name: verif.SigningUser.Name,
  88. Email: verif.SigningUser.Email,
  89. }
  90. }
  91. return commitVerification
  92. }
  93. // ToPublicKey convert models.PublicKey to api.PublicKey
  94. func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
  95. return &api.PublicKey{
  96. ID: key.ID,
  97. Key: key.Content,
  98. URL: apiLink + com.ToStr(key.ID),
  99. Title: key.Name,
  100. Fingerprint: key.Fingerprint,
  101. Created: key.CreatedUnix.AsTime(),
  102. }
  103. }
  104. // ToGPGKey converts models.GPGKey to api.GPGKey
  105. func ToGPGKey(key *models.GPGKey) *api.GPGKey {
  106. subkeys := make([]*api.GPGKey, len(key.SubsKey))
  107. for id, k := range key.SubsKey {
  108. subkeys[id] = &api.GPGKey{
  109. ID: k.ID,
  110. PrimaryKeyID: k.PrimaryKeyID,
  111. KeyID: k.KeyID,
  112. PublicKey: k.Content,
  113. Created: k.CreatedUnix.AsTime(),
  114. Expires: k.ExpiredUnix.AsTime(),
  115. CanSign: k.CanSign,
  116. CanEncryptComms: k.CanEncryptComms,
  117. CanEncryptStorage: k.CanEncryptStorage,
  118. CanCertify: k.CanSign,
  119. }
  120. }
  121. emails := make([]*api.GPGKeyEmail, len(key.Emails))
  122. for i, e := range key.Emails {
  123. emails[i] = ToGPGKeyEmail(e)
  124. }
  125. return &api.GPGKey{
  126. ID: key.ID,
  127. PrimaryKeyID: key.PrimaryKeyID,
  128. KeyID: key.KeyID,
  129. PublicKey: key.Content,
  130. Created: key.CreatedUnix.AsTime(),
  131. Expires: key.ExpiredUnix.AsTime(),
  132. Emails: emails,
  133. SubsKey: subkeys,
  134. CanSign: key.CanSign,
  135. CanEncryptComms: key.CanEncryptComms,
  136. CanEncryptStorage: key.CanEncryptStorage,
  137. CanCertify: key.CanSign,
  138. }
  139. }
  140. // ToGPGKeyEmail convert models.EmailAddress to api.GPGKeyEmail
  141. func ToGPGKeyEmail(email *models.EmailAddress) *api.GPGKeyEmail {
  142. return &api.GPGKeyEmail{
  143. Email: email.Email,
  144. Verified: email.IsActivated,
  145. }
  146. }
  147. // ToHook convert models.Webhook to api.Hook
  148. func ToHook(repoLink string, w *models.Webhook) *api.Hook {
  149. config := map[string]string{
  150. "url": w.URL,
  151. "content_type": w.ContentType.Name(),
  152. }
  153. if w.HookTaskType == models.SLACK {
  154. s := w.GetSlackHook()
  155. config["channel"] = s.Channel
  156. config["username"] = s.Username
  157. config["icon_url"] = s.IconURL
  158. config["color"] = s.Color
  159. }
  160. return &api.Hook{
  161. ID: w.ID,
  162. Type: w.HookTaskType.Name(),
  163. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  164. Active: w.IsActive,
  165. Config: config,
  166. Events: w.EventsArray(),
  167. Updated: w.UpdatedUnix.AsTime(),
  168. Created: w.CreatedUnix.AsTime(),
  169. }
  170. }
  171. // ToGitHook convert git.Hook to api.GitHook
  172. func ToGitHook(h *git.Hook) *api.GitHook {
  173. return &api.GitHook{
  174. Name: h.Name(),
  175. IsActive: h.IsActive,
  176. Content: h.Content,
  177. }
  178. }
  179. // ToDeployKey convert models.DeployKey to api.DeployKey
  180. func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
  181. return &api.DeployKey{
  182. ID: key.ID,
  183. KeyID: key.KeyID,
  184. Key: key.Content,
  185. Fingerprint: key.Fingerprint,
  186. URL: apiLink + com.ToStr(key.ID),
  187. Title: key.Name,
  188. Created: key.CreatedUnix.AsTime(),
  189. ReadOnly: key.Mode == models.AccessModeRead, // All deploy keys are read-only.
  190. }
  191. }
  192. // ToOrganization convert models.User to api.Organization
  193. func ToOrganization(org *models.User) *api.Organization {
  194. return &api.Organization{
  195. ID: org.ID,
  196. AvatarURL: org.AvatarLink(),
  197. UserName: org.Name,
  198. FullName: org.FullName,
  199. Description: org.Description,
  200. Website: org.Website,
  201. Location: org.Location,
  202. Visibility: org.Visibility.String(),
  203. RepoAdminChangeTeamAccess: org.RepoAdminChangeTeamAccess,
  204. }
  205. }
  206. // ToTeam convert models.Team to api.Team
  207. func ToTeam(team *models.Team) *api.Team {
  208. return &api.Team{
  209. ID: team.ID,
  210. Name: team.Name,
  211. Description: team.Description,
  212. Permission: team.Authorize.String(),
  213. Units: team.GetUnitNames(),
  214. }
  215. }
  216. // ToUser convert models.User to api.User
  217. func ToUser(user *models.User, signed, authed bool) *api.User {
  218. result := &api.User{
  219. ID: user.ID,
  220. UserName: user.Name,
  221. AvatarURL: user.AvatarLink(),
  222. FullName: markup.Sanitize(user.FullName),
  223. IsAdmin: user.IsAdmin,
  224. LastLogin: user.LastLoginUnix.AsTime(),
  225. Created: user.CreatedUnix.AsTime(),
  226. }
  227. // hide primary email if API caller isn't user itself or an admin
  228. if !signed {
  229. result.Email = ""
  230. } else if user.KeepEmailPrivate && !authed {
  231. result.Email = user.GetEmail()
  232. } else {
  233. result.Email = user.Email
  234. }
  235. return result
  236. }
  237. // ToAnnotatedTag convert git.Tag to api.AnnotatedTag
  238. func ToAnnotatedTag(repo *models.Repository, t *git.Tag, c *git.Commit) *api.AnnotatedTag {
  239. return &api.AnnotatedTag{
  240. Tag: t.Name,
  241. SHA: t.ID.String(),
  242. Object: ToAnnotatedTagObject(repo, c),
  243. Message: t.Message,
  244. URL: util.URLJoin(repo.APIURL(), "git/tags", t.ID.String()),
  245. Tagger: ToCommitUser(t.Tagger),
  246. Verification: ToVerification(c),
  247. }
  248. }
  249. // ToAnnotatedTagObject convert a git.Commit to an api.AnnotatedTagObject
  250. func ToAnnotatedTagObject(repo *models.Repository, commit *git.Commit) *api.AnnotatedTagObject {
  251. return &api.AnnotatedTagObject{
  252. SHA: commit.ID.String(),
  253. Type: string(git.ObjectCommit),
  254. URL: util.URLJoin(repo.APIURL(), "git/commits", commit.ID.String()),
  255. }
  256. }
  257. // ToCommitUser convert a git.Signature to an api.CommitUser
  258. func ToCommitUser(sig *git.Signature) *api.CommitUser {
  259. return &api.CommitUser{
  260. Identity: api.Identity{
  261. Name: sig.Name,
  262. Email: sig.Email,
  263. },
  264. Date: sig.When.UTC().Format(time.RFC3339),
  265. }
  266. }
  267. // ToCommitMeta convert a git.Tag to an api.CommitMeta
  268. func ToCommitMeta(repo *models.Repository, tag *git.Tag) *api.CommitMeta {
  269. return &api.CommitMeta{
  270. SHA: tag.Object.String(),
  271. // TODO: Add the /commits API endpoint and use it here (https://developer.github.com/v3/repos/commits/#get-a-single-commit)
  272. URL: util.URLJoin(repo.APIURL(), "git/commits", tag.ID.String()),
  273. }
  274. }
  275. // ToTopicResponse convert from models.Topic to api.TopicResponse
  276. func ToTopicResponse(topic *models.Topic) *api.TopicResponse {
  277. return &api.TopicResponse{
  278. ID: topic.ID,
  279. Name: topic.Name,
  280. RepoCount: topic.RepoCount,
  281. Created: topic.CreatedUnix.AsTime(),
  282. Updated: topic.UpdatedUnix.AsTime(),
  283. }
  284. }