2014-04-27 15:05:13 +08:00
|
|
|
// Copyright 2014 The Gogs Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2014-04-11 10:27:13 +08:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2014-06-24 04:22:34 +08:00
|
|
|
"fmt"
|
2014-04-11 10:27:13 +08:00
|
|
|
"strings"
|
2017-10-26 15:45:14 +08:00
|
|
|
"time"
|
2014-04-11 10:27:13 +08:00
|
|
|
|
2019-03-27 17:33:00 +08:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2019-08-15 22:46:21 +08:00
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2014-04-11 10:27:13 +08:00
|
|
|
)
|
|
|
|
|
2017-02-25 22:54:40 +08:00
|
|
|
// env keys for git hooks need
|
|
|
|
const (
|
|
|
|
EnvRepoName = "GITEA_REPO_NAME"
|
|
|
|
EnvRepoUsername = "GITEA_REPO_USER_NAME"
|
|
|
|
EnvRepoIsWiki = "GITEA_REPO_IS_WIKI"
|
|
|
|
EnvPusherName = "GITEA_PUSHER_NAME"
|
2018-07-27 00:38:55 +08:00
|
|
|
EnvPusherEmail = "GITEA_PUSHER_EMAIL"
|
2017-02-25 22:54:40 +08:00
|
|
|
EnvPusherID = "GITEA_PUSHER_ID"
|
2019-10-21 16:21:45 +08:00
|
|
|
EnvKeyID = "GITEA_KEY_ID"
|
|
|
|
EnvIsDeployKey = "GITEA_IS_DEPLOY_KEY"
|
2019-12-28 05:15:04 +08:00
|
|
|
EnvIsInternal = "GITEA_INTERNAL_PUSH"
|
2017-02-25 22:54:40 +08:00
|
|
|
)
|
2014-06-28 23:56:41 +08:00
|
|
|
|
2016-08-12 17:29:29 +08:00
|
|
|
// CommitToPushCommit transforms a git.Commit to PushCommit type.
|
|
|
|
func CommitToPushCommit(commit *git.Commit) *PushCommit {
|
|
|
|
return &PushCommit{
|
|
|
|
Sha1: commit.ID.String(),
|
|
|
|
Message: commit.Message(),
|
|
|
|
AuthorEmail: commit.Author.Email,
|
|
|
|
AuthorName: commit.Author.Name,
|
|
|
|
CommitterEmail: commit.Committer.Email,
|
|
|
|
CommitterName: commit.Committer.Name,
|
|
|
|
Timestamp: commit.Author.When,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-25 05:35:47 +08:00
|
|
|
// ListToPushCommits transforms a list.List to PushCommits type.
|
2015-12-10 09:46:05 +08:00
|
|
|
func ListToPushCommits(l *list.List) *PushCommits {
|
2016-11-29 21:05:26 +08:00
|
|
|
var commits []*PushCommit
|
2015-12-10 09:46:05 +08:00
|
|
|
var actEmail string
|
|
|
|
for e := l.Front(); e != nil; e = e.Next() {
|
|
|
|
commit := e.Value.(*git.Commit)
|
|
|
|
if actEmail == "" {
|
|
|
|
actEmail = commit.Committer.Email
|
|
|
|
}
|
2016-08-12 17:29:29 +08:00
|
|
|
commits = append(commits, CommitToPushCommit(commit))
|
2015-12-10 09:46:05 +08:00
|
|
|
}
|
2018-12-13 23:55:43 +08:00
|
|
|
return &PushCommits{l.Len(), commits, "", make(map[string]string), make(map[string]*User)}
|
2015-12-10 09:46:05 +08:00
|
|
|
}
|
|
|
|
|
2019-12-26 19:29:45 +08:00
|
|
|
// PushUpdateAddDeleteTags updates a number of added and delete tags
|
|
|
|
func PushUpdateAddDeleteTags(repo *Repository, gitRepo *git.Repository, addTags, delTags []string) error {
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return fmt.Errorf("Unable to begin sess in PushUpdateDeleteTags: %v", err)
|
|
|
|
}
|
|
|
|
if err := pushUpdateDeleteTags(sess, repo, delTags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := pushUpdateAddTags(sess, repo, gitRepo, addTags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
// PushUpdateDeleteTags updates a number of delete tags
|
|
|
|
func PushUpdateDeleteTags(repo *Repository, tags []string) error {
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return fmt.Errorf("Unable to begin sess in PushUpdateDeleteTags: %v", err)
|
|
|
|
}
|
|
|
|
if err := pushUpdateDeleteTags(sess, repo, tags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
|
|
|
|
func pushUpdateDeleteTags(e Engine, repo *Repository, tags []string) error {
|
|
|
|
if len(tags) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
lowerTags := make([]string, 0, len(tags))
|
|
|
|
for _, tag := range tags {
|
|
|
|
lowerTags = append(lowerTags, strings.ToLower(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := e.
|
|
|
|
Where("repo_id = ? AND is_tag = ?", repo.ID, true).
|
|
|
|
In("lower_tag_name", lowerTags).
|
|
|
|
Delete(new(Release)); err != nil {
|
|
|
|
return fmt.Errorf("Delete: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := e.
|
|
|
|
Where("repo_id = ? AND is_tag = ?", repo.ID, false).
|
|
|
|
In("lower_tag_name", lowerTags).
|
2019-12-27 22:18:07 +08:00
|
|
|
Cols("is_draft", "num_commits", "sha1").
|
|
|
|
Update(&Release{
|
|
|
|
IsDraft: true,
|
|
|
|
}); err != nil {
|
2019-12-26 19:29:45 +08:00
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 15:13:26 +08:00
|
|
|
// PushUpdateDeleteTag must be called for any push actions to delete tag
|
|
|
|
func PushUpdateDeleteTag(repo *Repository, tagName string) error {
|
2017-09-20 13:26:49 +08:00
|
|
|
rel, err := GetRelease(repo.ID, tagName)
|
|
|
|
if err != nil {
|
|
|
|
if IsErrReleaseNotExist(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("GetRelease: %v", err)
|
|
|
|
}
|
|
|
|
if rel.IsTag {
|
2017-10-05 12:43:04 +08:00
|
|
|
if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil {
|
2017-09-20 13:26:49 +08:00
|
|
|
return fmt.Errorf("Delete: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rel.IsDraft = true
|
|
|
|
rel.NumCommits = 0
|
|
|
|
rel.Sha1 = ""
|
2017-10-05 12:43:04 +08:00
|
|
|
if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
|
2017-09-20 13:26:49 +08:00
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-26 19:29:45 +08:00
|
|
|
// PushUpdateAddTags updates a number of add tags
|
|
|
|
func PushUpdateAddTags(repo *Repository, gitRepo *git.Repository, tags []string) error {
|
|
|
|
sess := x.NewSession()
|
|
|
|
defer sess.Close()
|
|
|
|
if err := sess.Begin(); err != nil {
|
|
|
|
return fmt.Errorf("Unable to begin sess in PushUpdateAddTags: %v", err)
|
|
|
|
}
|
|
|
|
if err := pushUpdateAddTags(sess, repo, gitRepo, tags); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sess.Commit()
|
|
|
|
}
|
|
|
|
func pushUpdateAddTags(e Engine, repo *Repository, gitRepo *git.Repository, tags []string) error {
|
|
|
|
if len(tags) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
lowerTags := make([]string, 0, len(tags))
|
|
|
|
for _, tag := range tags {
|
|
|
|
lowerTags = append(lowerTags, strings.ToLower(tag))
|
|
|
|
}
|
|
|
|
|
|
|
|
releases := make([]Release, 0, len(tags))
|
|
|
|
if err := e.Where("repo_id = ?", repo.ID).
|
|
|
|
In("lower_tag_name", lowerTags).Find(&releases); err != nil {
|
|
|
|
return fmt.Errorf("GetRelease: %v", err)
|
|
|
|
}
|
|
|
|
relMap := make(map[string]*Release)
|
|
|
|
for _, rel := range releases {
|
|
|
|
relMap[rel.LowerTagName] = &rel
|
|
|
|
}
|
|
|
|
|
|
|
|
newReleases := make([]*Release, 0, len(lowerTags)-len(relMap))
|
|
|
|
|
|
|
|
emailToUser := make(map[string]*User)
|
|
|
|
|
|
|
|
for i, lowerTag := range lowerTags {
|
|
|
|
tag, err := gitRepo.GetTag(tags[i])
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetTag: %v", err)
|
|
|
|
}
|
|
|
|
commit, err := tag.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Commit: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
sig := tag.Tagger
|
|
|
|
if sig == nil {
|
|
|
|
sig = commit.Author
|
|
|
|
}
|
|
|
|
if sig == nil {
|
|
|
|
sig = commit.Committer
|
|
|
|
}
|
|
|
|
var author *User
|
|
|
|
var createdAt = time.Unix(1, 0)
|
|
|
|
|
|
|
|
if sig != nil {
|
|
|
|
var ok bool
|
|
|
|
author, ok = emailToUser[sig.Email]
|
|
|
|
if !ok {
|
|
|
|
author, err = GetUserByEmail(sig.Email)
|
|
|
|
if err != nil && !IsErrUserNotExist(err) {
|
|
|
|
return fmt.Errorf("GetUserByEmail: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
createdAt = sig.When
|
|
|
|
}
|
|
|
|
|
|
|
|
commitsCount, err := commit.CommitsCount()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CommitsCount: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rel, has := relMap[lowerTag]
|
|
|
|
|
|
|
|
if !has {
|
|
|
|
rel = &Release{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Title: "",
|
|
|
|
TagName: tags[i],
|
|
|
|
LowerTagName: lowerTag,
|
|
|
|
Target: "",
|
|
|
|
Sha1: commit.ID.String(),
|
|
|
|
NumCommits: commitsCount,
|
|
|
|
Note: "",
|
|
|
|
IsDraft: false,
|
|
|
|
IsPrerelease: false,
|
|
|
|
IsTag: true,
|
|
|
|
CreatedUnix: timeutil.TimeStamp(createdAt.Unix()),
|
|
|
|
}
|
|
|
|
if author != nil {
|
|
|
|
rel.PublisherID = author.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
newReleases = append(newReleases, rel)
|
|
|
|
} else {
|
|
|
|
rel.Sha1 = commit.ID.String()
|
|
|
|
rel.CreatedUnix = timeutil.TimeStamp(createdAt.Unix())
|
|
|
|
rel.NumCommits = commitsCount
|
|
|
|
rel.IsDraft = false
|
|
|
|
if rel.IsTag && author != nil {
|
|
|
|
rel.PublisherID = author.ID
|
|
|
|
}
|
|
|
|
if _, err = e.ID(rel.ID).AllCols().Update(rel); err != nil {
|
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(newReleases) > 0 {
|
|
|
|
if _, err := e.Insert(newReleases); err != nil {
|
|
|
|
return fmt.Errorf("Insert: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 15:13:26 +08:00
|
|
|
// PushUpdateAddTag must be called for any push actions to add tag
|
|
|
|
func PushUpdateAddTag(repo *Repository, gitRepo *git.Repository, tagName string) error {
|
2017-09-20 13:26:49 +08:00
|
|
|
rel, err := GetRelease(repo.ID, tagName)
|
|
|
|
if err != nil && !IsErrReleaseNotExist(err) {
|
|
|
|
return fmt.Errorf("GetRelease: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tag, err := gitRepo.GetTag(tagName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("GetTag: %v", err)
|
|
|
|
}
|
|
|
|
commit, err := tag.Commit()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Commit: %v", err)
|
|
|
|
}
|
|
|
|
|
2017-10-26 15:45:14 +08:00
|
|
|
sig := tag.Tagger
|
|
|
|
if sig == nil {
|
|
|
|
sig = commit.Author
|
|
|
|
}
|
|
|
|
if sig == nil {
|
|
|
|
sig = commit.Committer
|
|
|
|
}
|
|
|
|
|
|
|
|
var author *User
|
|
|
|
var createdAt = time.Unix(1, 0)
|
|
|
|
|
|
|
|
if sig != nil {
|
|
|
|
author, err = GetUserByEmail(sig.Email)
|
|
|
|
if err != nil && !IsErrUserNotExist(err) {
|
|
|
|
return fmt.Errorf("GetUserByEmail: %v", err)
|
|
|
|
}
|
|
|
|
createdAt = sig.When
|
2017-09-20 13:26:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
commitsCount, err := commit.CommitsCount()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("CommitsCount: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rel == nil {
|
|
|
|
rel = &Release{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
Title: "",
|
|
|
|
TagName: tagName,
|
|
|
|
LowerTagName: strings.ToLower(tagName),
|
|
|
|
Target: "",
|
|
|
|
Sha1: commit.ID.String(),
|
|
|
|
NumCommits: commitsCount,
|
|
|
|
Note: "",
|
|
|
|
IsDraft: false,
|
|
|
|
IsPrerelease: false,
|
|
|
|
IsTag: true,
|
2019-08-15 22:46:21 +08:00
|
|
|
CreatedUnix: timeutil.TimeStamp(createdAt.Unix()),
|
2017-09-20 13:26:49 +08:00
|
|
|
}
|
|
|
|
if author != nil {
|
|
|
|
rel.PublisherID = author.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = x.InsertOne(rel); err != nil {
|
|
|
|
return fmt.Errorf("InsertOne: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rel.Sha1 = commit.ID.String()
|
2019-08-15 22:46:21 +08:00
|
|
|
rel.CreatedUnix = timeutil.TimeStamp(createdAt.Unix())
|
2017-09-20 13:26:49 +08:00
|
|
|
rel.NumCommits = commitsCount
|
|
|
|
rel.IsDraft = false
|
|
|
|
if rel.IsTag && author != nil {
|
|
|
|
rel.PublisherID = author.ID
|
|
|
|
}
|
2017-10-05 12:43:04 +08:00
|
|
|
if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
|
2017-09-20 13:26:49 +08:00
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|