Skip to content

Commit

Permalink
Added API: Get single workflow result + Bugfix (#71)
Browse files Browse the repository at this point in the history
* Added API: Get single workflow result

* Add assert for id field.

* Fix bug for no storage file.

* Bug fix.
  • Loading branch information
robinjhuang authored Jul 11, 2024
1 parent 201f58f commit 90f8b54
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 126 deletions.
262 changes: 173 additions & 89 deletions drip/api.gen.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions integration-tests/ci_cd_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ func TestCICD(t *testing.T) {
require.Len(t, *res200.JobResults, 1)
assert.Equal(t, *res200.TotalNumberOfPages, 1)
assert.Equal(t, drip.ActionJobResult{
Id: (*res200.JobResults)[0].Id,
ActionRunId: &body.RunId,
CommitHash: &body.CommitHash,
CommitId: proto.String(git.ID.String()),
Expand Down
85 changes: 49 additions & 36 deletions mapper/workflow_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,64 @@ import (
"registry-backend/ent/schema"
)

func CiWorkflowResultToActionJobResult(results []*ent.CIWorkflowResult) ([]drip.ActionJobResult, error) {
func CiWorkflowResultsToActionJobResults(results []*ent.CIWorkflowResult) ([]drip.ActionJobResult, error) {
var jobResultsData []drip.ActionJobResult

for _, result := range results {
storageFileData := drip.StorageFile{
PublicUrl: &result.Edges.StorageFile[0].FileURL,
}
commitId := result.Edges.Gitcommit.ID.String()
commitUnixTime := result.Edges.Gitcommit.CommitTimestamp.Unix()
apiStatus, err := DbWorkflowRunStatusToApi(result.Status)
jobResultData, err := CiWorkflowResultToActionJobResult(result)
if err != nil {
return jobResultsData, err
return nil, err
}
jobResultsData = append(jobResultsData, *jobResultData)
}
return jobResultsData, nil
}

machineStats, err := MapToMachineStats(result.Metadata)
if err != nil {
return jobResultsData, err
}
func CiWorkflowResultToActionJobResult(result *ent.CIWorkflowResult) (*drip.ActionJobResult, error) {
var storageFileData *drip.StorageFile

jobResultData := drip.ActionJobResult{
WorkflowName: &result.WorkflowName,
OperatingSystem: &result.OperatingSystem,
GpuType: &result.GpuType,
PytorchVersion: &result.PytorchVersion,
StorageFile: &storageFileData,
CommitHash: &result.Edges.Gitcommit.CommitHash,
CommitId: &commitId,
CommitTime: &commitUnixTime,
CommitMessage: &result.Edges.Gitcommit.CommitMessage,
GitRepo: &result.Edges.Gitcommit.RepoName,
ActionRunId: &result.RunID,
StartTime: &result.StartTime,
EndTime: &result.EndTime,
JobTriggerUser: &result.JobTriggerUser,
AvgVram: &result.AvgVram,
PeakVram: &result.PeakVram,
PythonVersion: &result.PythonVersion,
Status: &apiStatus,
PrNumber: &result.Edges.Gitcommit.PrNumber,
Author: &result.Edges.Gitcommit.Author,
MachineStats: machineStats,
// Check if the StorageFile slice is not empty before accessing
if len(result.Edges.StorageFile) > 0 {
storageFileData = &drip.StorageFile{
PublicUrl: &result.Edges.StorageFile[0].FileURL,
}
jobResultsData = append(jobResultsData, jobResultData)
}
return jobResultsData, nil
commitId := result.Edges.Gitcommit.ID.String()
commitUnixTime := result.Edges.Gitcommit.CommitTimestamp.Unix()
apiStatus, err := DbWorkflowRunStatusToApi(result.Status)
if err != nil {
return nil, err
}

machineStats, err := MapToMachineStats(result.Metadata)
if err != nil {
return nil, err
}

return &drip.ActionJobResult{
Id: &result.ID,
WorkflowName: &result.WorkflowName,
OperatingSystem: &result.OperatingSystem,
GpuType: &result.GpuType,
PytorchVersion: &result.PytorchVersion,
StorageFile: storageFileData,
CommitHash: &result.Edges.Gitcommit.CommitHash,
CommitId: &commitId,
CommitTime: &commitUnixTime,
CommitMessage: &result.Edges.Gitcommit.CommitMessage,
GitRepo: &result.Edges.Gitcommit.RepoName,
ActionRunId: &result.RunID,
StartTime: &result.StartTime,
EndTime: &result.EndTime,
JobTriggerUser: &result.JobTriggerUser,
AvgVram: &result.AvgVram,
PeakVram: &result.PeakVram,
PythonVersion: &result.PythonVersion,
Status: &apiStatus,
PrNumber: &result.Edges.Gitcommit.PrNumber,
Author: &result.Edges.Gitcommit.Author,
MachineStats: machineStats,
}, nil
}

func ApiWorkflowRunStatusToDb(status drip.WorkflowRunStatus) (schema.WorkflowRunStatusType, error) {
Expand Down
29 changes: 29 additions & 0 deletions openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,35 @@ paths:
description: Commit not found
'500':
description: Internal server error
/workflowresult/{workflowResultId}:
get:
summary: Retrieve a specific commit by ID
operationId: getWorkflowResult
parameters:
- in: path
name: workflowResultId
required: true
schema:
type: string
responses:
'200':
description: Commit details
content:
application/json:
schema:
$ref: '#/components/schemas/ActionJobResult'
'404':
description: Commit not found
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
'500':
description: Internal server error
content:
application/json:
schema:
$ref: '#/components/schemas/ErrorResponse'
/branch:
get:
summary: Retrieve all distinct branches for a given repo
Expand Down
26 changes: 25 additions & 1 deletion server/implementation/cicd.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (impl *DripStrictServerImplementation) GetGitcommit(ctx context.Context, re
return drip.GetGitcommit500Response{}, err
}

results, err := mapper.CiWorkflowResultToActionJobResult(runs)
results, err := mapper.CiWorkflowResultsToActionJobResults(runs)
if err != nil {
log.Ctx(ctx).Error().Msgf("Error mapping git commits to action job results w/ err: %v", err)
return drip.GetGitcommit500Response{}, err
Expand All @@ -120,6 +120,30 @@ func (impl *DripStrictServerImplementation) GetGitcommit(ctx context.Context, re
}, nil
}

func (impl *DripStrictServerImplementation) GetWorkflowResult(ctx context.Context, request drip.GetWorkflowResultRequestObject) (drip.GetWorkflowResultResponseObject, error) {
log.Ctx(ctx).Info().Msgf("Getting workflow result with ID %s", request.WorkflowResultId)
workflowId := uuid.MustParse(request.WorkflowResultId)
workflow, err := impl.Client.CIWorkflowResult.Query().WithGitcommit().WithStorageFile().Where(ciworkflowresult.IDEQ(workflowId)).First(ctx)

if err != nil {
log.Ctx(ctx).Error().Msgf("Error retrieving workflow result w/ err: %v", err)
return drip.GetWorkflowResult500JSONResponse{
Message: err.Error(),
}, nil
}

result, err := mapper.CiWorkflowResultToActionJobResult(workflow)
if err != nil {
log.Ctx(ctx).Error().Msgf("Error mapping workflow result to action job result w/ err: %v", err)
return drip.GetWorkflowResult500JSONResponse{
Message: err.Error(),
}, nil
}

log.Ctx(ctx).Info().Msgf("Workflow result retrieved successfully")
return drip.GetWorkflowResult200JSONResponse(*result), nil
}

func (impl *DripStrictServerImplementation) GetBranch(ctx context.Context, request drip.GetBranchRequestObject) (drip.GetBranchResponseObject, error) {
repoNameFilter := strings.ToLower(request.Params.RepoName)

Expand Down
1 change: 1 addition & 0 deletions server/middleware/authentication/firebase_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func FirebaseAuthMiddleware(entClient *ent.Client) echo.MiddlewareFunc {
regexp.MustCompile(`^/health$`): {"GET"},
regexp.MustCompile(`^/upload-artifact$`): {"POST"},
regexp.MustCompile(`^/gitcommit$`): {"POST", "GET"},
regexp.MustCompile(`^/workflowresult/[^/]+$`): {"GET"},
regexp.MustCompile(`^/branch$`): {"GET"},
regexp.MustCompile(`^/publishers/[^/]+/nodes/[^/]+/versions$`): {"POST"},
regexp.MustCompile(`^/publishers/[^/]+/nodes$`): {"GET"},
Expand Down

0 comments on commit 90f8b54

Please sign in to comment.