-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
/
Copy pathactions.go
93 lines (80 loc) · 2.34 KB
/
actions.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package common
import (
"fmt"
"net/http"
"strings"
actions_model "code.gitea.io/gitea/models/actions"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/modules/actions"
"code.gitea.io/gitea/services/context"
)
func DownloadActionsRunJobLogsWithIndex(ctx *context.Base, ctxRepo *repo_model.Repository, runID, jobIndex int64) {
if runID == 0 {
ctx.HTTPError(http.StatusBadRequest, "invalid run id")
return
}
runJobs, err := actions_model.GetRunJobsByRunID(ctx, runID)
if err != nil {
ctx.HTTPError(http.StatusInternalServerError, err.Error())
return
}
if len(runJobs) == 0 {
ctx.HTTPError(http.StatusNotFound)
return
}
if err := runJobs.LoadRepos(ctx); err != nil {
ctx.HTTPError(http.StatusInternalServerError, err.Error())
return
}
var curJob *actions_model.ActionRunJob
if jobIndex >= 0 && jobIndex < int64(len(runJobs)) {
curJob = runJobs[jobIndex]
}
if curJob == nil {
ctx.HTTPError(http.StatusNotFound)
return
}
DownloadActionsRunJobLogs(ctx, ctxRepo, curJob)
}
func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository, curJob *actions_model.ActionRunJob) {
if curJob.Repo.ID != ctxRepo.ID {
ctx.HTTPError(http.StatusNotFound)
return
}
if curJob.TaskID == 0 {
ctx.HTTPError(http.StatusNotFound, "job is not started")
return
}
if err := curJob.LoadRun(ctx); err != nil {
ctx.HTTPError(http.StatusInternalServerError, err.Error())
return
}
task, err := actions_model.GetTaskByID(ctx, curJob.TaskID)
if err != nil {
ctx.HTTPError(http.StatusInternalServerError, err.Error())
return
}
if task.LogExpired {
ctx.HTTPError(http.StatusNotFound, "logs have been cleaned up")
return
}
reader, err := actions.OpenLogs(ctx, task.LogInStorage, task.LogFilename)
if err != nil {
ctx.HTTPError(http.StatusInternalServerError, err.Error())
return
}
defer reader.Close()
workflowName := curJob.Run.WorkflowID
if p := strings.Index(workflowName, "."); p > 0 {
workflowName = workflowName[0:p]
}
ctx.ServeContent(reader, &context.ServeHeaderOptions{
Filename: fmt.Sprintf("%v-%v-%v.log", workflowName, curJob.Name, task.ID),
ContentLength: &task.LogSize,
ContentType: "text/plain",
ContentTypeCharset: "utf-8",
Disposition: "attachment",
})
}