Skip to content

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

test(bbc): add schema update integration test (#5272)

Joe Chen4d7021089b31
diff --git a/backend/plugin/vcs/bitbucket/bitbucket.go b/backend/plugin/vcs/bitbucket/bitbucket.go
index 11280af2de..3677643248 100644
--- a/backend/plugin/vcs/bitbucket/bitbucket.go
+++ b/backend/plugin/vcs/bitbucket/bitbucket.go
@@ -52,8 +52,11 @@ func newProvider(config vcs.ProviderConfig) vcs.Provider {
}
// APIURL returns the API URL path of Bitbucket Cloud.
-func (*Provider) APIURL(string) string {
- return "https://api.bitbucket.org/2.0"
+func (*Provider) APIURL(instanceURL string) string {
+ if instanceURL == bitbucketCloudURL {
+ return "https://api.bitbucket.org/2.0"
+ }
+ return fmt.Sprintf("%s/2.0", instanceURL)
}
// oauthResponse is a Bitbucket Cloud OAuth response.
@@ -126,6 +129,7 @@ func (*Provider) TryLogin(context.Context, common.OauthContext, string) (*vcs.Us
// User represents a Bitbucket Cloud API response for a user.
type User struct {
DisplayName string `json:"display_name"`
+ Nickname string `json:"nickname"`
}
// CommitAuthor represents a Bitbucket Cloud API response for a commit author.
@@ -187,11 +191,7 @@ func (p *Provider) FetchCommitByID(ctx context.Context, oauthCtx common.OauthCon
// CommitFile represents a Bitbucket Cloud API response for a file at a commit.
type CommitFile struct {
Path string `json:"path"`
- Links struct {
- Self struct {
- Href string `json:"href"`
- } `json:"self"`
- } `json:"links"`
+ Links Links `json:"links"`
}
// CommitDiffStat represents a Bitbucket Cloud API response for commit diff stat.
@@ -215,7 +215,7 @@ func (p *Provider) GetDiffFileList(ctx context.Context, oauthCtx common.OauthCon
params.Add("page", strconv.Itoa(page))
}
- url := fmt.Sprintf("%s/repositories/%s/diffstat/%s..%s?%s", p.APIURL(instanceURL), url.PathEscape(repositoryID), afterCommit, beforeCommit, params.Encode())
+ url := fmt.Sprintf("%s/repositories/%s/diffstat/%s..%s?%s", p.APIURL(instanceURL), repositoryID, afterCommit, beforeCommit, params.Encode())
diffs, hasNextPage, err := p.fetchPaginatedDiffFileList(ctx, oauthCtx, instanceURL, url)
if err != nil {
return nil, errors.Wrap(err, "fetch paginated list")
@@ -294,6 +294,7 @@ type Repository struct {
UUID string `json:"uuid"`
Name string `json:"name"`
FullName string `json:"full_name"`
+ Links Links `json:"links"`
}
// RepositoryPermission represents a Bitbucket Cloud API response for a
@@ -340,7 +341,6 @@ func (p *Provider) FetchAllRepositoryList(ctx context.Context, oauthCtx common.O
// user has admin access to in given page. It returns the paginated results
// along with a boolean indicating whether the next page exists.
func (p *Provider) fetchPaginatedRepositoryList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, page int) (repos []*Repository, hasNextPage bool, err error) {
- fmt.Println("fuck you", oauthCtx.AccessToken)
params := url.Values{}
params.Add("q", `permission="admin"`)
params.Add("pagelen", strconv.Itoa(apiPageSize))
@@ -452,7 +452,7 @@ func (p *Provider) fetchPaginatedRepositoryFileList(ctx context.Context, oauthCt
if page > 1 {
params.Add("page", strconv.Itoa(page))
}
- url := fmt.Sprintf("%s/repositories/%s/src/%s/%s?%s", p.APIURL(instanceURL), repositoryID, ref, url.PathEscape(filePath), params.Encode())
+ url := fmt.Sprintf("%s/repositories/%s/src/%s/%s?%s", p.APIURL(instanceURL), repositoryID, url.PathEscape(ref), url.PathEscape(filePath), params.Encode())
code, _, body, err := oauth.Get(
ctx,
p.client,
@@ -559,7 +559,7 @@ func (p *Provider) OverwriteFile(ctx context.Context, oauthCtx common.OauthConte
//
// Docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-source/#file-meta-data
func (p *Provider) ReadFileMeta(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID, filePath, ref string) (*vcs.FileMeta, error) {
- url := fmt.Sprintf("%s/repositories/%s/src/%s/%s?format=meta", p.APIURL(instanceURL), repositoryID, ref, url.PathEscape(filePath))
+ url := fmt.Sprintf("%s/repositories/%s/src/%s/%s?format=meta", p.APIURL(instanceURL), repositoryID, url.PathEscape(ref), url.PathEscape(filePath))
code, _, body, err := oauth.Get(
ctx,
p.client,
@@ -611,7 +611,7 @@ func (p *Provider) ReadFileMeta(ctx context.Context, oauthCtx common.OauthContex
//
// Docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-source/#raw-file-contents
func (p *Provider) ReadFileContent(ctx context.Context, oauthCtx common.OauthContext, instanceURL, repositoryID, filePath, ref string) (string, error) {
- url := fmt.Sprintf("%s/repositories/%s/src/%s/%s", p.APIURL(instanceURL), repositoryID, ref, url.PathEscape(filePath))
+ url := fmt.Sprintf("%s/repositories/%s/src/%s/%s", p.APIURL(instanceURL), repositoryID, url.PathEscape(ref), url.PathEscape(filePath))
code, _, body, err := oauth.Get(
ctx,
p.client,
@@ -896,56 +896,50 @@ func (*Provider) UpsertEnvironmentVariable(context.Context, common.OauthContext,
return errors.New("not supported")
}
+// Link is the API message for link.
+type Link struct {
+ Href string `json:"href"`
+}
+
+// Links is the API message for links.
+type Links struct {
+ Self Link `json:"self"`
+ HTML Link `json:"html"`
+}
+
+// Author is the API message for author.
+type Author struct {
+ Raw string `json:"raw"`
+ User User `json:"user"`
+}
+
// WebhookCommit is the API message for webhook commit.
type WebhookCommit struct {
- Hash string `json:"hash"`
- Date time.Time `json:"date"`
- Author struct {
- Raw string `json:"raw"`
- User struct {
- Nickname string `json:"nickname"`
- } `json:"user"`
- } `json:"author"`
- Message string `json:"message"`
- Links struct {
- HTML struct {
- Href string `json:"href"`
- } `json:"html"`
- } `json:"links"`
- Parents []struct {
- Hash string `json:"hash"`
- } `json:"parents"`
+ Hash string `json:"hash"`
+ Date time.Time `json:"date"`
+ Author Author `json:"author"`
+ Message string `json:"message"`
+ Links Links `json:"links"`
+ Parents []Target `json:"parents"`
+}
+
+// WebhookPushChange is the API message for webhook push change.
+type WebhookPushChange struct {
+ Old Branch `json:"old"`
+ New Branch `json:"new"`
+ Commits []WebhookCommit `json:"commits"`
+}
+
+// WebhookPush is the API message for webhook push.
+type WebhookPush struct {
+ Changes []WebhookPushChange `json:"changes"`
}
// WebhookPushEvent is the API message for webhook push event.
type WebhookPushEvent struct {
- Push struct {
- Changes []struct {
- Old struct {
- Target struct {
- Hash string `json:"hash"`
- } `json:"target"`
- } `json:"old"`
- New struct {
- Name string `json:"name"`
- Target struct {
- Hash string `json:"hash"`
- } `json:"target"`
- } `json:"new"`
- Commits []WebhookCommit `json:"commits"`
- } `json:"changes"`
- } `json:"push"`
- Repository struct {
- FullName string `json:"full_name"`
- Links struct {
- HTML struct {
- Href string `json:"href"`
- } `json:"html"`
- } `json:"links"`
- } `json:"repository"`
- Actor struct {
- Nickname string `json:"nickname"`
- } `json:"actor"`
+ Push WebhookPush `json:"push"`
+ Repository Repository `json:"repository"`
+ Actor User `json:"actor"`
}
// WebhookCreateOrUpdate represents a Bitbucket API request for creating or
@@ -957,6 +951,12 @@ type WebhookCreateOrUpdate struct {
Events []string `json:"events"`
}
+// Webhook represents a Bitbucket Cloud API response for the webhook
+// information.
+type Webhook struct {
+ UUID string `json:"uuid"`
+}
+
// CreateWebhook creates a webhook in the repository with given payload.
//
// Docs: https://developer.atlassian.com/cloud/bitbucket/rest/api-group-repositories/#api-repositories-workspace-repo-slug-hooks-post
@@ -996,9 +996,7 @@ func (p *Provider) CreateWebhook(ctx context.Context, oauthCtx common.OauthConte
)
}
- var resp struct {
- UUID string `json:"uuid"`
- }
+ var resp Webhook
if err = json.Unmarshal([]byte(body), &resp); err != nil {
return "", errors.Wrap(err, "unmarshal body")
}
diff --git a/backend/tests/fake/bitbucket.go b/backend/tests/fake/bitbucket.go
new file mode 100644
index 0000000000..fcf8f3db5c
--- /dev/null
+++ b/backend/tests/fake/bitbucket.go
@@ -0,0 +1,375 @@
+package fake
+
+import (
+ "bytes"
+ "encoding/json"
+ "fmt"
+ "io"
+ "net"
+ "net/http"
+ "net/url"
+ "strconv"
+ "strings"
+ "time"
+
+ "github.com/labstack/echo/v4"
+ "github.com/pkg/errors"
+
+ "github.com/bytebase/bytebase/backend/plugin/vcs"
+ "github.com/bytebase/bytebase/backend/plugin/vcs/bitbucket"
+)
+
+// Bitbucket is a fake implementation of Bitbucket VCS provider.
+type Bitbucket struct {
+ port int
+ echo *echo.Echo
+
+ client *http.Client
+
+ nextWebhookID int
+ repositories map[string]*bitbucketRepositoryData
+}
+
+type bitbucketRepositoryData struct {
+ webhooks []*bitbucket.WebhookCreateOrUpdate
+ // files is a map that the full file path is the key and the file content is the
+ // value.
+ files map[string]string
+ // refs is the map for repository branch. The map key is the branch ref, like
+ // "refs/heads/main".
+ refs map[string]*bitbucket.Branch
+ // diffStats is the map for commits compare. The map key has the format
+ // "to..from" which is SHA or branch name.
+ diffStats map[string][]*bitbucket.CommitDiffStat
+}
+
+// NewBitbucket creates a new fake implementation of Bitbucket VCS provider.
+func NewBitbucket(port int) VCSProvider {
+ e := newEchoServer()
+ bb := &Bitbucket{
+ port: port,
+ echo: e,
+ client: &http.Client{},
+ nextWebhookID: 20210113,
+ repositories: make(map[string]*bitbucketRepositoryData),
+ }
+
+ g := e.Group("/2.0")
+ g.POST("/repositories/:owner/:repo/hooks", bb.createRepositoryWebhook)
+ g.GET("/repositories/:owner/:repo/commit/:commitID", bb.getRepositoryCommit)
+ g.GET("/repositories/:owner/:repo/src/:ref/:filepath", bb.getRepositoryContent)
+ g.POST("/repositories/:owner/:repo/src", bb.createRepositoryFile)
+ g.GET("/repositories/:owner/:repo/refs/branches/:branchName", bb.getRepositoryBranch)
+ g.GET("/repositories/:owner/:repo/diffstat/:baseHead", bb.compareCommits)
+ return bb
+}
+
+func (bb *Bitbucket) createRepositoryWebhook(c echo.Context) error {
+ r, err := bb.validRepository(c)
+ if err != nil {
+ return err
+ }
+
+ body, err := io.ReadAll(c.Request().Body)
+ if err != nil {
+ return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to read request body for creating repository webhook: %v", err))
+ }
+
+ var webhookCreate bitbucket.WebhookCreateOrUpdate
+ if err = json.Unmarshal(body, &webhookCreate); err != nil {
+ return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to unmarshal request body for creating repository webhook: %v", err))
+ }
+ r.webhooks = append(r.webhooks, &webhookCreate)
+
+ buf, err := json.Marshal(bitbucket.Webhook{UUID: strconv.Itoa(bb.nextWebhookID)})
+ if err != nil {
+ return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to marshal response body for creating repository webhook: %v", err))
+ }
+ bb.nextWebhookID++
+ return c.String(http.StatusCreated, string(buf))
+}
+
+func (bb *Bitbucket) getRepositoryCommit(c echo.Context) error {
+ if _, err := bb.validRepository(c); err != nil {
+ return err
+ }
+
+ buf, err := json.Marshal(
+ bitbucket.Commit{
+ Hash: "fake_bitbucket_commit_sha",
+ Author: bitbucket.CommitAuthor{
+ User: bitbucket.User{
+ Nickname: "fake_bitbucket_author",
+ },
+ },
+ Date: time.Now(),
+ },
+ )
+ if err != nil {
+ return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to marshal response body for getting repository commit: %v", err))
+ }
+ return c.String(http.StatusOK, string(buf))
+}
+
+func (bb *Bitbucket) getRepositoryContent(c echo.Context) error {
+ r, err := bb.validRepository(c)
+ if err != nil {
+ return err
+ }
+
+ if strings.Contains(c.QueryString(), `type="commit_file"`) {
+ var treeEntries []*bitbucket.TreeEntry
+ for filePath := range r.files {
+ treeEntries = append(treeEntries,
+ &bitbucket.TreeEntry{
+ Type: "blob",
+ Path: filePath,
+ },
+ )
+ }
+ resp, err := json.Marshal(map[string]any{"values": treeEntries})
+ if err != nil {
+ return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to marshal response body for getting repository content: %v", err))
+ }
+ return c.String(http.StatusOK, string(resp))
+ }
+
+ filePathEscaped := c.Param("filepath")
+ filePath, err := url.QueryUnescape(filePathEscaped)
+ if err != nil {
+ return c.String(http.StatusBadRequest, fmt.Sprintf("failed to unescape file path %q: %v", filePathEscaped, err))
+ }
+
+ content, ok := r.files[filePath]
+ if !ok {
+ return c.String(http.StatusNotFound, fmt.Sprintf("file %q not found", filePath))
+ }
+
+ if strings.Contains(c.QueryString(), `format=meta`) {
+ resp, err := json.Marshal(
+ bitbucket.TreeEntry{
+ Type: "commit_file",
+ Path: filePath,
+ Size: int64(len(content)),
+ },
+ )
+ if err != nil {
+ return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to marshal response body for getting repository file meta: %v", err))
+ }
+ return c.String(http.StatusOK, string(resp))
+ }
+ return c.String(http.StatusOK, content)
+}
+
+func (bb *Bitbucket) createRepositoryFile(c echo.Context) error {
+ r, err := bb.validRepository(c)
+ if err != nil {
+ return err
+ }
+
+ params, err := c.FormParams()
+ if err != nil {
+ return c.String(http.StatusBadRequest, fmt.Sprintf("failed to get form params: %v", err))
+ }
+
+ for k, v := range params {
+ if k != "parents" && k != "branch" && k != "message" {
+ r.files[k] = v[0]
+ }
+ }
+ return nil
+}
+
+func (bb *Bitbucket) getRepositoryBranch(c echo.Context) error {
+ r, err := bb.validRepository(c)
+ if err != nil {
+ return err
+ }
+
+ branchName := c.Param("branchName")
+ if _, ok := r.refs[fmt.Sprintf("refs/heads/%s", branchName)]; !ok {
+ return c.String(http.StatusNotFound, fmt.Sprintf("branch not found: %v", branchName))
+ }
+
+ buf, err := json.Marshal(r.refs[fmt.Sprintf("refs/heads/%s", branchName)])
+ if err != nil {
+ return c.String(http.StatusInternalServerError, fmt.Sprintf("failed to marshal response body for getting repository branch: %v", err))
+ }
+ return c.String(http.StatusOK, string(buf))
+}
+
+func (bb *Bitbucket) compareCommits(c echo.Context) error {
+ r, err := bb.validRepository(c)
+ if err != nil {
+ return err
+ }
+
+ key := c.Param("baseHead")
+ diff, ok := r.diffStats[key]
+ if !ok {
+ return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Cannot find the diff key %s", key))
+ }
+
+ buf, err := json.Marshal(map[string]any{"values": diff})
+ if err != nil {
+ return echo.NewHTTPError(http.StatusInternalServerError, "Failed to marshal response body").SetInternal(err)
+ }
+ return c.String(http.StatusOK, string(buf))
+}
+
+func (bb *Bitbucket) validRepository(c echo.Context) (*bitbucketRepositoryData, error) {
+ repositoryID := fmt.Sprintf("%s/%s", c.Param("owner"), c.Param("repo"))
+ r, ok := bb.repositories[repositoryID]
+ if !ok {
+ return nil, c.String(http.StatusNotFound, fmt.Sprintf("Bitbucket repository %q does not exist", repositoryID))
+ }
+
+ return r, nil
+}
+
+// Run starts the Bitbucket VCS provider server.
+func (bb *Bitbucket) Run() error {
+ return bb.echo.Start(fmt.Sprintf(":%d", bb.port))
+}
+
+// Close shuts down the Bitbucket VCS provider server.
+func (bb *Bitbucket) Close() error {
+ return bb.echo.Close()
+}
+
+// ListenerAddr returns the Bitbucket VCS provider server listener address.
+func (bb *Bitbucket) ListenerAddr() net.Addr {
+ return bb.echo.ListenerAddr()
+}
+
+// APIURL returns the Bitbucket VCS provider API URL.
+func (*Bitbucket) APIURL(string) string {
+ return "https://api.bitbucket.org/2.0"
+}
+
+// CreateRepository creates a Bitbucket repository with given ID.
+func (bb *Bitbucket) CreateRepository(id string) {
+ bb.repositories[id] = &bitbucketRepositoryData{
+ files: make(map[string]string),
+ refs: map[string]*bitbucket.Branch{},
+ diffStats: map[string][]*bitbucket.CommitDiffStat{},
+ }
+}
+
+// CreateBranch creates a new branch with the given name.
+func (bb *Bitbucket) CreateBranch(id, branchName string) error {
+ pd, ok := bb.repositories[id]
+ if !ok {
+ return errors.Errorf("bitbucket project %q doesn't exist", id)
+ }
+
+ if _, ok := pd.refs[fmt.Sprintf("refs/heads/%s", branchName)]; ok {
+ return errors.Errorf("branch %q already exists", branchName)
+ }
+
+ pd.refs[fmt.Sprintf("refs/heads/%s", branchName)] = &bitbucket.Branch{
+ Name: branchName,
+ Target: bitbucket.Target{
+ Hash: "fake_bitbucket_commit_sha",
+ },
+ }
+ return nil
+}
+
+// AddCommitsDiff adds a commits diff.
+func (bb *Bitbucket) AddCommitsDiff(repositoryID, fromCommit, toCommit string, fileDiffList []vcs.FileDiff) error {
+ r, ok := bb.repositories[repositoryID]
+ if !ok {
+ return errors.Errorf("Bitbucket repository %s doesn't exist", repositoryID)
+ }
+ key := fmt.Sprintf("%s..%s", toCommit, fromCommit)
+ var diffStats []*bitbucket.CommitDiffStat
+ for _, fileDiff := range fileDiffList {
+ diffStat := &bitbucket.CommitDiffStat{
+ New: bitbucket.CommitFile{
+ Path: fileDiff.Path,
+ },
+ }
+ switch fileDiff.Type {
+ case vcs.FileDiffTypeAdded:
+ diffStat.Status = "added"
+ case vcs.FileDiffTypeModified:
+ diffStat.Status = "modified"
+ case vcs.FileDiffTypeRemoved:
+ diffStat.Status = "removed"
+ }
+ diffStats = append(diffStats, diffStat)
+ }
+ r.diffStats[key] = diffStats
+ return nil
+}
+
+// SendWebhookPush sends out a webhook for a push event for the Bitbucket
+// repository using given payload.
+func (bb *Bitbucket) SendWebhookPush(repositoryID string, payload []byte) error {
+ r, ok := bb.repositories[repositoryID]
+ if !ok {
+ return errors.Errorf("Bitbucket repository %q does not exist", repositoryID)
+ }
+
+ // Trigger all webhooks
+ for _, webhook := range r.webhooks {
+ req, err := http.NewRequest("POST", webhook.URL, bytes.NewReader(payload))
+ if err != nil {
+ return errors.Wrapf(err, "failed to create a new POST request to %q", webhook.URL)
+ }
+
+ req.Header.Set("X-Event-Key", "repo:push")
+
+ resp, err := bb.client.Do(req)
+ if err != nil {
+ return errors.Wrapf(err, "failed to send POST request to %q", webhook.URL)
+ }
+
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return errors.Wrap(err, "failed to read response body")
+ }
+ if resp.StatusCode != http.StatusOK {
+ return errors.Errorf("unexpected response status code %d, body: %s", resp.StatusCode, body)
+ }
+ bb.echo.Logger.Infof("SendWebhookPush response body %s\n", body)
+ }
+ return nil
+}
+
+// AddFiles adds given files to the Bitbucket repository.
+func (bb *Bitbucket) AddFiles(repositoryID string, files map[string]string) error {
+ r, ok := bb.repositories[repositoryID]
+ if !ok {
+ return errors.Errorf("Bitbucket repository %q does not exist", repositoryID)
+ }
+
+ // Save or overwrite files
+ for path, content := range files {
+ r.files[path] = content
+ }
+ return nil
+}
+
+// GetFiles returns files with given paths from the Bitbucket repository.
+func (bb *Bitbucket) GetFiles(repositoryID string, filePaths ...string) (map[string]string, error) {
+ r, ok := bb.repositories[repositoryID]
+ if !ok {
+ return nil, errors.Errorf("Bitbucket repository %q does not exist", repositoryID)
+ }
+
+ // Get files
+ files := make(map[string]string)
+ for _, path := range filePaths {
+ if content, ok := r.files[path]; ok {
+ files[path] = content
+ }
+ }
+ return files, nil
+}
+
+// AddPullRequest creates a new pull request and add changed files to it.
+func (*Bitbucket) AddPullRequest(string, int, []*vcs.PullRequestFile) error {
+ return errors.New("not implemented yet")
+}
diff --git a/backend/tests/fake/github.go b/backend/tests/fake/github.go
index 4d103e69fd..31917b3e0b 100644
--- a/backend/tests/fake/github.go
+++ b/backend/tests/fake/github.go
@@ -31,12 +31,12 @@ type GitHub struct {
client *http.Client
nextWebhookID int
- repositories map[string]*repositoryData
+ repositories map[string]*githubRepositoryData
}
const publicKeyName = "public-key"
-type repositoryData struct {
+type githubRepositoryData struct {
webhooks []*github.WebhookCreateOrUpdate
// files is a map that the full file path is the key and the file content is the
// value.
@@ -66,7 +66,7 @@ func NewGitHub(port int) VCSProvider {
echo: e,
client: &http.Client{},
nextWebhookID: 20210113,
- repositories: make(map[string]*repositoryData),
+ repositories: make(map[string]*githubRepositoryData),
}
g := e.Group("/api/v3")
@@ -408,7 +408,7 @@ func (gh *GitHub) compareCommits(c echo.Context) error {
return c.String(http.StatusOK, string(buf))
}
-func (gh *GitHub) validRepository(c echo.Context) (*repositoryData, error) {
+func (gh *GitHub) validRepository(c echo.Context) (*githubRepositoryData, error) {
repositoryID := fmt.Sprintf("%s/%s", c.Param("owner"), c.Param("repo"))
r, ok := gh.repositories[repositoryID]
if !ok {
@@ -440,7 +440,7 @@ func (*GitHub) APIURL(instanceURL string) string {
// CreateRepository creates a GitHub repository with given ID.
func (gh *GitHub) CreateRepository(id string) {
- gh.repositories[id] = &repositoryData{
+ gh.repositories[id] = &githubRepositoryData{
files: make(map[string]string),
secrets: map[string]*github.RepositorySecret{
publicKeyName: {
diff --git a/backend/tests/schema_update_test.go b/backend/tests/schema_update_test.go
index 267e23b455..42ba3ce0b4 100644
--- a/backend/tests/schema_update_test.go
+++ b/backend/tests/schema_update_test.go
@@ -26,6 +26,7 @@ import (
"github.com/bytebase/bytebase/backend/plugin/advisor"
"github.com/bytebase/bytebase/backend/plugin/db"
"github.com/bytebase/bytebase/backend/plugin/vcs"
+ "github.com/bytebase/bytebase/backend/plugin/vcs/bitbucket"
"github.com/bytebase/bytebase/backend/plugin/vcs/github"
"github.com/bytebase/bytebase/backend/plugin/vcs/gitlab"
"github.com/bytebase/bytebase/backend/resources/mysql"
@@ -349,6 +350,69 @@ func TestVCS(t *testing.T) {
}
},
},
+ {
+ name: "Bitbucket",
+ vcsProviderCreator: fake.NewBitbucket,
+ vcsType: vcs.Bitbucket,
+ externalID: "octocat/Hello-World",
+ repositoryFullPath: "octocat/Hello-World",
+ newWebhookPushEvent: func(added, _ [][]string, beforeSHA, afterSHA string) interface{} {
+ var commits []bitbucket.WebhookCommit
+ for range added {
+ commits = append(commits, bitbucket.WebhookCommit{
+ Hash: afterSHA,
+ Date: time.Now(),
+ Author: bitbucket.Author{
+ Raw: "fake_bitbucket_author <fake_bitbucket_author@localhost>",
+ User: bitbucket.User{
+ Nickname: "fake_bitbucket_author",
+ },
+ },
+ Message: "Fake Bitbucket commit message",
+ Links: bitbucket.Links{
+ HTML: bitbucket.Link{
+ Href: "https://bitbucket.org/octocat/Hello-World/commits/fake_github_commit_id",
+ },
+ },
+ Parents: []bitbucket.Target{
+ {Hash: beforeSHA},
+ },
+ })
+ }
+ return bitbucket.WebhookPushEvent{
+ Push: bitbucket.WebhookPush{
+ Changes: []bitbucket.WebhookPushChange{
+ {
+ Old: bitbucket.Branch{
+ Name: "feature/foo",
+ Target: bitbucket.Target{
+ Hash: beforeSHA,
+ },
+ },
+ New: bitbucket.Branch{
+ Name: "feature/foo",
+ Target: bitbucket.Target{
+ Hash: afterSHA,
+ },
+ },
+ Commits: commits,
+ },
+ },
+ },
+ Repository: bitbucket.Repository{
+ FullName: "octocat/Hello-World",
+ Links: bitbucket.Links{
+ HTML: bitbucket.Link{
+ Href: "https://bitbuket.org/octocat/Hello-World",
+ },
+ },
+ },
+ Actor: bitbucket.User{
+ Nickname: "fake_bitbucket_author",
+ },
+ }
+ },
+ },
}
for _, test := range tests {
// Fix the problem that closure in a for loop will always use the last element.