Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

chore: check branch existence (#11369)

Danny Xu9b80ffddb380
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 466fca93b7..8c4b112929 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -488,7 +488,8 @@ func (s *ProjectService) UpdateProjectGitOpsInfo(ctx context.Context, request *v
}
}
- if v := patch.Branch; v != nil && vcsConnector.Payload.Branch != *v {
+ // Check branch existence.
+ if v := patch.Branch; v != nil {
if *v == "" {
return nil, status.Errorf(codes.InvalidArgument, "branch must be specified")
}
@@ -967,7 +968,7 @@ func (s *ProjectService) createProjectGitOpsInfo(ctx context.Context, request *v
},
}
- // When the branch names doesn't contain wildcards, we should make sure the branch exists in the repo.
+ // Check branch existence.
notFound, err := isBranchNotFound(
ctx,
vcs,
diff --git a/backend/api/v1/vcs_connector_service.go b/backend/api/v1/vcs_connector_service.go
index f42c6d8080..f1aac3edac 100644
--- a/backend/api/v1/vcs_connector_service.go
+++ b/backend/api/v1/vcs_connector_service.go
@@ -88,6 +88,21 @@ func (s *VCSConnectorService) CreateVCSConnector(ctx context.Context, request *v
return nil, status.Errorf(codes.NotFound, "vcs %s not found", vcsResourceID)
}
+ // Check branch existence.
+ notFound, err := isBranchNotFound(
+ ctx,
+ vcs,
+ vcs.AccessToken,
+ request.GetVcsConnector().ExternalId,
+ request.GetVcsConnector().Branch,
+ )
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to check branch %s with error: %v", request.GetVcsConnector().Branch, err.Error())
+ }
+ if notFound {
+ return nil, status.Errorf(codes.NotFound, "branch %s not found in repository %s", request.GetVcsConnector().Branch, request.GetVcsConnector().FullPath)
+ }
+
baseDir := request.GetVcsConnector().BaseDirectory
// Azure DevOps base directory should start with /.
if vcs.Type == vcsplugin.AzureDevOps {
@@ -275,6 +290,27 @@ func (s *VCSConnectorService) UpdateVCSConnector(ctx context.Context, request *v
update.BaseDirectory = &baseDir
}
}
+
+ // Check branch existence.
+ if v := update.Branch; v != nil {
+ if *v == "" {
+ return nil, status.Errorf(codes.InvalidArgument, "branch must be specified")
+ }
+ notFound, err := isBranchNotFound(
+ ctx,
+ vcs,
+ vcs.AccessToken,
+ vcsConnector.Payload.ExternalId,
+ *v,
+ )
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to check branch: %v", err.Error())
+ }
+ if notFound {
+ return nil, status.Errorf(codes.NotFound, "branch %s not found in repository %s", *v, vcsConnector.Payload.FullPath)
+ }
+ }
+
if err := s.store.UpdateVCSConnector(ctx, update); err != nil {
return nil, err
}
diff --git a/backend/tests/schema_update_test.go b/backend/tests/schema_update_test.go
index c8ee536b97..3b1b68c265 100644
--- a/backend/tests/schema_update_test.go
+++ b/backend/tests/schema_update_test.go
@@ -318,8 +318,8 @@ func TestSimpleVCS(t *testing.T) {
Title: "Test VCS Connector",
FullPath: test.repositoryFullPath,
WebUrl: fmt.Sprintf("%s/%s", ctl.vcsURL, test.repositoryFullPath),
- Branch: "feature/bar",
- BaseDirectory: baseDirectory,
+ Branch: "feature/foo",
+ BaseDirectory: baseDirectory + "+invalid",
ExternalId: test.externalID,
},
VcsConnectorId: "default",
@@ -327,13 +327,13 @@ func TestSimpleVCS(t *testing.T) {
a.NoError(err)
vcsConnector, err := ctl.vcsConnectorServiceClient.UpdateVCSConnector(ctx, &v1pb.UpdateVCSConnectorRequest{
VcsConnector: &v1pb.VCSConnector{
- Name: oldVcsConnector.Name,
- Branch: "feature/foo",
+ Name: oldVcsConnector.Name,
+ BaseDirectory: baseDirectory,
},
- UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"branch"}},
+ UpdateMask: &fieldmaskpb.FieldMask{Paths: []string{"base_directory"}},
})
a.NoError(err)
- a.Equal("feature/foo", vcsConnector.Branch)
+ a.Equal(baseDirectory, vcsConnector.BaseDirectory)
// Provision an instance.
instanceName := "testInstance1"