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

webhooks: Add head_commit field to push payload. #5419

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
63 changes: 63 additions & 0 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,67 @@ type CommitRepoActionOptions struct {
Commits *PushCommits
}

func getHeadCommit(repo *Repository, headCommitID string) *api.PayloadCommit {
if headCommitID == git.EmptySHA {
return nil
}

gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil {
log.Error(4, "OpenRepository[%s]: %v", repo.RepoPath(), err)
}

headCommit, err := gitRepo.GetCommit(headCommitID)
if err != nil {
log.Error(4, "GetCommit[%s]: %v", headCommitID, err)
}

authorUsername := ""
if author, err := GetUserByEmail(headCommit.Author.Email); err == nil {
authorUsername = author.Name
} else if !IsErrUserNotExist(err) {
log.Error(4, "GetUserByEmail: %v", err)
}

committerUsername := ""
if committer, err := GetUserByEmail(headCommit.Committer.Email); err == nil {
committerUsername = committer.Name
} else if !IsErrUserNotExist(err) {
log.Error(4, "GetUserByEmail: %v", err)
}

verif := ParseCommitWithSignature(headCommit)
var signature, payload string
if headCommit.Signature != nil {
signature = headCommit.Signature.Signature
payload = headCommit.Signature.Payload
}

headPayloadCommit := &api.PayloadCommit{
ID: headCommitID,
Message: headCommit.CommitMessage,
URL: fmt.Sprintf("%s/commit/%s", repo.HTMLURL(), headCommit.ID),
Author: &api.PayloadUser{
Name: headCommit.Author.Name,
Email: headCommit.Author.Email,
UserName: authorUsername,
},
Committer: &api.PayloadUser{
Name: headCommit.Committer.Name,
Email: headCommit.Committer.Email,
UserName: committerUsername,
},
Timestamp: headCommit.Author.When,
Verification: &api.PayloadCommitVerification{
Verified: verif.Verified,
Reason: verif.Reason,
Signature: signature,
Payload: payload,
},
}
return headPayloadCommit
}

// CommitRepoAction adds new commit action to the repository, and prepare
// corresponding webhooks.
func CommitRepoAction(opts CommitRepoActionOptions) error {
Expand Down Expand Up @@ -720,6 +781,7 @@ func CommitRepoAction(opts CommitRepoActionOptions) error {
After: opts.NewCommitID,
CompareURL: setting.AppURL + opts.Commits.CompareURL,
Commits: opts.Commits.ToAPIPayloadCommits(repo.HTMLURL()),
HeadCommit: getHeadCommit(repo, opts.NewCommitID),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we get the head PayloadCommit from APIPayLoadCommits ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be safe to assume that head commit is present at a specific location in the list(like at the starting or end) or are you suggesting to iterate over the list?

Repo: apiRepo,
Pusher: apiPusher,
Sender: apiPusher,
Expand Down Expand Up @@ -817,6 +879,7 @@ func MirrorSyncPushAction(repo *Repository, opts MirrorSyncPushActionOptions) er
After: opts.NewCommitID,
CompareURL: setting.AppURL + opts.Commits.CompareURL,
Commits: apiCommits,
HeadCommit: getHeadCommit(repo, opts.NewCommitID),
Repo: repo.APIFormat(AccessModeOwner),
Pusher: apiPusher,
Sender: apiPusher,
Expand Down
6 changes: 3 additions & 3 deletions models/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,11 +276,11 @@ func TestCommitRepoAction(t *testing.T) {
}{
{
userID: 2,
repositoryID: 2,
repositoryID: 1,
commitRepoActionOptions: CommitRepoActionOptions{
RefFullName: "refName",
OldCommitID: "oldCommitID",
NewCommitID: "newCommitID",
NewCommitID: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
Commits: &PushCommits{
avatars: make(map[string]string),
Commits: []*PushCommit{
Expand Down Expand Up @@ -315,7 +315,7 @@ func TestCommitRepoAction(t *testing.T) {
commitRepoActionOptions: CommitRepoActionOptions{
RefFullName: git.TagPrefix + "v1.1",
OldCommitID: git.EmptySHA,
NewCommitID: "newCommitID",
NewCommitID: "65f1bf27bc3bf70f64657658635e66094edbcb4d",
Commits: &PushCommits{},
},
action: Action{
Expand Down
7 changes: 4 additions & 3 deletions routers/api/v1/repo/hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ func TestHook(ctx *context.APIContext) {
Commits: []*api.PayloadCommit{
convert.ToCommit(ctx.Repo.Repository, ctx.Repo.Commit),
},
Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
Pusher: ctx.User.APIFormat(),
Sender: ctx.User.APIFormat(),
HeadCommit: convert.ToCommit(ctx.Repo.Repository, ctx.Repo.Commit),
Repo: ctx.Repo.Repository.APIFormat(models.AccessModeNone),
Pusher: ctx.User.APIFormat(),
Sender: ctx.User.APIFormat(),
}); err != nil {
ctx.Error(500, "PrepareWebhook: ", err)
return
Expand Down