add find logs handler

This commit is contained in:
Vistaar Juneja 2023-08-13 18:54:07 +01:00
parent 6ae5145be4
commit 9ea22140aa
4 changed files with 119 additions and 0 deletions

View File

@ -0,0 +1,45 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package execution
import (
"context"
"fmt"
"io"
apiauth "github.com/harness/gitness/internal/api/auth"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/types/enum"
)
func (c *Controller) FindLogs(
ctx context.Context,
session *auth.Session,
spaceRef string,
pipelineUID string,
executionNum int64,
stageNum int64,
stepNum int64,
) (io.ReadCloser, error) {
space, err := c.spaceStore.FindByRef(ctx, spaceRef)
if err != nil {
return nil, fmt.Errorf("could not find parent space: %w", err)
}
pipeline, err := c.pipelineStore.FindByUID(ctx, space.ID, pipelineUID)
if err != nil {
return nil, fmt.Errorf("could not find pipeline: %w", err)
}
err = apiauth.CheckPipeline(ctx, c.authorizer, session, space.Path, pipeline.UID, enum.PermissionPipelineView)
if err != nil {
return nil, fmt.Errorf("could not authorize: %w", err)
}
// TODO: Figure out step ID by querying stages and steps tables
var id int64
return c.logStore.Find(ctx, id)
}

View File

@ -0,0 +1,59 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package execution
import (
"io"
"net/http"
"github.com/harness/gitness/internal/api/controller/execution"
"github.com/harness/gitness/internal/api/render"
"github.com/harness/gitness/internal/api/request"
"github.com/harness/gitness/internal/paths"
)
func HandleFindLogs(executionCtrl *execution.Controller) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
session, _ := request.AuthSessionFrom(ctx)
pipelineRef, err := request.GetPipelineRefFromPath(r)
if err != nil {
render.TranslatedUserError(w, err)
return
}
executionNum, err := request.GetExecutionNumberFromPath(r)
if err != nil {
render.TranslatedUserError(w, err)
return
}
stageNum, err := request.GetStageNumberFromPath(r)
if err != nil {
render.TranslatedUserError(w, err)
return
}
stepNum, err := request.GetStepNumberFromPath(r)
if err != nil {
render.TranslatedUserError(w, err)
return
}
spaceRef, pipelineUID, err := paths.DisectLeaf(pipelineRef)
if err != nil {
render.TranslatedUserError(w, err)
return
}
rc, err := executionCtrl.FindLogs(
ctx, session, spaceRef, pipelineUID,
executionNum, stageNum, stepNum)
if err != nil {
render.TranslatedUserError(w, err)
return
}
w.Header().Set("Content-Type", "application/json")
io.Copy(w, rc)
rc.Close()
}
}

View File

@ -12,6 +12,8 @@ import (
const (
PathParamPipelineRef = "pipeline_ref"
PathParamExecutionNumber = "execution_number"
PathParamStageNumber = "stage_number"
PathParamStepNumber = "step_number"
)
func GetPipelineRefFromPath(r *http.Request) (string, error) {
@ -27,3 +29,11 @@ func GetPipelineRefFromPath(r *http.Request) (string, error) {
func GetExecutionNumberFromPath(r *http.Request) (int64, error) {
return PathParamAsPositiveInt64(r, PathParamExecutionNumber)
}
func GetStageNumberFromPath(r *http.Request) (int64, error) {
return PathParamAsPositiveInt64(r, PathParamStageNumber)
}
func GetStepNumberFromPath(r *http.Request) (int64, error) {
return PathParamAsPositiveInt64(r, PathParamStepNumber)
}

View File

@ -323,6 +323,11 @@ func setupExecutions(r chi.Router, pipelineCtrl *pipeline.Controller, executionC
r.Get("/", handlerexecution.HandleFind(executionCtrl))
r.Patch("/", handlerexecution.HandleUpdate(executionCtrl))
r.Delete("/", handlerexecution.HandleDelete(executionCtrl))
r.Get(
fmt.Sprintf("/logs/{%s}/{%s}",
request.PathParamStageNumber,
request.PathParamStepNumber,
), handlerexecution.HandleFindLogs(executionCtrl))
})
})
}