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