gitea源码

actions.go 2.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // Copyright 2025 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package common
  4. import (
  5. "fmt"
  6. "strings"
  7. actions_model "code.gitea.io/gitea/models/actions"
  8. repo_model "code.gitea.io/gitea/models/repo"
  9. "code.gitea.io/gitea/modules/actions"
  10. "code.gitea.io/gitea/modules/util"
  11. "code.gitea.io/gitea/services/context"
  12. )
  13. func DownloadActionsRunJobLogsWithIndex(ctx *context.Base, ctxRepo *repo_model.Repository, runID, jobIndex int64) error {
  14. runJobs, err := actions_model.GetRunJobsByRunID(ctx, runID)
  15. if err != nil {
  16. return fmt.Errorf("GetRunJobsByRunID: %w", err)
  17. }
  18. if err = runJobs.LoadRepos(ctx); err != nil {
  19. return fmt.Errorf("LoadRepos: %w", err)
  20. }
  21. if jobIndex < 0 || jobIndex >= int64(len(runJobs)) {
  22. return util.NewNotExistErrorf("job index is out of range: %d", jobIndex)
  23. }
  24. return DownloadActionsRunJobLogs(ctx, ctxRepo, runJobs[jobIndex])
  25. }
  26. func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository, curJob *actions_model.ActionRunJob) error {
  27. if curJob.Repo.ID != ctxRepo.ID {
  28. return util.NewNotExistErrorf("job not found")
  29. }
  30. if curJob.TaskID == 0 {
  31. return util.NewNotExistErrorf("job not started")
  32. }
  33. if err := curJob.LoadRun(ctx); err != nil {
  34. return fmt.Errorf("LoadRun: %w", err)
  35. }
  36. task, err := actions_model.GetTaskByID(ctx, curJob.TaskID)
  37. if err != nil {
  38. return fmt.Errorf("GetTaskByID: %w", err)
  39. }
  40. if task.LogExpired {
  41. return util.NewNotExistErrorf("logs have been cleaned up")
  42. }
  43. reader, err := actions.OpenLogs(ctx, task.LogInStorage, task.LogFilename)
  44. if err != nil {
  45. return fmt.Errorf("OpenLogs: %w", err)
  46. }
  47. defer reader.Close()
  48. workflowName := curJob.Run.WorkflowID
  49. if p := strings.Index(workflowName, "."); p > 0 {
  50. workflowName = workflowName[0:p]
  51. }
  52. ctx.ServeContent(reader, &context.ServeHeaderOptions{
  53. Filename: fmt.Sprintf("%v-%v-%v.log", workflowName, curJob.Name, task.ID),
  54. ContentLength: &task.LogSize,
  55. ContentType: "text/plain",
  56. ContentTypeCharset: "utf-8",
  57. Disposition: "attachment",
  58. })
  59. return nil
  60. }