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

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