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

Jenkins.Init() should check Response.StatusCode #210

Closed
Bpazy opened this issue Jul 8, 2020 · 5 comments
Closed

Jenkins.Init() should check Response.StatusCode #210

Bpazy opened this issue Jul 8, 2020 · 5 comments

Comments

@Bpazy
Copy link
Contributor

Bpazy commented Jul 8, 2020

Jenkins.Init() return success even if invoking gojenkins.CraeteJenkins with wrong auth.

Here is the example code:

jenkins, err := gojenkins.CreateJenkins(nil, "http://somejenkins.org/", "username", "some wrong token").Init()
if err != nil {
        // Never invoked but shoud be.
	fmt.Printf("%+v", err)
	return
}

So Jenkins.Init() should check Response.StatusCode:

gojenkins/jenkins.go

Lines 51 to 67 in 65ee8c9

func (j *Jenkins) Init() (*Jenkins, error) {
j.initLoggers()
// Check Connection
j.Raw = new(ExecutorResponse)
rsp, err := j.Requester.GetJSON("/", j.Raw, nil)
if err != nil {
return nil, err
}
j.Version = rsp.Header.Get("X-Jenkins")
if j.Raw == nil {
return nil, errors.New("Connection Failed, Please verify that the host and credentials are correct.")
}
return j, nil
}

The code should be written like this:

func (j *Jenkins) Init() (*Jenkins, error) {
	j.initLoggers()

	// Check Connection
	j.Raw = new(ExecutorResponse)
	rsp, err := j.Requester.GetJSON("/", j.Raw, nil)
	if err != nil {
		return nil, err
	}

	j.Version = rsp.Header.Get("X-Jenkins")
	if j.Raw == nil || rsp.StatusCode != http.StatusOK { // Check rsp.StatusCode
		return nil, errors.New("Connection Failed, Please verify that the host and credentials are correct.")
	}

	return j, nil
}

Jenkins version: 2.176.1

Bpazy added a commit to Bpazy/gojenkins that referenced this issue Jul 8, 2020
@eryajf
Copy link

eryajf commented Feb 2, 2021

I have the same problem

@Bpazy
Copy link
Contributor Author

Bpazy commented Feb 2, 2021

@eryajf Maybe you can try my fork: https://github.com/Bpazy/gojenkins

PR: #214

figo added a commit that referenced this issue Feb 26, 2021
Jenkins.Init() should check Response.StatusCode #210
@figo
Copy link
Collaborator

figo commented Feb 26, 2021

#214 merged, try the latest code please @eryajf

@eryajf
Copy link

eryajf commented Feb 28, 2021

#214 merged, try the latest code please @eryajf

Thank you, it has been successful, but I have used it locally, I hope the official package can have more methods for folder operations

@vperson
Copy link

vperson commented Mar 28, 2021

I think you should put the status code of the request in the error message
For example like this

	j.Version = rsp.Header.Get("X-Jenkins")
	if j.Raw == nil || rsp.StatusCode != http.StatusOK {
		return nil, errors.New(fmt.Sprintf("Connection Failed, Please verify that the host and credentials are correct, Status Code %d", rsp.StatusCode))
	}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants