Skip to content

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

chore: use store vcs type (#11478)

Danny Xuf001b57411e0
diff --git a/backend/api/gitops/azure.go b/backend/api/gitops/azure.go
index 8ff16e1765..294824c766 100644
--- a/backend/api/gitops/azure.go
+++ b/backend/api/gitops/azure.go
@@ -10,6 +10,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/plugin/vcs/azure"
"github.com/bytebase/bytebase/backend/store"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
func getAzurePullRequestInfo(ctx context.Context, vcsProvider *store.VCSProviderMessage, vcsConnector *store.VCSConnectorMessage, body []byte) (*pullRequestInfo, error) {
@@ -35,7 +36,7 @@ func getAzurePullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvider
}
mrFiles, err := vcs.Get(
- vcs.AzureDevOps,
+ storepb.VCSType_AZURE_DEVOPS,
vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken},
).ListPullRequestFile(
ctx,
@@ -56,7 +57,7 @@ func getAzurePullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvider
for _, file := range prInfo.changes {
content, err := vcs.Get(
- vcs.AzureDevOps,
+ storepb.VCSType_AZURE_DEVOPS,
vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken},
).ReadFileContent(
ctx,
diff --git a/backend/api/gitops/bitbucket.go b/backend/api/gitops/bitbucket.go
index a562160c0e..0fe5a507a7 100644
--- a/backend/api/gitops/bitbucket.go
+++ b/backend/api/gitops/bitbucket.go
@@ -10,6 +10,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/plugin/vcs/bitbucket"
"github.com/bytebase/bytebase/backend/store"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
func getBitBucketPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProviderMessage, vcsConnector *store.VCSConnectorMessage, body []byte) (*pullRequestInfo, error) {
@@ -22,7 +23,7 @@ func getBitBucketPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProv
return nil, errors.Errorf("committed to branch %q, want branch %q", pushEvent.PullRequest.Destination.Branch.Name, vcsConnector.Payload.Branch)
}
- mrFiles, err := vcs.Get(vcs.Bitbucket, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ListPullRequestFile(ctx, vcsConnector.Payload.ExternalId, fmt.Sprintf("%d", pushEvent.PullRequest.ID))
+ mrFiles, err := vcs.Get(storepb.VCSType_BITBUCKET, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ListPullRequestFile(ctx, vcsConnector.Payload.ExternalId, fmt.Sprintf("%d", pushEvent.PullRequest.ID))
if err != nil {
return nil, errors.Errorf("failed to list merge %q request files, error %v", pushEvent.PullRequest.Links.HTML.Href, err)
}
@@ -36,7 +37,7 @@ func getBitBucketPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProv
}
for _, file := range prInfo.changes {
- content, err := vcs.Get(vcs.Bitbucket, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ReadFileContent(ctx, vcsConnector.Payload.ExternalId, file.path, vcs.RefInfo{RefType: vcs.RefTypeCommit, RefName: pushEvent.PullRequest.Source.Commit.Hash})
+ content, err := vcs.Get(storepb.VCSType_BITBUCKET, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ReadFileContent(ctx, vcsConnector.Payload.ExternalId, file.path, vcs.RefInfo{RefType: vcs.RefTypeCommit, RefName: pushEvent.PullRequest.Source.Commit.Hash})
if err != nil {
return nil, errors.Errorf("failed read file content, merge request %q, file %q, error %v", pushEvent.PullRequest.Links.HTML.Href, file.path, err)
}
diff --git a/backend/api/gitops/github.go b/backend/api/gitops/github.go
index 8c25ab8caa..eaeb4713d5 100644
--- a/backend/api/gitops/github.go
+++ b/backend/api/gitops/github.go
@@ -10,6 +10,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/plugin/vcs/github"
"github.com/bytebase/bytebase/backend/store"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
const (
@@ -29,7 +30,7 @@ func getGitHubPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvide
return nil, errors.Errorf("committed to branch %q, want branch %q", pushEvent.PullRequest.Base.Ref, vcsConnector.Payload.Branch)
}
- mrFiles, err := vcs.Get(vcs.GitHub, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ListPullRequestFile(ctx, vcsConnector.Payload.ExternalId, fmt.Sprintf("%d", pushEvent.Number))
+ mrFiles, err := vcs.Get(storepb.VCSType_GITHUB, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ListPullRequestFile(ctx, vcsConnector.Payload.ExternalId, fmt.Sprintf("%d", pushEvent.Number))
if err != nil {
return nil, errors.Errorf("failed to list merge %q request files, error %v", pushEvent.PullRequest.HTMLURL, err)
}
@@ -43,7 +44,7 @@ func getGitHubPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvide
}
for _, file := range prInfo.changes {
- content, err := vcs.Get(vcs.GitHub, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ReadFileContent(ctx, vcsConnector.Payload.ExternalId, file.path, vcs.RefInfo{RefType: vcs.RefTypeCommit, RefName: pushEvent.PullRequest.Head.SHA})
+ content, err := vcs.Get(storepb.VCSType_GITHUB, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ReadFileContent(ctx, vcsConnector.Payload.ExternalId, file.path, vcs.RefInfo{RefType: vcs.RefTypeCommit, RefName: pushEvent.PullRequest.Head.SHA})
if err != nil {
return nil, errors.Errorf("failed read file content, merge request %q, file %q, error %v", pushEvent.PullRequest.HTMLURL, file.path, err)
}
diff --git a/backend/api/gitops/gitlab.go b/backend/api/gitops/gitlab.go
index 5ce977a819..b8273f6f7c 100644
--- a/backend/api/gitops/gitlab.go
+++ b/backend/api/gitops/gitlab.go
@@ -10,6 +10,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/plugin/vcs/gitlab"
"github.com/bytebase/bytebase/backend/store"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
const (
@@ -33,7 +34,7 @@ func getGitLabPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvide
return nil, errors.Errorf("committed to branch %q, want branch %q", pushEvent.ObjectAttributes.TargetBranch, vcsConnector.Payload.Branch)
}
- mrFiles, err := vcs.Get(vcs.GitLab, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ListPullRequestFile(ctx, vcsConnector.Payload.ExternalId, fmt.Sprintf("%d", pushEvent.ObjectAttributes.IID))
+ mrFiles, err := vcs.Get(storepb.VCSType_GITLAB, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ListPullRequestFile(ctx, vcsConnector.Payload.ExternalId, fmt.Sprintf("%d", pushEvent.ObjectAttributes.IID))
if err != nil {
return nil, errors.Errorf("failed to list merge %q request files, error %v", pushEvent.ObjectAttributes.URL, err)
}
@@ -47,7 +48,7 @@ func getGitLabPullRequestInfo(ctx context.Context, vcsProvider *store.VCSProvide
}
for _, file := range prInfo.changes {
- content, err := vcs.Get(vcs.GitLab, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ReadFileContent(ctx, vcsConnector.Payload.ExternalId, file.path, vcs.RefInfo{RefType: vcs.RefTypeCommit, RefName: pushEvent.ObjectAttributes.LastCommit.ID})
+ content, err := vcs.Get(storepb.VCSType_GITLAB, vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken}).ReadFileContent(ctx, vcsConnector.Payload.ExternalId, file.path, vcs.RefInfo{RefType: vcs.RefTypeCommit, RefName: pushEvent.ObjectAttributes.LastCommit.ID})
if err != nil {
return nil, errors.Errorf("failed read file content, merge request %q, file %q, error %v", pushEvent.ObjectAttributes.URL, file.path, err)
}
diff --git a/backend/api/gitops/webhook.go b/backend/api/gitops/webhook.go
index 6470fb3b49..22cd0fe317 100644
--- a/backend/api/gitops/webhook.go
+++ b/backend/api/gitops/webhook.go
@@ -26,6 +26,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/store"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
@@ -82,7 +83,7 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
var prInfo *pullRequestInfo
switch vcsProvider.Type {
- case vcs.GitHub:
+ case storepb.VCSType_GITHUB:
secretToken := c.Request().Header.Get("X-Hub-Signature-256")
ok, err := validateGitHubWebhookSignature256(secretToken, vcsConnector.Payload.WebhookSecretToken, body)
if err != nil {
@@ -107,7 +108,7 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
if err != nil {
return c.String(http.StatusOK, fmt.Sprintf("failed to get pr info from pull request, error %v", err))
}
- case vcs.GitLab:
+ case storepb.VCSType_GITLAB:
secretToken := c.Request().Header.Get("X-Gitlab-Token")
if secretToken != vcsConnector.Payload.WebhookSecretToken {
return c.String(http.StatusOK, fmt.Sprintf("invalid webhook secret token %q", secretToken))
@@ -117,7 +118,7 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
if err != nil {
return c.String(http.StatusOK, fmt.Sprintf("failed to get pr info from pull request, error %v", err))
}
- case vcs.Bitbucket:
+ case storepb.VCSType_BITBUCKET:
eventType := c.Request().Header.Get("X-Event-Key")
switch eventType {
case "pullrequest:created", "pullrequest:updated":
@@ -131,7 +132,7 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
if err != nil {
return c.String(http.StatusOK, fmt.Sprintf("failed to get pr info from pull request, error %v", err))
}
- case vcs.AzureDevOps:
+ case storepb.VCSType_AZURE_DEVOPS:
secretToken := c.Request().Header.Get("X-Azure-Token")
if secretToken != vcsConnector.Payload.WebhookSecretToken {
return c.String(http.StatusOK, fmt.Sprintf("invalid webhook secret token %q", secretToken))
@@ -148,11 +149,11 @@ func (s *Service) RegisterWebhookRoutes(g *echo.Group) {
if err != nil {
return c.String(http.StatusOK, fmt.Sprintf("failed to create issue from pull request %s, error %v", prInfo.url, err))
}
- if vcsProvider.Type == vcs.GitHub {
+ if vcsProvider.Type == storepb.VCSType_GITHUB {
comment := getPullRequestComment(setting.ExternalUrl, issue.Name)
pullRequestID := getPullRequestID(prInfo.url)
if err := vcs.Get(
- vcs.GitHub,
+ storepb.VCSType_GITHUB,
vcs.ProviderConfig{InstanceURL: vcsProvider.InstanceURL, AuthToken: vcsProvider.AccessToken},
).CreatePullRequestComment(ctx, vcsConnector.Payload.ExternalId, pullRequestID, comment); err != nil {
return c.String(http.StatusOK, fmt.Sprintf("failed to create pull request comment, error %v", err))
@@ -218,7 +219,7 @@ func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.Proj
VcsSource: &v1pb.Plan_VCSSource{
VcsConnector: fmt.Sprintf("%s%s/%s%s", common.ProjectNamePrefix, vcsConnector.ProjectID, common.VCSConnectorPrefix, vcsConnector.ResourceID),
PullRequestUrl: prInfo.url,
- VcsType: getVCSType(vcsProvider.Type),
+ VcsType: v1pb.VCSType(vcsProvider.Type),
},
},
})
@@ -278,20 +279,6 @@ func (s *Service) createIssueFromPRInfo(ctx context.Context, project *store.Proj
return issue, nil
}
-func getVCSType(tp vcs.Type) v1pb.VCSType {
- switch tp {
- case vcs.GitHub:
- return v1pb.VCSType_GITHUB
- case vcs.GitLab:
- return v1pb.VCSType_GITLAB
- case vcs.Bitbucket:
- return v1pb.VCSType_BITBUCKET
- case vcs.AzureDevOps:
- return v1pb.VCSType_AZURE_DEVOPS
- }
- return v1pb.VCSType_VCS_TYPE_UNSPECIFIED
-}
-
func (s *Service) getChangeSteps(
ctx context.Context,
project *store.ProjectMessage,
diff --git a/backend/api/v1/vcs_connector_service.go b/backend/api/v1/vcs_connector_service.go
index 33cc3ce0c9..b0ae8e4553 100644
--- a/backend/api/v1/vcs_connector_service.go
+++ b/backend/api/v1/vcs_connector_service.go
@@ -110,7 +110,7 @@ func (s *VCSConnectorService) CreateVCSConnector(ctx context.Context, request *v
baseDir := request.GetVcsConnector().BaseDirectory
// Azure DevOps base directory should start with /.
- if vcsProvider.Type == vcs.AzureDevOps {
+ if vcsProvider.Type == storepb.VCSType_AZURE_DEVOPS {
if !strings.HasPrefix(baseDir, "/") {
baseDir = "/" + request.GetVcsConnector().BaseDirectory
}
@@ -292,7 +292,7 @@ func (s *VCSConnectorService) UpdateVCSConnector(ctx context.Context, request *v
case "base_directory":
baseDir := request.GetVcsConnector().BaseDirectory
// Azure DevOps base directory should start with /.
- if vcsProvider.Type == vcs.AzureDevOps {
+ if vcsProvider.Type == storepb.VCSType_AZURE_DEVOPS {
if !strings.HasPrefix(baseDir, "/") {
baseDir = "/" + request.GetVcsConnector().BaseDirectory
}
@@ -438,7 +438,7 @@ func createVCSWebhook(ctx context.Context, vcsProvider *store.VCSProviderMessage
var webhookCreatePayload []byte
var err error
switch vcsProvider.Type {
- case vcs.GitLab:
+ case storepb.VCSType_GITLAB:
webhookCreate := gitlab.WebhookCreate{
URL: fmt.Sprintf("%s/hook/%s", bytebaseEndpointURL, webhookEndpointID),
SecretToken: webhookSecretToken,
@@ -450,7 +450,7 @@ func createVCSWebhook(ctx context.Context, vcsProvider *store.VCSProviderMessage
if err != nil {
return "", errors.Wrap(err, "failed to marshal request body for creating webhook")
}
- case vcs.GitHub:
+ case storepb.VCSType_GITHUB:
webhookPost := github.WebhookCreateOrUpdate{
Config: github.WebhookConfig{
URL: fmt.Sprintf("%s/hook/%s", bytebaseEndpointURL, webhookEndpointID),
@@ -464,7 +464,7 @@ func createVCSWebhook(ctx context.Context, vcsProvider *store.VCSProviderMessage
if err != nil {
return "", errors.Wrap(err, "failed to marshal request body for creating webhook")
}
- case vcs.Bitbucket:
+ case storepb.VCSType_BITBUCKET:
webhookPost := bitbucket.WebhookCreateOrUpdate{
Description: "Bytebase GitOps",
URL: fmt.Sprintf("%s/hook/%s", bytebaseEndpointURL, webhookEndpointID),
@@ -475,7 +475,7 @@ func createVCSWebhook(ctx context.Context, vcsProvider *store.VCSProviderMessage
if err != nil {
return "", errors.Wrap(err, "failed to marshal request body for creating webhook")
}
- case vcs.AzureDevOps:
+ case storepb.VCSType_AZURE_DEVOPS:
part := strings.Split(externalRepoID, "/")
if len(part) != 3 {
return "", errors.Errorf("invalid external repo id %q", externalRepoID)
diff --git a/backend/api/v1/vcs_provider_service.go b/backend/api/v1/vcs_provider_service.go
index 9cb54130a4..fae7c9aa0c 100644
--- a/backend/api/v1/vcs_provider_service.go
+++ b/backend/api/v1/vcs_provider_service.go
@@ -13,6 +13,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/store"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
v1pb "github.com/bytebase/bytebase/proto/generated-go/v1"
)
@@ -202,22 +203,10 @@ func convertToVCSProviders(vcsProviders []*store.VCSProviderMessage) []*v1pb.VCS
}
func convertVCSProvider(vcsProvider *store.VCSProviderMessage) *v1pb.VCSProvider {
- tp := v1pb.VCSType_VCS_TYPE_UNSPECIFIED
- switch vcsProvider.Type {
- case vcs.GitHub:
- tp = v1pb.VCSType_GITHUB
- case vcs.GitLab:
- tp = v1pb.VCSType_GITLAB
- case vcs.Bitbucket:
- tp = v1pb.VCSType_BITBUCKET
- case vcs.AzureDevOps:
- tp = v1pb.VCSType_AZURE_DEVOPS
- }
-
return &v1pb.VCSProvider{
Name: fmt.Sprintf("%s%s", common.VCSProviderPrefix, vcsProvider.ResourceID),
Title: vcsProvider.Title,
- Type: tp,
+ Type: v1pb.VCSType(vcsProvider.Type),
Url: vcsProvider.InstanceURL,
}
}
@@ -236,32 +225,14 @@ func convertV1VCSProvider(request *v1pb.CreateVCSProviderRequest) (*store.VCSPro
if v1VCSProvider.GetAccessToken() == "" {
return nil, errors.Errorf("Empty VCSProvider.Secret")
}
- tp, err := convertVCSProviderTypeToVCSType(v1VCSProvider.GetType())
- if err != nil {
- return nil, err
- }
storeVCSProvider := &store.VCSProviderMessage{
ResourceID: request.GetVcsProviderId(),
Title: v1VCSProvider.GetTitle(),
- Type: tp,
+ Type: storepb.VCSType(v1VCSProvider.GetType()),
InstanceURL: strings.TrimRight(v1VCSProvider.GetUrl(), "/"),
AccessToken: v1VCSProvider.GetAccessToken(),
}
return storeVCSProvider, nil
}
-
-func convertVCSProviderTypeToVCSType(tp v1pb.VCSType) (vcs.Type, error) {
- switch tp {
- case v1pb.VCSType_GITHUB:
- return vcs.GitHub, nil
- case v1pb.VCSType_GITLAB:
- return vcs.GitLab, nil
- case v1pb.VCSType_BITBUCKET:
- return vcs.Bitbucket, nil
- case v1pb.VCSType_AZURE_DEVOPS:
- return vcs.AzureDevOps, nil
- }
- return "", errors.Errorf("unknown vcs provider type: %v", tp)
-}
diff --git a/backend/legacyapi/vcs.go b/backend/legacyapi/vcs.go
deleted file mode 100644
index c3de6f220b..0000000000
--- a/backend/legacyapi/vcs.go
+++ /dev/null
@@ -1,33 +0,0 @@
-package api
-
-import (
- "github.com/bytebase/bytebase/backend/plugin/advisor"
- "github.com/bytebase/bytebase/backend/plugin/vcs"
-)
-
-// VCSExchangeToken is the API message of exchanging token for a VCS.
-type VCSExchangeToken struct {
- Code string `jsonapi:"attr,code"`
- ID int `jsonapi:"attr,vcsId"`
- Type vcs.Type `jsonapi:"attr,vcsType"`
- InstanceURL string `jsonapi:"attr,instanceUrl"`
- ClientID string `jsonapi:"attr,clientId"`
- ClientSecret string `jsonapi:"attr,clientSecret"`
-}
-
-// VCSSQLReviewResult the is SQL review result in VCS workflow.
-type VCSSQLReviewResult struct {
- Status advisor.Status `json:"status"`
- Content []string `json:"content"`
-}
-
-// VCSSQLReviewRequest is the request from SQL review CI in VCS workflow.
-// In the VCS SQL review workflow, the CI will generate the request body then POST /hook/sql-review/:webhook_endpoint_id.
-type VCSSQLReviewRequest struct {
- RepositoryID string `json:"repositoryId"`
- PullRequestID string `json:"pullRequestId"`
- // WebURL is the server URL for GitOps CI.
- // In GitHub, the URL should be "https://github.com". Docs: https://docs.github.com/en/actions/learn-github-actions/environment-variables
- // In GitLab, the URL should be the base URL of the GitLab instance like "https://gitlab.bytebase.com". Docs: https://docs.gitlab.com/ee/ci/variables/predefined_variables.html
- WebURL string `json:"webURL"`
-}
diff --git a/backend/plugin/vcs/azure/azure.go b/backend/plugin/vcs/azure/azure.go
index bc30cab31f..e915f3c79a 100644
--- a/backend/plugin/vcs/azure/azure.go
+++ b/backend/plugin/vcs/azure/azure.go
@@ -17,10 +17,11 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/plugin/vcs/internal"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
func init() {
- vcs.Register(vcs.AzureDevOps, newProvider)
+ vcs.Register(storepb.VCSType_AZURE_DEVOPS, newProvider)
}
var _ vcs.Provider = (*Provider)(nil)
diff --git a/backend/plugin/vcs/bitbucket/bitbucket.go b/backend/plugin/vcs/bitbucket/bitbucket.go
index 63097b9ce2..c34ab2f013 100644
--- a/backend/plugin/vcs/bitbucket/bitbucket.go
+++ b/backend/plugin/vcs/bitbucket/bitbucket.go
@@ -16,6 +16,7 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/plugin/vcs/internal"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
const (
@@ -27,7 +28,7 @@ const (
)
func init() {
- vcs.Register(vcs.Bitbucket, newProvider)
+ vcs.Register(storepb.VCSType_BITBUCKET, newProvider)
}
var _ vcs.Provider = (*Provider)(nil)
diff --git a/backend/plugin/vcs/github/github.go b/backend/plugin/vcs/github/github.go
index 0ca923c886..3d8b9e0347 100644
--- a/backend/plugin/vcs/github/github.go
+++ b/backend/plugin/vcs/github/github.go
@@ -17,6 +17,7 @@ import (
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/plugin/vcs/internal"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
const (
@@ -28,7 +29,7 @@ const (
)
func init() {
- vcs.Register(vcs.GitHub, newProvider)
+ vcs.Register(storepb.VCSType_GITHUB, newProvider)
}
var _ vcs.Provider = (*Provider)(nil)
diff --git a/backend/plugin/vcs/gitlab/gitlab.go b/backend/plugin/vcs/gitlab/gitlab.go
index 9bc1092626..86b325fcc9 100644
--- a/backend/plugin/vcs/gitlab/gitlab.go
+++ b/backend/plugin/vcs/gitlab/gitlab.go
@@ -15,8 +15,15 @@ import (
"github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/plugin/vcs"
"github.com/bytebase/bytebase/backend/plugin/vcs/internal"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
+var _ vcs.Provider = (*Provider)(nil)
+
+func init() {
+ vcs.Register(storepb.VCSType_GITLAB, newProvider)
+}
+
const (
// SecretTokenLength is the length of secret token.
SecretTokenLength = 16
@@ -27,8 +34,6 @@ const (
apiPageSize = 100
)
-var _ vcs.Provider = (*Provider)(nil)
-
// WebhookType is the GitLab webhook type.
type WebhookType string
@@ -139,10 +144,6 @@ type gitLabRepository struct {
WebURL string `json:"web_url"`
}
-func init() {
- vcs.Register(vcs.GitLab, newProvider)
-}
-
// Provider is a GitLab self host VCS provider.
type Provider struct {
client *http.Client
diff --git a/backend/plugin/vcs/vcs.go b/backend/plugin/vcs/vcs.go
index d8aaceeab8..37fc29a4ef 100644
--- a/backend/plugin/vcs/vcs.go
+++ b/backend/plugin/vcs/vcs.go
@@ -2,22 +2,10 @@ package vcs
import (
"context"
+ "fmt"
"sync"
-)
-
-// Type is the type of a VCS.
-// nolint
-type Type string
-const (
- // GitLab is the VCS type for GitLab (both GitLab.com and self-hosted).
- GitLab Type = "GITLAB"
- // GitHub is the VCS type for GitHub (both GitHub.com and GitHun Enterprise).
- GitHub Type = "GITHUB"
- // Bitbucket is the VCS type for Bitbucket Cloud (bitbucket.org).
- Bitbucket Type = "BITBUCKET"
- // AzureDevOps is the VCS type for Azure DevOps.
- AzureDevOps Type = "AZURE_DEVOPS"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
// RefType is the type of a ref.
@@ -109,7 +97,7 @@ type Provider interface {
var (
providerMu sync.RWMutex
- providers = make(map[Type]providerFunc)
+ providers = make(map[storepb.VCSType]providerFunc)
)
// ProviderConfig is the provider configuration.
@@ -123,25 +111,25 @@ type providerFunc func(ProviderConfig) Provider
// Register makes a vcs provider available by the provided type.
// If Register is called twice with the same name or if provider is nil,
// it panics.
-func Register(vcsType Type, f providerFunc) {
+func Register(vcsType storepb.VCSType, f providerFunc) {
providerMu.Lock()
defer providerMu.Unlock()
if f == nil {
panic("vcs: Register provider is nil")
}
if _, dup := providers[vcsType]; dup {
- panic("vcs: Register called twice for provider " + vcsType)
+ panic(fmt.Sprintf("vcs: Register called twice for provider %q", vcsType.String()))
}
providers[vcsType] = f
}
// Get returns a vcs provider specified by its vcs type.
-func Get(vcsType Type, providerConfig ProviderConfig) Provider {
+func Get(vcsType storepb.VCSType, providerConfig ProviderConfig) Provider {
providerMu.RLock()
f, ok := providers[vcsType]
providerMu.RUnlock()
if !ok {
- panic("vcs: unknown provider " + vcsType)
+ panic(fmt.Sprintf("vcs: unknown provider %q", vcsType.String()))
}
return f(providerConfig)
diff --git a/backend/store/vcs.go b/backend/store/vcs_provider.go
similarity index 91%
rename from backend/store/vcs.go
rename to backend/store/vcs_provider.go
index f9b263aa7e..9540fed914 100644
--- a/backend/store/vcs.go
+++ b/backend/store/vcs_provider.go
@@ -9,7 +9,7 @@ import (
"github.com/pkg/errors"
"github.com/bytebase/bytebase/backend/common"
- "github.com/bytebase/bytebase/backend/plugin/vcs"
+ storepb "github.com/bytebase/bytebase/proto/generated-go/store"
)
// VCSProviderMessage is a message for VCS provider.
@@ -17,7 +17,7 @@ type VCSProviderMessage struct {
// ResourceID is the unique resource ID.
ResourceID string
// Type is the type of the VCS provider.
- Type vcs.Type
+ Type storepb.VCSType
// InstanceURL is the URL for the VCS provider instance.
InstanceURL string
// Title is the name of the VCS provider.
@@ -118,24 +118,18 @@ func (s *Store) CreateVCSProvider(ctx context.Context, principalUID int, create
access_token
)
VALUES ($1, $2, $3, $4, $5, $6, $7)
- RETURNING id, resource_id, name, type, instance_url, access_token
+ RETURNING id
`
- var vcsProvider VCSProviderMessage
if err := tx.QueryRowContext(ctx, query,
principalUID,
principalUID,
create.ResourceID,
create.Title,
- create.Type,
+ create.Type.String(),
create.InstanceURL,
create.AccessToken,
).Scan(
- &vcsProvider.ID,
- &vcsProvider.ResourceID,
- &vcsProvider.Title,
- &vcsProvider.Type,
- &vcsProvider.InstanceURL,
- &vcsProvider.AccessToken,
+ &create.ID,
); err != nil {
if err == sql.ErrNoRows {
return nil, common.FormatDBErrorEmptyRowWithQuery(query)
@@ -147,8 +141,8 @@ func (s *Store) CreateVCSProvider(ctx context.Context, principalUID int, create
return nil, errors.Wrapf(err, "failed to commit transaction")
}
- s.vcsIDCache.Add(vcsProvider.ID, &vcsProvider)
- return &vcsProvider, nil
+ s.vcsIDCache.Add(create.ID, create)
+ return create, nil
}
// UpdateVCSProvider updates an VCS provider.
@@ -170,7 +164,7 @@ func (s *Store) UpdateVCSProvider(ctx context.Context, principalUID int, vcsProv
defer tx.Rollback()
var vcsProvider VCSProviderMessage
- // Execute update query with RETURNING.
+ var vcsType string
if err := tx.QueryRowContext(ctx, fmt.Sprintf(`
UPDATE vcs
SET `+strings.Join(set, ", ")+`
@@ -182,15 +176,17 @@ func (s *Store) UpdateVCSProvider(ctx context.Context, principalUID int, vcsProv
&vcsProvider.ID,
&vcsProvider.ResourceID,
&vcsProvider.Title,
- &vcsProvider.Type,
+ &vcsType,
&vcsProvider.InstanceURL,
&vcsProvider.AccessToken,
); err != nil {
- if err == sql.ErrNoRows {
- return nil, &common.Error{Code: common.NotFound, Err: errors.Errorf("vcs ID not found: %d", vcsProviderUID)}
- }
return nil, err
}
+ vcsTypeValue, ok := storepb.VCSType_value[vcsType]
+ if !ok {
+ return nil, errors.Errorf("invalid vcs type %s", vcsType)
+ }
+ vcsProvider.Type = storepb.VCSType(vcsTypeValue)
if err := tx.Commit(); err != nil {
return nil, errors.Wrapf(err, "failed to commit transaction")
@@ -247,16 +243,22 @@ func (*Store) findVCSProvidersImpl(ctx context.Context, tx *Tx, find *FindVCSPro
var vcsProviders []*VCSProviderMessage
for rows.Next() {
var vcsProvider VCSProviderMessage
+ var vcsType string
if err := rows.Scan(
&vcsProvider.ID,
&vcsProvider.ResourceID,
&vcsProvider.Title,
- &vcsProvider.Type,
+ &vcsType,
&vcsProvider.InstanceURL,
&vcsProvider.AccessToken,
); err != nil {
return nil, err
}
+ vcsTypeValue, ok := storepb.VCSType_value[vcsType]
+ if !ok {
+ return nil, errors.Errorf("invalid vcs type %s", vcsType)
+ }
+ vcsProvider.Type = storepb.VCSType(vcsTypeValue)
vcsProviders = append(vcsProviders, &vcsProvider)
}
if err := rows.Err(); err != nil {