Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add push commits history comment on PR time-line #11167

Merged
merged 40 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
71bc5ab
Add push commits history comment on PR time-line
a1012112796 Apr 21, 2020
2bcb94c
Merge branch 'master' into commits_comment
a1012112796 Apr 21, 2020
fcedbcf
Add migrations for IsForcePush
a1012112796 Apr 21, 2020
cc37c83
fix code lint
a1012112796 Apr 22, 2020
6a713d9
Merge branch 'master' into commits_comment
a1012112796 Apr 22, 2020
89b67a9
fix rpo error
a1012112796 Apr 22, 2020
a9563d9
fix lint
a1012112796 Apr 22, 2020
4ea2b3a
fix typo
a1012112796 Apr 22, 2020
03b0992
Merge branch 'master' into commits_comment
a1012112796 Apr 23, 2020
cce92bd
Apply suggestions from code review
a1012112796 Apr 24, 2020
f0c7aa3
Merge branch 'master' into commits_comment
a1012112796 Apr 25, 2020
9a698d4
fix lint
a1012112796 Apr 25, 2020
bd363e5
fix style again, I forgot something before
a1012112796 Apr 25, 2020
ebde1d3
Change email notify way
a1012112796 Apr 26, 2020
69729ec
Merge branch 'master' into commits_comment
a1012112796 Apr 26, 2020
712a0cc
fix api
a1012112796 Apr 26, 2020
e28fa6c
add number check if It's force-push
a1012112796 Apr 27, 2020
e3481b0
Merge branch 'master' into commits_comment
a1012112796 Apr 27, 2020
7d6fb31
Add repo commit link fuction
a1012112796 Apr 29, 2020
57fcd82
Merge branch 'master' into commits_comment
a1012112796 Apr 29, 2020
b2ab404
Update issue_comment.go
a1012112796 Apr 30, 2020
84217da
Apply suggestions from code review
a1012112796 May 1, 2020
cf05dc4
Merge branch 'master' into commits_comment
a1012112796 May 1, 2020
8f6f2b7
Apply suggestions from code review
a1012112796 May 3, 2020
9f1d123
Merge branch 'master' into commits_comment
a1012112796 May 3, 2020
e3f9aa4
Merge branch 'master' into commits_comment
a1012112796 May 3, 2020
5caae89
Merge branch 'master' into commits_comment
lafriks May 5, 2020
b73d975
fix ui view
a1012112796 May 6, 2020
dcf100e
Merge branch 'master' into commits_comment
a1012112796 May 6, 2020
bcbc8b2
fix height
a1012112796 May 6, 2020
22ef854
remove unnecessary style define
a1012112796 May 6, 2020
7994f4d
Merge branch 'master' into commits_comment
a1012112796 May 6, 2020
9001a4f
simplify GetBranchName
a1012112796 May 11, 2020
24cddbd
Merge branch 'master' into commits_comment
a1012112796 May 11, 2020
92eb002
Merge branch 'master' into commits_comment
a1012112796 May 14, 2020
2ca5230
Apply suggestions from code review
a1012112796 May 18, 2020
c1bfcad
Merge branch 'master' into commits_comment
a1012112796 May 18, 2020
d8e414b
fix bug
a1012112796 May 18, 2020
1b1aed6
Merge branch 'master' into commits_comment
a1012112796 May 18, 2020
c4f9fec
Merge branch 'master' into commits_comment
lafriks May 20, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 182 additions & 0 deletions models/issue_comment.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
package models

