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

fix job import of project with whitespace #13

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
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
17 changes: 14 additions & 3 deletions jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import (
"encoding/xml"
"errors"
"fmt"
"strconv"
"io/ioutil"
"net/http"
"os"
"strings"
"net/url"
)

type JobList struct {
Expand Down Expand Up @@ -296,9 +298,11 @@ func ImportJob(name string, folderName string, server string, username string, p
return err
}

url := fmt.Sprintf("%s/createItem?name=%s", GetFolderURL(server, folderName), name)
// escape is mandatory if the job has special char in the name
name = url.PathEscape(name)
createURL := fmt.Sprintf("%s/createItem?name=%s", GetFolderURL(server, folderName), name)
client := &http.Client{}
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
req, err := http.NewRequest("POST", createURL, bytes.NewBuffer(jsonStr))
if err != nil {
return err
}
Expand All @@ -324,7 +328,14 @@ func ImportJob(name string, folderName string, server string, username string, p
}

if resp.StatusCode != 200 {
return errors.New("Job couldn't not be imported: check if it is already existing and verify all plugins used in this job are installed on the target jenkins instance")
errorMsg := fmt.Sprintf(`Job couldn't not be imported (code %s).
Check if it is already existing and verify all plugins used in this job are installed on the target jenkins instance`,
strconv.Itoa(resp.StatusCode))
b, errRead := ioutil.ReadAll(resp.Body)
if errRead == nil {
errorMsg = errorMsg + fmt.Sprintf("%s", b)
}
return errors.New(errorMsg)
}

return nil
Expand Down