Show latest run when visit /run/latest (#31808)

Proposal from
https://github.com/go-gitea/gitea/issues/27911#issuecomment-2271982172

When visit latest run path, such as
`/{user}/{repo}/actions/runs/latest`. It renders latest run instead of
index=0 currently.
This commit is contained in:
FuXiaoHei 2024-08-10 08:40:41 +08:00 committed by GitHub
parent 42841aab59
commit df27846628
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 32 additions and 10 deletions

View File

@ -361,6 +361,19 @@ func GetRunByIndex(ctx context.Context, repoID, index int64) (*ActionRun, error)
return run, nil return run, nil
} }
func GetLatestRun(ctx context.Context, repoID int64) (*ActionRun, error) {
run := &ActionRun{
RepoID: repoID,
}
has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).Desc("index").Get(run)
if err != nil {
return nil, err
} else if !has {
return nil, fmt.Errorf("latest run with repo_id %d: %w", repoID, util.ErrNotExist)
}
return run, nil
}
func GetWorkflowLatestRun(ctx context.Context, repoID int64, workflowFile, branch, event string) (*ActionRun, error) { func GetWorkflowLatestRun(ctx context.Context, repoID int64, workflowFile, branch, event string) (*ActionRun, error) {
var run ActionRun var run ActionRun
q := db.GetEngine(ctx).Where("repo_id=?", repoID). q := db.GetEngine(ctx).Where("repo_id=?", repoID).

View File

@ -33,9 +33,19 @@ import (
"xorm.io/builder" "xorm.io/builder"
) )
func getRunIndex(ctx *context_module.Context) int64 {
// if run param is "latest", get the latest run index
if ctx.PathParam("run") == "latest" {
if run, _ := actions_model.GetLatestRun(ctx, ctx.Repo.Repository.ID); run != nil {
return run.Index
}
}
return ctx.PathParamInt64("run")
}
func View(ctx *context_module.Context) { func View(ctx *context_module.Context) {
ctx.Data["PageIsActions"] = true ctx.Data["PageIsActions"] = true
runIndex := ctx.PathParamInt64("run") runIndex := getRunIndex(ctx)
jobIndex := ctx.PathParamInt64("job") jobIndex := ctx.PathParamInt64("job")
ctx.Data["RunIndex"] = runIndex ctx.Data["RunIndex"] = runIndex
ctx.Data["JobIndex"] = jobIndex ctx.Data["JobIndex"] = jobIndex
@ -130,7 +140,7 @@ type ViewStepLogLine struct {
func ViewPost(ctx *context_module.Context) { func ViewPost(ctx *context_module.Context) {
req := web.GetForm(ctx).(*ViewRequest) req := web.GetForm(ctx).(*ViewRequest)
runIndex := ctx.PathParamInt64("run") runIndex := getRunIndex(ctx)
jobIndex := ctx.PathParamInt64("job") jobIndex := ctx.PathParamInt64("job")
current, jobs := getRunJobs(ctx, runIndex, jobIndex) current, jobs := getRunJobs(ctx, runIndex, jobIndex)
@ -289,7 +299,7 @@ func ViewPost(ctx *context_module.Context) {
// Rerun will rerun jobs in the given run // Rerun will rerun jobs in the given run
// If jobIndexStr is a blank string, it means rerun all jobs // If jobIndexStr is a blank string, it means rerun all jobs
func Rerun(ctx *context_module.Context) { func Rerun(ctx *context_module.Context) {
runIndex := ctx.PathParamInt64("run") runIndex := getRunIndex(ctx)
jobIndexStr := ctx.PathParam("job") jobIndexStr := ctx.PathParam("job")
var jobIndex int64 var jobIndex int64
if jobIndexStr != "" { if jobIndexStr != "" {
@ -379,7 +389,7 @@ func rerunJob(ctx *context_module.Context, job *actions_model.ActionRunJob, shou
} }
func Logs(ctx *context_module.Context) { func Logs(ctx *context_module.Context) {
runIndex := ctx.PathParamInt64("run") runIndex := getRunIndex(ctx)
jobIndex := ctx.PathParamInt64("job") jobIndex := ctx.PathParamInt64("job")
job, _ := getRunJobs(ctx, runIndex, jobIndex) job, _ := getRunJobs(ctx, runIndex, jobIndex)
@ -428,7 +438,7 @@ func Logs(ctx *context_module.Context) {
} }
func Cancel(ctx *context_module.Context) { func Cancel(ctx *context_module.Context) {
runIndex := ctx.PathParamInt64("run") runIndex := getRunIndex(ctx)
_, jobs := getRunJobs(ctx, runIndex, -1) _, jobs := getRunJobs(ctx, runIndex, -1)
if ctx.Written() { if ctx.Written() {
@ -469,7 +479,7 @@ func Cancel(ctx *context_module.Context) {
} }
func Approve(ctx *context_module.Context) { func Approve(ctx *context_module.Context) {
runIndex := ctx.PathParamInt64("run") runIndex := getRunIndex(ctx)
current, jobs := getRunJobs(ctx, runIndex, -1) current, jobs := getRunJobs(ctx, runIndex, -1)
if ctx.Written() { if ctx.Written() {
@ -518,7 +528,6 @@ func getRunJobs(ctx *context_module.Context, runIndex, jobIndex int64) (*actions
return nil, nil return nil, nil
} }
run.Repo = ctx.Repo.Repository run.Repo = ctx.Repo.Repository
jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID) jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error()) ctx.Error(http.StatusInternalServerError, err.Error())
@ -550,7 +559,7 @@ type ArtifactsViewItem struct {
} }
func ArtifactsView(ctx *context_module.Context) { func ArtifactsView(ctx *context_module.Context) {
runIndex := ctx.PathParamInt64("run") runIndex := getRunIndex(ctx)
run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex) run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)
if err != nil { if err != nil {
if errors.Is(err, util.ErrNotExist) { if errors.Is(err, util.ErrNotExist) {
@ -588,7 +597,7 @@ func ArtifactsDeleteView(ctx *context_module.Context) {
return return
} }
runIndex := ctx.PathParamInt64("run") runIndex := getRunIndex(ctx)
artifactName := ctx.PathParam("artifact_name") artifactName := ctx.PathParam("artifact_name")
run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex) run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)
@ -606,7 +615,7 @@ func ArtifactsDeleteView(ctx *context_module.Context) {
} }
func ArtifactsDownloadView(ctx *context_module.Context) { func ArtifactsDownloadView(ctx *context_module.Context) {
runIndex := ctx.PathParamInt64("run") runIndex := getRunIndex(ctx)
artifactName := ctx.PathParam("artifact_name") artifactName := ctx.PathParam("artifact_name")
run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex) run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)