convert.go 3.0 KB

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