Skip to content
Merged
6 changes: 3 additions & 3 deletions models/actions/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,14 +70,14 @@ func (run *ActionRun) HTMLURL() string {
if run.Repo == nil {
return ""
}
return fmt.Sprintf("%s/actions/runs/%d", run.Repo.HTMLURL(), run.Index)
return fmt.Sprintf("%s/actions/runs/%d", run.Repo.HTMLURL(), run.ID)
}

func (run *ActionRun) Link() string {
if run.Repo == nil {
return ""
}
return fmt.Sprintf("%s/actions/runs/%d", run.Repo.Link(), run.Index)
return fmt.Sprintf("%s/actions/runs/%d", run.Repo.Link(), run.ID)
}

func (run *ActionRun) WorkflowLink() string {
Expand Down Expand Up @@ -299,7 +299,7 @@ func CancelJobs(ctx context.Context, jobs []*ActionRunJob) ([]*ActionRunJob, err
if err := StopTask(ctx, job.TaskID, StatusCancelled); err != nil {
return cancelledJobs, err
}
updatedJob, err := GetRunJobByID(ctx, job.ID)
updatedJob, err := GetRunJobByRunAndID(ctx, job.RunID, job.ID)
if err != nil {
return cancelledJobs, fmt.Errorf("get job: %w", err)
}
Expand Down
20 changes: 16 additions & 4 deletions models/actions/run_job.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,13 +118,25 @@ func (job *ActionRunJob) ParseJob() (*jobparser.Job, error) {
return workflowJob, nil
}

func GetRunJobByID(ctx context.Context, id int64) (*ActionRunJob, error) {
func GetRunJobByRepoAndID(ctx context.Context, repoID, jobID int64) (*ActionRunJob, error) {
var job ActionRunJob
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&job)
has, err := db.GetEngine(ctx).Where("id=? AND repo_id=?", jobID, repoID).Get(&job)
if err != nil {
return nil, err
} else if !has {
return nil, fmt.Errorf("run job with id %d: %w", id, util.ErrNotExist)
return nil, fmt.Errorf("run job with id %d: %w", jobID, util.ErrNotExist)
}

return &job, nil
}

func GetRunJobByRunAndID(ctx context.Context, runID, jobID int64) (*ActionRunJob, error) {
var job ActionRunJob
has, err := db.GetEngine(ctx).Where("id=? AND run_id=?", jobID, runID).Get(&job)
if err != nil {
return nil, err
} else if !has {
return nil, fmt.Errorf("run job with id %d: %w", jobID, util.ErrNotExist)
}

return &job, nil
Expand Down Expand Up @@ -168,7 +180,7 @@ func UpdateRunJob(ctx context.Context, job *ActionRunJob, cond builder.Cond, col

if job.RunID == 0 {
var err error
if job, err = GetRunJobByID(ctx, job.ID); err != nil {
if job, err = GetRunJobByRepoAndID(ctx, job.RepoID, job.ID); err != nil {
return 0, err
}
}
Expand Down
4 changes: 3 additions & 1 deletion models/actions/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (task *ActionTask) GetRepoLink() string {

func (task *ActionTask) LoadJob(ctx context.Context) error {
if task.Job == nil {
job, err := GetRunJobByID(ctx, task.JobID)
job, err := GetRunJobByRepoAndID(ctx, task.RepoID, task.JobID)
if err != nil {
return err
}
Expand Down Expand Up @@ -388,6 +388,7 @@ func UpdateTaskByState(ctx context.Context, runnerID int64, state *runnerv1.Task
}
if _, err := UpdateRunJob(ctx, &ActionRunJob{
ID: task.JobID,
RepoID: task.RepoID,
Status: task.Status,
Stopped: task.Stopped,
}, nil); err != nil {
Expand Down Expand Up @@ -449,6 +450,7 @@ func StopTask(ctx context.Context, taskID int64, status Status) error {
task.Stopped = now
if _, err := UpdateRunJob(ctx, &ActionRunJob{
ID: task.JobID,
RepoID: task.RepoID,
Status: task.Status,
Stopped: task.Stopped,
}, nil); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion models/git/commit_status_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ func TestCommitStatusesHideActionsURL(t *testing.T) {
statuses := []*git_model.CommitStatus{
{
RepoID: repo.ID,
TargetURL: fmt.Sprintf("%s/jobs/%d", run.Link(), run.Index),
TargetURL: fmt.Sprintf("%s/jobs/%d", run.Link(), run.ID),
},
{
RepoID: repo.ID,
Expand Down
8 changes: 1 addition & 7 deletions routers/api/v1/repo/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,15 +1041,9 @@ func ActionsDispatchWorkflow(ctx *context.APIContext) {
return
}

workflowRun, err := actions_model.GetRunByRepoAndID(ctx, ctx.Repo.Repository.ID, runID)
if err != nil {
ctx.APIErrorInternal(err)
return
}

ctx.JSON(http.StatusOK, &api.RunDetails{
WorkflowRunID: runID,
HTMLURL: fmt.Sprintf("%s/actions/runs/%d", ctx.Repo.Repository.HTMLURL(ctx), workflowRun.Index),
HTMLURL: fmt.Sprintf("%s/actions/runs/%d", ctx.Repo.Repository.HTMLURL(ctx), runID),
RunURL: fmt.Sprintf("%s/actions/runs/%d", ctx.Repo.Repository.APIURL(), runID),
})
}
Expand Down
2 changes: 1 addition & 1 deletion routers/api/v1/repo/actions_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func DownloadActionsRunJobLogs(ctx *context.APIContext) {
// "$ref": "#/responses/notFound"

jobID := ctx.PathParamInt64("job_id")
curJob, err := actions_model.GetRunJobByID(ctx, jobID)
curJob, err := actions_model.GetRunJobByRepoAndID(ctx, ctx.Repo.Repository.ID, jobID)
if err != nil {
ctx.APIErrorInternal(err)
return
Expand Down
15 changes: 6 additions & 9 deletions routers/common/actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,15 @@ import (
"code.gitea.io/gitea/services/context"
)

func DownloadActionsRunJobLogsWithIndex(ctx *context.Base, ctxRepo *repo_model.Repository, runID, jobIndex int64) error {
runJobs, err := actions_model.GetRunJobsByRunID(ctx, runID)
func DownloadActionsRunJobLogsWithID(ctx *context.Base, ctxRepo *repo_model.Repository, runID, jobID int64) error {
job, err := actions_model.GetRunJobByRunAndID(ctx, runID, jobID)
if err != nil {
return fmt.Errorf("GetRunJobsByRunID: %w", err)
return err
}
if err = runJobs.LoadRepos(ctx); err != nil {
return fmt.Errorf("LoadRepos: %w", err)
if err := job.LoadRepo(ctx); err != nil {
return fmt.Errorf("LoadRepo: %w", err)
}
if jobIndex < 0 || jobIndex >= int64(len(runJobs)) {
return util.NewNotExistErrorf("job index is out of range: %d", jobIndex)
}
return DownloadActionsRunJobLogs(ctx, ctxRepo, runJobs[jobIndex])
return DownloadActionsRunJobLogs(ctx, ctxRepo, job)
}

func DownloadActionsRunJobLogs(ctx *context.Base, ctxRepo *repo_model.Repository, curJob *actions_model.ActionRunJob) error {
Expand Down
4 changes: 2 additions & 2 deletions routers/web/devtest/mock_actions.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ func generateMockStepsLog(logCur actions.LogCursor, opts generateMockStepsLogOpt
}

func MockActionsView(ctx *context.Context) {
ctx.Data["RunIndex"] = ctx.PathParam("run")
ctx.Data["JobIndex"] = ctx.PathParam("job")
ctx.Data["RunID"] = ctx.PathParam("run")
ctx.Data["JobID"] = ctx.PathParam("job")
ctx.HTML(http.StatusOK, "devtest/repo-action-view")
}

Expand Down
Loading
Loading