convert.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. "github.com/gogs/git-module"
  9. api "github.com/gogs/go-gogs-client"
  10. "github.com/gogs/gogs/models"
  11. )
  12. func ToEmail(email *models.EmailAddress) *api.Email {
  13. return &api.Email{
  14. Email: email.Email,
  15. Verified: email.IsActivated,
  16. Primary: email.IsPrimary,
  17. }
  18. }
  19. func ToBranch(b *models.Branch, c *git.Commit) *api.Branch {
  20. return &api.Branch{
  21. Name: b.Name,
  22. Commit: ToCommit(c),
  23. }
  24. }
  25. func ToCommit(c *git.Commit) *api.PayloadCommit {
  26. authorUsername := ""
  27. author, err := models.GetUserByEmail(c.Author.Email)
  28. if err == nil {
  29. authorUsername = author.Name
  30. }
  31. committerUsername := ""
  32. committer, err := models.GetUserByEmail(c.Committer.Email)
  33. if err == nil {
  34. committerUsername = committer.Name
  35. }
  36. return &api.PayloadCommit{
  37. ID: c.ID.String(),
  38. Message: c.Message(),
  39. URL: "Not implemented",
  40. Author: &api.PayloadUser{
  41. Name: c.Author.Name,
  42. Email: c.Author.Email,
  43. UserName: authorUsername,
  44. },
  45. Committer: &api.PayloadUser{
  46. Name: c.Committer.Name,
  47. Email: c.Committer.Email,
  48. UserName: committerUsername,
  49. },
  50. Timestamp: c.Author.When,
  51. }
  52. }
  53. func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
  54. return &api.PublicKey{
  55. ID: key.ID,
  56. Key: key.Content,
  57. URL: apiLink + com.ToStr(key.ID),
  58. Title: key.Name,
  59. Created: key.Created,
  60. }
  61. }
  62. func ToHook(repoLink string, w *models.Webhook) *api.Hook {
  63. config := map[string]string{
  64. "url": w.URL,
  65. "content_type": w.ContentType.Name(),
  66. }
  67. if w.HookTaskType == models.SLACK {
  68. s := w.GetSlackHook()
  69. config["channel"] = s.Channel
  70. config["username"] = s.Username
  71. config["icon_url"] = s.IconURL
  72. config["color"] = s.Color
  73. }
  74. return &api.Hook{
  75. ID: w.ID,
  76. Type: w.HookTaskType.Name(),
  77. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  78. Active: w.IsActive,
  79. Config: config,
  80. Events: w.EventsArray(),
  81. Updated: w.Updated,
  82. Created: w.Created,
  83. }
  84. }
  85. func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
  86. return &api.DeployKey{
  87. ID: key.ID,
  88. Key: key.Content,
  89. URL: apiLink + com.ToStr(key.ID),
  90. Title: key.Name,
  91. Created: key.Created,
  92. ReadOnly: true, // All deploy keys are read-only.
  93. }
  94. }
  95. func ToOrganization(org *models.User) *api.Organization {
  96. return &api.Organization{
  97. ID: org.ID,
  98. AvatarUrl: org.AvatarLink(),
  99. UserName: org.Name,
  100. FullName: org.FullName,
  101. Description: org.Description,
  102. Website: org.Website,
  103. Location: org.Location,
  104. }
  105. }
  106. func ToTeam(team *models.Team) *api.Team {
  107. return &api.Team{
  108. ID: team.ID,
  109. Name: team.Name,
  110. Description: team.Description,
  111. Permission: team.Authorize.String(),
  112. }
  113. }