Skip to content

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

feat: add fetch repo node list method (#954)

STEVENf81bec4eb05c
diff --git a/plugin/vcs/gitlab/gitlab.go b/plugin/vcs/gitlab/gitlab.go
index 2eccf579c1..489068c5fc 100644
--- a/plugin/vcs/gitlab/gitlab.go
+++ b/plugin/vcs/gitlab/gitlab.go
@@ -324,6 +324,45 @@ func (provider *Provider) FetchRepositoryActiveMemberList(ctx context.Context, o
return activeRepositoryMemberList, nil
}
+// FetchRepositoryFileList fetch the files from repository tree
+func (provider *Provider) FetchRepositoryFileList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, repositoryID string, ref string, filePath string) ([]*vcs.RepositoryTreeNode, error) {
+ code, body, err := httpGet(
+ instanceURL,
+ fmt.Sprintf("projects/%s/repository/tree?recursive=true&ref=%s&path=%s", repositoryID, ref, filePath),
+ &oauthCtx.AccessToken,
+ oauthContext{
+ ClientID: oauthCtx.ClientID,
+ ClientSecret: oauthCtx.ClientSecret,
+ RefreshToken: oauthCtx.RefreshToken,
+ },
+ oauthCtx.Refresher,
+ )
+ if err != nil {
+ return nil, fmt.Errorf("failed to fetch repository tree on GitLab instance %s, err: %w", instanceURL, err)
+ }
+ if code >= 300 {
+ return nil, fmt.Errorf("failed to fetch repository tree on GitLab instance %s, status code: %d",
+ instanceURL,
+ code,
+ )
+ }
+
+ var nodeList []*vcs.RepositoryTreeNode
+ if err := json.Unmarshal([]byte(body), &nodeList); err != nil {
+ return nil, fmt.Errorf("failed to unmarshal repository tree from GitLab instance %s, err: %w", instanceURL, err)
+ }
+
+ // Filter out folder nodes and we only need the file nodes.
+ var fileList []*vcs.RepositoryTreeNode
+ for _, node := range nodeList {
+ if node.Type == "blob" {
+ fileList = append(fileList, node)
+ }
+ }
+
+ return fileList, nil
+}
+
// CreateFile creates a file.
func (provider *Provider) CreateFile(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, repositoryID string, filePath string, fileCommitCreate vcs.FileCommitCreate) error {
body, err := json.Marshal(FileCommit{
diff --git a/plugin/vcs/vcs.go b/plugin/vcs/vcs.go
index 2de537a1bc..cac6bf788d 100644
--- a/plugin/vcs/vcs.go
+++ b/plugin/vcs/vcs.go
@@ -52,6 +52,12 @@ type FileMeta struct {
LastCommitID string
}
+// RepositoryTreeNode records the node(file/folder) of a repository tree from `git ls-tree`.
+type RepositoryTreeNode struct {
+ Path string `json:"path"`
+ Type string `json:"type"`
+}
+
// PushEvent is the API message for a VCS push event.
type PushEvent struct {
VCSType Type `json:"vcsType"`
@@ -96,27 +102,31 @@ type RepositoryMember struct {
type Provider interface {
// Returns the API URL for a given VCS instance URL
APIURL(instanceURL string) string
-
// Try to use this provider as an auth provider and fetch the user info from the OAuth context
//
// oauthCtx: OAuth context to write the file content
// instanceURL: VCS instance URL
TryLogin(ctx context.Context, oauthCtx common.OauthContext, instanceURL string) (*UserInfo, error)
-
// Fetch the user info of the given userID
//
// oauthCtx: OAuth context to write the file content
// instanceURL: VCS instance URL
// userID: the ID of the desired user
FetchUserInfo(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, userID int) (*UserInfo, error)
-
// Fetch all active members of a given repository
//
// oauthCtx: OAuth context to write the file content
// instanceURL: VCS instance URL
// repositoryID: the repository ID from the external VCS system (note this is NOT the ID of Bytebase's own repository resource)
FetchRepositoryActiveMemberList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, repositoryID string) ([]*RepositoryMember, error)
-
+ // Fetch the repository file list
+ //
+ // oauthCtx: OAuth context to read the repository tree
+ // instanceURL: VCS instance URL
+ // repositoryID: the repository ID from the external VCS system (note this is NOT the ID of Bytebase's own repository resource)
+ // ref: the unique name of a repository tree, could be a branch name in GitLab or a tree sha in GitHub
+ // filePath: the path inside repository, used to get content of subdirectories
+ FetchRepositoryFileList(ctx context.Context, oauthCtx common.OauthContext, instanceURL string, repositoryID string, ref string, filePath string) ([]*RepositoryTreeNode, error)
// Commits a new file
//
// oauthCtx: OAuth context to write the file content