jenkins-zh/jenkins-cli

job search not worked

Closed this issue · 9 comments

Describe the bug
Err occured while I used jcli job search myjob, and I tried to find the err reason,
I found that JENKINS_URL/items/list is not exists and JENKINS_URL/api/json/items/list is ok,
but while I changed the jenkins_servers.0.url=JENKINS_URL/api/json in config file,
it will unmarshal failed.

To Reproduce
Run jcli job search myjob and appeared Error: not found resources.

Expected behavior
Show some jobs I searched.

Desktop (please complete the following information):
Independent

Jenkins version

  • 2.222.1
  • 2.235.5
  • 2.263.4

This command relies on the plugin pipeline-restful-api. Install it is very simple: jcli plugin install pipeline-restful-api.

You can get the tips from the command jcli job search --doctor.

This command relies on the plugin pipeline-restful-api. Install it is very simple: jcli plugin install pipeline-restful-api.

You can get the tips from the command jcli job search --doctor.

As you said, I tried to install the plugin pipeline-restful-api, but it still not work.
And I have built the jcli binary file via source code, as the same time, print some logs,
I found that jcli will GET the url JENKINS_URL/items/list?name=myjob&type=&start=0&limit=50&parent= to search jobs as before,
I log the request url as follow code.

// Search find a set of jobs by name
func (q *JobClient) Search(name, kind string, start, limit int) (items []JenkinsItem, err error) {
	log.Println(name)
	err = q.RequestWithData(http.MethodGet, fmt.Sprintf("/items/list?name=%s&type=%s&start=%d&limit=%d&parent=%s",
		name, kind, start, limit, q.Parent),
		nil, nil, 200, &items)
	return
}

// RequestWithData requests the api and parse the data into an interface
func (j *JenkinsCore) RequestWithData(method, api string, headers map[string]string,
	payload io.Reader, successCode int, obj interface{}) (err error) {
	var (
		statusCode int
		data       []byte
	)

	if statusCode, data, err = j.Request(method, api, headers, payload); err == nil {
		log.Println(statusCode)
		if statusCode == successCode {
			err = json.Unmarshal(data, obj)
		} else {
			err = j.ErrorHandle(statusCode, data)
		}
	}
	return
}

// Request make a common request
func (j *JenkinsCore) Request(method, api string, headers map[string]string, payload io.Reader) (
	statusCode int, data []byte, err error) {
	var (
		req        *http.Request
		response   *http.Response
		requestURL string
	)

	log.Printf("%s%s", j.URL, api)
	if requestURL, err = util.URLJoinAsString(j.URL, api); err != nil {
		err = fmt.Errorf("cannot parse the URL of Jenkins, error is %v", err)
		return
	}

	logger.Debug("send HTTP request", zap.String("URL", requestURL), zap.String("method", method))
	if req, err = http.NewRequest(method, requestURL, payload); err != nil {
		return
	}
	if language != "" {
		req.Header.Set("Accept-Language", language)
	}
	if err = j.AuthHandle(req); err != nil {
		return
	}

	for k, v := range headers {
		req.Header.Add(k, v)
	}

	if curlCmd, curlErr := http2curl.GetCurlCommand(req); curlErr == nil {
		logger.Debug("HTTP request as curl", zap.String("cmd", curlCmd.String()))
	}

	client := j.GetClient()
	if response, err = client.Do(req); err == nil {
		statusCode = response.StatusCode
		data, err = ioutil.ReadAll(response.Body)
	}
	return
}

It works from my side:

➜  ~ jcli job search a
Name               DisplayName        Type        URL
master             master             WorkflowJob job/testfkznq/job/111/job/master/
a%2Fb              a/b                WorkflowJob job/testfkznq/job/git1/job/a%252Fb/
➜  ~ jcli version
Version: dev-v0.0.35
Last Commit: c19bc64
Build Date: 2021-03-12
➜  ~ jcli plugin list | grep restful
pipeline-restful-api               0.9                                   true

It works from my side:

➜  ~ jcli job search a
Name               DisplayName        Type        URL
master             master             WorkflowJob job/testfkznq/job/111/job/master/
a%2Fb              a/b                WorkflowJob job/testfkznq/job/git1/job/a%252Fb/
➜  ~ jcli version
Version: dev-v0.0.35
Last Commit: c19bc64
Build Date: 2021-03-12
➜  ~ jcli plugin list | grep restful
pipeline-restful-api               0.9                                   true

I found that there is pipeline-rest-api plugin instead of pipeline-restful-api plugin, but I have installed it by running jcli plugin install pipeline-restful-api and no any output.

➜ jcli plugin list | grep pipeline-rest
pipeline-rest-api                  2.12      true

They're different.

solved, it worked after I restart the jenkins server, because the installed plugin requires restart to apply, but it's really not friendly~

Happy to helped you.

Error: Post "http://pluginManager/updateCenter/": dial tcp: lookup pluginManager on 100.100.2.138:53: no such host
Usage:
jcli plugin install [flags]

Error: Post "http://pluginManager/updateCenter/": dial tcp: lookup pluginManager on 100.100.2.138:53: no such host Usage: jcli plugin install [flags]

See alos #737 (comment)