convert.go 2.8 KB

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