convert.go 3.2 KB

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