This commit is contained in:
Lunny Xiao 2022-10-16 23:23:47 +08:00 committed by Jason Song
parent 482832da76
commit f75a4abc5d
6 changed files with 26 additions and 17 deletions

View File

@ -23,7 +23,7 @@ import (
// Run represents a run of a workflow file // Run represents a run of a workflow file
type Run struct { type Run struct {
ID int64 ID int64
Name string Title string
RepoID int64 `xorm:"index unique(repo_workflow_index)"` RepoID int64 `xorm:"index unique(repo_workflow_index)"`
Repo *repo_model.Repository `xorm:"-"` Repo *repo_model.Repository `xorm:"-"`
WorkflowID string `xorm:"index unique(repo_workflow_index)"` // the name of workflow file WorkflowID string `xorm:"index unique(repo_workflow_index)"` // the name of workflow file
@ -52,6 +52,10 @@ func (Run) TableName() string {
return "bots_run" return "bots_run"
} }
func (run *Run) HTMLURL() string {
return fmt.Sprintf("%s/builds/run/%d", run.Repo.HTMLURL(), run.Index)
}
// LoadAttributes load Repo TriggerUser if not loaded // LoadAttributes load Repo TriggerUser if not loaded
func (r *Run) LoadAttributes(ctx context.Context) error { func (r *Run) LoadAttributes(ctx context.Context) error {
if r == nil { if r == nil {

View File

@ -16,10 +16,10 @@ import (
type RunList []*Run type RunList []*Run
// GetUserIDs returns a slice of user's id // GetUserIDs returns a slice of user's id
func (builds RunList) GetUserIDs() []int64 { func (runs RunList) GetUserIDs() []int64 {
userIDsMap := make(map[int64]struct{}) userIDsMap := make(map[int64]struct{})
for _, build := range builds { for _, run := range runs {
userIDsMap[build.TriggerUserID] = struct{}{} userIDsMap[run.TriggerUserID] = struct{}{}
} }
userIDs := make([]int64, 0, len(userIDsMap)) userIDs := make([]int64, 0, len(userIDsMap))
for userID := range userIDsMap { for userID := range userIDsMap {
@ -28,14 +28,14 @@ func (builds RunList) GetUserIDs() []int64 {
return userIDs return userIDs
} }
func (builds RunList) LoadTriggerUser() error { func (runs RunList) LoadTriggerUser() error {
userIDs := builds.GetUserIDs() userIDs := runs.GetUserIDs()
users := make(map[int64]*user_model.User, len(userIDs)) users := make(map[int64]*user_model.User, len(userIDs))
if err := db.GetEngine(db.DefaultContext).In("id", userIDs).Find(&users); err != nil { if err := db.GetEngine(db.DefaultContext).In("id", userIDs).Find(&users); err != nil {
return err return err
} }
for _, task := range builds { for _, run := range runs {
task.TriggerUser = users[task.TriggerUserID] run.TriggerUser = users[run.TriggerUserID]
} }
return nil return nil
} }
@ -52,16 +52,14 @@ func (opts FindRunOptions) toConds() builder.Cond {
cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
} }
if opts.IsClosed.IsFalse() { if opts.IsClosed.IsFalse() {
} else if opts.IsClosed.IsTrue() { } else if opts.IsClosed.IsTrue() {
} }
return cond return cond
} }
func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, int64, error) { func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, int64, error) {
e := db.GetEngine(ctx).Where(opts.toConds()) e := db.GetEngine(ctx).Where(opts.toConds())
if opts.PageSize>0&&opts.Page >=1 { if opts.PageSize > 0 && opts.Page >= 1 {
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize) e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
} }
var runs RunList var runs RunList

View File

@ -50,7 +50,7 @@ func addBotTables(x *xorm.Engine) error {
type BotsRun struct { type BotsRun struct {
ID int64 ID int64
Name string Title string
RepoID int64 `xorm:"index unique(repo_workflow_index)"` RepoID int64 `xorm:"index unique(repo_workflow_index)"`
WorkflowID string `xorm:"index unique(repo_workflow_index)"` // the name of workflow file WorkflowID string `xorm:"index unique(repo_workflow_index)"` // the name of workflow file
Index int64 `xorm:"index unique(repo_workflow_index)"` // a unique number for each run of a particular workflow in a repository Index int64 `xorm:"index unique(repo_workflow_index)"` // a unique number for each run of a particular workflow in a repository

View File

@ -86,7 +86,7 @@ func notify(repo *repo_model.Repository, doer *user_model.User, payload, ref str
for id, content := range workflows { for id, content := range workflows {
run := bots_model.Run{ run := bots_model.Run{
Name: commit.Message(), Title: commit.Message(),
RepoID: repo.ID, RepoID: repo.ID,
WorkflowID: id, WorkflowID: id,
TriggerUserID: doer.ID, TriggerUserID: doer.ID,
@ -96,6 +96,9 @@ func notify(repo *repo_model.Repository, doer *user_model.User, payload, ref str
EventPayload: payload, EventPayload: payload,
Status: core.StatusPending, Status: core.StatusPending,
} }
if len(run.Title) > 255 {
run.Title = run.Title[:255]
}
jobs, err := jobparser.Parse(content) // TODO: parse with options jobs, err := jobparser.Parse(content) // TODO: parse with options
if err != nil { if err != nil {
log.Error("jobparser.Parse: %v", err) log.Error("jobparser.Parse: %v", err)

View File

@ -106,7 +106,7 @@ func BuildViewPost(ctx *context.Context) {
} }
resp := &BuildViewResponse{} resp := &BuildViewResponse{}
resp.StateData.BuildInfo.Title = run.Name resp.StateData.BuildInfo.Title = run.Title
respJobs := make([]*BuildViewJob, len(jobs)) respJobs := make([]*BuildViewJob, len(jobs))
for i, v := range jobs { for i, v := range jobs {

View File

@ -57,18 +57,22 @@ func List(ctx *context.Context) {
} else { } else {
opts.IsClosed = util.OptionalBoolFalse opts.IsClosed = util.OptionalBoolFalse
} }
builds, total, err := bots_model.FindRuns(ctx, opts) runs, total, err := bots_model.FindRuns(ctx, opts)
if err != nil { if err != nil {
ctx.Error(http.StatusInternalServerError, err.Error()) ctx.Error(http.StatusInternalServerError, err.Error())
return return
} }
if err := builds.LoadTriggerUser(); err != nil { for _, run := range runs {
run.Repo = ctx.Repo.Repository
}
if err := runs.LoadTriggerUser(); err != nil {
ctx.Error(http.StatusInternalServerError, err.Error()) ctx.Error(http.StatusInternalServerError, err.Error())
return return
} }
ctx.Data["Builds"] = builds ctx.Data["Builds"] = runs
pager := context.NewPagination(int(total), opts.PageSize, opts.Page, 5) pager := context.NewPagination(int(total), opts.PageSize, opts.Page, 5)
pager.SetDefaultParams(ctx) pager.SetDefaultParams(ctx)