2014-04-27 11:05:13 +04: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 06:27:13 +04:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
|
|
|
"container/list"
|
2014-06-24 00:22:34 +04:00
|
|
|
"fmt"
|
2014-04-11 06:27:13 +04:00
|
|
|
"strings"
|
2017-10-26 10:45:14 +03:00
|
|
|
"time"
|
2014-04-11 06:27:13 +04:00
|
|
|
|
2019-03-27 12:33:00 +03:00
|
|
|
"code.gitea.io/gitea/modules/git"
|
2017-12-11 07:37:04 +03:00
|
|
|
"code.gitea.io/gitea/modules/util"
|
2014-04-11 06:27:13 +04:00
|
|
|
)
|
|
|
|
|
2017-02-25 17:54:40 +03: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-26 19:38:55 +03:00
|
|
|
EnvPusherEmail = "GITEA_PUSHER_EMAIL"
|
2017-02-25 17:54:40 +03:00
|
|
|
EnvPusherID = "GITEA_PUSHER_ID"
|
|
|
|
)
|
2014-06-28 19:56:41 +04:00
|
|
|
|
2016-08-12 12:29:29 +03: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 00:35:47 +03:00
|
|
|
// ListToPushCommits transforms a list.List to PushCommits type.
|
2015-12-10 04:46:05 +03:00
|
|
|
func ListToPushCommits(l *list.List) *PushCommits {
|
2016-11-29 16:05:26 +03:00
|
|
|
var commits []*PushCommit
|
2015-12-10 04:46:05 +03: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 12:29:29 +03:00
|
|
|
commits = append(commits, CommitToPushCommit(commit))
|
2015-12-10 04:46:05 +03:00
|
|
|
}
|
2018-12-13 18:55:43 +03:00
|
|
|
return &PushCommits{l.Len(), commits, "", make(map[string]string), make(map[string]*User)}
|
2015-12-10 04:46:05 +03:00
|
|
|
}
|
|
|
|
|
2016-11-25 00:35:47 +03:00
|
|
|
// PushUpdateOptions defines the push update options
|
2016-02-18 06:47:06 +03:00
|
|
|
type PushUpdateOptions struct {
|
|
|
|
PusherID int64
|
|
|
|
PusherName string
|
|
|
|
RepoUserName string
|
|
|
|
RepoName string
|
2016-08-17 09:06:38 +03:00
|
|
|
RefFullName string
|
|
|
|
OldCommitID string
|
|
|
|
NewCommitID string
|
2016-02-18 06:47:06 +03:00
|
|
|
}
|
|
|
|
|
2019-07-24 10:13:26 +03:00
|
|
|
// PushUpdateDeleteTag must be called for any push actions to delete tag
|
|
|
|
func PushUpdateDeleteTag(repo *Repository, tagName string) error {
|
2017-09-20 08:26:49 +03: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 07:43:04 +03:00
|
|
|
if _, err = x.ID(rel.ID).Delete(new(Release)); err != nil {
|
2017-09-20 08:26:49 +03:00
|
|
|
return fmt.Errorf("Delete: %v", err)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rel.IsDraft = true
|
|
|
|
rel.NumCommits = 0
|
|
|
|
rel.Sha1 = ""
|
2017-10-05 07:43:04 +03:00
|
|
|
if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
|
2017-09-20 08:26:49 +03:00
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-07-24 10:13:26 +03: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 08:26:49 +03: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 10:45:14 +03: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 08:26:49 +03: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,
|
2017-12-11 07:37:04 +03:00
|
|
|
CreatedUnix: util.TimeStamp(createdAt.Unix()),
|
2017-09-20 08:26:49 +03: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()
|
2017-12-11 07:37:04 +03:00
|
|
|
rel.CreatedUnix = util.TimeStamp(createdAt.Unix())
|
2017-09-20 08:26:49 +03:00
|
|
|
rel.NumCommits = commitsCount
|
|
|
|
rel.IsDraft = false
|
|
|
|
if rel.IsTag && author != nil {
|
|
|
|
rel.PublisherID = author.ID
|
|
|
|
}
|
2017-10-05 07:43:04 +03:00
|
|
|
if _, err = x.ID(rel.ID).AllCols().Update(rel); err != nil {
|
2017-09-20 08:26:49 +03:00
|
|
|
return fmt.Errorf("Update: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|