Skip to content

Commit 3dd913a

Browse files
committed
add file status on API
1 parent 5661773 commit 3dd913a

File tree

3 files changed

+24
-5
lines changed

3 files changed

+24
-5
lines changed

models/action.go

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"unicode"
1818

1919
"code.gitea.io/gitea/modules/base"
20+
"code.gitea.io/gitea/modules/git"
2021
"code.gitea.io/gitea/modules/log"
2122
"code.gitea.io/gitea/modules/setting"
2223
api "code.gitea.io/gitea/modules/structs"
@@ -385,7 +386,7 @@ func NewPushCommits() *PushCommits {
385386

386387
// ToAPIPayloadCommits converts a PushCommits object to
387388
// api.PayloadCommit format.
388-
func (pc *PushCommits) ToAPIPayloadCommits(repoLink string) []*api.PayloadCommit {
389+
func (pc *PushCommits) ToAPIPayloadCommits(repoPath, repoLink string) ([]*api.PayloadCommit, error) {
389390
commits := make([]*api.PayloadCommit, len(pc.Commits))
390391

391392
if pc.emailUsers == nil {
@@ -417,6 +418,12 @@ func (pc *PushCommits) ToAPIPayloadCommits(repoLink string) []*api.PayloadCommit
417418
} else {
418419
committerUsername = committer.Name
419420
}
421+
422+
fileStatus, err := git.GetCommitFileStatus(repoPath, commit.Sha1)
423+
if err != nil {
424+
return nil, fmt.Errorf("FileStatus [commit_sha1: %s]: %v", commit.Sha1, err)
425+
}
426+
420427
commits[i] = &api.PayloadCommit{
421428
ID: commit.Sha1,
422429
Message: commit.Message,
@@ -431,10 +438,13 @@ func (pc *PushCommits) ToAPIPayloadCommits(repoLink string) []*api.PayloadCommit
431438
Email: commit.CommitterEmail,
432439
UserName: committerUsername,
433440
},
441+
Added: fileStatus.Added,
442+
Removed: fileStatus.Removed,
443+
Modified: fileStatus.Modified,
434444
Timestamp: commit.Timestamp,
435445
}
436446
}
437-
return commits
447+
return commits, nil
438448
}
439449

440450
// AvatarLink tries to match user in database with e-mail
@@ -738,7 +748,10 @@ func MirrorSyncPushAction(repo *Repository, opts MirrorSyncPushActionOptions) er
738748
opts.Commits.Commits = opts.Commits.Commits[:setting.UI.FeedMaxCommitNum]
739749
}
740750

741-
apiCommits := opts.Commits.ToAPIPayloadCommits(repo.HTMLURL())
751+
apiCommits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
752+
if err != nil {
753+
return err
754+
}
742755

743756
opts.Commits.CompareURL = repo.ComposeCompareURL(opts.OldCommitID, opts.NewCommitID)
744757
apiPusher := repo.MustOwner().APIFormat()

models/action_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,9 @@ func TestPushCommits_ToAPIPayloadCommits(t *testing.T) {
103103
}
104104
pushCommits.Len = len(pushCommits.Commits)
105105

106-
payloadCommits := pushCommits.ToAPIPayloadCommits("/username/reponame")
106+
repo := AssertExistsAndLoadBean(t, &Repository{ID: 1}).(*Repository)
107+
payloadCommits, err := pushCommits.ToAPIPayloadCommits(repo.RepoPath(), "/username/reponame")
108+
assert.NoError(t, err)
107109
if assert.Len(t, payloadCommits, 2) {
108110
assert.Equal(t, "abcdef1", payloadCommits[0].ID)
109111
assert.Equal(t, "message1", payloadCommits[0].Message)

modules/repofiles/action.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,12 +193,16 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
193193
}
194194

195195
if isHookEventPush {
196+
commits, err := opts.Commits.ToAPIPayloadCommits(repo.RepoPath(), repo.HTMLURL())
197+
if err != nil {
198+
return err
199+
}
196200
if err = models.PrepareWebhooks(repo, models.HookEventPush, &api.PushPayload{
197201
Ref: opts.RefFullName,
198202
Before: opts.OldCommitID,
199203
After: opts.NewCommitID,
200204
CompareURL: setting.AppURL + opts.Commits.CompareURL,
201-
Commits: opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()),
205+
Commits: commits,
202206
Repo: apiRepo,
203207
Pusher: apiPusher,
204208
Sender: apiPusher,

0 commit comments

Comments
 (0)