diff --git a/internal/api/handlers.go b/internal/api/handlers.go index cf251a3..e9af556 100644 --- a/internal/api/handlers.go +++ b/internal/api/handlers.go @@ -86,6 +86,7 @@ func (h *Handlers) Update(w http.ResponseWriter, r *http.Request) { Project: res.Job.Project, Service: res.Job.Service, ComposeFile: composeFile, + ServiceID: res.Job.ServiceID, Status: string(res.Status), Error: res.Error, DurationMs: res.DurationMs, diff --git a/internal/api/swarm_result_test.go b/internal/api/swarm_result_test.go new file mode 100644 index 0000000..e6d978b --- /dev/null +++ b/internal/api/swarm_result_test.go @@ -0,0 +1,48 @@ +package api_test + +import ( + "context" + "net/http" + "net/http/httptest" + "strings" + "testing" + + "github.com/docker/docker/api/types" + "github.com/shcizo/package-updater/internal/api" + "github.com/shcizo/package-updater/internal/discovery" + "github.com/shcizo/package-updater/internal/updater" + "github.com/stretchr/testify/require" +) + +type swarmResultFinder struct{ jobs []discovery.Job } + +func (f *swarmResultFinder) FindJobs(_ context.Context, _ string) ([]discovery.Job, error) { + return f.jobs, nil +} + +type swarmResultSubmitter struct{ results []updater.Result } + +func (s *swarmResultSubmitter) Submit(_ context.Context, _ []discovery.Job) []updater.Result { + return s.results +} + +type swarmResultPinger struct{} + +func (swarmResultPinger) Ping(_ context.Context) (types.Ping, error) { return types.Ping{}, nil } + +func TestUpdate_SwarmJobPopulatesServiceID(t *testing.T) { + job := discovery.Job{Service: "myapp_web", ServiceID: "svc123", Image: "registry.example.com/myapp:v2"} + finder := &swarmResultFinder{jobs: []discovery.Job{job}} + submitter := &swarmResultSubmitter{results: []updater.Result{ + {Job: job, Status: updater.StatusUpdated, DurationMs: 42}, + }} + h := api.NewHandlers(finder, submitter, swarmResultPinger{}, "v", "c", "b", nil) + + req := httptest.NewRequest(http.MethodPost, "/update", strings.NewReader(`{"image":"registry.example.com/myapp"}`)) + rec := httptest.NewRecorder() + h.Update(rec, req) + + require.Equal(t, http.StatusOK, rec.Code) + require.Contains(t, rec.Body.String(), `"service_id":"svc123"`) + require.Contains(t, rec.Body.String(), `"service":"myapp_web"`) +} diff --git a/internal/api/types.go b/internal/api/types.go index fc3cf51..02f1f63 100644 --- a/internal/api/types.go +++ b/internal/api/types.go @@ -20,6 +20,7 @@ type ResultDTO struct { Project string `json:"project"` Service string `json:"service"` ComposeFile string `json:"compose_file"` + ServiceID string `json:"service_id,omitempty"` Status string `json:"status"` Error string `json:"error,omitempty"` DurationMs int64 `json:"duration_ms"`