github.com/discordapp/buildkite-agent@v2.6.6+incompatible/api/jobs.go (about) 1 package api 2 3 import ( 4 "fmt" 5 ) 6 7 // JobsService handles communication with the job related methods of the 8 // Buildkite Agent API. 9 type JobsService struct { 10 client *Client 11 } 12 13 // Job represents a Buildkite Agent API Job 14 type Job struct { 15 ID string `json:"id,omitempty"` 16 Endpoint string `json:"endpoint"` 17 State string `json:"state,omitempty"` 18 Env map[string]string `json:"env,omitempty"` 19 ChunksMaxSizeBytes int `json:"chunks_max_size_bytes,omitempty"` 20 ExitStatus string `json:"exit_status,omitempty"` 21 StartedAt string `json:"started_at,omitempty"` 22 FinishedAt string `json:"finished_at,omitempty"` 23 ChunksFailedCount int `json:"chunks_failed_count,omitempty"` 24 } 25 26 type JobState struct { 27 State string `json:"state,omitempty"` 28 } 29 30 type jobStartRequest struct { 31 StartedAt string `json:"started_at,omitempty"` 32 } 33 34 type jobFinishRequest struct { 35 ExitStatus string `json:"exit_status,omitempty"` 36 FinishedAt string `json:"finished_at,omitempty"` 37 ChunksFailedCount int `json:"chunks_failed_count"` 38 } 39 40 // Fetches a job 41 func (js *JobsService) GetState(id string) (*JobState, *Response, error) { 42 u := fmt.Sprintf("jobs/%s", id) 43 44 req, err := js.client.NewRequest("GET", u, nil) 45 if err != nil { 46 return nil, nil, err 47 } 48 49 s := new(JobState) 50 resp, err := js.client.Do(req, s) 51 if err != nil { 52 return nil, resp, err 53 } 54 55 return s, resp, err 56 } 57 58 // Accepts the passed in job. Returns the job with it's finalized set of 59 // environment variables (when a job is accepted, the agents environment is 60 // applied to the job) 61 func (js *JobsService) Accept(job *Job) (*Job, *Response, error) { 62 u := fmt.Sprintf("jobs/%s/accept", job.ID) 63 64 req, err := js.client.NewRequest("PUT", u, nil) 65 if err != nil { 66 return nil, nil, err 67 } 68 69 j := new(Job) 70 resp, err := js.client.Do(req, j) 71 if err != nil { 72 return nil, resp, err 73 } 74 75 return j, resp, err 76 } 77 78 // Starts the passed in job 79 func (js *JobsService) Start(job *Job) (*Response, error) { 80 u := fmt.Sprintf("jobs/%s/start", job.ID) 81 82 req, err := js.client.NewRequest("PUT", u, &jobStartRequest{ 83 StartedAt: job.StartedAt, 84 }) 85 if err != nil { 86 return nil, err 87 } 88 89 return js.client.Do(req, nil) 90 } 91 92 // Finishes the passed in job 93 func (js *JobsService) Finish(job *Job) (*Response, error) { 94 u := fmt.Sprintf("jobs/%s/finish", job.ID) 95 96 req, err := js.client.NewRequest("PUT", u, &jobFinishRequest{ 97 FinishedAt: job.FinishedAt, 98 ExitStatus: job.ExitStatus, 99 ChunksFailedCount: job.ChunksFailedCount, 100 }) 101 if err != nil { 102 return nil, err 103 } 104 105 return js.client.Do(req, nil) 106 }