convert.go 3.0 KB

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