import (
"container/list"
"fmt"
"strings"

Expand Down Expand Up @@ -90,6 +91,8 @@ const (
CommentTypeReviewRequest
// merge pull request
CommentTypeMergePull
// push to PR head branch
CommentTypePullPush
)

// CommentTag defines comment tag type
Expand Down Expand Up @@ -167,6 +170,11 @@ type Comment struct {
RefRepo *Repository `xorm:"-"`
RefIssue *Issue `xorm:"-"`
RefComment *Comment `xorm:"-"`

Commits *list.List `xorm:"-"`
OldCommit string `xorm:"-"`
NewCommit string `xorm:"-"`
IsForcePush bool
}

// LoadIssue loads issue from database
Expand Down Expand Up @@ -543,6 +551,34 @@ func (c *Comment) CodeCommentURL() string {
return fmt.Sprintf("%s/files#%s", c.Issue.HTMLURL(), c.HashTag())
}

// LoadPushCommits Load push commits
func (c *Comment) LoadPushCommits() (err error) {
if c.Content == "" {
return err
}

commitIDs := strings.Split(c.Content, ":")
if int64(len(commitIDs)) != c.Line {
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
return fmt.Errorf("LoadPushCommits: len of commitIDs is wrong %d - %d",
len(commitIDs),
c.Line)
}
c.Commits, err = getCommitsFromCommitIDs(c.Issue.Repo, commitIDs)
if err != nil {
return err
}

c.Commits = ValidateCommitsWithEmails(c.Commits)
c.Commits = ParseCommitsWithSignature(c.Commits, c.Issue.Repo)
c.Commits = ParseCommitsWithStatus(c.Commits, c.Issue.Repo)

if c.IsForcePush {
c.OldCommit = commitIDs[0]
c.NewCommit = commitIDs[1]
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
}
return err
}

func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err error) {
var LabelID int64
if opts.Label != nil {
Expand Down Expand Up @@ -576,6 +612,7 @@ func createComment(e *xorm.Session, opts *CreateCommentOptions) (_ *Comment, err
RefCommentID: opts.RefCommentID,
RefAction: opts.RefAction,
RefIsPull: opts.RefIsPull,
IsForcePush: opts.IsForcePush,
}
if _, err = e.Insert(comment); err != nil {
return nil, err
Expand Down Expand Up @@ -738,6 +775,7 @@ type CreateCommentOptions struct {
RefCommentID int64
RefAction references.XRefAction
RefIsPull bool
IsForcePush bool
}

// CreateComment creates comment of issue or commit.
Expand Down Expand Up @@ -1016,3 +1054,147 @@ func UpdateCommentsMigrationsByType(tp structs.GitServiceType, originalAuthorID
})
return err
}

// CreatePushPullComment create push code to pull base commend
func CreatePushPullComment(pusher *User, pr *PullRequest, oldCommitID, newCommitID string) (comment *Comment, err error) {
if pr.HasMerged || oldCommitID == "" || newCommitID == "" {
return nil, nil
}

ops := &CreateCommentOptions{
Type: CommentTypePullPush,
Doer: pusher,
Repo: pr.BaseRepo,
}

var commitIDs []string
messages := ""

var isForcePush bool
commitIDs, messages, isForcePush, err = getCommitIDsFromRepo(pr.BaseRepo, oldCommitID, newCommitID, pr.BaseBranch)
if err != nil {
return nil, err
}

ops.LineNum = int64(len(commitIDs))
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
ops.Issue = pr.Issue
ops.IsForcePush = isForcePush

commitIDlist := ""

for _, commitID := range commitIDs {
commitIDlist = commitID + ":" + commitIDlist
}

ops.Content = commitIDlist[0 : len(commitIDlist)-1]

comment, err = CreateComment(ops)
if err != nil {
return
}

// Prepare the contents for notify
prmessages := "@" + ops.Doer.Name
if ops.IsForcePush {
prmessages += " force-pushed the " + pr.HeadBranch + " branch from " + oldCommitID + " to " + newCommitID
} else {
if ops.LineNum == 1 {
prmessages += fmt.Sprintf(" pushed 1 commit to %s: \n ", pr.HeadBranch)
} else {
prmessages += fmt.Sprintf(" pushed %d commits to %s: \n ", ops.LineNum, pr.HeadBranch)
guillep2k marked this conversation as resolved.
Show resolved Hide resolved
}
prmessages += messages
}

comment.Content = prmessages

return
}

// getCommitsFromRepo get commit IDs from repo in betwern oldCommitID and newCommitID
// isForcePush will be true if oldCommit isn't on the branch
// Commit on baseBranch will skip
func getCommitIDsFromRepo(repo *Repository, oldCommitID, newCommitID, baseBranch string) (commitIDs []string, messages string, isForcePush bool, err error) {
repoPath := repo.RepoPath()
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
return nil, "", false, err
}
defer gitRepo.Close()

oldCommit, err := gitRepo.GetCommit(oldCommitID)
if err != nil {
return nil, "", false, err
}

oldCommitBranch, err := oldCommit.GetBranchName()
if err != nil {
return nil, "", false, err
}

if oldCommitBranch == "undefined" {
commitIDs = make([]string, 2)
commitIDs[1] = oldCommitID
commitIDs[0] = newCommitID

return commitIDs, "", true, err
}

newCommit, err := gitRepo.GetCommit(newCommitID)
if err != nil {
return nil, "", false, err
}

var commits *list.List
commits, err = newCommit.CommitsBeforeUntil(oldCommitID)
if err != nil {
return nil, "", false, err
}

commitIDs = make([]string, 0, commits.Len())
messages = ""

for e := commits.Front(); e != nil; e = e.Next() {
commit := e.Value.(*git.Commit)
commitBranch, err := commit.GetBranchName()
guillep2k marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, "", false, err
}

if commitBranch != baseBranch {
commitID := commit.ID.String()
commitMsg := commit.Message()
commitMsg = strings.Split(commitMsg, "\n")[0]
messages = "* " + commitID[0:10] + " - " + commitMsg + " \n" + messages
commitIDs = append(commitIDs, commitID)
}
}

