From 079a1cea1be72dd9d9faea34ea8836a6240290e2 Mon Sep 17 00:00:00 2001 From: Barak Amar Date: Sun, 23 Feb 2025 00:23:34 +0200 Subject: [PATCH 1/3] feat: hook pre/post merge include merge source The new field `merge_source` include source branch/tag/commit as requested. The current event include `source_ref` which resolved to the specific commit the merge operation will merge. --- docs/howto/hooks/webhooks.md | 4 +++- esti/hooks_test.go | 3 +++ pkg/graveler/graveler.go | 33 +++++++++++++++++---------------- pkg/graveler/hooks_handler.go | 2 ++ 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/docs/howto/hooks/webhooks.md b/docs/howto/hooks/webhooks.md index c523ff46ab3..cc5f4126dbb 100644 --- a/docs/howto/hooks/webhooks.md +++ b/docs/howto/hooks/webhooks.md @@ -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 | | tag_id[^3] | The ID of the created/deleted tag | string | +| merge_source[^5] | The ID of the created/deleted tag | string | [^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, if can be equal to the source_ref in case the source is a commit. Example: ```json diff --git a/esti/hooks_test.go b/esti/hooks_test.go index c78a4621a34..8a27d911473 100644 --- a/esti/hooks_test.go +++ b/esti/hooks_test.go @@ -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), @@ -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, @@ -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"` diff --git a/pkg/graveler/graveler.go b/pkg/graveler/graveler.go index 55dc3a96805..c578c2731eb 100644 --- a/pkg/graveler/graveler.go +++ b/pkg/graveler/graveler.go @@ -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{ @@ -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). diff --git a/pkg/graveler/hooks_handler.go b/pkg/graveler/hooks_handler.go index c43020bb436..293d572fa86 100644 --- a/pkg/graveler/hooks_handler.go +++ b/pkg/graveler/hooks_handler.go @@ -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 { From 846a28e78fffa634b055bb3388cb98cfd73a494a Mon Sep 17 00:00:00 2001 From: Barak Amar Date: Sun, 23 Feb 2025 08:23:12 +0200 Subject: [PATCH 2/3] Passing merge source to event --- pkg/actions/event.go | 2 ++ pkg/actions/service_test.go | 3 +++ 2 files changed, 5 insertions(+) diff --git a/pkg/actions/event.go b/pkg/actions/event.go index e0d285b2a1a..1bf8280f688 100644 --- a/pkg/actions/event.go +++ b/pkg/actions/event.go @@ -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) { @@ -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(), } return json.Marshal(info) } diff --git a/pkg/actions/service_test.go b/pkg/actions/service_test.go index 9a8cca0ac7b..c52d69088c0 100644 --- a/pkg/actions/service_test.go +++ b/pkg/actions/service_test.go @@ -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) } From 4151122a9f59d5e2d2742345a531265f6a48ec2f Mon Sep 17 00:00:00 2001 From: Barak Amar Date: Mon, 24 Feb 2025 20:42:43 +0200 Subject: [PATCH 3/3] docs: update merge_source description --- docs/howto/hooks/webhooks.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/howto/hooks/webhooks.md b/docs/howto/hooks/webhooks.md index cc5f4126dbb..469f3ecc490 100644 --- a/docs/howto/hooks/webhooks.md +++ b/docs/howto/hooks/webhooks.md @@ -75,13 +75,13 @@ Upon execution, a webhook will send a request containing a JSON object with the | 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 | | tag_id[^3] | The ID of the created/deleted tag | string | -| merge_source[^5] | The ID of the created/deleted tag | string | +| merge_source[^5] | The source branch/tag/ref on merge operation | string | [^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, if can be equal to the source_ref in case the source is a commit. +[^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