mirror of
https://github.com/go-gitea/gitea.git
synced 2026-08-04 14:17:38 +00:00
feat(actions): Add Actions API endpoints for workflow run management and logs (#35382)
Implements the missing REST API endpoints for Actions workflow run
management:
1. `POST /actions/runs/{run}/cancel` cancels a run and its jobs, `409`
when it already finished
1. `POST /actions/runs/{run}/approve` approves a run awaiting approval,
idempotent, `409` when it never awaited one
1. `GET /actions/runs/{run}/logs` downloads the latest attempt's job
logs as a zip archive
`ActionWorkflowRun` gains `created_at`, `updated_at` and the `jobs_url`,
`logs_url`, `artifacts_url`, `cancel_url` and `rerun_url` fields, and
now always emits `conclusion` and `head_branch`.
Cancellation is shared with the web handler in `services/actions`.
Fixes https://github.com/go-gitea/gitea/issues/35176
Fixes https://github.com/go-gitea/gitea/issues/36554
---------
Co-authored-by: Claude Sonnet 4.6 <claude-sonnet-4-6@anthropic.com>
Co-authored-by: OpenCode Agent <opencode@rossgolder.com>
Co-authored-by: Nicolas <bircni@icloud.com>
Co-authored-by: silverwind <me@silverwind.io>
Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
+11
-5
@@ -192,16 +192,22 @@ func OpenLogs(ctx context.Context, inStorage bool, filename string) (io.ReadSeek
|
||||
return nil, fmt.Errorf("storage open %q: %w", filename, err)
|
||||
}
|
||||
|
||||
var reader io.ReadSeekCloser = f
|
||||
if strings.HasSuffix(filename, ".zst") {
|
||||
r, err := zstd.NewSeekableReader(f)
|
||||
reader, err := zstd.NewSeekableReader(f) // reads the seek table, so a lazily opened object already fails here
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("zstd NewSeekableReader: %w", err)
|
||||
f.Close()
|
||||
return nil, fmt.Errorf("zstd NewSeekableReader %q: %w", filename, err)
|
||||
}
|
||||
reader = r
|
||||
return reader, nil
|
||||
}
|
||||
|
||||
return reader, nil
|
||||
// object storage opens lazily, force a missing object to surface before the caller commits a response
|
||||
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
||||
f.Close()
|
||||
return nil, fmt.Errorf("storage open %q: %w", filename, err)
|
||||
}
|
||||
|
||||
return f, nil
|
||||
}
|
||||
|
||||
func FormatLog(timestamp time.Time, content string) string {
|
||||
|
||||
@@ -39,6 +39,23 @@ func (m *minioObject) Stat() (os.FileInfo, error) {
|
||||
return &minioFileInfo{oi}, nil
|
||||
}
|
||||
|
||||
// minio reports a missing key on the first Read, ReadAt or Seek rather than on Open, so all
|
||||
// of them convert it like Stat does.
|
||||
func (m *minioObject) Read(p []byte) (int, error) {
|
||||
n, err := m.Object.Read(p)
|
||||
return n, convertMinioErr(err)
|
||||
}
|
||||
|
||||
func (m *minioObject) ReadAt(p []byte, off int64) (int, error) {
|
||||
n, err := m.Object.ReadAt(p, off)
|
||||
return n, convertMinioErr(err)
|
||||
}
|
||||
|
||||
func (m *minioObject) Seek(offset int64, whence int) (int64, error) {
|
||||
n, err := m.Object.Seek(offset, whence)
|
||||
return n, convertMinioErr(err)
|
||||
}
|
||||
|
||||
// MinioStorage returns a minio bucket storage
|
||||
type MinioStorage struct {
|
||||
cfg *setting.MinioStorageConfig
|
||||
|
||||
@@ -111,6 +111,11 @@ type ActionWorkflowRun struct {
|
||||
// It is set only when the current attempt is > 1 (i.e. a rerun). For the first attempt, or for legacy runs that pre-date ActionRunAttempt, it is null.
|
||||
PreviousAttemptURL *string `json:"previous_attempt_url"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
JobsURL string `json:"jobs_url"`
|
||||
LogsURL string `json:"logs_url"`
|
||||
ArtifactsURL string `json:"artifacts_url"`
|
||||
CancelURL string `json:"cancel_url"`
|
||||
RerunURL string `json:"rerun_url"`
|
||||
DisplayTitle string `json:"display_title"`
|
||||
Path string `json:"path"`
|
||||
Event string `json:"event"`
|
||||
@@ -130,6 +135,10 @@ type ActionWorkflowRun struct {
|
||||
Conclusion string `json:"conclusion,omitempty"`
|
||||
PullRequests []*PullRequestMinimal `json:"pull_requests"`
|
||||
// swagger:strfmt date-time
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
// swagger:strfmt date-time
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
// swagger:strfmt date-time
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
// swagger:strfmt date-time
|
||||
CompletedAt time.Time `json:"completed_at"`
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// FileNameJoinFields joins the fields by separator "-" as a safe filename,
|
||||
// the last field is used as extname (no sep is added before it).
|
||||
// This function does its best to avoid the filename exceeding the max filename length (255 bytes on Linux),
|
||||
// Each field needs at least about 3 bytes, if the caller passes too many fields (e.g.: 100 fields),
|
||||
// then it's unavoidable, just do not write such code in real world.
|
||||
func FileNameJoinFields(fields ...any) string {
|
||||
// linux max filename length is 255, we leave some space for the separators and extname
|
||||
const maxFilenameLen = 210
|
||||
return fileNameJoinFields(maxFilenameLen, fields...)
|
||||
}
|
||||
|
||||
func fileNameJoinFields(stemNameLimit int, fields ...any) string {
|
||||
// stemNameLimit is just a suggested limit, when adding more fields, the length might still exceed a little
|
||||
sb := strings.Builder{}
|
||||
for i, f := range fields {
|
||||
var field string
|
||||
switch v := f.(type) {
|
||||
case string:
|
||||
field = v
|
||||
case int64:
|
||||
field = strconv.FormatInt(v, 10)
|
||||
default:
|
||||
field = fmt.Sprint(v)
|
||||
}
|
||||
field = PathNameValidator().InvalidChars.ReplaceAllString(field, "_")
|
||||
if i < len(fields)-1 {
|
||||
field = strings.ReplaceAll(strings.ReplaceAll(field, ".", "_"), "-", "_")
|
||||
estimatedRemainingLen := (len(fields) - 1 - i) * 3
|
||||
estimatedLimit := stemNameLimit - estimatedRemainingLen
|
||||
if sb.Len()+len(field) > estimatedLimit {
|
||||
field = TruncateStringBytes(field, estimatedLimit-sb.Len()-2) + "__"
|
||||
}
|
||||
sb.WriteString(field)
|
||||
if i < len(fields)-2 {
|
||||
sb.WriteString("-")
|
||||
}
|
||||
} else {
|
||||
// last field is extname, no need to do more processing
|
||||
sb.WriteString(field)
|
||||
}
|
||||
}
|
||||
return sb.String()
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
// Copyright 2026 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package util
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestFileNameJoinFields(t *testing.T) {
|
||||
assert.Equal(t, "a_b-1.txt", FileNameJoinFields("a-b", 1, ".txt"))
|
||||
assert.Equal(t, "a-____-b.txt", FileNameJoinFields("a", "/../", "b", ".txt"))
|
||||
assert.Equal(t, "a-____-b.txt", FileNameJoinFields("a", "\\..\\", "b", ".txt"))
|
||||
|
||||
assert.Equal(t, "🌞-🌛.txt", fileNameJoinFields(14, "🌞", "🌛", ".txt"))
|
||||
assert.Equal(t, "🌞-🌛__.txt", fileNameJoinFields(14, "🌞", "🌛🌛🌛🌛", ".txt"))
|
||||
assert.Equal(t, "🌞__-__.txt", fileNameJoinFields(14, "🌞🌞🌞🌞", "🌛🌛🌛🌛", ".txt"))
|
||||
}
|
||||
@@ -10,9 +10,27 @@ import (
|
||||
"os"
|
||||
"path"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var PathNameValidator = sync.OnceValue(func() (ret struct {
|
||||
InvalidChars *regexp.Regexp
|
||||
InvalidNames *regexp.Regexp
|
||||
},
|
||||
) {
|
||||
ret.InvalidChars = regexp.MustCompile(`(?i)[<>:"/\\|?*\x{0000}-\x{001F}]`)
|
||||
// invalid filename contents, based on https://github.com/sindresorhus/filename-reserved-regex
|
||||
// "COM10" needs to be opened with UNC "\\.\COM10" on Windows, so itself is valid
|
||||
ret.InvalidNames = regexp.MustCompile(`(?i)^(con|prn|aux|nul|com\d|lpt\d)$`)
|
||||
return ret
|
||||
})
|
||||
|
||||
func PathBaseStem(s string) string {
|
||||
return strings.TrimSuffix(path.Base(s), path.Ext(s))
|
||||
}
|
||||
|
||||
// PathJoinRel joins the path elements into a single path, each element is cleaned by path.Clean separately.
|
||||
// It only returns the following values (like path.Join), any redundant part (empty, relative dots, slashes) is removed.
|
||||
// It's caller's duty to make every element not bypass its own directly level, to avoid security issues.
|
||||
|
||||
@@ -130,3 +130,17 @@ func TruncateRunes(str string, limit int) string {
|
||||
}
|
||||
return string([]rune(str)[:limit])
|
||||
}
|
||||
|
||||
// TruncateStringBytes returns a truncated string with given byte limit,
|
||||
// it returns input string if its byte length doesn't exceed the limit.
|
||||
func TruncateStringBytes(str string, limit int) string {
|
||||
l := 0
|
||||
for i, r := range str {
|
||||
rl := utf8.RuneLen(r)
|
||||
if l+rl > limit {
|
||||
return str[:i]
|
||||
}
|
||||
l += rl
|
||||
}
|
||||
return str
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user