gitforge/services/actions/job_logs.go
zachhandley 64bce47672 feat(api): expose action job + run logs via REST (#12666)
Closes #11859 🙂

Wanted to be able to grab action logs from my homelab dashboard without juggling session cookies. Two endpoints so scripts and webhooks can pull logs without scraping the rendered UI:

- `GET /repos/{owner}/{repo}/actions/jobs/{job_id}/logs` returns plaintext for a single job's latest task. The underlying reader is `io.ReadSeekCloser`, so passing it through `http.ServeContent` gives you HTTP `Range:` for free.
- `GET /repos/{owner}/{repo}/actions/runs/{run_id}/logs` streams a zip of every job's logs in the run. If a job hasn't started (`TaskID == 0`), its log expired, or opening the log file fails, the zip gets a `.MISSING` placeholder entry rather than bailing on the whole archive.

Both endpoints get `reqToken()` per-route. Logs can have secrets accidentally echoed into them, so I wanted auth required even though the outer `/repos` group's `tokenRequiresScopes(AccessTokenScopeCategoryRepository)` already covers scope.

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests for Go changes

- I added test coverage for Go changes...
  - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. (`tests/integration/api_actions_job_logs_test.go`, `tests/integration/api_actions_run_logs_test.go`)
- I ran...
  - [x] `make pr-go` before pushing

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it. (The new endpoints are covered by the regenerated swagger spec in this PR; `docs/user/api-usage.md` is general auth/usage guidance and doesn't need changes. Happy to open a docs PR if reviewers prefer.)

### Release notes

- [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.

Release note added as `release-notes/12666.md`.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/12666
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Reviewed-by: Mathieu Fenniak <mfenniak@noreply.codeberg.org>
2026-05-30 01:45:32 +02:00

173 lines
5.3 KiB
Go

// Copyright 2026 The Forgejo Authors. All rights reserved.
// SPDX-License-Identifier: GPL-3.0-or-later
package actions
import (
"archive/zip"
"context"
"errors"
"fmt"
"io"
"strings"
"time"
actions_model "forgejo.org/models/actions"
repo_model "forgejo.org/models/repo"
"forgejo.org/modules/actions"
"forgejo.org/modules/optional"
"forgejo.org/modules/util"
)
// Sentinel errors returned by OpenJobLogReader. The HTTP handler maps each
// of these (and util.ErrNotExist) to 404; anything else is a 500.
var (
ErrJobNotExecuted = errors.New("job has not been executed yet")
ErrLogsExpired = errors.New("logs have expired")
)
// OpenJobLogReader returns a reader for an action job's log along with the
// filename and modtime to expose via Content-Disposition / Last-Modified.
//
// attempt, when set, selects a specific historical attempt (uses
// GetTaskByJobAttempt). When unset, the latest attempt is used via the
// job.TaskID pointer maintained by the runner.
//
// The caller is responsible for closing the returned reader.
func OpenJobLogReader(
ctx context.Context,
repo *repo_model.Repository,
jobID int64,
attempt optional.Option[int64],
) (io.ReadSeekCloser, string, time.Time, error) {
job, err := actions_model.GetRunJobByID(ctx, jobID)
if err != nil {
return nil, "", time.Time{}, err
}
// Run-jobs live in their own table; enforce repo ownership here so the
// API layer can stay thin.
if job.RepoID != repo.ID {
return nil, "", time.Time{}, util.ErrNotExist
}
hasAttempt, attemptVal := attempt.Get()
var task *actions_model.ActionTask
switch {
case hasAttempt:
task, err = actions_model.GetTaskByJobAttempt(ctx, job.ID, attemptVal)
if err != nil {
return nil, "", time.Time{}, err
}
case job.TaskID == 0:
// Job exists, but no runner has picked it up yet (or a re-run has
// zeroed TaskID and the next runner hasn't claimed it).
return nil, "", time.Time{}, ErrJobNotExecuted
default:
task, err = actions_model.GetTaskByID(ctx, job.TaskID)
if err != nil {
return nil, "", time.Time{}, err
}
}
if task.LogExpired {
return nil, "", time.Time{}, ErrLogsExpired
}
reader, err := actions.OpenLogs(ctx, task.LogInStorage, task.LogFilename)
if err != nil {
return nil, "", time.Time{}, fmt.Errorf("open logs for task %d: %w", task.ID, err)
}
modtime := task.Stopped.AsTime()
if task.Stopped == 0 {
modtime = task.Updated.AsTime() // Best-guess modtime while still running.
}
filename := fmt.Sprintf("job-%d-attempt-%d.log", job.ID, task.Attempt)
return reader, filename, modtime, nil
}
// WriteRunLogsZip writes a ZIP of the latest per-job logs for the run to w.
// Each entry is named {job-name}-{job-id}-attempt-{N}.log, where N is that
// job's current attempt — the run itself has no attempt number, so jobs that
// were re-run independently show different attempts here. Jobs that haven't
// run, can't be looked up, or have expired logs get a .MISSING marker; a
// mid-stream read failure gets a sibling .PARTIAL marker. Any ZIP-level
// write failure (e.g. the HTTP client disconnects mid-stream) is propagated
// so the caller can abort instead of churning through the remaining jobs.
// Caller sets Content-Type / Content-Disposition before calling.
func WriteRunLogsZip(ctx context.Context, w io.Writer, run *actions_model.ActionRun) error {
jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID)
if err != nil {
return fmt.Errorf("get jobs for run %d: %w", run.ID, err)
}
zw := zip.NewWriter(w)
defer zw.Close()
// strip control bytes and path separators; UTF-8 passes through.
sanitize := func(name string) string {
cleaned := strings.Map(func(r rune) rune {
if r < 0x20 || r == 0x7f || r == '/' || r == '\\' {
return -1
}
return r
}, name)
cleaned = strings.TrimSpace(cleaned)
if cleaned == "" {
cleaned = "job"
}
return cleaned
}
entryName := func(job *actions_model.ActionRunJob, suffix string) string {
return fmt.Sprintf("%s-%d-attempt-%d.%s", sanitize(job.Name), job.ID, job.Attempt, suffix)
}
writeMarker := func(job *actions_model.ActionRunJob, suffix, msg string) error {
entry, werr := zw.Create(entryName(job, suffix))
if werr != nil {
return werr
}
_, werr = entry.Write([]byte(msg))
return werr
}
// Inner closure so reader.Close runs per iteration via defer.
processJob := func(job *actions_model.ActionRunJob) error {
if job.TaskID == 0 {
return writeMarker(job, "MISSING", "job has not been executed yet\n")
}
task, err := actions_model.GetTaskByID(ctx, job.TaskID)
if err != nil {
return writeMarker(job, "MISSING", fmt.Sprintf("task lookup failed: %v\n", err))
}
if task.LogExpired {
return writeMarker(job, "MISSING", "logs have been cleaned up\n")
}
reader, err := actions.OpenLogs(ctx, task.LogInStorage, task.LogFilename)
if err != nil {
return writeMarker(job, "MISSING", fmt.Sprintf("log open failed: %v\n", err))
}
defer reader.Close()
entry, err := zw.Create(entryName(job, "log"))
if err != nil {
return writeMarker(job, "MISSING", fmt.Sprintf("zip entry create failed: %v\n", err))
}
if _, copyErr := io.Copy(entry, reader); copyErr != nil {
return writeMarker(job, "PARTIAL", fmt.Sprintf("log read failed mid-stream: %v\n", copyErr))
}
return nil
}
for _, job := range jobs {
if err := processJob(job); err != nil {
return err
}
}
return nil
}