From 7856de56b5cc7e8f2532abc1f6fca4b7f8b035ce Mon Sep 17 00:00:00 2001 From: Massaki Archambault Date: Thu, 18 Jul 2024 00:25:13 -0400 Subject: [PATCH] hide archived project by default prefix archived project name with a "." by default so they appear hidden on the filesystem. At the same time, added the configuration gitlab.archived_project_handling to set how archived projects are handled --- config.example.yaml | 7 ++++ config/loader.go | 18 ++++++---- config/loader_test.go | 66 +++++++++++++++++++++++-------------- platforms/gitlab/client.go | 17 ++++++---- platforms/gitlab/group.go | 4 ++- platforms/gitlab/project.go | 16 ++++++--- platforms/gitlab/user.go | 4 ++- 7 files changed, 90 insertions(+), 42 deletions(-) diff --git a/config.example.yaml b/config.example.yaml index 2634bd2..f80d607 100644 --- a/config.example.yaml +++ b/config.example.yaml @@ -27,6 +27,13 @@ gitlab: # A list of the user ids to expose their personal projects in the filesystem. user_ids: [] + # Set how archived projects are handled. + # If set to "show", it will add them to the filesystem and treat them like any other repository + # If set to "hide", it will add them to the filesystem, but prefix the symlink with a "." + # If set to "ignore", it will make them absent from the filesystem + # Default to "hide" + archived_project_handling: hide + # If set to true, the user the api token belongs to will automatically be added to the list of users exposed by the filesystem. include_current_user: true diff --git a/config/loader.go b/config/loader.go index d9002e9..b0d5b5d 100644 --- a/config/loader.go +++ b/config/loader.go @@ -36,12 +36,13 @@ func LoadConfig(configPath string) (*Config, error) { MountOptions: "nodev,nosuid", }, Gitlab: gitlab.GitlabClientConfig{ - URL: "https://gitlab.com", - Token: "", - PullMethod: "http", - GroupIDs: []int{9970}, - UserIDs: []int{}, - IncludeCurrentUser: true, + URL: "https://gitlab.com", + Token: "", + PullMethod: "http", + GroupIDs: []int{9970}, + UserIDs: []int{}, + ArchivedProjectHandling: "hide", + IncludeCurrentUser: true, }, Git: git.GitClientParam{ CloneLocation: defaultCloneLocation, @@ -85,5 +86,10 @@ func MakeGitlabConfig(config *Config) (*gitlab.GitlabClientConfig, error) { return nil, fmt.Errorf("pull_method must be either \"%v\" or \"%v\"", gitlab.PullMethodHTTP, gitlab.PullMethodSSH) } + // parse archive_handing + if config.Gitlab.ArchivedProjectHandling != gitlab.ArchivedProjectShow && config.Gitlab.ArchivedProjectHandling != gitlab.ArchivedProjectHide && config.Gitlab.ArchivedProjectHandling != gitlab.ArchivedProjectIgnore { + return nil, fmt.Errorf("pull_method must be either \"%v\", \"%v\" or \"%v\"", gitlab.ArchivedProjectShow, gitlab.ArchivedProjectHide, gitlab.ArchivedProjectIgnore) + } + return &config.Gitlab, nil } diff --git a/config/loader_test.go b/config/loader_test.go index 49e259e..0d6bf95 100644 --- a/config/loader_test.go +++ b/config/loader_test.go @@ -22,12 +22,13 @@ func TestLoadConfig(t *testing.T) { MountOptions: "nodev", }, Gitlab: gitlab.GitlabClientConfig{ - URL: "https://example.com", - Token: "12345", - PullMethod: "ssh", - GroupIDs: []int{123}, - UserIDs: []int{456}, - IncludeCurrentUser: true, + URL: "https://example.com", + Token: "12345", + PullMethod: "ssh", + GroupIDs: []int{123}, + UserIDs: []int{456}, + ArchivedProjectHandling: "hide", + IncludeCurrentUser: true, }, Git: git.GitClientParam{ CloneLocation: "/tmp/gitlabfs/test/clone", @@ -118,32 +119,49 @@ func TestMakeGitlabConfig(t *testing.T) { "ValidConfig": { input: &config.Config{ Gitlab: gitlab.GitlabClientConfig{ - URL: "https://gitlab.com", - Token: "", - GroupIDs: []int{9970}, - UserIDs: []int{}, - IncludeCurrentUser: true, - PullMethod: "http", + URL: "https://gitlab.com", + PullMethod: "http", + Token: "", + GroupIDs: []int{9970}, + UserIDs: []int{}, + ArchivedProjectHandling: "hide", + IncludeCurrentUser: true, }, }, expected: &gitlab.GitlabClientConfig{ - URL: "https://gitlab.com", - Token: "", - GroupIDs: []int{9970}, - UserIDs: []int{}, - IncludeCurrentUser: true, - PullMethod: "http", + URL: "https://gitlab.com", + PullMethod: "http", + Token: "", + GroupIDs: []int{9970}, + UserIDs: []int{}, + ArchivedProjectHandling: "hide", + IncludeCurrentUser: true, }, }, "InvalidPullMethod": { input: &config.Config{ Gitlab: gitlab.GitlabClientConfig{ - URL: "https://gitlab.com", - Token: "", - GroupIDs: []int{9970}, - UserIDs: []int{}, - IncludeCurrentUser: true, - PullMethod: "invalid", + URL: "https://gitlab.com", + PullMethod: "invalid", + Token: "", + GroupIDs: []int{9970}, + UserIDs: []int{}, + ArchivedProjectHandling: "hide", + IncludeCurrentUser: true, + }, + }, + expected: nil, + }, + "InvalidArchiveHandling": { + input: &config.Config{ + Gitlab: gitlab.GitlabClientConfig{ + URL: "https://gitlab.com", + PullMethod: "http", + Token: "", + GroupIDs: []int{9970}, + UserIDs: []int{}, + IncludeCurrentUser: true, + ArchivedProjectHandling: "invalid", }, }, expected: nil, diff --git a/platforms/gitlab/client.go b/platforms/gitlab/client.go index fc885c5..d7d99f0 100644 --- a/platforms/gitlab/client.go +++ b/platforms/gitlab/client.go @@ -13,15 +13,20 @@ import ( const ( PullMethodHTTP = "http" PullMethodSSH = "ssh" + + ArchivedProjectShow = "show" + ArchivedProjectHide = "hide" + ArchivedProjectIgnore = "ignore" ) type GitlabClientConfig struct { - URL string `yaml:"url,omitempty"` - Token string `yaml:"token,omitempty"` - GroupIDs []int `yaml:"group_ids,omitempty"` - UserIDs []int `yaml:"user_ids,omitempty"` - IncludeCurrentUser bool `yaml:"include_current_user,omitempty"` - PullMethod string `yaml:"pull_method,omitempty"` + URL string `yaml:"url,omitempty"` + Token string `yaml:"token,omitempty"` + GroupIDs []int `yaml:"group_ids,omitempty"` + UserIDs []int `yaml:"user_ids,omitempty"` + ArchivedProjectHandling string `yaml:"archived_project_handling,omitempty"` + IncludeCurrentUser bool `yaml:"include_current_user,omitempty"` + PullMethod string `yaml:"pull_method,omitempty"` } type gitlabClient struct { diff --git a/platforms/gitlab/group.go b/platforms/gitlab/group.go index 417f904..37d5507 100644 --- a/platforms/gitlab/group.go +++ b/platforms/gitlab/group.go @@ -163,7 +163,9 @@ func (c *gitlabClient) fetchGroupContent(group *Group) (map[string]fstree.GroupS } for _, gitlabProject := range gitlabProjects { project := c.newProjectFromGitlabProject(gitlabProject) - childProjects[project.Name] = &project + if project != nil { + childProjects[project.Path] = project + } } if response.CurrentPage >= response.TotalPages { break diff --git a/platforms/gitlab/project.go b/platforms/gitlab/project.go index 8a5416c..af366a2 100644 --- a/platforms/gitlab/project.go +++ b/platforms/gitlab/project.go @@ -1,12 +1,14 @@ package gitlab import ( + "path" + "github.com/xanzy/go-gitlab" ) type Project struct { ID int - Name string + Path string CloneURL string DefaultBranch string } @@ -23,11 +25,14 @@ func (p *Project) GetDefaultBranch() string { return p.DefaultBranch } -func (c *gitlabClient) newProjectFromGitlabProject(project *gitlab.Project) Project { +func (c *gitlabClient) newProjectFromGitlabProject(project *gitlab.Project) *Project { // https://godoc.org/github.com/xanzy/go-gitlab#Project + if c.ArchivedProjectHandling == ArchivedProjectIgnore && project.Archived { + return nil + } p := Project{ ID: project.ID, - Name: project.Path, + Path: project.Path, DefaultBranch: project.DefaultBranch, } if p.DefaultBranch == "" { @@ -38,5 +43,8 @@ func (c *gitlabClient) newProjectFromGitlabProject(project *gitlab.Project) Proj } else { p.CloneURL = project.HTTPURLToRepo } - return p + if c.ArchivedProjectHandling == ArchivedProjectHide && project.Archived { + p.Path = path.Join(path.Dir(p.Path), "."+path.Base(p.Path)) + } + return &p } diff --git a/platforms/gitlab/user.go b/platforms/gitlab/user.go index 75acd9d..3ce4d9d 100644 --- a/platforms/gitlab/user.go +++ b/platforms/gitlab/user.go @@ -105,7 +105,9 @@ func (c *gitlabClient) fetchUserContent(user *User) (map[string]fstree.GroupSour } for _, gitlabProject := range gitlabProjects { project := c.newProjectFromGitlabProject(gitlabProject) - childProjects[project.Name] = &project + if project != nil { + childProjects[project.Path] = project + } } if response.CurrentPage >= response.TotalPages { break