From 3d4d44dadf31769c5a3fc3ed3f768afa4f1c7dcd Mon Sep 17 00:00:00 2001 From: Jason Song Date: Fri, 21 Oct 2022 15:37:10 +0800 Subject: [PATCH] fix: use index of run --- models/bots/run.go | 45 +++++++++++++++++++------ models/bots/run_job.go | 2 +- routers/web/dev/buildview.go | 42 ++++++++--------------- routers/web/web.go | 15 +++++---- templates/dev/buildview.tmpl | 2 +- web_src/js/components/RepoBuildView.vue | 19 +++++++---- 6 files changed, 70 insertions(+), 55 deletions(-) diff --git a/models/bots/run.go b/models/bots/run.go index c2df288632..c3f10f7870 100644 --- a/models/bots/run.go +++ b/models/bots/run.go @@ -52,32 +52,32 @@ func (Run) TableName() string { } func (run *Run) HTMLURL() string { - return fmt.Sprintf("%s/builds/run/%d", run.Repo.HTMLURL(), run.Index) + return fmt.Sprintf("%s/builds/runs/%d", run.Repo.HTMLURL(), run.Index) } // LoadAttributes load Repo TriggerUser if not loaded -func (r *Run) LoadAttributes(ctx context.Context) error { - if r == nil { +func (run *Run) LoadAttributes(ctx context.Context) error { + if run == nil { return nil } - if r.Repo == nil { - repo, err := repo_model.GetRepositoryByIDCtx(ctx, r.RepoID) + if run.Repo == nil { + repo, err := repo_model.GetRepositoryByIDCtx(ctx, run.RepoID) if err != nil { return err } - r.Repo = repo + run.Repo = repo } - if err := r.Repo.LoadAttributes(ctx); err != nil { + if err := run.Repo.LoadAttributes(ctx); err != nil { return err } - if r.TriggerUser == nil { - u, err := user_model.GetUserByIDCtx(ctx, r.TriggerUserID) + if run.TriggerUser == nil { + u, err := user_model.GetUserByIDCtx(ctx, run.TriggerUserID) if err != nil { return err } - r.TriggerUser = u + run.TriggerUser = u } return nil @@ -159,10 +159,15 @@ func InsertRun(run *Run, jobs []*jobparser.SingleWorkflow) error { // ErrRunNotExist represents an error for bot run not exist type ErrRunNotExist struct { - ID int64 + ID int64 + RepoID int64 + Index int64 } func (err ErrRunNotExist) Error() string { + if err.RepoID > 0 { + return fmt.Sprintf("run repe_id [%d] index [%d] is not exist", err.RepoID, err.Index) + } return fmt.Sprintf("run [%d] is not exist", err.ID) } @@ -180,6 +185,24 @@ func GetRunByID(ctx context.Context, id int64) (*Run, error) { return &run, nil } +func GetRunByIndex(ctx context.Context, repoID, index int64) (*Run, error) { + run := &Run{ + RepoID: repoID, + Index: index, + } + has, err := db.GetEngine(ctx).Get(run) + if err != nil { + return nil, err + } else if !has { + return nil, ErrRunNotExist{ + RepoID: repoID, + Index: index, + } + } + + return run, nil +} + func UpdateRun(ctx context.Context, run *Run, cols ...string) error { sess := db.GetEngine(ctx).ID(run.ID) if len(cols) > 0 { diff --git a/models/bots/run_job.go b/models/bots/run_job.go index 103b9f53ce..f4df80bfdd 100644 --- a/models/bots/run_job.go +++ b/models/bots/run_job.go @@ -84,7 +84,7 @@ func GetRunJobByID(ctx context.Context, id int64) (*RunJob, error) { func GetRunJobsByRunID(ctx context.Context, runID int64) ([]*RunJob, error) { var jobs []*RunJob - if err := db.GetEngine(ctx).Where("run_id=?", runID).Find(&jobs); err != nil { + if err := db.GetEngine(ctx).Where("run_id=?", runID).OrderBy("id").Find(&jobs); err != nil { return nil, err } return jobs, nil diff --git a/routers/web/dev/buildview.go b/routers/web/dev/buildview.go index 270125e027..571cb1d66b 100644 --- a/routers/web/dev/buildview.go +++ b/routers/web/dev/buildview.go @@ -13,20 +13,8 @@ import ( ) func BuildView(ctx *context.Context) { - runID := ctx.ParamsInt64("runid") - ctx.Data["RunID"] = runID - jobID := ctx.ParamsInt64("jobid") - if jobID <= 0 { - runJobs, err := bots_model.GetRunJobsByRunID(ctx, runID) - if err != nil { - return - } - if len(runJobs) <= 0 { - return - } - jobID = runJobs[0].ID - } - ctx.Data["JobID"] = jobID + ctx.Data["RunIndex"] = ctx.ParamsInt64("run") + ctx.Data["JobIndex"] = ctx.ParamsInt64("job") ctx.HTML(http.StatusOK, "dev/buildview") } @@ -42,7 +30,8 @@ type BuildViewRequest struct { type BuildViewResponse struct { StateData struct { BuildInfo struct { - Title string `json:"title"` + HTMLURL string `json:"htmlurl"` + Title string `json:"title"` } `json:"buildInfo"` AllJobGroups []BuildViewGroup `json:"allJobGroups"` CurrentJobInfo struct { @@ -87,10 +76,10 @@ type BuildViewStepLogLine struct { func BuildViewPost(ctx *context.Context) { req := web.GetForm(ctx).(*BuildViewRequest) - runID := ctx.ParamsInt64("runid") - jobID := ctx.ParamsInt64("jobid") + runIndex := ctx.ParamsInt64("run") + jobIndex := ctx.ParamsInt64("job") - run, err := bots_model.GetRunByID(ctx, runID) + run, err := bots_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex) if err != nil { if _, ok := err.(bots_model.ErrRunNotExist); ok { ctx.Error(http.StatusNotFound, err.Error()) @@ -99,28 +88,25 @@ func BuildViewPost(ctx *context.Context) { ctx.Error(http.StatusInternalServerError, err.Error()) return } + run.Repo = ctx.Repo.Repository + jobs, err := bots_model.GetRunJobsByRunID(ctx, run.ID) if err != nil { ctx.Error(http.StatusInternalServerError, err.Error()) return } - var job *bots_model.RunJob - if jobID != 0 { - for _, v := range jobs { - if v.ID == jobID { - job = v - break - } - } - if job == nil { - ctx.Error(http.StatusNotFound, fmt.Sprintf("run %v has no job %v", runID, jobID)) + if jobIndex < 0 || jobIndex >= int64(len(jobs)) { + if len(jobs) == 0 { + ctx.Error(http.StatusNotFound, fmt.Sprintf("run %v has no job %v", runIndex, jobIndex)) return } } + job := jobs[jobIndex] resp := &BuildViewResponse{} resp.StateData.BuildInfo.Title = run.Title + resp.StateData.BuildInfo.HTMLURL = run.HTMLURL() respJobs := make([]*BuildViewJob, len(jobs)) for i, v := range jobs { diff --git a/routers/web/web.go b/routers/web/web.go index 9d91a1285c..9cd9dbaa37 100644 --- a/routers/web/web.go +++ b/routers/web/web.go @@ -1201,13 +1201,14 @@ func RegisterRoutes(m *web.Route) { m.Group("/builds", func() { m.Get("", builds.List) - m.Combo("/run/{runid}"). - Get(dev.BuildView). - Post(bindIgnErr(dev.BuildViewRequest{}), dev.BuildViewPost) - - m.Combo("/run/{runid}/jobs/{jobid}"). - Get(dev.BuildView). - Post(bindIgnErr(dev.BuildViewRequest{}), dev.BuildViewPost) + m.Group("/runs/{run}", func() { + m.Combo(""). + Get(dev.BuildView). + Post(bindIgnErr(dev.BuildViewRequest{}), dev.BuildViewPost) + m.Combo("/jobs/{job}"). + Get(dev.BuildView). + Post(bindIgnErr(dev.BuildViewRequest{}), dev.BuildViewPost) + }) }, reqRepoBuildsReader, builds.MustEnableBuilds) m.Group("/wiki", func() { diff --git a/templates/dev/buildview.tmpl b/templates/dev/buildview.tmpl index 044adff7c0..cc0e4257f2 100644 --- a/templates/dev/buildview.tmpl +++ b/templates/dev/buildview.tmpl @@ -1,6 +1,6 @@ {{template "base/head" .}} -
+
diff --git a/web_src/js/components/RepoBuildView.vue b/web_src/js/components/RepoBuildView.vue index 6e6fee68b2..b6a3259a68 100644 --- a/web_src/js/components/RepoBuildView.vue +++ b/web_src/js/components/RepoBuildView.vue @@ -10,7 +10,7 @@ {{ jobGroup.summary }}
- + @@ -72,14 +72,16 @@ import {SvgIcon} from '../svg.js'; import Vue, {createApp} from 'vue'; import AnsiToHTML from `ansi-to-html`; +const {csrfToken} = window.config; + const sfc = { name: 'RepoBuildView', components: { SvgIcon, }, props: { - runId: Number, - jobId: Number, + runIndex: Number, + jobIndex: Number, }, data() { @@ -255,9 +257,12 @@ const sfc = { }, async fetchJobData(reqData) { - const resp = await fetch(`/dev/buildview/runs/${this.runId}/jobs/${this.jobId}`, { // FIXME: hard code path + const resp = await fetch(``, { method: 'POST', - headers: { 'Content-Type': 'application/json' }, + headers: { + 'Content-Type': 'application/json', + 'X-Csrf-Token': csrfToken, + }, body: JSON.stringify(reqData), }); return await resp.json(); @@ -312,8 +317,8 @@ export function initRepositoryBuildView() { if (!el) return; const view = createApp(sfc, { - jobId: el.getAttribute("job-id"), - runId: el.getAttribute("run-id"), + jobIndex: el.getAttribute("job-index"), + runIndex: el.getAttribute("run-index"), }); view.mount(el); }