Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple enhancements #6

Merged
merged 14 commits into from
Dec 3, 2018
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added flag to ignore folders
icereed committed Nov 27, 2018

Verified

This commit was signed with the committer’s verified signature.
agostbiro Agost Biro
commit 7d9707432b91d9bb6a7198aafc837de621ff2aa0
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ go get -u github.com/mlabouardy/butler
### Jobs Management

```
$ butler jobs export --server localhost:8080 --username admin --password admin
$ butler jobs export --server localhost:8080 --username admin --password admin --skip-folder
```

```
25 changes: 21 additions & 4 deletions jobs.go
Original file line number Diff line number Diff line change
@@ -27,8 +27,8 @@ func (job *Job) IsFolder() bool {
return strings.HasSuffix(job.Class, "Folder")
}

func ExportJobs(server string, username string, password string) error {
jobs, err := GetJobs(server, username, password)
func ExportJobs(server string, username string, password string, skipFolder bool) error {
jobs, err := GetJobs(server, username, password, skipFolder)
if err != nil {
return err
}
@@ -87,11 +87,13 @@ func ExportJob(job Job, username string, password string) error {
defer f.Close()

fmt.Fprintf(f, "%s", data)
fmt.Printf("Job %s is class %s", job.Name, job.Class)
if job.IsFolder() {
fmt.Printf("\tJob is a folder.\n")
}
return nil
}

func GetJobs(server string, username string, password string) ([]Job, error) {
func GetJobs(server string, username string, password string, skipFolder bool) ([]Job, error) {
url := fmt.Sprintf("%s/view/all/api/xml", server)

client := &http.Client{}
@@ -118,9 +120,24 @@ func GetJobs(server string, username string, password string) ([]Job, error) {

var view AllView
xml.Unmarshal(data, &view)

if skipFolder {
view.Jobs = filterOutFolders(view.Jobs)
}

return view.Jobs, nil
}

func filterOutFolders(unfiltered []Job) []Job {
filtered := make([]Job, 0)
for _, job := range unfiltered {
if !job.IsFolder() {
filtered = append(filtered, job)
}
}
return filtered
}

func GetCrumb(host string, username string, password string) ([]string, error) {
crumbUrl := `%s/crumbIssuer/api/xml?xpath=concat(//crumbRequestField,":",//crumb)`
url := fmt.Sprintf(crumbUrl, host)
48 changes: 47 additions & 1 deletion jobs_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package main

import "testing"
import (
"reflect"
"testing"
)

func TestJob_IsFolder(t *testing.T) {
tests := []struct {
@@ -19,3 +22,46 @@ func TestJob_IsFolder(t *testing.T) {
})
}
}

func Test_filterOutFolders(t *testing.T) {
tests := []struct {
name string
unfiltered []Job
want []Job
}{
{
"Filter out folder",
[]Job{
Job{Class: "com.cloudbees.hudson.plugins.folder.Folder"},
Job{Class: "org.jenkinsci.plugins.workflow.job.WorkflowJob"},
},
[]Job{
Job{Class: "org.jenkinsci.plugins.workflow.job.WorkflowJob"},
},
},
{
"Filter out many folders",
[]Job{
Job{Class: "com.cloudbees.hudson.plugins.folder.Folder"},
Job{Class: "com.cloudbees.hudson.plugins.folder.Folder"},
Job{Class: "com.cloudbees.hudson.plugins.folder.Folder"},
Job{Class: "com.cloudbees.hudson.plugins.folder.Folder"},
Job{Class: "com.cloudbees.hudson.plugins.folder.Folder"},
Job{Class: "com.cloudbees.hudson.plugins.folder.Folder"},
Job{Class: "com.cloudbees.hudson.plugins.folder.Folder"},
Job{Class: "com.cloudbees.hudson.plugins.folder.Folder"},
Job{Class: "org.jenkinsci.plugins.workflow.job.WorkflowJob"},
},
[]Job{
Job{Class: "org.jenkinsci.plugins.workflow.job.WorkflowJob"},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := filterOutFolders(tt.unfiltered); !reflect.DeepEqual(got, tt.want) {
t.Errorf("filterOutFolders() = %v, want %v", got, tt.want)
}
})
}
}
7 changes: 6 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -79,17 +79,22 @@ func main() {
Name: "password, p",
Usage: "Jenkins password",
},
cli.BoolFlag{
Name: "skip-folder, sf",
Usage: "Skip folder",
},
},
Action: func(c *cli.Context) error {
var server = getSanitizedUrl(c.String("server"))
var username = c.String("username")
var password = c.String("password")
var skipFolder = c.Bool("skip-folder")

if server == "" {
cli.ShowSubcommandHelp(c)
}

err := ExportJobs(server, username, password)
err := ExportJobs(server, username, password, skipFolder)
if err != nil {
return cli.NewExitError(err.Error(), 1)
}