convert.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 "github.com/gogits/go-gogs-client"
  9. "github.com/gogits/git-module"
  10. "github.com/gogits/gogs/models"
  11. "github.com/gogits/gogs/modules/setting"
  12. )
  13. // ToApiUser converts user to its API format.
  14. func ToApiUser(u *models.User) *api.User {
  15. return &api.User{
  16. ID: u.Id,
  17. UserName: u.Name,
  18. FullName: u.FullName,
  19. Email: u.Email,
  20. AvatarUrl: u.AvatarLink(),
  21. }
  22. }
  23. func ToApiEmail(email *models.EmailAddress) *api.Email {
  24. return &api.Email{
  25. Email: email.Email,
  26. Verified: email.IsActivated,
  27. Primary: email.IsPrimary,
  28. }
  29. }
  30. // ToApiRepository converts repository to API format.
  31. func ToApiRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository {
  32. cl := repo.CloneLink()
  33. return &api.Repository{
  34. Id: repo.ID,
  35. Owner: *ToApiUser(owner),
  36. FullName: owner.Name + "/" + repo.Name,
  37. Private: repo.IsPrivate,
  38. Fork: repo.IsFork,
  39. HtmlUrl: setting.AppUrl + owner.Name + "/" + repo.Name,
  40. CloneUrl: cl.HTTPS,
  41. SshUrl: cl.SSH,
  42. Permissions: permission,
  43. }
  44. }
  45. // ToApiBranch converts user to its API format.
  46. func ToApiBranch(b *models.Branch,c *git.Commit) *api.Branch {
  47. return &api.Branch{
  48. Name: b.Name,
  49. Commit: ToApiCommit(c),
  50. }
  51. }
  52. // ToApiCommit converts user to its API format.
  53. func ToApiCommit(c *git.Commit) *api.PayloadCommit {
  54. return &api.PayloadCommit{
  55. ID: c.ID.String(),
  56. Message: c.Message(),
  57. URL: "Not implemented",
  58. Author: &api.PayloadAuthor{
  59. Name: c.Committer.Name,
  60. Email: c.Committer.Email,
  61. /* UserName: c.Committer.UserName, */
  62. },
  63. }
  64. }
  65. // ToApiPublicKey converts public key to its API format.
  66. func ToApiPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
  67. return &api.PublicKey{
  68. ID: key.ID,
  69. Key: key.Content,
  70. URL: apiLink + com.ToStr(key.ID),
  71. Title: key.Name,
  72. Created: key.Created,
  73. }
  74. }
  75. // ToApiHook converts webhook to its API format.
  76. func ToApiHook(repoLink string, w *models.Webhook) *api.Hook {
  77. config := map[string]string{
  78. "url": w.URL,
  79. "content_type": w.ContentType.Name(),
  80. }
  81. if w.HookTaskType == models.SLACK {
  82. s := w.GetSlackHook()
  83. config["channel"] = s.Channel
  84. config["username"] = s.Username
  85. config["icon_url"] = s.IconURL
  86. config["color"] = s.Color
  87. }
  88. return &api.Hook{
  89. ID: w.ID,
  90. Type: w.HookTaskType.Name(),
  91. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  92. Active: w.IsActive,
  93. Config: config,
  94. Events: w.EventsArray(),
  95. Updated: w.Updated,
  96. Created: w.Created,
  97. }
  98. }
  99. // ToApiDeployKey converts deploy key to its API format.
  100. func ToApiDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
  101. return &api.DeployKey{
  102. ID: key.ID,
  103. Key: key.Content,
  104. URL: apiLink + com.ToStr(key.ID),
  105. Title: key.Name,
  106. Created: key.Created,
  107. ReadOnly: true, // All deploy keys are read-only.
  108. }
  109. }
  110. func ToApiOrganization(org *models.User) *api.Organization {
  111. return &api.Organization{
  112. ID: org.Id,
  113. AvatarUrl: org.AvatarLink(),
  114. UserName: org.Name,
  115. FullName: org.FullName,
  116. Description: org.Description,
  117. Website: org.Website,
  118. Location: org.Location,
  119. }
  120. }