Skip to content

Commit

Permalink
feat(server): View tasks (#126)
Browse files Browse the repository at this point in the history
  • Loading branch information
wndhydrnt authored Dec 23, 2024
1 parent dd2a6c9 commit 12d399a
Show file tree
Hide file tree
Showing 24 changed files with 1,141 additions and 59 deletions.
66 changes: 66 additions & 0 deletions pkg/server/api/openapi/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,36 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Error"
/api/v1/taskResults:
get:
operationId: listTaskResultsV1
summary: Task results
description: A list of the recent run results of the task.
tags:
- task
parameters:
- in: query
name: runId
schema:
type: integer
description: ID of a run to filter by.
- in: query
name: status
schema:
type: array
items:
$ref: "#/components/schemas/TaskResultStatusV1"
- in: query
name: listOptions
schema:
$ref: "#/components/schemas/ListOptions"
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/ListTaskResultsV1Response"
/api/v1/worker/work:
get:
operationId: getWorkV1
Expand Down Expand Up @@ -407,3 +437,39 @@ components:
run:
$ref: "#/components/schemas/RunV1"
required: ["run"]
TaskResultV1:
type: object
properties:
error:
description: Error that occurred while creating the pull request, if any.
type: string
repositoryName:
description: Name of the repository.
type: string
pullRequestUrl:
description: URL of the pull request opened by saturn-bot.
type: string
status:
$ref: "#/components/schemas/TaskResultStatusV1"
runId:
description: Numeric identifier of the run this result is a part of.
type: integer
required: ["repositoryName", "status", "runId"]
TaskResultStatusV1:
description: Status of the pull request.
type: string
enum:
- closed
- error
- merged
- open
ListTaskResultsV1Response:
type: object
properties:
page:
$ref: "#/components/schemas/Page"
taskResults:
type: array
items:
$ref: "#/components/schemas/TaskResultV1"
required: ["page", "taskResults"]
144 changes: 144 additions & 0 deletions pkg/server/api/openapi/server.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 53 additions & 0 deletions pkg/server/api/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import (
"context"
"errors"

"github.com/wndhydrnt/saturn-bot/pkg/ptr"
"github.com/wndhydrnt/saturn-bot/pkg/server/api/openapi"
"github.com/wndhydrnt/saturn-bot/pkg/server/db"
sberror "github.com/wndhydrnt/saturn-bot/pkg/server/error"
"github.com/wndhydrnt/saturn-bot/pkg/server/service"
)

// GetTaskV1 implements [openapi.ServerInterface].
Expand Down Expand Up @@ -42,3 +45,53 @@ func (th *APIServer) ListTasksV1(_ context.Context, request openapi.ListTasksV1R

return resp, nil
}

// ListTaskResultsV1 implements [openapi.ServerInterface].
func (a *APIServer) ListTaskResultsV1(ctx context.Context, request openapi.ListTaskResultsV1RequestObject) (openapi.ListTaskResultsV1ResponseObject, error) {
opts := service.ListTaskResultsOptions{}
if request.Params.RunId != nil {
opts.RunId = ptr.From(request.Params.RunId)
}

if request.Params.Status != nil {
for _, apiStatus := range ptr.From(request.Params.Status) {
opts.Status = append(opts.Status, db.TaskResultStatus(apiStatus))
}
}

listOpts := toListOptions(request.Params.ListOptions)
taskResults, err := a.WorkerService.ListTaskResults(opts, &listOpts)
if err != nil {
return nil, err
}

resp := openapi.ListTaskResultsV1200JSONResponse{
Page: openapi.Page{
PreviousPage: listOpts.Previous(),
CurrentPage: listOpts.Page,
NextPage: listOpts.Next(),
ItemsPerPage: listOpts.Limit,
TotalItems: listOpts.TotalItems(),
TotalPages: listOpts.TotalPages(),
},
TaskResults: []openapi.TaskResultV1{},
}
for _, tr := range taskResults {
resp.TaskResults = append(resp.TaskResults, mapTaskResultFromDbToApi(tr))
}

return resp, nil
}

func mapTaskResultFromDbToApi(db db.TaskResult) openapi.TaskResultV1 {
api := openapi.TaskResultV1{
RepositoryName: db.RepositoryName,
RunId: int(db.RunID), // #nosec G115 -- no info by gosec on how to fix this
Status: openapi.TaskResultStatusV1(db.Status),
}
if db.Error != nil {
api.Error = db.Error
}

return api
}
13 changes: 11 additions & 2 deletions pkg/server/db/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,21 @@ type Task struct {
Hash string
}

type TaskResultStatus string

const (
TaskResultStatusClosed = "closed"
TaskResultStatusError = "error"
TaskResultStatusMerged = "merged"
TaskResultStatusOpen = "open"
)

type TaskResult struct {
CreatedAt time.Time
Error *string
ID uint `gorm:"primarykey"`
RepositoryName string
Result uint
Result int
Status TaskResultStatus
RunID uint
TaskName string
}
3 changes: 2 additions & 1 deletion pkg/server/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ type apiCall struct {
method string
// Path of the request.
path string
// Query parameters of the request.
// Query parameters of the request,
// like a=b&c=d
query string
// Request headers, if any.
requestHeaders map[string]string
Expand Down
Loading

0 comments on commit 12d399a

Please sign in to comment.