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

Add a new function which creates a job in a folder #610

Merged
merged 2 commits into from
Aug 15, 2021
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
37 changes: 37 additions & 0 deletions client/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,43 @@ func (q *JobClient) Create(jobPayload CreateJobPayload) (err error) {
return
}

// CreateJobInFolder creates a job in a specific folder and create folder first if the folder does not exist
func (q *JobClient) CreateJobInFolder(jobPayload CreateJobPayload, path string) (err error) {
// check if folder exists
_, err = q.GetJob(path)
// create folder if not exist
if err != nil {
createFolderPayload := CreateJobPayload{
Name: path,
Mode: "com.cloudbees.hudson.plugins.folder.Folder",
From: jobPayload.From,
}
err = q.Create(createFolderPayload)
if err != nil {
return
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer don't check and create the folder here. Users can check if the folder exists before calling this function.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function is quite similar to https://github.com/jenkins-zh/jenkins-cli/pull/610/files#diff-e829a497145e43bd7519ccab0bc1d6d68e11dc969faeec623b564216d3b1d13eR358.

Please consider reusing it. Such as, you can refactor the function Create to:

func (q *JobClient) Create(jobPayload CreateJobPayload) (err error) {
return CreateJobInFolder(jobPayload, "/")
}


// create a job in path
playLoadData, _ := json.Marshal(jobPayload)
formData := url.Values{
"json": {string(playLoadData)},
"name": {jobPayload.Name},
"mode": {jobPayload.Mode},
"from": {jobPayload.From},
}
payload := strings.NewReader(formData.Encode())
path = ParseJobPath(path)
api := fmt.Sprintf("/view/all%s/createItem", path)
var code int
code, err = q.RequestWithoutData(http.MethodPost, api,
map[string]string{httpdownloader.ContentType: httpdownloader.ApplicationForm}, payload, 200)
if code == 302 {
err = nil
}
return
}

// Delete will delete a job by name
func (q *JobClient) Delete(jobName string) (err error) {
var (
Expand Down