Coder Social home page Coder Social logo

golang-jenkins's Introduction

golang-jenkins

Join the chat at https://gitter.im/yosida95/golang-jenkins

About

This is a API client of Jenkins API written in Go.

Usage

import "github.com/yosida95/golang-jenkins"

Configure authentication and create an instance of the client:

auth := &gojenkins.Auth{
   Username: "[jenkins user name]",
   ApiToken: "[jenkins API token]",
}
jenkins := gojenkins.NewJenkins(auth, "[jenkins instance base url]")

Make calls against the desired resources:

job, err := jenkins.GetJob("[job name]")

License

golang-jenkins is licensed under the MIT LICENSE. See ./LICENSE.

golang-jenkins's People

Contributors

akashshinde avatar dennisdenuto avatar gitter-badger avatar icholy avatar jeanlucmongrain avatar joelnb avatar kpedersen avatar kucuny avatar msneeden avatar nearlyunique avatar ota42y avatar pcmind avatar stuartskelton avatar vetheslav avatar yosida95 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

golang-jenkins's Issues

Deserialize build parameters and values

Parameters and their values are retrieved via jenkins.GetJob() but the response body is not is not pulled into the Action struct slice.

I have left the Value as interface{} as it is sent over as a mixed type.

I did consider adding something to simplify the casting but it seems overkill.

Jenkins 2.x changes + CSRF

The 2.x API is a bit differen than the 1.x, also is an annoying thing with the Parameterized build vs. non parameterized build.

  1. hasParams function is deprecated because the parameterDefinitions are no longer in the Actions list in Jenkins 2.x
  2. Due to the csrf protection, all POST attempt is failing, so there should be a feature to get the actual crumb and provide the crumb during the post.
  3. Can't trigger a job inside of a folder like: /job/FolderA/job/JobB

hasParams

Not sure why we really need to detect if the build has parameters or not. There is an option to pass paramets to the Build function for example. If we pass parameters, the build is parameterized. If not, it's not parameterized. So this might be enough to check and don't need to parse the Job JSON for parameterDefiniton "somewhere". It can change time to time so better not to rely on it.

CSRF

Before every POST, the code should check the /crumbIssuer/api/json and get the current crumb and field name. Otherwise you'll get error like no valid crumb.... The CSRF should be always turned on due to security reasons.

The implementation PoC looks like below, pretty easy. If the CSRF is not enabled, you'll get a 404 on the /crumbIssuer, so actually it just will leave the Crumb/CrumbRequestField an empty string so easy to check/handle.

type CrumbIssuer struct {
        Crumb             string `json:"crumb"`
        CrumbRequestField string `json:"crumbRequestField"`
}
...

// getCrumb returns a csrf crumb for post.
func (jenkins *Jenkins) getCrumb() (CrumbIssuer, error) {
        payload := CrumbIssuer{}
        err := jenkins.get("/crumbIssuer", nil, &payload)

        return payload, err
}

Triggering jobs inside of a folder

Another issue is that the Build() function uses the job.Name and not the job.Url for the post, so it generates a wrong URL for a job. The job.Url would be a better approach, so relying on the job URL provided by jenkins itself.

    job := jenkins.GetJob('folderA/job/JobB')
    // this will successfully populate the the job.
    // job.Name will be JobB which will cause a 404 on this post.
    jenkins.post(fmt.Sprintf("/job/%s/buildWithParameters", job.Name), params, nil)

Can not detective parameter property of Pipeline Jenkins job

Hi there,
This should require an enhancement to jenkins.go.
When I request a pipeline jenkins job (with parameters), there's no any "actions" has ParameterDefinitions returned.
So when the process goes to below function, it always returns false.
// hasParams returns a boolean value indicating if the job is parameterized func hasParams(job Job) bool { for _, action := range job.Actions { if len(action.ParameterDefinitions) > 0 { return true } } return false }

I just found the difference of the way Jenkins API indicates parameter property of a job between freestyle project and pipeline:
with freestyle project,the parameter property is wrapped in "actions":
"actions": [ { "_class": "hudson.model.ParametersDefinitionProperty", "parameterDefinitions": [ { "_class": "hudson.model.ChoiceParameterDefinition", "defaultParameterValue": { "_class": "hudson.model.StringParameterValue", "value": "app" }, "description": "", "name": "app", "type": "ChoiceParameterDefinition", "choices": [ "print-app", "idm-app", "file-app" ] } ] }, {}, {}, {}, {}, {}, { "_class": "com.cloudbees.plugins.credentials.ViewCredentialsAction" } ],

with pipeline , the parameter property is wrapped in "property":
"property": [ { "_class": "hudson.model.ParametersDefinitionProperty", "parameterDefinitions": [ { "_class": "com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition", "defaultParameterValue": null, "description": "", "name": "Jar_to_Publish", "type": "PT_CHECKBOX" } ] } ],

Getting test result data from jenkins

Hi,
what if user wanted test result of last build,I think its better to add api's which will just return json with last successful run test cases,failed and skipped one.For sake of simplicity at this moment you can just consider maven-junit test report which provides there own rest-api to get numbers.I have did that in one of my go project.

404 and 500 errors from Jenkins are lost

When calling GetBuild(), the error returned can be this:

invalid character '<' looking for beginning of value

This is an error from the encoding/json package produced when trying to decode the response to /job/name/number. If the job doesn't exist, the 404 response from Jenkins unfortunately has a HTML body, not a JSON body.

I'd like to change the code so that this error is caught by checking the response body and replacing it with something like ErrJobNotFound, so that my calling code can handle this more cleanly.

This actually happens quite often, as I often try to submit a job then immediately get its status, and Jenkins sends a 404 when the job is still queued.

The code for this is trivial, but I thought I'd put this in as an issue to get any feedback before I put in a PR.

Requests can't be parallelized

At present, a Jenkins object has a single Requester, and context (headers, "api/json" suffix, etc.) is cached in the singleton Requester for the duration of a request/response. This means that any kind of data mining (getting the SCM data for each job, say) must be done one-at-a-time.

Response from jenkins.Build

When I call Build() my Job in jenkins runs but I never get the results of that build as it retuns as soon as jenkins has recieved the request. Can you tell me how I would get the status for that build (which I do not even get a build number from the Build() request? Thanks!

getting job status with jenkins server with the job in sub folder

i have jenkins server with multiple projects folders. and all the jobs are hosted inside this sub-folder.
now this api trigger the pipeline with job name as 'only job name' and url device with full path including this project folder. however it is not getting the build from jenkins with this way. if i include the path in job variable and leave url is base url. it can rigger and get build number but can not get its status of pass/fail

Allow custom http.Client

Specific scenario is to allow ignoring expired SSL certificates but this also allows for additional scenarios with custom timeout values.

	tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	client := &http.Client{
		Transport: tr,
		Timeout:   time.Duration(2 * time.Second),
	}
	jenkins.EnhanceClient(client)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.