Skip to content

Commit

Permalink
fix(api): pullrequest branch filter is on target branch (#7248)
Browse files Browse the repository at this point in the history
  • Loading branch information
sguiheux authored Jan 3, 2025
1 parent 2293efe commit 1e04233
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 11 deletions.
1 change: 1 addition & 0 deletions contrib/vscode-cds/webviews/workflow-preview/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"@angular-devkit/build-angular": "16.2.11",
"@angular/cli": "16.2.11",
"@angular/compiler-cli": "16.2.12",
"@jridgewell/gen-mapping": "0.3.5",
"@types/d3": "5.9.2",
"@types/dagre": "0.7.52",
"@types/dagre-d3": "0.6.6",
Expand Down
4 changes: 2 additions & 2 deletions engine/api/v2_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ func (api *API) postRetrieveWorkflowToTriggerHandler() ([]service.RbacChecker, s

hooksWithReadRight := make([]sdk.V2WorkflowHook, 0)
for _, h := range filteredWorkflowHooks {
if !hookRequest.AnayzedProjectKeys.Contains(h.ProjectKey) {
if !hookRequest.AnalyzedProjectKeys.Contains(h.ProjectKey) {
// Check project right
vcsClient, err := repositoriesmanager.AuthorizedClient(ctx, db, api.Cache, h.ProjectKey, hookRequest.VCSName)
if err != nil {
Expand Down Expand Up @@ -428,7 +428,7 @@ func LoadWorkflowHooksWithRepositoryWebHooks(ctx context.Context, db gorp.SqlExe
if len(w.Data.TypesFilter) > 0 {
validType = sdk.IsInArray(hookRequest.RepositoryEventType, w.Data.TypesFilter)
}
if w.Data.ValidateRef(ctx, hookRequest.Ref) && validType {
if w.Data.ValidateRef(ctx, hookRequest.PullRequestRefTo) && validType {
filteredWorkflowHooks = append(filteredWorkflowHooks, w)
}
continue
Expand Down
8 changes: 4 additions & 4 deletions engine/api/v2_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func TestPostRetrieveWorkflowToTriggerHandler_RepositoryWebHooks(t *testing.T) {
RepositoryName: repo.Name,
VCSName: vcs.Name,
RepositoryEventName: sdk.WorkflowHookEventNamePush,
AnayzedProjectKeys: []string{p.Key},
AnalyzedProjectKeys: []string{p.Key},
Ref: "refs/heads/master",
Sha: "123456",
}
Expand Down Expand Up @@ -230,7 +230,7 @@ func TestPostRetrieveWorkflowToTriggerHandler_RepositoryWebHooksPullRequest(t *t
RepositoryEventType: sdk.WorkflowHookEventTypePullRequestOpened,
Ref: "refs/heads/master",
Sha: "123456",
AnayzedProjectKeys: []string{p.Key},
AnalyzedProjectKeys: []string{p.Key},
}

uri := api.Router.GetRouteV2("POST", api.postRetrieveWorkflowToTriggerHandler, nil)
Expand Down Expand Up @@ -290,7 +290,7 @@ func TestPostRetrieveWorkflowToTriggerHandler_RepositoryWebHooksPullRequestFilte
VCSName: vcs.Name,
RepositoryEventName: sdk.WorkflowHookEventNamePullRequest,
RepositoryEventType: sdk.WorkflowHookEventTypePullRequestOpened,
AnayzedProjectKeys: []string{p.Key},
AnalyzedProjectKeys: []string{p.Key},
}

uri := api.Router.GetRouteV2("POST", api.postRetrieveWorkflowToTriggerHandler, nil)
Expand Down Expand Up @@ -347,7 +347,7 @@ func TestPostRetrieveWorkflowToTriggerHandler_WorkerModels(t *testing.T) {
Ref: "refs/heads/master",
Sha: "123456",
RepositoryEventName: sdk.WorkflowHookEventNamePush,
AnayzedProjectKeys: []string{p.Key},
AnalyzedProjectKeys: []string{p.Key},
Models: []sdk.EntityFullName{
{
Name: "MySuperModel",
Expand Down
2 changes: 1 addition & 1 deletion engine/api/v2_repository_analyze.go
Original file line number Diff line number Diff line change
Expand Up @@ -1031,7 +1031,7 @@ func manageWorkflowHooks(ctx context.Context, db gorpmapper.SqlExecutorWithTx, c
}
hooks = append(hooks, wh)

if e.Commit == defaultBranch.LatestCommit {
if e.Ref == defaultBranch.ID && e.Commit == defaultBranch.LatestCommit {
// Load existing head hook
existingHook, err := workflow_v2.LoadHookHeadRepositoryWebHookByWorkflowAndEvent(ctx, db, e.ProjectKey, workflowDefVCSName, workflowDefRepositoryName, e.Name, sdk.WorkflowHookEventNamePullRequest, defaultBranch.ID)
if err != nil && !sdk.ErrorIs(err, sdk.ErrNotFound) {
Expand Down
7 changes: 4 additions & 3 deletions engine/hooks/trigger_workflow_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ func (s *Service) handleWorkflowHook(ctx context.Context, hre *sdk.HookRepositor
request := sdk.HookListWorkflowRequest{
HookEventUUID: hre.UUID,
Ref: hre.ExtractData.Ref,
PullRequestRefTo: hre.ExtractData.PullRequestRefTo,
Sha: hre.ExtractData.Commit,
Models: hre.ModelUpdated,
Workflows: hre.WorkflowUpdated,
Expand All @@ -87,15 +88,15 @@ func (s *Service) handleWorkflowHook(ctx context.Context, hre *sdk.HookRepositor
RepositoryEventType: hre.EventType,
VCSName: hre.VCSServerName,
RepositoryName: hre.RepositoryName,
AnayzedProjectKeys: sdk.StringSlice{},
AnalyzedProjectKeys: sdk.StringSlice{},
}
for _, a := range hre.Analyses {
// Only retrieve hooks from project where analysis is OK
if a.Status == sdk.RepositoryAnalysisStatusSucceed {
request.AnayzedProjectKeys = append(request.AnayzedProjectKeys, a.ProjectKey)
request.AnalyzedProjectKeys = append(request.AnalyzedProjectKeys, a.ProjectKey)
}
}
request.AnayzedProjectKeys.Unique()
request.AnalyzedProjectKeys.Unique()
workflowHooks, err := s.Client.ListWorkflowToTrigger(ctx, request)
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion sdk/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ type HookListWorkflowRequest struct {
VCSName string `json:"vcs_name"`
RepositoryName string `json:"repository_name"`
Ref string `json:"ref"`
PullRequestRefTo string `json:"pullrequest_ref_to"`
Sha string `json:"sha"`
Paths []string `json:"paths"`
RepositoryEventName WorkflowHookEventName `json:"repository_event"`
RepositoryEventType WorkflowHookEventType `json:"repository_event_type"`
AnayzedProjectKeys StringSlice `json:"project_keys"`
AnalyzedProjectKeys StringSlice `json:"project_keys"`
Models []EntityFullName `json:"models"`
Workflows []EntityFullName `json:"workflows"`
}
Expand Down

0 comments on commit 1e04233

Please sign in to comment.