Add builds UI
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
@@ -40,7 +41,7 @@ type Runner struct {
|
||||
}
|
||||
|
||||
func (Runner) TableName() string {
|
||||
return "actions_runner"
|
||||
return "bots_runner"
|
||||
}
|
||||
|
||||
func init() {
|
||||
|
||||
@@ -5,16 +5,26 @@
|
||||
package bots
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"code.gitea.io/gitea/models/db"
|
||||
repo_model "code.gitea.io/gitea/models/repo"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
"code.gitea.io/gitea/models/webhook"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
"code.gitea.io/gitea/modules/util"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"xorm.io/builder"
|
||||
)
|
||||
|
||||
func init() {
|
||||
db.RegisterModel(new(Task))
|
||||
db.RegisterModel(new(BuildIndex))
|
||||
}
|
||||
|
||||
// TaskStatus represents a task status
|
||||
type TaskStatus int
|
||||
|
||||
@@ -31,27 +41,70 @@ const (
|
||||
|
||||
// Task represnets bot tasks
|
||||
type Task struct {
|
||||
ID int64
|
||||
UUID string `xorm:"CHAR(36)"`
|
||||
RepoID int64 `xorm:"index"`
|
||||
TriggerUserID int64
|
||||
Ref string
|
||||
CommitSHA string
|
||||
Event webhook.HookEventType
|
||||
Token string // token for this task
|
||||
Grant string // permissions for this task
|
||||
EventPayload string `xorm:"LONGTEXT"`
|
||||
RunnerID int64 `xorm:"index"`
|
||||
Status TaskStatus `xorm:"index"`
|
||||
Created timeutil.TimeStamp `xorm:"created"`
|
||||
StartTime timeutil.TimeStamp
|
||||
EndTime timeutil.TimeStamp
|
||||
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||
ID int64
|
||||
Title string
|
||||
UUID string `xorm:"CHAR(36)"`
|
||||
Index int64 `xorm:"index unique(repo_index)"`
|
||||
RepoID int64 `xorm:"index unique(repo_index)"`
|
||||
TriggerUserID int64
|
||||
TriggerUser *user_model.User `xorm:"-"`
|
||||
Ref string
|
||||
CommitSHA string
|
||||
Event webhook.HookEventType
|
||||
Token string // token for this task
|
||||
Grant string // permissions for this task
|
||||
EventPayload string `xorm:"LONGTEXT"`
|
||||
RunnerID int64 `xorm:"index"`
|
||||
Status TaskStatus `xorm:"index"`
|
||||
WorkflowsStatuses map[string]map[string]TaskStatus `xorm:"LONGTEXT"`
|
||||
Created timeutil.TimeStamp `xorm:"created"`
|
||||
StartTime timeutil.TimeStamp
|
||||
EndTime timeutil.TimeStamp
|
||||
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||
}
|
||||
|
||||
func (t *Task) IsPending() bool {
|
||||
return t.Status == TaskPending || t.Status == TaskAssigned
|
||||
}
|
||||
|
||||
func (t *Task) IsRunning() bool {
|
||||
return t.Status == TaskRunning
|
||||
}
|
||||
|
||||
func (t *Task) IsFailed() bool {
|
||||
return t.Status == TaskFailed || t.Status == TaskCanceled || t.Status == TaskTimeout
|
||||
}
|
||||
|
||||
func (t *Task) IsSuccess() bool {
|
||||
return t.Status == TaskFinished
|
||||
}
|
||||
|
||||
// TableName represents a bot task
|
||||
func (Task) TableName() string {
|
||||
return "actions_task"
|
||||
return "bots_task"
|
||||
}
|
||||
|
||||
func (t *Task) HTMLURL() string {
|
||||
return fmt.Sprintf("")
|
||||
}
|
||||
|
||||
func updateRepoBuildsNumbers(ctx context.Context, repo *repo_model.Repository) error {
|
||||
_, err := db.GetEngine(ctx).ID(repo.ID).
|
||||
SetExpr("num_builds",
|
||||
builder.Select("count(*)").From("bots_task").
|
||||
Where(builder.Eq{"repo_id": repo.ID}),
|
||||
).
|
||||
SetExpr("num_closed_builds",
|
||||
builder.Select("count(*)").From("bots_task").
|
||||
Where(builder.Eq{
|
||||
"repo_id": repo.ID,
|
||||
}.And(
|
||||
builder.In("status", TaskFailed, TaskCanceled, TaskTimeout, TaskFinished),
|
||||
),
|
||||
),
|
||||
).
|
||||
Update(repo)
|
||||
return err
|
||||
}
|
||||
|
||||
// InsertTask inserts a bot task
|
||||
@@ -59,7 +112,27 @@ func InsertTask(t *Task) error {
|
||||
if t.UUID == "" {
|
||||
t.UUID = uuid.New().String()
|
||||
}
|
||||
return db.Insert(db.DefaultContext, t)
|
||||
index, err := db.GetNextResourceIndex("build_index", t.RepoID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
t.Index = index
|
||||
|
||||
ctx, commiter, err := db.TxContext()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer commiter.Close()
|
||||
|
||||
if err := db.Insert(ctx, t); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := updateRepoBuildsNumbers(ctx, &repo_model.Repository{ID: t.RepoID}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return commiter.Commit()
|
||||
}
|
||||
|
||||
// UpdateTask updates bot task
|
||||
@@ -70,7 +143,9 @@ func UpdateTask(t *Task, cols ...string) error {
|
||||
|
||||
// ErrTaskNotExist represents an error for bot task not exist
|
||||
type ErrTaskNotExist struct {
|
||||
UUID string
|
||||
RepoID int64
|
||||
Index int64
|
||||
UUID string
|
||||
}
|
||||
|
||||
func (err ErrTaskNotExist) Error() string {
|
||||
@@ -91,8 +166,8 @@ func GetTaskByUUID(taskUUID string) (*Task, error) {
|
||||
return &task, nil
|
||||
}
|
||||
|
||||
// GetCurTask return the task for the bot
|
||||
func GetCurTask(runnerID int64) (*Task, error) {
|
||||
// GetCurTaskByID return the task for the bot
|
||||
func GetCurTaskByID(runnerID int64) (*Task, error) {
|
||||
var tasks []Task
|
||||
// FIXME: for test, just return all tasks
|
||||
err := db.GetEngine(db.DefaultContext).Where("status=?", TaskPending).Find(&tasks)
|
||||
@@ -108,6 +183,31 @@ func GetCurTask(runnerID int64) (*Task, error) {
|
||||
return &tasks[0], err
|
||||
}
|
||||
|
||||
// GetCurTaskByUUID return the task for the bot
|
||||
func GetCurTaskByUUID(runnerUUID string) (*Task, error) {
|
||||
runner, err := GetRunnerByUUID(runnerUUID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return GetCurTaskByID(runner.ID)
|
||||
}
|
||||
|
||||
func GetTaskByRepoAndIndex(repoID, index int64) (*Task, error) {
|
||||
var task Task
|
||||
has, err := db.GetEngine(db.DefaultContext).Where("repo_id=?", repoID).
|
||||
And("`index` = ?", index).
|
||||
Get(&task)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else if !has {
|
||||
return nil, ErrTaskNotExist{
|
||||
RepoID: repoID,
|
||||
Index: index,
|
||||
}
|
||||
}
|
||||
return &task, nil
|
||||
}
|
||||
|
||||
// AssignTaskToRunner assign a task to a runner
|
||||
func AssignTaskToRunner(taskID int64, runnerID int64) error {
|
||||
cnt, err := db.GetEngine(db.DefaultContext).
|
||||
@@ -126,6 +226,41 @@ func AssignTaskToRunner(taskID int64, runnerID int64) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
type FindTaskOptions struct {
|
||||
db.ListOptions
|
||||
RepoID int64
|
||||
IsClosed util.OptionalBool
|
||||
}
|
||||
|
||||
func (opts FindTaskOptions) toConds() builder.Cond {
|
||||
cond := builder.NewCond()
|
||||
if opts.RepoID > 0 {
|
||||
cond = cond.And(builder.Eq{"repo_id": opts.RepoID})
|
||||
}
|
||||
if opts.IsClosed.IsTrue() {
|
||||
cond = cond.And(builder.Expr("status IN (?,?,?,?)", TaskCanceled, TaskFailed, TaskTimeout, TaskFinished))
|
||||
} else if opts.IsClosed.IsFalse() {
|
||||
cond = cond.And(builder.Expr("status IN (?,?,?)", TaskPending, TaskAssigned, TaskRunning))
|
||||
}
|
||||
return cond
|
||||
}
|
||||
|
||||
func FindTasks(opts FindTaskOptions) (TaskList, error) {
|
||||
sess := db.GetEngine(db.DefaultContext).Where(opts.toConds())
|
||||
if opts.ListOptions.PageSize > 0 {
|
||||
skip, take := opts.GetSkipTake()
|
||||
sess.Limit(take, skip)
|
||||
}
|
||||
var tasks []*Task
|
||||
return tasks, sess.Find(&tasks)
|
||||
}
|
||||
|
||||
func CountTasks(opts FindTaskOptions) (int64, error) {
|
||||
return db.GetEngine(db.DefaultContext).Table("bots_task").Where(opts.toConds()).Count()
|
||||
}
|
||||
|
||||
type TaskStage struct{}
|
||||
|
||||
type StageStep struct{}
|
||||
|
||||
type BuildIndex db.ResourceIndex
|
||||
|
||||
37
models/bots/task_list.go
Normal file
37
models/bots/task_list.go
Normal file
@@ -0,0 +1,37 @@
|
||||
// Copyright 2022 The Gitea Authors. All rights reserved.
|
||||
// Use of this source code is governed by a MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package bots
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/models/db"
|
||||
user_model "code.gitea.io/gitea/models/user"
|
||||
)
|
||||
|
||||
type TaskList []*Task
|
||||
|
||||
// GetUserIDs returns a slice of user's id
|
||||
func (tasks TaskList) GetUserIDs() []int64 {
|
||||
userIDsMap := make(map[int64]struct{})
|
||||
for _, task := range tasks {
|
||||
userIDsMap[task.TriggerUserID] = struct{}{}
|
||||
}
|
||||
userIDs := make([]int64, 0, len(userIDsMap))
|
||||
for userID := range userIDsMap {
|
||||
userIDs = append(userIDs, userID)
|
||||
}
|
||||
return userIDs
|
||||
}
|
||||
|
||||
func (tasks TaskList) LoadTriggerUser() error {
|
||||
userIDs := tasks.GetUserIDs()
|
||||
users := make(map[int64]*user_model.User, len(userIDs))
|
||||
if err := db.GetEngine(db.DefaultContext).In("id", userIDs).Find(&users); err != nil {
|
||||
return err
|
||||
}
|
||||
for _, task := range tasks {
|
||||
task.TriggerUser = users[task.TriggerUserID]
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -5,13 +5,14 @@
|
||||
package migrations
|
||||
|
||||
import (
|
||||
"code.gitea.io/gitea/models/db"
|
||||
"code.gitea.io/gitea/modules/timeutil"
|
||||
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
func addBotTables(x *xorm.Engine) error {
|
||||
type BotRunner struct {
|
||||
type BotsRunner struct {
|
||||
ID int64
|
||||
UUID string `xorm:"CHAR(36) UNIQUE"`
|
||||
Name string `xorm:"VARCHAR(32) UNIQUE"`
|
||||
@@ -24,29 +25,37 @@ func addBotTables(x *xorm.Engine) error {
|
||||
Base int // 0 native 1 docker 2 virtual machine
|
||||
RepoRange string // glob match which repositories could use this runner
|
||||
Token string
|
||||
LastOnline timeutil.TimeStamp
|
||||
LastOnline timeutil.TimeStamp `xorm:"index"`
|
||||
Created timeutil.TimeStamp `xorm:"created"`
|
||||
}
|
||||
|
||||
type BotTask struct {
|
||||
ID int64
|
||||
UUID string `xorm:"CHAR(36)"`
|
||||
RepoID int64 `xorm:"index"`
|
||||
Type string `xorm:"VARCHAR(16)"` // 0 commit 1 pullrequest
|
||||
Ref string
|
||||
CommitSHA string
|
||||
Event string
|
||||
Token string // token for this task
|
||||
Grant string // permissions for this task
|
||||
EventPayload string `xorm:"LONGTEXT"`
|
||||
RunnerID int64 `xorm:"index"`
|
||||
Status int
|
||||
Content string `xorm:"LONGTEXT"`
|
||||
Created timeutil.TimeStamp `xorm:"created"`
|
||||
StartTime timeutil.TimeStamp
|
||||
EndTime timeutil.TimeStamp
|
||||
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||
type BotsTask struct {
|
||||
ID int64
|
||||
Title string
|
||||
UUID string `xorm:"CHAR(36)"`
|
||||
Index int64 `xorm:"index unique(repo_index)"`
|
||||
RepoID int64 `xorm:"index unique(repo_index)"`
|
||||
TriggerUserID int64
|
||||
Ref string
|
||||
CommitSHA string
|
||||
Event string
|
||||
Token string // token for this task
|
||||
Grant string // permissions for this task
|
||||
EventPayload string `xorm:"LONGTEXT"`
|
||||
RunnerID int64 `xorm:"index"`
|
||||
Status int `xorm:"index"`
|
||||
Created timeutil.TimeStamp `xorm:"created"`
|
||||
StartTime timeutil.TimeStamp
|
||||
EndTime timeutil.TimeStamp
|
||||
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||
}
|
||||
|
||||
return x.Sync2(new(BotRunner), new(BotTask))
|
||||
type Repository struct {
|
||||
NumBuilds int `xorm:"NOT NULL DEFAULT 0"`
|
||||
NumClosedBuilds int `xorm:"NOT NULL DEFAULT 0"`
|
||||
}
|
||||
|
||||
type BuildIndex db.ResourceIndex
|
||||
|
||||
return x.Sync2(new(BotsRunner), new(BotsTask), new(Repository), new(BuildIndex))
|
||||
}
|
||||
|
||||
@@ -142,6 +142,9 @@ type Repository struct {
|
||||
NumProjects int `xorm:"NOT NULL DEFAULT 0"`
|
||||
NumClosedProjects int `xorm:"NOT NULL DEFAULT 0"`
|
||||
NumOpenProjects int `xorm:"-"`
|
||||
NumBuilds int `xorm:"NOT NULL DEFAULT 0"`
|
||||
NumClosedBuilds int `xorm:"NOT NULL DEFAULT 0"`
|
||||
NumOpenBuilds int `xorm:"-"`
|
||||
|
||||
IsPrivate bool `xorm:"INDEX"`
|
||||
IsEmpty bool `xorm:"INDEX"`
|
||||
@@ -234,6 +237,7 @@ func (repo *Repository) AfterLoad() {
|
||||
repo.NumOpenPulls = repo.NumPulls - repo.NumClosedPulls
|
||||
repo.NumOpenMilestones = repo.NumMilestones - repo.NumClosedMilestones
|
||||
repo.NumOpenProjects = repo.NumProjects - repo.NumClosedProjects
|
||||
repo.NumOpenBuilds = repo.NumBuilds - repo.NumClosedBuilds
|
||||
}
|
||||
|
||||
// LoadAttributes loads attributes of the repository.
|
||||
|
||||
@@ -175,7 +175,7 @@ func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
|
||||
r.Config = new(PullRequestsConfig)
|
||||
case unit.TypeIssues:
|
||||
r.Config = new(IssuesConfig)
|
||||
case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects, unit.TypePackages:
|
||||
case unit.TypeCode, unit.TypeReleases, unit.TypeWiki, unit.TypeProjects, unit.TypePackages, unit.TypeBuilds:
|
||||
fallthrough
|
||||
default:
|
||||
r.Config = new(UnitConfig)
|
||||
|
||||
@@ -28,6 +28,7 @@ const (
|
||||
TypeExternalTracker // 7 ExternalTracker
|
||||
TypeProjects // 8 Kanban board
|
||||
TypePackages // 9 Packages
|
||||
TypeBuilds // 10 Builds
|
||||
)
|
||||
|
||||
// Value returns integer value for unit type
|
||||
@@ -55,6 +56,8 @@ func (u Type) String() string {
|
||||
return "TypeProjects"
|
||||
case TypePackages:
|
||||
return "TypePackages"
|
||||
case TypeBuilds:
|
||||
return "TypeBuilds"
|
||||
}
|
||||
return fmt.Sprintf("Unknown Type %d", u)
|
||||
}
|
||||
@@ -78,6 +81,7 @@ var (
|
||||
TypeExternalTracker,
|
||||
TypeProjects,
|
||||
TypePackages,
|
||||
TypeBuilds,
|
||||
}
|
||||
|
||||
// DefaultRepoUnits contains the default unit types
|
||||
@@ -289,6 +293,15 @@ var (
|
||||
perm.AccessModeRead,
|
||||
}
|
||||
|
||||
UnitBuilds = Unit{
|
||||
TypeBuilds,
|
||||
"repo.builds",
|
||||
"/builds",
|
||||
"repo.builds.desc",
|
||||
7,
|
||||
perm.AccessModeOwner,
|
||||
}
|
||||
|
||||
// Units contains all the units
|
||||
Units = map[Type]Unit{
|
||||
TypeCode: UnitCode,
|
||||
@@ -300,6 +313,7 @@ var (
|
||||
TypeExternalWiki: UnitExternalWiki,
|
||||
TypeProjects: UnitProjects,
|
||||
TypePackages: UnitPackages,
|
||||
TypeBuilds: UnitBuilds,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user