diff --git a/cmd/server/docs/docs.go b/cmd/server/docs/docs.go index ddf38ecc1..3514a4a76 100644 --- a/cmd/server/docs/docs.go +++ b/cmd/server/docs/docs.go @@ -2012,6 +2012,53 @@ const docTemplate = `{ } } }, + "/repos/{repo_id}/logs/{number}/{stepId}": { + "delete": { + "produces": [ + "text/plain" + ], + "tags": [ + "Pipeline logs" + ], + "summary": "Deletes step log", + "parameters": [ + { + "type": "string", + "default": "Bearer \u003cpersonal access token\u003e", + "description": "Insert your personal access token", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "integer", + "description": "the repository id", + "name": "repo_id", + "in": "path", + "required": true + }, + { + "type": "integer", + "description": "the number of the pipeline", + "name": "number", + "in": "path", + "required": true + }, + { + "type": "integer", + "description": "the step id", + "name": "stepId", + "in": "path", + "required": true + } + ], + "responses": { + "204": { + "description": "No Content" + } + } + } + }, "/repos/{repo_id}/move": { "post": { "produces": [ diff --git a/server/api/pipeline.go b/server/api/pipeline.go index 4cd6f43e3..0c5cdc5e1 100644 --- a/server/api/pipeline.go +++ b/server/api/pipeline.go @@ -224,6 +224,66 @@ func GetStepLogs(c *gin.Context) { c.JSON(http.StatusOK, logs) } +// DeleteStepLogs +// +// @Summary Deletes step log +// @Router /repos/{repo_id}/logs/{number}/{stepId} [delete] +// @Produce plain +// @Success 204 +// @Tags Pipeline logs +// @Param Authorization header string true "Insert your personal access token" default(Bearer ) +// @Param repo_id path int true "the repository id" +// @Param number path int true "the number of the pipeline" +// @Param stepId path int true "the step id" +func DeleteStepLogs(c *gin.Context) { + _store := store.FromContext(c) + repo := session.Repo(c) + + pipelineNumber, err := strconv.ParseInt(c.Params.ByName("number"), 10, 64) + if err != nil { + _ = c.AbortWithError(http.StatusBadRequest, err) + return + } + + _pipeline, err := _store.GetPipelineNumber(repo, pipelineNumber) + if err != nil { + handleDBError(c, err) + return + } + + stepID, err := strconv.ParseInt(c.Params.ByName("stepId"), 10, 64) + if err != nil { + _ = c.AbortWithError(http.StatusBadRequest, err) + return + } + + _step, err := _store.StepLoad(stepID) + if err != nil { + handleDBError(c, err) + return + } + + if _step.PipelineID != _pipeline.ID { + // make sure we cannot read arbitrary logs by id + _ = c.AbortWithError(http.StatusBadRequest, fmt.Errorf("step with id %d is not part of repo %s", stepID, repo.FullName)) + return + } + + switch _step.State { + case model.StatusRunning, model.StatusPending: + c.String(http.StatusUnprocessableEntity, "Cannot delete logs for a pending or running step") + return + } + + err = _store.LogDelete(_step) + if err != nil { + handleDBError(c, err) + return + } + + c.Status(http.StatusNoContent) +} + // GetPipelineConfig // // @Summary Pipeline configuration diff --git a/server/router/api.go b/server/router/api.go index 95eff6327..29926987a 100644 --- a/server/router/api.go +++ b/server/router/api.go @@ -104,6 +104,7 @@ func apiRoutes(e *gin.RouterGroup) { repo.POST("/pipelines/:number/decline", session.MustPush, api.PostDecline) repo.GET("/logs/:number/:stepId", api.GetStepLogs) + repo.DELETE("/logs/:number/:stepId", session.MustPush, api.DeleteStepLogs) // requires push permissions repo.DELETE("/logs/:number", session.MustPush, api.DeletePipelineLogs) diff --git a/web/src/assets/locales/en.json b/web/src/assets/locales/en.json index e792c6eee..63c27116a 100644 --- a/web/src/assets/locales/en.json +++ b/web/src/assets/locales/en.json @@ -245,6 +245,8 @@ "pipeline": "Pipeline #{pipelineId}", "log_title": "Step Logs", "log_download_error": "There was an error while downloading the log file", + "log_delete_confirm": "Do you really want to delete logs of this step?", + "log_delete_error": "There was an error while deleting the step logs", "actions": { "cancel": "Cancel", "restart": "Restart", @@ -253,6 +255,7 @@ "deploy": "Deploy", "restart_success": "Pipeline restarted", "log_download": "Download", + "log_delete": "Delete", "log_auto_scroll": "Automatically scroll down", "log_auto_scroll_off": "Turn off automatic scrolling" }, diff --git a/web/src/components/repo/pipeline/PipelineLog.vue b/web/src/components/repo/pipeline/PipelineLog.vue index 9fec6a2f0..8a505b304 100644 --- a/web/src/components/repo/pipeline/PipelineLog.vue +++ b/web/src/components/repo/pipeline/PipelineLog.vue @@ -20,6 +20,13 @@ icon="download" @click="download" /> +