-
-
Notifications
You must be signed in to change notification settings - Fork 6.7k
Expand file tree
/
Copy pathjob_summary.go
More file actions
96 lines (82 loc) · 2.52 KB
/
job_summary.go
File metadata and controls
96 lines (82 loc) · 2.52 KB
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
94
95
96
// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package actions
import (
"errors"
"io"
"mime"
"net/http"
"strconv"
actions_model "code.gitea.io/gitea/models/actions"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/util"
)
const jobSummaryRouteBase = "/_apis/pipelines/workflows/{run_id}/jobs/{job_id}/summary"
func uploadJobSummary(ctx *ArtifactContext) {
task, runID, ok := validateRunID(ctx)
if !ok {
return
}
jobID := ctx.PathParamInt64("job_id")
if jobID <= 0 {
ctx.HTTPError(http.StatusBadRequest, "invalid job_id")
return
}
if task == nil || task.Job == nil {
ctx.HTTPError(http.StatusInternalServerError, "task/job not loaded")
return
}
if task.Job.ID != jobID {
ctx.HTTPError(http.StatusBadRequest, "job_id mismatch")
return
}
if task.Job.RunID != runID {
ctx.HTTPError(http.StatusBadRequest, "run_id mismatch")
return
}
body, err := io.ReadAll(io.LimitReader(ctx.Req.Body, actions_model.MaxJobSummarySize+1))
if err != nil {
log.Error("Error reading job summary request body: %v", err)
ctx.HTTPError(http.StatusInternalServerError, "read request body")
return
}
if len(body) == 0 {
ctx.JSON(http.StatusOK, map[string]string{"message": "empty"})
return
}
contentType, ok := normalizeJobSummaryContentType(ctx.Req.Header.Get("Content-Type"))
if !ok {
ctx.HTTPError(http.StatusBadRequest, "invalid summary content type")
return
}
if err := actions_model.UpsertActionRunJobSummary(ctx, task.Job.RepoID, task.Job.RunID, task.Job.RunAttemptID, task.Job.ID, contentType, body); err != nil {
if errorsIsInvalidArg(err) {
ctx.HTTPError(http.StatusBadRequest, "invalid summary")
return
}
log.Error("Error upsert job summary: %v", err)
ctx.HTTPError(http.StatusInternalServerError, "Error upsert job summary")
return
}
ctx.JSON(http.StatusOK, map[string]string{
"message": "success",
"sizeBytes": strconv.Itoa(len(body)),
"runAttempt": strconv.FormatInt(task.Job.RunAttemptID, 10),
})
}
func errorsIsInvalidArg(err error) bool {
return errors.Is(err, util.ErrInvalidArgument)
}
func normalizeJobSummaryContentType(contentType string) (string, bool) {
if contentType == "" || contentType == "application/octet-stream" {
return actions_model.JobSummaryContentTypeMarkdown, true
}
mediaType, _, err := mime.ParseMediaType(contentType)
if err != nil {
return "", false
}
if mediaType != actions_model.JobSummaryContentTypeMarkdown {
return "", false
}
return actions_model.JobSummaryContentTypeMarkdown, true
}