Add file status on API (#7671)

* add file status on API

* fix tests

* fix tests

* fix tests
This commit is contained in:
Lunny Xiao 2019-08-21 13:16:22 +08:00 committed by GitHub
parent 5f7fccaf55
commit 09ca391230
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 84 additions and 40 deletions

View file

@ -17,6 +17,7 @@ import (
"unicode" "unicode"
"code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/base"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
@ -385,7 +386,7 @@ func NewPushCommits() *PushCommits {
// ToAPIPayloadCommits converts a PushCommits object to // ToAPIPayloadCommits converts a PushCommits object to
// api.PayloadCommit format. // api.PayloadCommit format.
func (pc *PushCommits) ToAPIPayloadCommits(repoLink string) []*api.PayloadCommit { func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, error) {
commits := make([]*api.PayloadCommit, len(pc.Commits)) commits := make([]*api.PayloadCommit, len(pc.Commits))
if pc.emailUsers == nil { if pc.emailUsers == nil {
@ -417,6 +418,12 @@ func (pc *PushCommits) ToAPIPayloadCommits(repoLink string) []*api.PayloadCommit
} else { } else {
committerUsername = committer.Name committerUsername = committer.Name
} }
fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1)
if err != nil {
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err)
}
commits[i] = &api.PayloadCommit{ commits[i] = &api.PayloadCommit{
ID: commit.Sha1, ID: commit.Sha1,
Message: commit.Message, Message: commit.Message,
@ -431,10 +438,13 @@ func (pc *PushCommits) ToAPIPayloadCommits(repoLink string) []*api.PayloadCommit
Email: commit.CommitterEmail, Email: commit.CommitterEmail,
UserName: committerUsername, UserName: committerUsername,
}, },
Added: fileStatus.Added,
Removed: fileStatus.Removed,
Modified: fileStatus.Modified,
Timestamp: commit.Timestamp, Timestamp: commit.Timestamp,
} }
} }
return commits return commits, nil
} }
// AvatarLink tries to match user in database with e-mail // AvatarLink tries to match user in database with e-mail
@ -738,7 +748,10 @@ func MirrorSyncPushAction(repo *Repository, opts MirrorSyncPushActionOptions) er
opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum] opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
} }
apiCommits := opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()) apiCommits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
if err != nil {
return err
}
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID) opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
apiPusher := repo.MustOwner().APIFormat() apiPusher := repo.MustOwner().APIFormat()

View file

