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

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