Skip to content

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

chore: use pipeline variable in azure pipeline (#7410)

ecmadaocc2e769a9a66
diff --git a/backend/api/v1/project_service.go b/backend/api/v1/project_service.go
index 9ddd782490..1f96d597a0 100644
--- a/backend/api/v1/project_service.go
+++ b/backend/api/v1/project_service.go
@@ -1382,7 +1382,7 @@ func (s *ProjectService) setupVCSSQLReviewCIForAzureDevOps(
branch *vcsPlugin.BranchInfo,
sqlReviewEndpoint string,
) error {
- sqlReviewConfig := azure.SetupSQLReviewCI(sqlReviewEndpoint, repository.BranchFilter, repository.WebhookSecretToken)
+ sqlReviewConfig := azure.SetupSQLReviewCI(sqlReviewEndpoint, repository.BranchFilter)
fileLastCommitID := ""
fileSHA := ""
diff --git a/backend/plugin/vcs/azure/azure.go b/backend/plugin/vcs/azure/azure.go
index 69a569c694..5a872569da 100644
--- a/backend/plugin/vcs/azure/azure.go
+++ b/backend/plugin/vcs/azure/azure.go
@@ -1017,10 +1017,7 @@ func (p *Provider) GetBranch(ctx context.Context, oauthCtx common.OauthContext,
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
- if code == http.StatusNotFound {
- return nil, common.Errorf(common.NotFound, fmt.Sprintf("branch %q does not exist in the repository %s", branchName, repositoryID))
- }
- if code != http.StatusOK {
+ if code >= 300 {
return nil, errors.Errorf("non-200 GET %s status code %d with body %q", url, code, string(body))
}
@@ -1091,9 +1088,7 @@ func (p *Provider) CreateBranch(ctx context.Context, oauthCtx common.OauthContex
if err != nil {
return errors.Wrapf(err, "GET %s", url)
}
- if code == http.StatusNotFound {
- return common.Errorf(common.NotFound, "failed to create branch from URL %s", url)
- } else if code >= 300 {
+ if code >= 300 {
return errors.Errorf("failed to create branch from URL %s, status code: %d, body: %s",
url,
code,
@@ -1141,9 +1136,7 @@ func (p *Provider) ListPullRequestFile(ctx context.Context, oauthCtx common.Oaut
if err != nil {
return nil, errors.Wrapf(err, "GET %s", url)
}
- if code == http.StatusNotFound {
- return nil, common.Errorf(common.NotFound, "failed to create merge request from URL %s", url)
- } else if code >= 300 {
+ if code >= 300 {
return nil, errors.Errorf("failed to create merge request from URL %s, status code: %d, body: %s",
url,
code,
@@ -1224,9 +1217,7 @@ func (p *Provider) CreatePullRequest(ctx context.Context, oauthCtx common.OauthC
return nil, errors.Wrapf(err, "POST %s", url)
}
- if code == http.StatusNotFound {
- return nil, common.Errorf(common.NotFound, "failed to create pull request from URL %s", url)
- } else if code >= 300 {
+ if code >= 300 {
return nil, errors.Errorf("failed to create pull request from URL %s, status code: %d, body: %s",
url,
code,
@@ -1249,7 +1240,7 @@ func (p *Provider) CreatePullRequest(ctx context.Context, oauthCtx common.OauthC
// UpsertEnvironmentVariable creates or updates the environment variable in the repository.
func (*Provider) UpsertEnvironmentVariable(context.Context, common.OauthContext, string, string, string, string) error {
- // TODO: set api token to pipeline variable.
+ // We will set the variable in pipeline. Check sql_review.go/createSQLReviewPipeline function.
return nil
}
diff --git a/backend/plugin/vcs/azure/bytebase-sql-review.yml b/backend/plugin/vcs/azure/bytebase-sql-review.yml
index 510db3d98c..5180c36ec6 100644
--- a/backend/plugin/vcs/azure/bytebase-sql-review.yml
+++ b/backend/plugin/vcs/azure/bytebase-sql-review.yml
@@ -28,7 +28,7 @@ jobs:
'$ARGS.named')
response=$(curl -s --show-error -X POST ${{ variables.api }} \
- -H "X-SQL-Review-Token: %s" \
+ -H "X-SQL-Review-Token: $(%s)" \
-H "Content-Type: application/json" \
-d "$request_body")
diff --git a/backend/plugin/vcs/azure/sql_review.go b/backend/plugin/vcs/azure/sql_review.go
index 698a20898a..7d0c1a44ef 100644
--- a/backend/plugin/vcs/azure/sql_review.go
+++ b/backend/plugin/vcs/azure/sql_review.go
@@ -13,6 +13,7 @@ import (
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
+ "github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/plugin/vcs/internal/oauth"
)
@@ -29,8 +30,8 @@ const (
)
// SetupSQLReviewCI will setup the SQL review CI content with SQL review endpoint.
-func SetupSQLReviewCI(endpoint, branch, token string) string {
- return fmt.Sprintf(sqlReviewPipeline, endpoint, branch, token)
+func SetupSQLReviewCI(endpoint, branch string) string {
+ return fmt.Sprintf(sqlReviewPipeline, endpoint, branch, vcs.SQLReviewAPISecretName)
}
type pipeline struct {
@@ -49,9 +50,16 @@ type pipelineRepository struct {
}
type pipelineConfig struct {
- Type string `json:"type"`
- Path string `json:"path"`
- Repository *pipelineRepository `json:"repository"`
+ Type string `json:"type"`
+ Path string `json:"path"`
+ Repository *pipelineRepository `json:"repository"`
+ Variables map[string]*pipelineVariable `json:"variables"`
+}
+
+type pipelineVariable struct {
+ Value string `json:"value"`
+ IsSecret bool `json:"isSecret"`
+ AllowOverride bool `json:"allowOverride"`
}
type branchPolicyCreate struct {
@@ -83,15 +91,15 @@ type branchPolicyScope struct {
// policy is the API message for azure policy.
// It is hard code with specific id.
-// We can check all the policies in docs: https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/list?view=azure-devops-rest-7.1
+// We can check all the policies in docs: https://learn.microsoft.com/en-us/rest/api/azure/devops/policy/types/list?view=azure-devops-rest-7.1
// For branch validation policy, we will hardcode the id as "0609b952-1397-4640-95ec-e00a01b2c241".
type policy struct {
ID string `json:"id"`
}
// EnableSQLReviewCI enables the SQL review pipeline and policy.
-func EnableSQLReviewCI(ctx context.Context, oauthCtx common.OauthContext, repositoryID, branch string) error {
- pipeline, err := createSQLReviewPipeline(ctx, oauthCtx, repositoryID)
+func EnableSQLReviewCI(ctx context.Context, oauthCtx common.OauthContext, repositoryID, branch, token string) error {
+ pipeline, err := createSQLReviewPipeline(ctx, oauthCtx, repositoryID, token)
if err != nil {
return err
}
@@ -101,7 +109,7 @@ func EnableSQLReviewCI(ctx context.Context, oauthCtx common.OauthContext, reposi
// createSQLReviewPipeline creates the SQL Review pipeline.
//
// Docs: https://learn.microsoft.com/en-us/rest/api/azure/devops/pipelines/pipelines/create?view=azure-devops-rest-7.1
-func createSQLReviewPipeline(ctx context.Context, oauthCtx common.OauthContext, repositoryID string) (*pipeline, error) {
+func createSQLReviewPipeline(ctx context.Context, oauthCtx common.OauthContext, repositoryID, token string) (*pipeline, error) {
parts := strings.Split(repositoryID, "/")
if len(parts) != 3 {
return nil, errors.Errorf("invalid repository ID %q", repositoryID)
@@ -122,6 +130,13 @@ func createSQLReviewPipeline(ctx context.Context, oauthCtx common.OauthContext,
ID: repositoryID,
Type: "azureReposGit",
},
+ Variables: map[string]*pipelineVariable{
+ vcs.SQLReviewAPISecretName: {
+ Value: token,
+ AllowOverride: false,
+ IsSecret: true,
+ },
+ },
},
}
@@ -148,7 +163,7 @@ func createSQLReviewPipeline(ctx context.Context, oauthCtx common.OauthContext,
if err != nil {
return nil, errors.Wrapf(err, "POST %s", apiURL)
}
- if code != 200 {
+ if code >= 300 {
return nil, errors.Errorf("non-200 POST %s status code %d with body %q", apiURL, code, string(resp))
}
@@ -221,7 +236,7 @@ func createSQLReviewBranchPolicy(ctx context.Context, oauthCtx common.OauthConte
if err != nil {
return errors.Wrapf(err, "POST %s", apiURL)
}
- if code != 200 {
+ if code >= 300 {
return errors.Errorf("non-200 POST %s status code %d with body %q", apiURL, code, string(resp))
}
diff --git a/backend/server/webhook.go b/backend/server/webhook.go
index b875b818ed..418f9ce69c 100644
--- a/backend/server/webhook.go
+++ b/backend/server/webhook.go
@@ -502,7 +502,7 @@ func (s *Server) registerWebhookRoutes(g *echo.Group) {
if len(backfillCommits) == 1 && len(backfillCommits[0].AddedList) == 1 && strings.HasSuffix(backfillCommits[0].AddedList[0], azure.SQLReviewPipelineFilePath) {
// Setup SQL review pipeline and policy.
- if err := azure.EnableSQLReviewCI(ctx, oauthContext, repositoryList[0].repository.ExternalID, repositoryList[0].repository.BranchFilter); err != nil {
+ if err := azure.EnableSQLReviewCI(ctx, oauthContext, repositoryList[0].repository.ExternalID, repositoryList[0].repository.BranchFilter, repositoryList[0].repository.WebhookSecretToken); err != nil {
log.Error("failed to setup pipeline", zap.Error(err), zap.String("repository", repositoryList[0].repository.ExternalID))
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to setup SQL review pipeline").SetInternal(err)
}