convert.go 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 utils
  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. // ApiUser converts user to its API format.
  13. func ApiUser(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. // ApiRepository converts repository to API format.
  23. func ApiRepository(owner *models.User, repo *models.Repository, permission api.Permission) *api.Repository {
  24. cl := repo.CloneLink()
  25. return &api.Repository{
  26. Id: repo.ID,
  27. Owner: *ApiUser(owner),
  28. FullName: owner.Name + "/" + repo.Name,
  29. Private: repo.IsPrivate,
  30. Fork: repo.IsFork,
  31. HtmlUrl: setting.AppUrl + owner.Name + "/" + repo.Name,
  32. CloneUrl: cl.HTTPS,
  33. SshUrl: cl.SSH,
  34. Permissions: permission,
  35. }
  36. }
  37. // ApiPublicKey converts public key to its API format.
  38. func ApiPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
  39. return &api.PublicKey{
  40. ID: key.ID,
  41. Key: key.Content,
  42. URL: apiLink + com.ToStr(key.ID),
  43. Title: key.Name,
  44. Created: key.Created,
  45. }
  46. }
  47. // ApiHook converts webhook to its API format.
  48. func ApiHook(repoLink string, w *models.Webhook) *api.Hook {
  49. config := map[string]string{
  50. "url": w.URL,
  51. "content_type": w.ContentType.Name(),
  52. }
  53. if w.HookTaskType == models.SLACK {
  54. s := w.GetSlackHook()
  55. config["channel"] = s.Channel
  56. config["username"] = s.Username
  57. config["icon_url"] = s.IconURL
  58. config["color"] = s.Color
  59. }
  60. return &api.Hook{
  61. ID: w.ID,
  62. Type: w.HookTaskType.Name(),
  63. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  64. Active: w.IsActive,
  65. Config: config,
  66. Events: w.EventsArray(),
  67. Updated: w.Updated,
  68. Created: w.Created,
  69. }
  70. }
  71. // ApiDeployKey converts deploy key to its API format.
  72. func ApiDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
  73. return &api.DeployKey{
  74. ID: key.ID,
  75. Key: key.Content,
  76. URL: apiLink + com.ToStr(key.ID),
  77. Title: key.Name,
  78. Created: key.Created,
  79. ReadOnly: true, // All deploy keys are read-only.
  80. }
  81. }