Skip to content

Commit df27846

Browse files
authored
Show latest run when visit /run/latest (#31808)
Proposal from #27911 (comment) When visit latest run path, such as `/{user}/{repo}/actions/runs/latest`. It renders latest run instead of index=0 currently.
1 parent 42841aa commit df27846

File tree

2 files changed

+32
-10
lines changed

2 files changed

+32
-10
lines changed

models/actions/run.go

+13
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,19 @@ func GetRunByIndex(ctx context.Context, repoID, index int64) (*ActionRun, error)
361361
return run, nil
362362
}
363363

364+
func GetLatestRun(ctx context.Context, repoID int64) (*ActionRun, error) {
365+
run := &ActionRun{
366+
RepoID: repoID,
367+
}
368+
has, err := db.GetEngine(ctx).Where("repo_id=?", repoID).Desc("index").Get(run)
369+
if err != nil {
370+
return nil, err
371+
} else if !has {
372+
return nil, fmt.Errorf("latest run with repo_id %d: %w", repoID, util.ErrNotExist)
373+
}
374+
return run, nil
375+
}
376+
364377
func GetWorkflowLatestRun(ctx context.Context, repoID int64, workflowFile, branch, event string) (*ActionRun, error) {
365378
var run ActionRun
366379
q := db.GetEngine(ctx).Where("repo_id=?", repoID).

routers/web/repo/actions/view.go

+19-10
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,19 @@ import (
3333
"xorm.io/builder"
3434
)
3535

36+
func getRunIndex(ctx *context_module.Context) int64 {
37+
// if run param is "latest", get the latest run index
38+
if ctx.PathParam("run") == "latest" {
39+
if run, _ := actions_model.GetLatestRun(ctx, ctx.Repo.Repository.ID); run != nil {
40+
return run.Index
41+
}
42+
}
43+
return ctx.PathParamInt64("run")
44+
}
45+
3646
func View(ctx *context_module.Context) {
3747
ctx.Data["PageIsActions"] = true
38-
runIndex := ctx.PathParamInt64("run")
48+
runIndex := getRunIndex(ctx)
3949
jobIndex := ctx.PathParamInt64("job")
4050
ctx.Data["RunIndex"] = runIndex
4151
ctx.Data["JobIndex"] = jobIndex
@@ -130,7 +140,7 @@ type ViewStepLogLine struct {
130140

131141
func ViewPost(ctx *context_module.Context) {
132142
req := web.GetForm(ctx).(*ViewRequest)
133-
runIndex := ctx.PathParamInt64("run")
143+
runIndex := getRunIndex(ctx)
134144
jobIndex := ctx.PathParamInt64("job")
135145

136146
current, jobs := getRunJobs(ctx, runIndex, jobIndex)
@@ -289,7 +299,7 @@ func ViewPost(ctx *context_module.Context) {
289299
// Rerun will rerun jobs in the given run
290300
// If jobIndexStr is a blank string, it means rerun all jobs
291301
func Rerun(ctx *context_module.Context) {
292-
runIndex := ctx.PathParamInt64("run")
302+
runIndex := getRunIndex(ctx)
293303
jobIndexStr := ctx.PathParam("job")
294304
var jobIndex int64
295305
if jobIndexStr != "" {
@@ -379,7 +389,7 @@ func rerunJob(ctx *context_module.Context, job *actions_model.ActionRunJob, shou
379389
}
380390

381391
func Logs(ctx *context_module.Context) {
382-
runIndex := ctx.PathParamInt64("run")
392+
runIndex := getRunIndex(ctx)
383393
jobIndex := ctx.PathParamInt64("job")
384394

385395
job, _ := getRunJobs(ctx, runIndex, jobIndex)
@@ -428,7 +438,7 @@ func Logs(ctx *context_module.Context) {
428438
}
429439

430440
func Cancel(ctx *context_module.Context) {
431-
runIndex := ctx.PathParamInt64("run")
441+
runIndex := getRunIndex(ctx)
432442

433443
_, jobs := getRunJobs(ctx, runIndex, -1)
434444
if ctx.Written() {
@@ -469,7 +479,7 @@ func Cancel(ctx *context_module.Context) {
469479
}
470480

471481
func Approve(ctx *context_module.Context) {
472-
runIndex := ctx.PathParamInt64("run")
482+
runIndex := getRunIndex(ctx)
473483

474484
current, jobs := getRunJobs(ctx, runIndex, -1)
475485
if ctx.Written() {
@@ -518,7 +528,6 @@ func getRunJobs(ctx *context_module.Context, runIndex, jobIndex int64) (*actions
518528
return nil, nil
519529
}
520530
run.Repo = ctx.Repo.Repository
521-
522531
jobs, err := actions_model.GetRunJobsByRunID(ctx, run.ID)
523532
if err != nil {
524533
ctx.Error(http.StatusInternalServerError, err.Error())
@@ -550,7 +559,7 @@ type ArtifactsViewItem struct {
550559
}
551560

552561
func ArtifactsView(ctx *context_module.Context) {
553-
runIndex := ctx.PathParamInt64("run")
562+
runIndex := getRunIndex(ctx)
554563
run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)
555564
if err != nil {
556565
if errors.Is(err, util.ErrNotExist) {
@@ -588,7 +597,7 @@ func ArtifactsDeleteView(ctx *context_module.Context) {
588597
return
589598
}
590599

591-
runIndex := ctx.PathParamInt64("run")
600+
runIndex := getRunIndex(ctx)
592601
artifactName := ctx.PathParam("artifact_name")
593602

594603
run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)
@@ -606,7 +615,7 @@ func ArtifactsDeleteView(ctx *context_module.Context) {
606615
}
607616

608617
func ArtifactsDownloadView(ctx *context_module.Context) {
609-
runIndex := ctx.PathParamInt64("run")
618+
runIndex := getRunIndex(ctx)
610619
artifactName := ctx.PathParam("artifact_name")
611620

612621
run, err := actions_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)

0 commit comments

Comments
 (0)