return
}

// getCommitsFromCommitIDs get commits from commitIDs
func getCommitsFromCommitIDs(repo *Repository, commitIDs []string) (commits *list.List, err error) {
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
if commitIDs == nil {
return nil, nil
}

repoPath := repo.RepoPath()
gitRepo, err := git.OpenRepository(repoPath)
if err != nil {
return nil, err
}
defer gitRepo.Close()

commits = list.New()

for _, commitID := range commitIDs {
commit, err := gitRepo.GetCommit(commitID)
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
commits.PushBack(commit)
}

return commits, nil
}
2 changes: 2 additions & 0 deletions models/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,8 @@ var migrations = []Migration{
NewMigration("Add Branch Protection Block Outdated Branch", addBlockOnOutdatedBranch),
// v138 -> v139
NewMigration("Add ResolveDoerID to Comment table", addResolveDoerIDCommentColumn),
// v139 -> v140
NewMigration("Add IsForcePush to Comment table", addIsForcePushCommentColumn),
}

// GetCurrentDBVersion returns the current db version
Expand Down
22 changes: 22 additions & 0 deletions models/migrations/v139.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
"fmt"

"xorm.io/xorm"
)

func addIsForcePushCommentColumn(x *xorm.Engine) error {
type Comment struct {
IsForcePush bool
}

if err := x.Sync2(new(Comment)); err != nil {
return fmt.Errorf("Sync2: %v", err)
}
return nil
}
2 changes: 1 addition & 1 deletion modules/git/commit.go
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ func (c *Commit) GetBranchName() (string, error) {
}

// name-rev commitID output will be "COMMIT_ID master" or "COMMIT_ID master~12"
lafriks marked this conversation as resolved.
Show resolved Hide resolved
return strings.Split(strings.Split(string(data), " ")[1], "~")[0], nil
return strings.Split(strings.Split(strings.Split(string(data), " ")[1], "~")[0], "\n")[0], nil
guillep2k marked this conversation as resolved.
Show resolved Hide resolved
}