@ -85,42 +85,69 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
pushCommits := NewPushCommits() pushCommits := NewPushCommits()
pushCommits.Commits = []*PushCommit{ pushCommits.Commits = []*PushCommit{
{ {
Sha1: "abcdef1", Sha1: "69554a6",
CommitterEmail: "user2@example.com", CommitterEmail: "user2@example.com",
CommitterName: "User Two", CommitterName: "User2",
AuthorEmail: "user4@example.com", AuthorEmail: "user2@example.com",
AuthorName: "User Four", AuthorName: "User2",
Message: "message1", Message: "not signed commit",
}, },
{ {
Sha1: "abcdef2", Sha1: "27566bd",
CommitterEmail: "user2@example.com", CommitterEmail: "user2@example.com",
CommitterName: "User Two", CommitterName: "User2",
AuthorEmail: "user2@example.com", AuthorEmail: "user2@example.com",
AuthorName: "User Two", AuthorName: "User2",
Message: "message2", Message: "good signed commit (with not yet validated email)",
},
{
Sha1: "5099b81",
CommitterEmail: "user2@example.com",
CommitterName: "User2",
AuthorEmail: "user2@example.com",
AuthorName: "User2",
Message: "good signed commit",
}, },
} }
pushCommits.Len = len(pushCommits.Commits) pushCommits.Len = len(pushCommits.Commits)
payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame") repo := AssertExistsAndLoadBean(t, &Repository{ID: 16}).(*Repository)
if assert.Len(t, payloadCommits, 2) { payloadCommits, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/user2/repo16")
assert.Equal(t, "abcdef1", payloadCommits[0].ID) assert.NoError(t, err)
assert.Equal(t, "message1", payloadCommits[0].Message) assert.EqualValues(t, 3, len(payloadCommits))
assert.Equal(t, "/username/reponame/commit/abcdef1", payloadCommits[0].URL)
assert.Equal(t, "User Two", payloadCommits[0].Committer.Name)
assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
assert.Equal(t, "User Four", payloadCommits[0].Author.Name)
assert.Equal(t, "user4", payloadCommits[0].Author.UserName)
assert.Equal(t, "abcdef2", payloadCommits[1].ID) assert.Equal(t, "69554a6", payloadCommits[0].ID)
assert.Equal(t, "message2", payloadCommits[1].Message) assert.Equal(t, "not signed commit", payloadCommits[0].Message)
assert.Equal(t, "/username/reponame/commit/abcdef2", payloadCommits[1].URL) assert.Equal(t, "/user2/repo16/commit/69554a6", payloadCommits[0].URL)
assert.Equal(t, "User Two", payloadCommits[1].Committer.Name) assert.Equal(t, "User2", payloadCommits[0].Committer.Name)
assert.Equal(t, "user2", payloadCommits[1].Committer.UserName) assert.Equal(t, "user2", payloadCommits[0].Committer.UserName)
assert.Equal(t, "User Two", payloadCommits[1].Author.Name) assert.Equal(t, "User2", payloadCommits[0].Author.Name)
assert.Equal(t, "user2", payloadCommits[1].Author.UserName) assert.Equal(t, "user2", payloadCommits[0].Author.UserName)
} assert.EqualValues(t, []string{}, payloadCommits[0].Added)
assert.EqualValues(t, []string{}, payloadCommits[0].Removed)
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[0].Modified)
assert.Equal(t, "27566bd", payloadCommits[1].ID)
assert.Equal(t, "good signed commit (with not yet validated email)", payloadCommits[1].Message)
assert.Equal(t, "/user2/repo16/commit/27566bd", payloadCommits[1].URL)
assert.Equal(t, "User2", payloadCommits[1].Committer.Name)
assert.Equal(t, "user2", payloadCommits[1].Committer.UserName)
assert.Equal(t, "User2", payloadCommits[1].Author.Name)
assert.Equal(t, "user2", payloadCommits[1].Author.UserName)
assert.EqualValues(t, []string{}, payloadCommits[1].Added)
assert.EqualValues(t, []string{}, payloadCommits[1].Removed)
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[1].Modified)
assert.Equal(t, "5099b81", payloadCommits[2].ID)
assert.Equal(t, "good signed commit", payloadCommits[2].Message)
assert.Equal(t, "/user2/repo16/commit/5099b81", payloadCommits[2].URL)
assert.Equal(t, "User2", payloadCommits[2].Committer.Name)
assert.Equal(t, "user2", payloadCommits[2].Committer.UserName)
assert.Equal(t, "User2", payloadCommits[2].Author.Name)
assert.Equal(t, "user2", payloadCommits[2].Author.UserName)
assert.EqualValues(t, []string{"readme.md"}, payloadCommits[2].Added)
assert.EqualValues(t, []string{}, payloadCommits[2].Removed)
assert.EqualValues(t, []string{}, payloadCommits[2].Modified)
} }
func TestPushCommits_AvatarLink(t *testing.T) { func TestPushCommits_AvatarLink(t *testing.T) {

View file

@ -193,12 +193,16 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
} }
if isHookEventPush { if isHookEventPush {
commits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
if err != nil {
return err
}
if err = models.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{ if err = models.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
Ref: opts.RefFullName, Ref: opts.RefFullName,
Before: opts.OldCommitID, Before: opts.OldCommitID,
After: opts.NewCommitID, After: opts.NewCommitID,
CompareURL: setting.AppURL + opts.Commits.CompareURL, CompareURL: setting.AppURL + opts.Commits.CompareURL,
Commits: opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()), Commits: commits,
Repo: apiRepo, Repo: apiRepo,
Pusher: apiPusher, Pusher: apiPusher,
Sender: apiPusher, Sender: apiPusher,

View file

@ -29,7 +29,7 @@ func TestCommitRepoAction(t *testing.T) {
}{ }{
{ {
userID: 2, userID: 2,
repositoryID: 2, repositoryID: 16,
commitRepoActionOptions: CommitRepoActionOptions{ commitRepoActionOptions: CommitRepoActionOptions{
RefFullName: "refName", RefFullName: "refName",
OldCommitID: "oldCommitID", OldCommitID: "oldCommitID",
@ -37,20 +37,20 @@ func TestCommitRepoAction(t *testing.T) {
Commits: &models.PushCommits{ Commits: &models.PushCommits{
Commits: []*models.PushCommit{ Commits: []*models.PushCommit{
{ {
Sha1: "abcdef1", Sha1: "69554a6",
CommitterEmail: "user2@example.com", CommitterEmail: "user2@example.com",
CommitterName: "User Two", CommitterName: "User2",
AuthorEmail: "user4@example.com", AuthorEmail: "user2@example.com",
AuthorName: "User Four", AuthorName: "User2",
Message: "message1", Message: "not signed commit",
}, },
{ {
Sha1: "abcdef2", Sha1: "27566bd",
CommitterEmail: "user2@example.com", CommitterEmail: "user2@example.com",
CommitterName: "User Two", CommitterName: "User2",
AuthorEmail: "user2@example.com", AuthorEmail: "user2@example.com",
AuthorName: "User Two", AuthorName: "User2",
Message: "message2", Message: "good signed commit (with not yet validated email)",
}, },
}, },
Len: 2, Len: 2,