mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-27 04:11:03 +00:00
Merge pull request #1488 from athieriot/archives
Add ability to hide Archived projects from Gitlab
This commit is contained in:
commit
998ddac8bc
6 changed files with 91 additions and 18 deletions
|
@ -33,6 +33,7 @@ This section lists all connection options used in the connection string format.
|
|||
* `open=false` allows users to self-register. Defaults to false for security reasons.
|
||||
* `orgs=drone&orgs=docker` restricts access to these GitLab organizations. **Optional**
|
||||
* `skip_verify=false` skip ca verification if self-signed certificate. Defaults to false for security reasons.
|
||||
* `hide_archives=false` hide projects archived in GitLab from the listing.
|
||||
* `clone_mode=token` a strategy for clone authorization, by default use repo token, but can be changed to `oauth` ( This is not secure, because your user token, with full access to your gitlab account will be written to .netrc, and it can be read by all who have access to project builds )
|
||||
|
||||
## Gitlab registration
|
||||
|
|
|
@ -15,12 +15,12 @@ const (
|
|||
)
|
||||
|
||||
// Get a list of all projects owned by the authenticated user.
|
||||
func (g *Client) AllProjects() ([]*Project, error) {
|
||||
func (g *Client) AllProjects(hide_archives bool) ([]*Project, error) {
|
||||
var per_page = 100
|
||||
var projects []*Project
|
||||
|
||||
for i := 1; true; i++ {
|
||||
contents, err := g.Projects(i, per_page)
|
||||
contents, err := g.Projects(i, per_page, hide_archives)
|
||||
if err != nil {
|
||||
return projects, err
|
||||
}
|
||||
|
@ -42,12 +42,17 @@ func (g *Client) AllProjects() ([]*Project, error) {
|
|||
}
|
||||
|
||||
// Get a list of projects owned by the authenticated user.
|
||||
func (c *Client) Projects(page int, per_page int) ([]*Project, error) {
|
||||
|
||||
url, opaque := c.ResourceUrl(projectsUrl, nil, QMap{
|
||||
func (c *Client) Projects(page int, per_page int, hide_archives bool) ([]*Project, error) {
|
||||
projectsOptions := QMap{
|
||||
"page": strconv.Itoa(page),
|
||||
"per_page": strconv.Itoa(per_page),
|
||||
})
|
||||
}
|
||||
|
||||
if hide_archives {
|
||||
projectsOptions["archived"] = "false"
|
||||
}
|
||||
|
||||
url, opaque := c.ResourceUrl(projectsUrl, nil, projectsOptions)
|
||||
|
||||
var projects []*Project
|
||||
|
||||
|
|
|
@ -31,6 +31,7 @@ type Gitlab struct {
|
|||
Open bool
|
||||
PrivateMode bool
|
||||
SkipVerify bool
|
||||
HideArchives bool
|
||||
Search bool
|
||||
}
|
||||
|
||||
|
@ -50,6 +51,7 @@ func Load(env envconfig.Env) *Gitlab {
|
|||
gitlab.Secret = params.Get("client_secret")
|
||||
gitlab.AllowedOrgs = params["orgs"]
|
||||
gitlab.SkipVerify, _ = strconv.ParseBool(params.Get("skip_verify"))
|
||||
gitlab.HideArchives, _ = strconv.ParseBool(params.Get("hide_archives"))
|
||||
gitlab.Open, _ = strconv.ParseBool(params.Get("open"))
|
||||
|
||||
switch params.Get("clone_mode") {
|
||||
|
@ -170,7 +172,7 @@ func (g *Gitlab) Repos(u *model.User) ([]*model.RepoLite, error) {
|
|||
|
||||
var repos = []*model.RepoLite{}
|
||||
|
||||
all, err := client.AllProjects()
|
||||
all, err := client.AllProjects(g.HideArchives)
|
||||
if err != nil {
|
||||
return repos, err
|
||||
}
|
||||
|
|
|
@ -32,6 +32,25 @@ func Test_Gitlab(t *testing.T) {
|
|||
|
||||
g := goblin.Goblin(t)
|
||||
g.Describe("Gitlab Plugin", func() {
|
||||
// Test projects method
|
||||
g.Describe("AllProjects", func() {
|
||||
g.It("Should return only non-archived projects is hidden", func() {
|
||||
gitlab.HideArchives = true
|
||||
_projects, err := gitlab.Repos(&user)
|
||||
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(len(_projects)).Equal(1)
|
||||
})
|
||||
|
||||
g.It("Should return all the projects", func() {
|
||||
gitlab.HideArchives = false
|
||||
_projects, err := gitlab.Repos(&user)
|
||||
|
||||
g.Assert(err == nil).IsTrue()
|
||||
g.Assert(len(_projects)).Equal(2)
|
||||
})
|
||||
})
|
||||
|
||||
// Test repository method
|
||||
g.Describe("Repo", func() {
|
||||
g.It("Should return valid repo", func() {
|
||||
|
|
43
remote/gitlab/testdata/projects.go
vendored
43
remote/gitlab/testdata/projects.go
vendored
|
@ -1,7 +1,7 @@
|
|||
package testdata
|
||||
|
||||
// sample repository list
|
||||
var projectsPayload = []byte(`
|
||||
var allProjectsPayload = []byte(`
|
||||
[
|
||||
{
|
||||
"id": 4,
|
||||
|
@ -73,6 +73,47 @@ var projectsPayload = []byte(`
|
|||
"path": "brightbox",
|
||||
"updated_at": "2013-09-30T13:46:02Z"
|
||||
},
|
||||
"archived": true
|
||||
}
|
||||
]
|
||||
`)
|
||||
|
||||
var notArchivedProjectsPayload = []byte(`
|
||||
[
|
||||
{
|
||||
"id": 4,
|
||||
"description": null,
|
||||
"default_branch": "master",
|
||||
"public": false,
|
||||
"visibility_level": 0,
|
||||
"ssh_url_to_repo": "git@example.com:diaspora/diaspora-client.git",
|
||||
"http_url_to_repo": "http://example.com/diaspora/diaspora-client.git",
|
||||
"web_url": "http://example.com/diaspora/diaspora-client",
|
||||
"owner": {
|
||||
"id": 3,
|
||||
"name": "Diaspora",
|
||||
"username": "some_user",
|
||||
"created_at": "2013-09-30T13: 46: 02Z"
|
||||
},
|
||||
"name": "Diaspora Client",
|
||||
"name_with_namespace": "Diaspora / Diaspora Client",
|
||||
"path": "diaspora-client",
|
||||
"path_with_namespace": "diaspora/diaspora-client",
|
||||
"issues_enabled": true,
|
||||
"merge_requests_enabled": true,
|
||||
"wiki_enabled": true,
|
||||
"snippets_enabled": false,
|
||||
"created_at": "2013-09-30T13: 46: 02Z",
|
||||
"last_activity_at": "2013-09-30T13: 46: 02Z",
|
||||
"namespace": {
|
||||
"created_at": "2013-09-30T13: 46: 02Z",
|
||||
"description": "",
|
||||
"id": 3,
|
||||
"name": "Diaspora",
|
||||
"owner_id": 1,
|
||||
"path": "diaspora",
|
||||
"updated_at": "2013-09-30T13: 46: 02Z"
|
||||
},
|
||||
"archived": false
|
||||
}
|
||||
]
|
||||
|
|
7
remote/gitlab/testdata/testdata.go
vendored
7
remote/gitlab/testdata/testdata.go
vendored
|
@ -16,7 +16,12 @@ func NewServer() *httptest.Server {
|
|||
// evaluate the path to serve a dummy data file
|
||||
switch r.URL.Path {
|
||||
case "/api/v3/projects":
|
||||
w.Write(projectsPayload)
|
||||
if r.URL.Query().Get("archived") == "false" {
|
||||
w.Write(notArchivedProjectsPayload)
|
||||
} else {
|
||||
w.Write(allProjectsPayload)
|
||||
}
|
||||
|
||||
return
|
||||
case "/api/v3/projects/diaspora/diaspora-client":
|
||||
w.Write(project4Paylod)
|
||||
|
|
Loading…
Reference in a new issue