From a3e8c3fd25b02be1307bcf37eec0e7a533ecbf8f Mon Sep 17 00:00:00 2001 From: Dimitri Roche Date: Thu, 11 Jan 2018 12:13:05 -0500 Subject: [PATCH 1/2] show job in api and cli returns runs --- commands/commands.go | 2 +- web/jobs_controller.go | 11 ++++++++++- web/jobs_controller_test.go | 6 +++++- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index 76c3b3eb38f..7535edb8cde 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -58,7 +58,7 @@ func (self *Client) ShowJob(c *cli.Context) error { return self.cliError(err) } defer resp.Body.Close() - var job models.Job + var job web.JobResponse return self.deserializeResponse(resp, &job) } diff --git a/web/jobs_controller.go b/web/jobs_controller.go index 2d9f3101448..23dc7118599 100644 --- a/web/jobs_controller.go +++ b/web/jobs_controller.go @@ -43,6 +43,11 @@ func (self *JobsController) Create(c *gin.Context) { } } +type JobResponse struct { + models.Job + Runs []models.JobRun `json:"runs,omitempty"` +} + func (self *JobsController) Show(c *gin.Context) { id := c.Param("ID") var j models.Job @@ -55,7 +60,11 @@ func (self *JobsController) Show(c *gin.Context) { c.JSON(500, gin.H{ "errors": []string{err.Error()}, }) + } else if runs, err := self.App.Store.JobRunsFor(j); err != nil { + c.JSON(500, gin.H{ + "errors": []string{err.Error()}, + }) } else { - c.JSON(200, j) + c.JSON(200, JobResponse{j, runs}) } } diff --git a/web/jobs_controller_test.go b/web/jobs_controller_test.go index 14f73d0e682..0a5cbe29894 100644 --- a/web/jobs_controller_test.go +++ b/web/jobs_controller_test.go @@ -14,6 +14,7 @@ import ( "github.com/smartcontractkit/chainlink-go/store" "github.com/smartcontractkit/chainlink-go/store/models" "github.com/smartcontractkit/chainlink-go/utils" + "github.com/smartcontractkit/chainlink-go/web" "github.com/stretchr/testify/assert" ) @@ -267,6 +268,8 @@ func TestShowJobs(t *testing.T) { j := cltest.NewJobWithSchedule("9 9 9 9 6") app.Store.Save(&j) + jr := j.NewRun() + app.Store.Save(&jr) resp, err := cltest.BasicAuthGet(server.URL + "/jobs/" + j.ID) assert.Nil(t, err) @@ -274,9 +277,10 @@ func TestShowJobs(t *testing.T) { b, err := ioutil.ReadAll(resp.Body) defer resp.Body.Close() - var respJob models.Job + var respJob web.JobResponse json.Unmarshal(b, &respJob) assert.Equal(t, respJob.Initiators[0].Schedule, j.Initiators[0].Schedule, "should have the same schedule") + assert.Equal(t, respJob.Runs[0].ID, jr.ID, "should have the job runs") } func TestShowNotFoundJobs(t *testing.T) { From 3a87218d5f2ad291c90b3707ecba0eb9b84d7ca5 Mon Sep 17 00:00:00 2001 From: Dimitri Roche Date: Thu, 11 Jan 2018 12:26:19 -0500 Subject: [PATCH 2/2] Rename JobResponse to JobPresenter --- commands/commands.go | 2 +- web/jobs_controller.go | 4 ++-- web/jobs_controller_test.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/commands/commands.go b/commands/commands.go index 7535edb8cde..04fe8c7ce0a 100644 --- a/commands/commands.go +++ b/commands/commands.go @@ -58,7 +58,7 @@ func (self *Client) ShowJob(c *cli.Context) error { return self.cliError(err) } defer resp.Body.Close() - var job web.JobResponse + var job web.JobPresenter return self.deserializeResponse(resp, &job) } diff --git a/web/jobs_controller.go b/web/jobs_controller.go index 23dc7118599..1bf643beefc 100644 --- a/web/jobs_controller.go +++ b/web/jobs_controller.go @@ -43,7 +43,7 @@ func (self *JobsController) Create(c *gin.Context) { } } -type JobResponse struct { +type JobPresenter struct { models.Job Runs []models.JobRun `json:"runs,omitempty"` } @@ -65,6 +65,6 @@ func (self *JobsController) Show(c *gin.Context) { "errors": []string{err.Error()}, }) } else { - c.JSON(200, JobResponse{j, runs}) + c.JSON(200, JobPresenter{j, runs}) } } diff --git a/web/jobs_controller_test.go b/web/jobs_controller_test.go index 0a5cbe29894..bd3edc442bb 100644 --- a/web/jobs_controller_test.go +++ b/web/jobs_controller_test.go @@ -277,7 +277,7 @@ func TestShowJobs(t *testing.T) { b, err := ioutil.ReadAll(resp.Body) defer resp.Body.Close() - var respJob web.JobResponse + var respJob web.JobPresenter json.Unmarshal(b, &respJob) assert.Equal(t, respJob.Initiators[0].Schedule, j.Initiators[0].Schedule, "should have the same schedule") assert.Equal(t, respJob.Runs[0].ID, jr.ID, "should have the job runs")