// CommitFileStatus represents status of files in a commit.
Expand Down
3 changes: 3 additions & 0 deletions modules/notification/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ func (a *actionNotifier) NotifyIssueChangeStatus(doer *models.User, issue *model
// NotifyCreateIssueComment notifies comment on an issue to notifiers
func (a *actionNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
issue *models.Issue, comment *models.Comment) {
if comment.Type == models.CommentTypePullPush {
return
}
act := &models.Action{
ActUserID: doer.ID,
ActUser: doer,
Expand Down
4 changes: 4 additions & 0 deletions modules/notification/indexer/indexer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ func NewNotifier() base.Notifier {

func (r *indexerNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
issue *models.Issue, comment *models.Comment) {
if comment.Type == models.CommentTypePullPush {
return
}

if comment.Type == models.CommentTypeComment {
if issue.Comments == nil {
if err := issue.LoadDiscussComments(); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions modules/notification/mail/mail.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ func (m *mailNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.
act = models.ActionCommentIssue
} else if comment.Type == models.CommentTypeCode {
act = models.ActionCommentIssue
} else if comment.Type == models.CommentTypePullPush {
act = models.ActionCommentIssue
}

if err := mailer.MailParticipantsComment(comment, act, issue); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions modules/notification/webhook/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,10 @@ func (m *webhookNotifier) NotifyUpdateComment(doer *models.User, c *models.Comme

func (m *webhookNotifier) NotifyCreateIssueComment(doer *models.User, repo *models.Repository,
issue *models.Issue, comment *models.Comment) {
if comment.Type == models.CommentTypePullPush {
return
}

mode, _ := models.AccessLevel(doer, repo)

var err error
Expand Down
3 changes: 3 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,9 @@ issues.due_date = Due Date
issues.invalid_due_date_format = "Due date format must be 'yyyy-mm-dd'."
issues.error_modifying_due_date = "Failed to modify the due date."
issues.error_removing_due_date = "Failed to remove the due date."
issues.push_commit_1 = "add %d commit %s"
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
issues.push_commits_n = "add %d commits %s"
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
issues.force_push_codes = `force-pushed the %[1]s branch from <a href="%[3]s">%[2]s</a> to <a href="%[5]s">%[4]s</a>. %[6]s`
issues.due_date_form = "yyyy-mm-dd"
issues.due_date_form_add = "Add due date"
issues.due_date_form_edit = "Edit"
Expand Down
6 changes: 6 additions & 0 deletions routers/repo/issue.go
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,12 @@ func ViewIssue(ctx *context.Context) {
ctx.ServerError("LoadResolveDoer", err)
return
}
} else if comment.Type == models.CommentTypePullPush {
participants = addParticipant(comment.Poster, participants)
if err = comment.LoadPushCommits(); err != nil {
ctx.ServerError("LoadPushCommits", err)
return
}
}
}

Expand Down
41 changes: 41 additions & 0 deletions services/pull/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,41 @@ func NewPullRequest(repo *models.Repository, pull *models.Issue, labelIDs []int6

notification.NotifyNewPullRequest(pr)

// add first push codes command
baseGitRepo, err := git.OpenRepository(pr.BaseRepo.RepoPath())
if err != nil {
return err
}
defer baseGitRepo.Close()

compareInfo, err := baseGitRepo.GetCompareInfo(pr.BaseRepo.RepoPath(),
pr.BaseBranch, pr.GetGitRefName())
if err != nil {
return err
}

if compareInfo.Commits.Len() > 0 {
guillep2k marked this conversation as resolved.
Show resolved Hide resolved
comitIDs := ""
for e := compareInfo.Commits.Front(); e != nil; e = e.Next() {
commitID := e.Value.(*git.Commit).ID.String()
comitIDs = commitID + ":" + comitIDs
}

comitIDs = comitIDs[0 : len(comitIDs)-1]

ops := &models.CreateCommentOptions{
Type: models.CommentTypePullPush,
Doer: pull.Poster,
Repo: repo,
LineNum: int64(compareInfo.Commits.Len()),
a1012112796 marked this conversation as resolved.
Show resolved Hide resolved
Issue: pr.Issue,
IsForcePush: false,
Content: comitIDs,
guillep2k marked this conversation as resolved.
Show resolved Hide resolved
}

_, _ = models.CreateComment(ops)
}

return nil
}

Expand Down Expand Up @@ -237,6 +272,12 @@ func AddTestPullRequestTask(doer *models.User, repoID int64, branch string, isSy
}

addHeadRepoTasks(prs)
for _, pr := range prs {
comment, err := models.CreatePushPullComment(doer, pr, oldCommitID, newCommitID)
if err == nil && comment != nil {
notification.NotifyCreateIssueComment(doer, pr.BaseRepo, pr.Issue, comment)
}
}

log.Trace("AddTestPullRequestTask [base_repo_id: %d, base_branch: %s]: finding pull requests", repoID, branch)
prs, err = models.GetUnmergedPullRequestsByBaseInfo(repoID, branch)
Expand Down
Loading