|
8 | 8 | "errors"
|
9 | 9 | "fmt"
|
10 | 10 | "net/http"
|
| 11 | + "strings" |
11 | 12 | "time"
|
12 | 13 |
|
13 | 14 | actions_model "code.gitea.io/gitea/models/actions"
|
@@ -310,6 +311,55 @@ func rerunJob(ctx *context_module.Context, job *actions_model.ActionRunJob) erro
|
310 | 311 | return nil
|
311 | 312 | }
|
312 | 313 |
|
| 314 | +func Logs(ctx *context_module.Context) { |
| 315 | + runIndex := ctx.ParamsInt64("run") |
| 316 | + jobIndex := ctx.ParamsInt64("job") |
| 317 | + |
| 318 | + job, _ := getRunJobs(ctx, runIndex, jobIndex) |
| 319 | + if ctx.Written() { |
| 320 | + return |
| 321 | + } |
| 322 | + if job.TaskID == 0 { |
| 323 | + ctx.Error(http.StatusNotFound, "job is not started") |
| 324 | + return |
| 325 | + } |
| 326 | + |
| 327 | + err := job.LoadRun(ctx) |
| 328 | + if err != nil { |
| 329 | + ctx.Error(http.StatusInternalServerError, err.Error()) |
| 330 | + return |
| 331 | + } |
| 332 | + |
| 333 | + task, err := actions_model.GetTaskByID(ctx, job.TaskID) |
| 334 | + if err != nil { |
| 335 | + ctx.Error(http.StatusInternalServerError, err.Error()) |
| 336 | + return |
| 337 | + } |
| 338 | + if task.LogExpired { |
| 339 | + ctx.Error(http.StatusNotFound, "logs have been cleaned up") |
| 340 | + return |
| 341 | + } |
| 342 | + |
| 343 | + reader, err := actions.OpenLogs(ctx, task.LogInStorage, task.LogFilename) |
| 344 | + if err != nil { |
| 345 | + ctx.Error(http.StatusInternalServerError, err.Error()) |
| 346 | + return |
| 347 | + } |
| 348 | + defer reader.Close() |
| 349 | + |
| 350 | + workflowName := job.Run.WorkflowID |
| 351 | + if p := strings.Index(workflowName, "."); p > 0 { |
| 352 | + workflowName = workflowName[0:p] |
| 353 | + } |
| 354 | + ctx.ServeContent(reader, &context_module.ServeHeaderOptions{ |
| 355 | + Filename: fmt.Sprintf("%v-%v-%v.log", workflowName, job.Name, task.ID), |
| 356 | + ContentLength: &task.LogSize, |
| 357 | + ContentType: "text/plain", |
| 358 | + ContentTypeCharset: "utf-8", |
| 359 | + Disposition: "attachment", |
| 360 | + }) |
| 361 | +} |
| 362 | + |
313 | 363 | func Cancel(ctx *context_module.Context) {
|
314 | 364 | runIndex := ctx.ParamsInt64("run")
|
315 | 365 |
|
|
0 commit comments