本站源代码
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.

168 lines
4.1KB

  1. // Copyright 2014 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 models
  5. import (
  6. "container/list"
  7. "fmt"
  8. "strings"
  9. "time"
  10. "code.gitea.io/gitea/modules/git"
  11. "code.gitea.io/gitea/modules/timeutil"
  12. )
  13. // env keys for git hooks need
  14. const (
  15. EnvRepoName = "GITEA_REPO_NAME"
  16. EnvRepoUsername = "GITEA_REPO_USER_NAME"
  17. EnvRepoIsWiki = "GITEA_REPO_IS_WIKI"
  18. EnvPusherName = "GITEA_PUSHER_NAME"
  19. EnvPusherEmail = "GITEA_PUSHER_EMAIL"
  20. EnvPusherID = "GITEA_PUSHER_ID"
  21. EnvKeyID = "GITEA_KEY_ID"
  22. EnvIsDeployKey = "GITEA_IS_DEPLOY_KEY"
  23. )
  24. // CommitToPushCommit transforms a git.Commit to PushCommit type.
  25. func CommitToPushCommit(commit *git.Commit) *PushCommit {
  26. return &PushCommit{
  27. Sha1: commit.ID.String(),
  28. Message: commit.Message(),
  29. AuthorEmail: commit.Author.Email,
  30. AuthorName: commit.Author.Name,
  31. CommitterEmail: commit.Committer.Email,
  32. CommitterName: commit.Committer.Name,
  33. Timestamp: commit.Author.When,
  34. }
  35. }
  36. // ListToPushCommits transforms a list.List to PushCommits type.
  37. func ListToPushCommits(l *list.List) *PushCommits {
  38. var commits []*PushCommit
  39. var actEmail string
  40. for e := l.Front(); e != nil; e = e.Next() {
  41. commit := e.Value.(*git.Commit)
  42. if actEmail == "" {
  43. actEmail = commit.Committer.Email
  44. }
  45. commits = append(commits, CommitToPushCommit(commit))
  46. }
  47. return &PushCommits{l.Len(), commits, "", make(map[string]string), make(map[string]*User)}
  48. }
  49. // PushUpdateOptions defines the push update options
  50. type PushUpdateOptions struct {
  51. PusherID int64
  52. PusherName string
  53. RepoUserName string
  54. RepoName string
  55. RefFullName string
  56. OldCommitID string
  57. NewCommitID string
  58. }
  59. // PushUpdateDeleteTag must be called for any push actions to delete tag
  60. func PushUpdateDeleteTag(repo *Repository, tagName string) error {
  61. rel, err := GetRelease(repo.ID, tagName)
  62. if err != nil {
  63. if IsErrReleaseNotExist(err) {
  64. return nil
  65. }
  66. return fmt.Errorf("GetRelease: %v", err)
  67. }
  68. if rel.IsTag {
  69. if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil {
  70. return fmt.Errorf("Delete: %v", err)
  71. }
  72. } else {
  73. rel.IsDraft = true
  74. rel.NumCommits = 0
  75. rel.Sha1 = ""
  76. if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
  77. return fmt.Errorf("Update: %v", err)
  78. }
  79. }
  80. return nil
  81. }
  82. // PushUpdateAddTag must be called for any push actions to add tag
  83. func PushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string) error {
  84. rel, err := GetRelease(repo.ID, tagName)
  85. if err != nil && !IsErrReleaseNotExist(err) {
  86. return fmt.Errorf("GetRelease: %v", err)
  87. }
  88. tag, err := gitRepo.GetTag(tagName)
  89. if err != nil {
  90. return fmt.Errorf("GetTag: %v", err)
  91. }
  92. commit, err := tag.Commit()
  93. if err != nil {
  94. return fmt.Errorf("Commit: %v", err)
  95. }
  96. sig := tag.Tagger
  97. if sig == nil {
  98. sig = commit.Author
  99. }
  100. if sig == nil {
  101. sig = commit.Committer
  102. }
  103. var author *User
  104. var createdAt = time.Unix(1, 0)
  105. if sig != nil {
  106. author, err = GetUserByEmail(sig.Email)
  107. if err != nil && !IsErrUserNotExist(err) {
  108. return fmt.Errorf("GetUserByEmail: %v", err)
  109. }
  110. createdAt = sig.When
  111. }
  112. commitsCount, err := commit.CommitsCount()
  113. if err != nil {
  114. return fmt.Errorf("CommitsCount: %v", err)
  115. }
  116. if rel == nil {
  117. rel = &Release{
  118. RepoID: repo.ID,
  119. Title: "",
  120. TagName: tagName,
  121. LowerTagName: strings.ToLower(tagName),
  122. Target: "",
  123. Sha1: commit.ID.String(),
  124. NumCommits: commitsCount,
  125. Note: "",
  126. IsDraft: false,
  127. IsPrerelease: false,
  128. IsTag: true,
  129. CreatedUnix: timeutil.TimeStamp(createdAt.Unix()),
  130. }
  131. if author != nil {
  132. rel.PublisherID = author.ID
  133. }
  134. if _, err = x.InsertOne(rel); err != nil {
  135. return fmt.Errorf("InsertOne: %v", err)
  136. }
  137. } else {
  138. rel.Sha1 = commit.ID.String()
  139. rel.CreatedUnix = timeutil.TimeStamp(createdAt.Unix())
  140. rel.NumCommits = commitsCount
  141. rel.IsDraft = false
  142. if rel.IsTag && author != nil {
  143. rel.PublisherID = author.ID
  144. }
  145. if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
  146. return fmt.Errorf("Update: %v", err)
  147. }
  148. }
  149. return nil
  150. }
上海开阖软件有限公司 沪ICP备12045867号-1