本站源代码
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 line
4.2KB

  1. // Copyright 2019 The Gitea 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 repofiles
  5. import (
  6. "fmt"
  7. "net/url"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/models"
  11. "code.gitea.io/gitea/modules/git"
  12. api "code.gitea.io/gitea/modules/structs"
  13. )
  14. // GetFileResponseFromCommit Constructs a FileResponse from a Commit object
  15. func GetFileResponseFromCommit(repo *models.Repository, commit *git.Commit, branch, treeName string) (*api.FileResponse, error) {
  16. fileContents, _ := GetContents(repo, treeName, branch, false) // ok if fails, then will be nil
  17. fileCommitResponse, _ := GetFileCommitResponse(repo, commit) // ok if fails, then will be nil
  18. verification := GetPayloadCommitVerification(commit)
  19. fileResponse := &api.FileResponse{
  20. Content: fileContents,
  21. Commit: fileCommitResponse,
  22. Verification: verification,
  23. }
  24. return fileResponse, nil
  25. }
  26. // GetFileCommitResponse Constructs a FileCommitResponse from a Commit object
  27. func GetFileCommitResponse(repo *models.Repository, commit *git.Commit) (*api.FileCommitResponse, error) {
  28. if repo == nil {
  29. return nil, fmt.Errorf("repo cannot be nil")
  30. }
  31. if commit == nil {
  32. return nil, fmt.Errorf("commit cannot be nil")
  33. }
  34. commitURL, _ := url.Parse(repo.APIURL() + "/git/commits/" + commit.ID.String())
  35. commitTreeURL, _ := url.Parse(repo.APIURL() + "/git/trees/" + commit.Tree.ID.String())
  36. parents := make([]*api.CommitMeta, commit.ParentCount())
  37. for i := 0; i <= commit.ParentCount(); i++ {
  38. if parent, err := commit.Parent(i); err == nil && parent != nil {
  39. parentCommitURL, _ := url.Parse(repo.APIURL() + "/git/commits/" + parent.ID.String())
  40. parents[i] = &api.CommitMeta{
  41. SHA: parent.ID.String(),
  42. URL: parentCommitURL.String(),
  43. }
  44. }
  45. }
  46. commitHTMLURL, _ := url.Parse(repo.HTMLURL() + "/commit/" + commit.ID.String())
  47. fileCommit := &api.FileCommitResponse{
  48. CommitMeta: api.CommitMeta{
  49. SHA: commit.ID.String(),
  50. URL: commitURL.String(),
  51. },
  52. HTMLURL: commitHTMLURL.String(),
  53. Author: &api.CommitUser{
  54. Identity: api.Identity{
  55. Name: commit.Author.Name,
  56. Email: commit.Author.Email,
  57. },
  58. Date: commit.Author.When.UTC().Format(time.RFC3339),
  59. },
  60. Committer: &api.CommitUser{
  61. Identity: api.Identity{
  62. Name: commit.Committer.Name,
  63. Email: commit.Committer.Email,
  64. },
  65. Date: commit.Committer.When.UTC().Format(time.RFC3339),
  66. },
  67. Message: commit.Message(),
  68. Tree: &api.CommitMeta{
  69. URL: commitTreeURL.String(),
  70. SHA: commit.Tree.ID.String(),
  71. },
  72. Parents: parents,
  73. }
  74. return fileCommit, nil
  75. }
  76. // GetAuthorAndCommitterUsers Gets the author and committer user objects from the IdentityOptions
  77. func GetAuthorAndCommitterUsers(author, committer *IdentityOptions, doer *models.User) (committerUser, authorUser *models.User) {
  78. // Committer and author are optional. If they are not the doer (not same email address)
  79. // then we use bogus User objects for them to store their FullName and Email.
  80. // If only one of the two are provided, we set both of them to it.
  81. // If neither are provided, both are the doer.
  82. if committer != nil && committer.Email != "" {
  83. if doer != nil && strings.EqualFold(doer.Email, committer.Email) {
  84. committerUser = doer // the committer is the doer, so will use their user object
  85. if committer.Name != "" {
  86. committerUser.FullName = committer.Name
  87. }
  88. } else {
  89. committerUser = &models.User{
  90. FullName: committer.Name,
  91. Email: committer.Email,
  92. }
  93. }
  94. }
  95. if author != nil && author.Email != "" {
  96. if doer != nil && strings.EqualFold(doer.Email, author.Email) {
  97. authorUser = doer // the author is the doer, so will use their user object
  98. if authorUser.Name != "" {
  99. authorUser.FullName = author.Name
  100. }
  101. } else {
  102. authorUser = &models.User{
  103. FullName: author.Name,
  104. Email: author.Email,
  105. }
  106. }
  107. }
  108. if authorUser == nil {
  109. if committerUser != nil {
  110. authorUser = committerUser // No valid author was given so use the committer
  111. } else if doer != nil {
  112. authorUser = doer // No valid author was given and no valid committer so use the doer
  113. }
  114. }
  115. if committerUser == nil {
  116. committerUser = authorUser // No valid committer so use the author as the committer (was set to a valid user above)
  117. }
  118. return authorUser, committerUser
  119. }
上海开阖软件有限公司 沪ICP备12045867号-1