convert.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // Copyright 2015 - Present, The Gogs Authors. All rights reserved.
  2. // Copyright 2018 - Present, Gitote. All rights reserved.
  3. //
  4. // This source code is licensed under the MIT license found in the
  5. // LICENSE file in the root directory of this source tree.
  6. package convert
  7. import (
  8. "fmt"
  9. "gitote/gitote/models"
  10. "gitlab.com/gitote/com"
  11. "gitlab.com/gitote/git-module"
  12. api "gitlab.com/gitote/go-gitote-client"
  13. )
  14. // ToEmail convert models.EmailAddress to api.Email
  15. func ToEmail(email *models.EmailAddress) *api.Email {
  16. return &api.Email{
  17. Email: email.Email,
  18. Verified: email.IsActivated,
  19. Primary: email.IsPrimary,
  20. }
  21. }
  22. // ToBranch convert a commit and branch to an api.Branch
  23. func ToBranch(b *models.Branch, c *git.Commit) *api.Branch {
  24. return &api.Branch{
  25. Name: b.Name,
  26. Commit: ToCommit(c),
  27. }
  28. }
  29. // ToCommit convert a commit to api.PayloadCommit
  30. func ToCommit(c *git.Commit) *api.PayloadCommit {
  31. authorUsername := ""
  32. author, err := models.GetUserByEmail(c.Author.Email)
  33. if err == nil {
  34. authorUsername = author.Name
  35. }
  36. committerUsername := ""
  37. committer, err := models.GetUserByEmail(c.Committer.Email)
  38. if err == nil {
  39. committerUsername = committer.Name
  40. }
  41. return &api.PayloadCommit{
  42. ID: c.ID.String(),
  43. Message: c.Message(),
  44. URL: "Not implemented",
  45. Author: &api.PayloadUser{
  46. Name: c.Author.Name,
  47. Email: c.Author.Email,
  48. UserName: authorUsername,
  49. },
  50. Committer: &api.PayloadUser{
  51. Name: c.Committer.Name,
  52. Email: c.Committer.Email,
  53. UserName: committerUsername,
  54. },
  55. Timestamp: c.Author.When,
  56. }
  57. }
  58. // ToPublicKey convert models.PublicKey to api.PublicKey
  59. func ToPublicKey(apiLink string, key *models.PublicKey) *api.PublicKey {
  60. return &api.PublicKey{
  61. ID: key.ID,
  62. Key: key.Content,
  63. URL: apiLink + com.ToStr(key.ID),
  64. Title: key.Name,
  65. Created: key.Created,
  66. }
  67. }
  68. // ToHook convert models.Webhook to api.Hook
  69. func ToHook(repoLink string, w *models.Webhook) *api.Hook {
  70. config := map[string]string{
  71. "url": w.URL,
  72. "content_type": w.ContentType.Name(),
  73. }
  74. if w.HookTaskType == models.SLACK {
  75. s := w.GetSlackHook()
  76. config["channel"] = s.Channel
  77. config["username"] = s.Username
  78. config["icon_url"] = s.IconURL
  79. config["color"] = s.Color
  80. }
  81. return &api.Hook{
  82. ID: w.ID,
  83. Type: w.HookTaskType.Name(),
  84. URL: fmt.Sprintf("%s/settings/hooks/%d", repoLink, w.ID),
  85. Active: w.IsActive,
  86. Config: config,
  87. Events: w.EventsArray(),
  88. Updated: w.Updated,
  89. Created: w.Created,
  90. }
  91. }
  92. // ToDeployKey convert models.DeployKey to api.DeployKey
  93. func ToDeployKey(apiLink string, key *models.DeployKey) *api.DeployKey {
  94. return &api.DeployKey{
  95. ID: key.ID,
  96. Key: key.Content,
  97. URL: apiLink + com.ToStr(key.ID),
  98. Title: key.Name,
  99. Created: key.Created,
  100. ReadOnly: true, // All deploy keys are read-only.
  101. }
  102. }
  103. // ToOrganization convert models.User to api.Organization
  104. func ToOrganization(org *models.User) *api.Organization {
  105. return &api.Organization{
  106. ID: org.ID,
  107. AvatarUrl: org.AvatarLink(),
  108. UserName: org.Name,
  109. FullName: org.FullName,
  110. Description: org.Description,
  111. Website: org.Website,
  112. Location: org.Location,
  113. }
  114. }
  115. // ToTeam convert models.Team to api.Team
  116. func ToTeam(team *models.Team) *api.Team {
  117. return &api.Team{
  118. ID: team.ID,
  119. Name: team.Name,
  120. Description: team.Description,
  121. Permission: team.Authorize.String(),
  122. }
  123. }