convert.go 2.7 KB

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