|
|
@ -16,6 +16,8 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
"github.com/go-xorm/xorm" |
|
|
|
"github.com/go-xorm/xorm" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
api "github.com/gogits/go-gogs-client" |
|
|
|
|
|
|
|
|
|
|
|
"github.com/gogits/gogs/modules/base" |
|
|
|
"github.com/gogits/gogs/modules/base" |
|
|
|
"github.com/gogits/gogs/modules/git" |
|
|
|
"github.com/gogits/gogs/modules/git" |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
|
"github.com/gogits/gogs/modules/log" |
|
|
@ -290,49 +292,59 @@ func updateIssuesCommit(u *User, repo *Repository, repoUserName, repoName string |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// CommitRepoAction adds new action for committing repository.
|
|
|
|
// CommitRepoAction adds new action for committing repository.
|
|
|
|
func CommitRepoAction(userID, repoUserID int64, userName, actEmail string, |
|
|
|
func CommitRepoAction( |
|
|
|
repoID int64, repoUserName, repoName string, refFullName string, commit *base.PushCommits, oldCommitID string, newCommitID string) error { |
|
|
|
userID, repoUserID int64, |
|
|
|
|
|
|
|
userName, actEmail string, |
|
|
|
|
|
|
|
repoID int64, |
|
|
|
|
|
|
|
repoUserName, repoName string, |
|
|
|
|
|
|
|
refFullName string, |
|
|
|
|
|
|
|
commit *base.PushCommits, |
|
|
|
|
|
|
|
oldCommitID string, newCommitID string) error { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
u, err := GetUserByID(userID) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("GetUserByID: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
repo, err := GetRepositoryByName(repoUserID, repoName) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("GetRepositoryByName: %v", err) |
|
|
|
|
|
|
|
} else if err = repo.GetOwner(); err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("GetOwner: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
isNewBranch := false |
|
|
|
opType := COMMIT_REPO |
|
|
|
opType := COMMIT_REPO |
|
|
|
// Check it's tag push or branch.
|
|
|
|
// Check it's tag push or branch.
|
|
|
|
if strings.HasPrefix(refFullName, "refs/tags/") { |
|
|
|
if strings.HasPrefix(refFullName, "refs/tags/") { |
|
|
|
opType = PUSH_TAG |
|
|
|
opType = PUSH_TAG |
|
|
|
commit = &base.PushCommits{} |
|
|
|
commit = &base.PushCommits{} |
|
|
|
} |
|
|
|
} else { |
|
|
|
|
|
|
|
|
|
|
|
repoLink := fmt.Sprintf("%s%s/%s", setting.AppUrl, repoUserName, repoName) |
|
|
|
|
|
|
|
// if not the first commit, set the compareUrl
|
|
|
|
// if not the first commit, set the compareUrl
|
|
|
|
if !strings.HasPrefix(oldCommitID, "0000000") { |
|
|
|
if !strings.HasPrefix(oldCommitID, "0000000") { |
|
|
|
commit.CompareUrl = fmt.Sprintf("%s/%s/compare/%s...%s", repoUserName, repoName, oldCommitID, newCommitID) |
|
|
|
commit.CompareUrl = fmt.Sprintf("%s/%s/compare/%s...%s", repoUserName, repoName, oldCommitID, newCommitID) |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
isNewBranch = true |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
bs, err := json.Marshal(commit) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("Marshal: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
refName := git.RefEndName(refFullName) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Change repository bare status and update last updated time.
|
|
|
|
// Change repository bare status and update last updated time.
|
|
|
|
repo, err := GetRepositoryByName(repoUserID, repoName) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("GetRepositoryByName: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
repo.IsBare = false |
|
|
|
repo.IsBare = false |
|
|
|
if err = UpdateRepository(repo, false); err != nil { |
|
|
|
if err = UpdateRepository(repo, false); err != nil { |
|
|
|
return fmt.Errorf("UpdateRepository: %v", err) |
|
|
|
return fmt.Errorf("UpdateRepository: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
u, err := GetUserByID(userID) |
|
|
|
if err = updateIssuesCommit(u, repo, repoUserName, repoName, commit.Commits); err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Debug("updateIssuesCommit: %v", err) |
|
|
|
return fmt.Errorf("GetUserByID: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
err = updateIssuesCommit(u, repo, repoUserName, repoName, commit.Commits) |
|
|
|
bs, err := json.Marshal(commit) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
log.Debug("updateIssuesCommit: ", err) |
|
|
|
return fmt.Errorf("Marshal: %v", err) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
refName := git.RefEndName(refFullName) |
|
|
|
|
|
|
|
|
|
|
|
if err = NotifyWatchers(&Action{ |
|
|
|
if err = NotifyWatchers(&Action{ |
|
|
|
ActUserID: u.Id, |
|
|
|
ActUserID: u.Id, |
|
|
|
ActUserName: userName, |
|
|
|
ActUserName: userName, |
|
|
@ -345,32 +357,24 @@ func CommitRepoAction(userID, repoUserID int64, userName, actEmail string, |
|
|
|
RefName: refName, |
|
|
|
RefName: refName, |
|
|
|
IsPrivate: repo.IsPrivate, |
|
|
|
IsPrivate: repo.IsPrivate, |
|
|
|
}); err != nil { |
|
|
|
}); err != nil { |
|
|
|
return errors.New("NotifyWatchers: " + err.Error()) |
|
|
|
return fmt.Errorf("NotifyWatchers: %v", err) |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// New push event hook.
|
|
|
|
|
|
|
|
if err := repo.GetOwner(); err != nil { |
|
|
|
|
|
|
|
return errors.New("GetOwner: " + err.Error()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ws, err := GetActiveWebhooksByRepoId(repo.ID) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return errors.New("GetActiveWebhooksByRepoId: " + err.Error()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// check if repo belongs to org and append additional webhooks
|
|
|
|
|
|
|
|
if repo.Owner.IsOrganization() { |
|
|
|
|
|
|
|
// get hooks for org
|
|
|
|
|
|
|
|
orgws, err := GetActiveWebhooksByOrgId(repo.OwnerID) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
return errors.New("GetActiveWebhooksByOrgId: " + err.Error()) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
ws = append(ws, orgws...) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if len(ws) == 0 { |
|
|
|
repoLink := fmt.Sprintf("%s%s/%s", setting.AppUrl, repoUserName, repoName) |
|
|
|
return nil |
|
|
|
payloadRepo := &api.PayloadRepo{ |
|
|
|
|
|
|
|
ID: repo.ID, |
|
|
|
|
|
|
|
Name: repo.LowerName, |
|
|
|
|
|
|
|
URL: repoLink, |
|
|
|
|
|
|
|
Description: repo.Description, |
|
|
|
|
|
|
|
Website: repo.Website, |
|
|
|
|
|
|
|
Watchers: repo.NumWatches, |
|
|
|
|
|
|
|
Owner: &api.PayloadAuthor{ |
|
|
|
|
|
|
|
Name: repo.Owner.DisplayName(), |
|
|
|
|
|
|
|
Email: repo.Owner.Email, |
|
|
|
|
|
|
|
UserName: repo.Owner.Name, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Private: repo.IsPrivate, |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
pusher_email, pusher_name := "", "" |
|
|
|
pusher_email, pusher_name := "", "" |
|
|
@ -379,83 +383,66 @@ func CommitRepoAction(userID, repoUserID int64, userName, actEmail string, |
|
|
|
pusher_email = pusher.Email |
|
|
|
pusher_email = pusher.Email |
|
|
|
pusher_name = pusher.DisplayName() |
|
|
|
pusher_name = pusher.DisplayName() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
payloadSender := &api.PayloadUser{ |
|
|
|
|
|
|
|
UserName: pusher.Name, |
|
|
|
|
|
|
|
ID: pusher.Id, |
|
|
|
|
|
|
|
AvatarUrl: setting.AppUrl + pusher.RelAvatarLink(), |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
commits := make([]*PayloadCommit, len(commit.Commits)) |
|
|
|
switch opType { |
|
|
|
|
|
|
|
case COMMIT_REPO: // Push
|
|
|
|
|
|
|
|
commits := make([]*api.PayloadCommit, len(commit.Commits)) |
|
|
|
for i, cmt := range commit.Commits { |
|
|
|
for i, cmt := range commit.Commits { |
|
|
|
author_username := "" |
|
|
|
author_username := "" |
|
|
|
author, err := GetUserByEmail(cmt.AuthorEmail) |
|
|
|
author, err := GetUserByEmail(cmt.AuthorEmail) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
author_username = author.Name |
|
|
|
author_username = author.Name |
|
|
|
} |
|
|
|
} |
|
|
|
commits[i] = &PayloadCommit{ |
|
|
|
commits[i] = &api.PayloadCommit{ |
|
|
|
Id: cmt.Sha1, |
|
|
|
ID: cmt.Sha1, |
|
|
|
Message: cmt.Message, |
|
|
|
Message: cmt.Message, |
|
|
|
Url: fmt.Sprintf("%s/commit/%s", repoLink, cmt.Sha1), |
|
|
|
URL: fmt.Sprintf("%s/commit/%s", repoLink, cmt.Sha1), |
|
|
|
Author: &PayloadAuthor{ |
|
|
|
Author: &api.PayloadAuthor{ |
|
|
|
Name: cmt.AuthorName, |
|
|
|
Name: cmt.AuthorName, |
|
|
|
Email: cmt.AuthorEmail, |
|
|
|
Email: cmt.AuthorEmail, |
|
|
|
UserName: author_username, |
|
|
|
UserName: author_username, |
|
|
|
}, |
|
|
|
}, |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
p := &Payload{ |
|
|
|
p := &api.PushPayload{ |
|
|
|
Ref: refFullName, |
|
|
|
Ref: refFullName, |
|
|
|
|
|
|
|
Before: oldCommitID, |
|
|
|
|
|
|
|
After: newCommitID, |
|
|
|
|
|
|
|
CompareUrl: setting.AppUrl + commit.CompareUrl, |
|
|
|
Commits: commits, |
|
|
|
Commits: commits, |
|
|
|
Repo: &PayloadRepo{ |
|
|
|
Repo: payloadRepo, |
|
|
|
Id: repo.ID, |
|
|
|
Pusher: &api.PayloadAuthor{ |
|
|
|
Name: repo.LowerName, |
|
|
|
|
|
|
|
Url: repoLink, |
|
|
|
|
|
|
|
Description: repo.Description, |
|
|
|
|
|
|
|
Website: repo.Website, |
|
|
|
|
|
|
|
Watchers: repo.NumWatches, |
|
|
|
|
|
|
|
Owner: &PayloadAuthor{ |
|
|
|
|
|
|
|
Name: repo.Owner.DisplayName(), |
|
|
|
|
|
|
|
Email: repo.Owner.Email, |
|
|
|
|
|
|
|
UserName: repo.Owner.Name, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Private: repo.IsPrivate, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
Pusher: &PayloadAuthor{ |
|
|
|
|
|
|
|
Name: pusher_name, |
|
|
|
Name: pusher_name, |
|
|
|
Email: pusher_email, |
|
|
|
Email: pusher_email, |
|
|
|
UserName: userName, |
|
|
|
UserName: userName, |
|
|
|
}, |
|
|
|
}, |
|
|
|
Before: oldCommitID, |
|
|
|
Sender: payloadSender, |
|
|
|
After: newCommitID, |
|
|
|
|
|
|
|
CompareUrl: setting.AppUrl + commit.CompareUrl, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
if err = PrepareWebhooks(repo, HOOK_EVENT_PUSH, p); err != nil { |
|
|
|
for _, w := range ws { |
|
|
|
return fmt.Errorf("PrepareWebhooks: %v", err) |
|
|
|
w.GetEvent() |
|
|
|
|
|
|
|
if !w.HasPushEvent() { |
|
|
|
|
|
|
|
continue |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
var payload BasePayload |
|
|
|
if isNewBranch { |
|
|
|
switch w.HookTaskType { |
|
|
|
return PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{ |
|
|
|
case SLACK: |
|
|
|
Ref: refName, |
|
|
|
s, err := GetSlackPayload(p, w.Meta) |
|
|
|
RefType: "branch", |
|
|
|
if err != nil { |
|
|
|
Repo: payloadRepo, |
|
|
|
return errors.New("action.GetSlackPayload: " + err.Error()) |
|
|
|
Sender: payloadSender, |
|
|
|
} |
|
|
|
}) |
|
|
|
payload = s |
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
payload = p |
|
|
|
|
|
|
|
p.Secret = w.Secret |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if err = CreateHookTask(&HookTask{ |
|
|
|
case PUSH_TAG: // Create
|
|
|
|
RepoID: repo.ID, |
|
|
|
return PrepareWebhooks(repo, HOOK_EVENT_CREATE, &api.CreatePayload{ |
|
|
|
HookID: w.ID, |
|
|
|
Ref: refName, |
|
|
|
Type: w.HookTaskType, |
|
|
|
RefType: "tag", |
|
|
|
URL: w.URL, |
|
|
|
Repo: payloadRepo, |
|
|
|
BasePayload: payload, |
|
|
|
Sender: payloadSender, |
|
|
|
ContentType: w.ContentType, |
|
|
|
}) |
|
|
|
EventType: HOOK_EVENT_PUSH, |
|
|
|
|
|
|
|
IsSSL: w.IsSSL, |
|
|
|
|
|
|
|
}); err != nil { |
|
|
|
|
|
|
|
return fmt.Errorf("CreateHookTask: %v", err) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
return nil |
|
|
|
return nil |
|
|
|