You've already forked golang-jenkins
mirror of
https://github.com/newrelic-forks/golang-jenkins.git
synced 2026-06-18 20:15:04 +02:00
Update all necessary methods to support contexts
This commit is contained in:
+43
-15
@@ -122,12 +122,12 @@ func (jenkins *Jenkins) parseResponse(resp *http.Response, body interface{}) (er
|
||||
return json.Unmarshal(data, body)
|
||||
}
|
||||
|
||||
// get performs an HTTP GET request and returns the parsed response.
|
||||
// Performs an HTTP GET request and returns the parsed response.
|
||||
func (jenkins *Jenkins) get(path string, params url.Values, body interface{}) (err error) {
|
||||
return jenkins.getWithContext(context.Background(), path, params, body)
|
||||
}
|
||||
|
||||
// getWithContext uses the provided context to perform an HTTP GET request and returns the parsed response.
|
||||
// Performs an HTTP GET request and returns the parsed response using the provided context.
|
||||
func (jenkins *Jenkins) getWithContext(ctx context.Context, path string, params url.Values, body interface{}) (err error) {
|
||||
requestUrl := jenkins.buildUrl(path, params)
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", requestUrl, nil)
|
||||
@@ -156,7 +156,13 @@ func (jenkins *Jenkins) getXml(path string, params url.Values, body interface{})
|
||||
return jenkins.parseXmlResponse(resp, body)
|
||||
}
|
||||
|
||||
// Performs a POST to Jenkins.
|
||||
func (jenkins *Jenkins) post(path string, params url.Values, body interface{}) (err error) {
|
||||
return jenkins.postWithContext(context.Background(), path, params, body)
|
||||
}
|
||||
|
||||
// Performs a POST to Jenkins using the provided context.
|
||||
func (jenkins *Jenkins) postWithContext(ctx context.Context, path string, params url.Values, body interface{}) (err error) {
|
||||
requestUrl := jenkins.buildUrl(path, nil)
|
||||
|
||||
var bodyReader io.Reader
|
||||
@@ -164,7 +170,7 @@ func (jenkins *Jenkins) post(path string, params url.Values, body interface{}) (
|
||||
bodyReader = strings.NewReader(params.Encode())
|
||||
}
|
||||
|
||||
req, err := http.NewRequest("POST", requestUrl, bodyReader)
|
||||
req, err := http.NewRequestWithContext(ctx, "POST", requestUrl, bodyReader)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -187,6 +193,7 @@ func (jenkins *Jenkins) post(path string, params url.Values, body interface{}) (
|
||||
|
||||
return jenkins.parseResponse(resp, body)
|
||||
}
|
||||
|
||||
func (jenkins *Jenkins) postXml(path string, params url.Values, xmlBody io.Reader, body interface{}) (err error) {
|
||||
requestUrl := jenkins.baseUrl + path
|
||||
if params != nil {
|
||||
@@ -217,22 +224,27 @@ func (jenkins *Jenkins) postXml(path string, params url.Values, xmlBody io.Reade
|
||||
return jenkins.parseXmlResponse(resp, body)
|
||||
}
|
||||
|
||||
// GetJobs returns all jobs you can read.
|
||||
// Returns all jobs you can read.
|
||||
func (jenkins *Jenkins) GetJobs() ([]Job, error) {
|
||||
return jenkins.GetJobsWithContext(context.Background())
|
||||
}
|
||||
|
||||
// Returns all jobs you can read using the provided context.
|
||||
func (jenkins *Jenkins) GetJobsWithContext(ctx context.Context) ([]Job, error) {
|
||||
var payload = struct {
|
||||
Jobs []Job `json:"jobs"`
|
||||
}{}
|
||||
err := jenkins.get("", nil, &payload)
|
||||
err := jenkins.getWithContext(ctx, "", nil, &payload)
|
||||
return payload.Jobs, err
|
||||
}
|
||||
|
||||
// GetJob returns a job which has specified name.
|
||||
// Returns a job which has specified name.
|
||||
func (jenkins *Jenkins) GetJob(name string, params url.Values) (job Job, err error) {
|
||||
err = jenkins.get(fmt.Sprintf("/job/%s", name), params, &job)
|
||||
return
|
||||
}
|
||||
|
||||
// GetJobWithContext uses the provided context and returns a job which has the specified name.
|
||||
// Returns a job which has the specified name using the provided context.
|
||||
func (jenkins *Jenkins) GetJobWithContext(ctx context.Context, name string, params url.Values) (job Job, err error) {
|
||||
err = jenkins.getWithContext(ctx, fmt.Sprintf("/job/%s", name), params, &job)
|
||||
return
|
||||
@@ -244,21 +256,26 @@ func (jenkins *Jenkins) GetJobConfig(name string) (job MavenJobItem, err error)
|
||||
return
|
||||
}
|
||||
|
||||
// GetBuild returns a number-th build result of specified job.
|
||||
// Returns a number-th build result of specified job.
|
||||
func (jenkins *Jenkins) GetBuild(job Job, number int) (build Build, err error) {
|
||||
err = jenkins.get(fmt.Sprintf("/job/%s/%d", job.Name, number), nil, &build)
|
||||
return
|
||||
}
|
||||
|
||||
// GetBuildWithContext uses the provided context and returns a number-th build result of specified job.
|
||||
// Returns a number-th build result of specified job using the provided context.
|
||||
func (jenkins *Jenkins) GetBuildWithContext(ctx context.Context, job Job, number int) (build Build, err error) {
|
||||
err = jenkins.getWithContext(ctx, fmt.Sprintf("/job/%s/%d", job.Name, number), nil, &build)
|
||||
return
|
||||
}
|
||||
|
||||
// GetLastBuild returns the last build of specified job.
|
||||
// Returns the last build of specified job.
|
||||
func (jenkins *Jenkins) GetLastBuild(job Job) (build Build, err error) {
|
||||
err = jenkins.get(fmt.Sprintf("/job/%s/lastBuild", job.Name), nil, &build)
|
||||
return jenkins.GetLastBuildWithContext(context.Background(), job)
|
||||
}
|
||||
|
||||
// Returns the last build of the specified job using the provided context.
|
||||
func (jenkins *Jenkins) GetLastBuildWithContext(ctx context.Context, job Job) (build Build, err error) {
|
||||
err = jenkins.getWithContext(ctx, fmt.Sprintf("/job/%s/lastBuild", job.Name), nil, &build)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -271,9 +288,14 @@ func (jenkins *Jenkins) CreateJob(mavenJobItem MavenJobItem, jobName string) err
|
||||
return jenkins.postXml("/createItem", params, reader, nil)
|
||||
}
|
||||
|
||||
// Delete a job
|
||||
// Delete a job.
|
||||
func (jenkins *Jenkins) DeleteJob(job Job) error {
|
||||
return jenkins.post(fmt.Sprintf("/job/%s/doDelete", job.Name), nil, nil)
|
||||
return jenkins.DeleteJobWithContext(context.Background(), job)
|
||||
}
|
||||
|
||||
// Delete a job using the provided context.
|
||||
func (jenkins *Jenkins) DeleteJobWithContext(ctx context.Context, job Job) error {
|
||||
return jenkins.postWithContext(ctx, fmt.Sprintf("/job/%s/doDelete", job.Name), nil, nil)
|
||||
}
|
||||
|
||||
// Add job to view
|
||||
@@ -294,10 +316,16 @@ func (jenkins *Jenkins) CreateView(listView ListView) error {
|
||||
// Create a new build for this job.
|
||||
// Params can be nil.
|
||||
func (jenkins *Jenkins) Build(job Job, params url.Values) error {
|
||||
return jenkins.BuildWithContext(context.Background(), job, params)
|
||||
}
|
||||
|
||||
// Create a new build for this job using the provided context.
|
||||
// Params can be nil.
|
||||
func (jenkins *Jenkins) BuildWithContext(ctx context.Context, job Job, params url.Values) error {
|
||||
if hasParams(job) {
|
||||
return jenkins.post(fmt.Sprintf("/job/%s/buildWithParameters", job.Name), params, nil)
|
||||
return jenkins.postWithContext(ctx, fmt.Sprintf("/job/%s/buildWithParameters", job.Name), params, nil)
|
||||
} else {
|
||||
return jenkins.post(fmt.Sprintf("/job/%s/build", job.Name), params, nil)
|
||||
return jenkins.postWithContext(ctx, fmt.Sprintf("/job/%s/build", job.Name), params, nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user