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

feat: hook pre/post merge include merge source #8703

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 3 additions & 1 deletion docs/howto/hooks/webhooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,15 @@ Upon execution, a webhook will send a request containing a JSON object with the
| commit_message[^2] | The message for the commit (or merge) that is taking place | string |
| committer[^2] | Name of the committer | string |
| commit_metadata[^2] | The metadata for the commit that is taking place | string |
| commit_id[^2,^4] | The ID of the commit that is being created | string |
| commit_id[^2,^4] | The ID of the commit that is being created | string |
Copy link
Contributor

Choose a reason for hiding this comment

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

not rendering (see screenshot in comment blueish' arrow)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks, I'll fix it

| tag_id[^3] | The ID of the created/deleted tag | string |
| merge_source[^5] | The source branch/tag/ref on merge operation | string |
Copy link
Contributor

Choose a reason for hiding this comment

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

This renders as 4? (see image with purple arrow)
Screenshot 2025-02-27 at 11 38 12


[^1]: N\A for Tag events
[^2]: N\A for Tag and Create/Delete Branch events
[^3]: Applicable only for Tag events
[^4]: Applicable to commit/merge events. For merges, this represents the merge commit ID to be created if the merge operation succeeds.
[^5]: Applicable to merge events. This represents the merge source, it may be same as `source_ref` in case the source is a commit.

Example:
```json
Expand Down
3 changes: 3 additions & 0 deletions esti/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ func testCommitMerge(t *testing.T, ctx context.Context, repo string) {
ActionName: "Test Pre Merge",
HookID: "test_webhook",
RepositoryID: repo,
MergeSource: branch,
BranchID: mainBranch,
Committer: commitRecord.Committer,
CommitMessage: fmt.Sprintf("Merge '%s' into '%s'", branch, mainBranch),
Expand All @@ -212,6 +213,7 @@ func testCommitMerge(t *testing.T, ctx context.Context, repo string) {
ActionName: "Test Post Merge",
HookID: "test_webhook",
RepositoryID: repo,
MergeSource: branch,
BranchID: mainBranch,
CommitID: mergeRef,
Committer: commitRecord.Committer,
Expand Down Expand Up @@ -473,6 +475,7 @@ type webhookEventInfo struct {
HookID string `json:"hook_id"`
RepositoryID string `json:"repository_id"`
BranchID string `json:"branch_id"`
MergeSource string `json:"merge_source"`
SourceRef string `json:"source_ref"`
TagID string `json:"tag_id"`
CommitID string `json:"commit_id"`
Expand Down
2 changes: 2 additions & 0 deletions pkg/actions/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type EventInfo struct {
CommitMessage string `json:"commit_message,omitempty"`
Committer string `json:"committer,omitempty"`
CommitMetadata map[string]string `json:"commit_metadata,omitempty"`
MergeSource string `json:"merge_source,omitempty"`
}

func marshalEventInformation(actionName, hookID string, record graveler.HookRecord) ([]byte, error) {
Expand All @@ -37,6 +38,7 @@ func marshalEventInformation(actionName, hookID string, record graveler.HookReco
CommitMessage: record.Commit.Message,
Committer: record.Commit.Committer,
CommitMetadata: record.Commit.Metadata,
MergeSource: record.MergeSource.String(),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this left out from lua type on purpose? (https://github.com/treeverse/lakeFS/blob/master/pkg/actions/lua.go#L47)

Copy link
Contributor Author

@nopcoder nopcoder Feb 27, 2025

Choose a reason for hiding this comment

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

No, It is a bug - will fix it

}
return json.Marshal(info)
}
3 changes: 3 additions & 0 deletions pkg/actions/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,9 @@ func checkEvent(t *testing.T, record graveler.HookRecord, event actions.EventInf
if event.SourceRef != record.SourceRef.String() {
t.Errorf("Webhook post SourceRef=%s, expected=%s", event.SourceRef, record.SourceRef)
}
if event.MergeSource != record.MergeSource.String() {
t.Errorf("Webhook post MergeSource=%s, expected=%s", event.MergeSource, record.MergeSource)
}
if event.CommitMessage != record.Commit.Message {
t.Errorf("Webhook post CommitMessage=%s, expected=%s", event.CommitMessage, record.Commit.Message)
}
Expand Down
33 changes: 17 additions & 16 deletions pkg/graveler/graveler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2948,13 +2948,14 @@ func (g *Graveler) Merge(ctx context.Context, repository *RepositoryRecord, dest
if !repository.ReadOnly {
preRunID = g.hooks.NewRunID()
err = g.hooks.PreMergeHook(ctx, HookRecord{
EventType: EventTypePreMerge,
RunID: preRunID,
Repository: repository,
BranchID: destination,
SourceRef: fromCommit.CommitID.Ref(),
Commit: commit,
CommitID: commitID,
EventType: EventTypePreMerge,
RunID: preRunID,
Repository: repository,
BranchID: destination,
SourceRef: fromCommit.CommitID.Ref(),
Commit: commit,
CommitID: commitID,
MergeSource: source,
})
if err != nil {
return nil, &HookAbortError{
Expand All @@ -2977,15 +2978,15 @@ func (g *Graveler) Merge(ctx context.Context, repository *RepositoryRecord, dest
if !repository.ReadOnly {
postRunID := g.hooks.NewRunID()
err = g.hooks.PostMergeHook(ctx, HookRecord{
EventType: EventTypePostMerge,
RunID: postRunID,
Repository: repository,
BranchID: destination,

SourceRef: commitID.Ref(),
Commit: commit,
CommitID: commitID,
PreRunID: preRunID,
EventType: EventTypePostMerge,
RunID: postRunID,
Repository: repository,
BranchID: destination,
MergeSource: source,
SourceRef: commitID.Ref(),
Commit: commit,
CommitID: commitID,
PreRunID: preRunID,
})
if err != nil {
g.log(ctx).
Expand Down
2 changes: 2 additions & 0 deletions pkg/graveler/hooks_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ type HookRecord struct {
PreRunID string
// Exists only in tag actions.
TagID TagID
// Exists only in merge actions.
MergeSource Ref
}

type HooksHandler interface {
Expand Down
Loading