mirror of
https://github.com/go-gitea/gitea.git
synced 2025-06-06 06:40:28 +02:00
refactor: rename tables to bot_*
This commit is contained in:
parent
3ac6bf3db4
commit
04d72d3500
@ -22,8 +22,8 @@ import (
|
|||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Run represents a run of a workflow file
|
// BotRun represents a run of a workflow file
|
||||||
type Run struct {
|
type BotRun struct {
|
||||||
ID int64
|
ID int64
|
||||||
Title string
|
Title string
|
||||||
RepoID int64 `xorm:"index unique(repo_index)"`
|
RepoID int64 `xorm:"index unique(repo_index)"`
|
||||||
@ -46,20 +46,16 @@ type Run struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
db.RegisterModel(new(Run))
|
db.RegisterModel(new(BotRun))
|
||||||
db.RegisterModel(new(RunIndex))
|
db.RegisterModel(new(BotRunIndex))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Run) TableName() string {
|
func (run *BotRun) HTMLURL() string {
|
||||||
return "bots_run"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (run *Run) HTMLURL() string {
|
|
||||||
return fmt.Sprintf("%s/bots/runs/%d", run.Repo.HTMLURL(), run.Index)
|
return fmt.Sprintf("%s/bots/runs/%d", run.Repo.HTMLURL(), run.Index)
|
||||||
}
|
}
|
||||||
|
|
||||||
// LoadAttributes load Repo TriggerUser if not loaded
|
// LoadAttributes load Repo TriggerUser if not loaded
|
||||||
func (run *Run) LoadAttributes(ctx context.Context) error {
|
func (run *BotRun) LoadAttributes(ctx context.Context) error {
|
||||||
if run == nil {
|
if run == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -86,7 +82,7 @@ func (run *Run) LoadAttributes(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (run *Run) TakeTime() time.Duration {
|
func (run *BotRun) TakeTime() time.Duration {
|
||||||
if run.Started == 0 {
|
if run.Started == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -98,7 +94,7 @@ func (run *Run) TakeTime() time.Duration {
|
|||||||
return time.Since(started).Truncate(time.Second)
|
return time.Since(started).Truncate(time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (run *Run) GetPushEventPayload() (*api.PushPayload, error) {
|
func (run *BotRun) GetPushEventPayload() (*api.PushPayload, error) {
|
||||||
if run.Event == webhook.HookEventPush {
|
if run.Event == webhook.HookEventPush {
|
||||||
var payload api.PushPayload
|
var payload api.PushPayload
|
||||||
if err := json.Unmarshal([]byte(run.EventPayload), &payload); err != nil {
|
if err := json.Unmarshal([]byte(run.EventPayload), &payload); err != nil {
|
||||||
@ -134,7 +130,7 @@ func updateRepoRunsNumbers(ctx context.Context, repo *repo_model.Repository) err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// InsertRun inserts a bot run
|
// InsertRun inserts a bot run
|
||||||
func InsertRun(run *Run, jobs []*jobparser.SingleWorkflow) error {
|
func InsertRun(run *BotRun, jobs []*jobparser.SingleWorkflow) error {
|
||||||
ctx, commiter, err := db.TxContext(db.DefaultContext)
|
ctx, commiter, err := db.TxContext(db.DefaultContext)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -167,7 +163,7 @@ func InsertRun(run *Run, jobs []*jobparser.SingleWorkflow) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
runJobs := make([]*RunJob, 0, len(jobs))
|
runJobs := make([]*BotRunJob, 0, len(jobs))
|
||||||
for _, v := range jobs {
|
for _, v := range jobs {
|
||||||
id, job := v.Job()
|
id, job := v.Job()
|
||||||
needs := job.Needs()
|
needs := job.Needs()
|
||||||
@ -177,7 +173,7 @@ func InsertRun(run *Run, jobs []*jobparser.SingleWorkflow) error {
|
|||||||
if len(needs) > 0 {
|
if len(needs) > 0 {
|
||||||
status = StatusBlocked
|
status = StatusBlocked
|
||||||
}
|
}
|
||||||
runJobs = append(runJobs, &RunJob{
|
runJobs = append(runJobs, &BotRunJob{
|
||||||
RunID: run.ID,
|
RunID: run.ID,
|
||||||
RepoID: run.RepoID,
|
RepoID: run.RepoID,
|
||||||
OwnerID: run.OwnerID,
|
OwnerID: run.OwnerID,
|
||||||
@ -212,8 +208,8 @@ func (err ErrRunNotExist) Error() string {
|
|||||||
return fmt.Sprintf("run [%d] is not exist", err.ID)
|
return fmt.Sprintf("run [%d] is not exist", err.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRunByID(ctx context.Context, id int64) (*Run, error) {
|
func GetRunByID(ctx context.Context, id int64) (*BotRun, error) {
|
||||||
var run Run
|
var run BotRun
|
||||||
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&run)
|
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&run)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -226,8 +222,8 @@ func GetRunByID(ctx context.Context, id int64) (*Run, error) {
|
|||||||
return &run, nil
|
return &run, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRunByIndex(ctx context.Context, repoID, index int64) (*Run, error) {
|
func GetRunByIndex(ctx context.Context, repoID, index int64) (*BotRun, error) {
|
||||||
run := &Run{
|
run := &BotRun{
|
||||||
RepoID: repoID,
|
RepoID: repoID,
|
||||||
Index: index,
|
Index: index,
|
||||||
}
|
}
|
||||||
@ -244,7 +240,7 @@ func GetRunByIndex(ctx context.Context, repoID, index int64) (*Run, error) {
|
|||||||
return run, nil
|
return run, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateRun(ctx context.Context, run *Run, cols ...string) error {
|
func UpdateRun(ctx context.Context, run *BotRun, cols ...string) error {
|
||||||
sess := db.GetEngine(ctx).ID(run.ID)
|
sess := db.GetEngine(ctx).ID(run.ID)
|
||||||
if len(cols) > 0 {
|
if len(cols) > 0 {
|
||||||
sess.Cols(cols...)
|
sess.Cols(cols...)
|
||||||
@ -273,8 +269,4 @@ func UpdateRun(ctx context.Context, run *Run, cols ...string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
type RunIndex db.ResourceIndex
|
type BotRunIndex db.ResourceIndex
|
||||||
|
|
||||||
func (RunIndex) TableName() string {
|
|
||||||
return "bots_run_index"
|
|
||||||
}
|
|
||||||
|
@ -16,14 +16,14 @@ import (
|
|||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
// RunJob represents a job of a run
|
// BotRunJob represents a job of a run
|
||||||
type RunJob struct {
|
type BotRunJob struct {
|
||||||
ID int64
|
ID int64
|
||||||
RunID int64 `xorm:"index"`
|
RunID int64 `xorm:"index"`
|
||||||
Run *Run `xorm:"-"`
|
Run *BotRun `xorm:"-"`
|
||||||
RepoID int64 `xorm:"index"`
|
RepoID int64 `xorm:"index"`
|
||||||
OwnerID int64 `xorm:"index"`
|
OwnerID int64 `xorm:"index"`
|
||||||
CommitSHA string `xorm:"index"`
|
CommitSHA string `xorm:"index"`
|
||||||
IsForkPullRequest bool
|
IsForkPullRequest bool
|
||||||
Name string
|
Name string
|
||||||
Attempt int64
|
Attempt int64
|
||||||
@ -40,14 +40,10 @@ type RunJob struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
db.RegisterModel(new(RunJob))
|
db.RegisterModel(new(BotRunJob))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (RunJob) TableName() string {
|
func (job *BotRunJob) TakeTime() time.Duration {
|
||||||
return "bots_run_job"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (job *RunJob) TakeTime() time.Duration {
|
|
||||||
if job.Started == 0 {
|
if job.Started == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -59,7 +55,7 @@ func (job *RunJob) TakeTime() time.Duration {
|
|||||||
return time.Since(started).Truncate(time.Second)
|
return time.Since(started).Truncate(time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (job *RunJob) LoadRun(ctx context.Context) error {
|
func (job *BotRunJob) LoadRun(ctx context.Context) error {
|
||||||
if job.Run == nil {
|
if job.Run == nil {
|
||||||
run, err := GetRunByID(ctx, job.RunID)
|
run, err := GetRunByID(ctx, job.RunID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -71,7 +67,7 @@ func (job *RunJob) LoadRun(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LoadAttributes load Run if not loaded
|
// LoadAttributes load Run if not loaded
|
||||||
func (job *RunJob) LoadAttributes(ctx context.Context) error {
|
func (job *BotRunJob) LoadAttributes(ctx context.Context) error {
|
||||||
if job == nil {
|
if job == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -92,8 +88,8 @@ func (err ErrRunJobNotExist) Error() string {
|
|||||||
return fmt.Sprintf("run job [%d] is not exist", err.ID)
|
return fmt.Sprintf("run job [%d] is not exist", err.ID)
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRunJobByID(ctx context.Context, id int64) (*RunJob, error) {
|
func GetRunJobByID(ctx context.Context, id int64) (*BotRunJob, error) {
|
||||||
var job RunJob
|
var job BotRunJob
|
||||||
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&job)
|
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&job)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -106,15 +102,15 @@ func GetRunJobByID(ctx context.Context, id int64) (*RunJob, error) {
|
|||||||
return &job, nil
|
return &job, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRunJobsByRunID(ctx context.Context, runID int64) ([]*RunJob, error) {
|
func GetRunJobsByRunID(ctx context.Context, runID int64) ([]*BotRunJob, error) {
|
||||||
var jobs []*RunJob
|
var jobs []*BotRunJob
|
||||||
if err := db.GetEngine(ctx).Where("run_id=?", runID).OrderBy("id").Find(&jobs); err != nil {
|
if err := db.GetEngine(ctx).Where("run_id=?", runID).OrderBy("id").Find(&jobs); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return jobs, nil
|
return jobs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateRunJob(ctx context.Context, job *RunJob, cond builder.Cond, cols ...string) (int64, error) {
|
func UpdateRunJob(ctx context.Context, job *BotRunJob, cond builder.Cond, cols ...string) (int64, error) {
|
||||||
e := db.GetEngine(ctx)
|
e := db.GetEngine(ctx)
|
||||||
|
|
||||||
sess := e.ID(job.ID)
|
sess := e.ID(job.ID)
|
||||||
@ -149,7 +145,7 @@ func UpdateRunJob(ctx context.Context, job *RunJob, cond builder.Cond, cols ...s
|
|||||||
|
|
||||||
runStatus := aggregateJobStatus(jobs)
|
runStatus := aggregateJobStatus(jobs)
|
||||||
|
|
||||||
run := &Run{
|
run := &BotRun{
|
||||||
ID: job.RunID,
|
ID: job.RunID,
|
||||||
Status: runStatus,
|
Status: runStatus,
|
||||||
}
|
}
|
||||||
@ -159,7 +155,7 @@ func UpdateRunJob(ctx context.Context, job *RunJob, cond builder.Cond, cols ...s
|
|||||||
return affected, UpdateRun(ctx, run)
|
return affected, UpdateRun(ctx, run)
|
||||||
}
|
}
|
||||||
|
|
||||||
func aggregateJobStatus(jobs []*RunJob) Status {
|
func aggregateJobStatus(jobs []*BotRunJob) Status {
|
||||||
allDone := true
|
allDone := true
|
||||||
allWaiting := true
|
allWaiting := true
|
||||||
hasFailure := false
|
hasFailure := false
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RunJobList []*RunJob
|
type RunJobList []*BotRunJob
|
||||||
|
|
||||||
func (jobs RunJobList) GetRunIDs() []int64 {
|
func (jobs RunJobList) GetRunIDs() []int64 {
|
||||||
runIDsMap := make(map[int64]struct{})
|
runIDsMap := make(map[int64]struct{})
|
||||||
@ -32,7 +32,7 @@ func (jobs RunJobList) GetRunIDs() []int64 {
|
|||||||
|
|
||||||
func (jobs RunJobList) LoadRuns(ctx context.Context, withRepo bool) error {
|
func (jobs RunJobList) LoadRuns(ctx context.Context, withRepo bool) error {
|
||||||
runIDs := jobs.GetRunIDs()
|
runIDs := jobs.GetRunIDs()
|
||||||
runs := make(map[int64]*Run, len(runIDs))
|
runs := make(map[int64]*BotRun, len(runIDs))
|
||||||
if err := db.GetEngine(ctx).In("id", runIDs).Find(&runs); err != nil {
|
if err := db.GetEngine(ctx).In("id", runIDs).Find(&runs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -42,7 +42,7 @@ func (jobs RunJobList) LoadRuns(ctx context.Context, withRepo bool) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if withRepo {
|
if withRepo {
|
||||||
var runsList RunList = make([]*Run, 0, len(runs))
|
var runsList RunList = make([]*BotRun, 0, len(runs))
|
||||||
for _, r := range runs {
|
for _, r := range runs {
|
||||||
runsList = append(runsList, r)
|
runsList = append(runsList, r)
|
||||||
}
|
}
|
||||||
@ -99,5 +99,5 @@ func FindRunJobs(ctx context.Context, opts FindRunJobOptions) (RunJobList, int64
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CountRunJobs(ctx context.Context, opts FindRunJobOptions) (int64, error) {
|
func CountRunJobs(ctx context.Context, opts FindRunJobOptions) (int64, error) {
|
||||||
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(RunJob))
|
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(BotRunJob))
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ import (
|
|||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RunList []*Run
|
type RunList []*BotRun
|
||||||
|
|
||||||
// GetUserIDs returns a slice of user's id
|
// GetUserIDs returns a slice of user's id
|
||||||
func (runs RunList) GetUserIDs() []int64 {
|
func (runs RunList) GetUserIDs() []int64 {
|
||||||
@ -111,5 +111,5 @@ func FindRuns(ctx context.Context, opts FindRunOptions) (RunList, int64, error)
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CountRuns(ctx context.Context, opts FindRunOptions) (int64, error) {
|
func CountRuns(ctx context.Context, opts FindRunOptions) (int64, error) {
|
||||||
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Run))
|
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(BotRun))
|
||||||
}
|
}
|
||||||
|
@ -34,8 +34,8 @@ func (err ErrRunnerNotExist) Error() string {
|
|||||||
return fmt.Sprintf("Bot runner token [%s] is not exist", err.Token)
|
return fmt.Sprintf("Bot runner token [%s] is not exist", err.Token)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Runner represents runner machines
|
// BotRunner represents runner machines
|
||||||
type Runner struct {
|
type BotRunner struct {
|
||||||
ID int64
|
ID int64
|
||||||
UUID string `xorm:"CHAR(36) UNIQUE"`
|
UUID string `xorm:"CHAR(36) UNIQUE"`
|
||||||
Name string `xorm:"VARCHAR(32)"`
|
Name string `xorm:"VARCHAR(32)"`
|
||||||
@ -65,11 +65,7 @@ type Runner struct {
|
|||||||
Deleted timeutil.TimeStamp `xorm:"deleted"`
|
Deleted timeutil.TimeStamp `xorm:"deleted"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Runner) TableName() string {
|
func (r *BotRunner) OwnType() string {
|
||||||
return "bots_runner"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (r *Runner) OwnType() string {
|
|
||||||
if r.OwnerID == 0 {
|
if r.OwnerID == 0 {
|
||||||
return "Global"
|
return "Global"
|
||||||
}
|
}
|
||||||
@ -80,7 +76,7 @@ func (r *Runner) OwnType() string {
|
|||||||
return r.Repo.FullName()
|
return r.Repo.FullName()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Runner) Status() runnerv1.RunnerStatus {
|
func (r *BotRunner) Status() runnerv1.RunnerStatus {
|
||||||
if time.Since(r.LastOnline.AsTime()) > time.Minute {
|
if time.Since(r.LastOnline.AsTime()) > time.Minute {
|
||||||
return runnerv1.RunnerStatus_RUNNER_STATUS_OFFLINE
|
return runnerv1.RunnerStatus_RUNNER_STATUS_OFFLINE
|
||||||
}
|
}
|
||||||
@ -90,11 +86,11 @@ func (r *Runner) Status() runnerv1.RunnerStatus {
|
|||||||
return runnerv1.RunnerStatus_RUNNER_STATUS_ACTIVE
|
return runnerv1.RunnerStatus_RUNNER_STATUS_ACTIVE
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Runner) StatusName() string {
|
func (r *BotRunner) StatusName() string {
|
||||||
return strings.ToLower(strings.TrimPrefix(r.Status().String(), "RUNNER_STATUS_"))
|
return strings.ToLower(strings.TrimPrefix(r.Status().String(), "RUNNER_STATUS_"))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Runner) IsOnline() bool {
|
func (r *BotRunner) IsOnline() bool {
|
||||||
status := r.Status()
|
status := r.Status()
|
||||||
if status == runnerv1.RunnerStatus_RUNNER_STATUS_IDLE || status == runnerv1.RunnerStatus_RUNNER_STATUS_ACTIVE {
|
if status == runnerv1.RunnerStatus_RUNNER_STATUS_IDLE || status == runnerv1.RunnerStatus_RUNNER_STATUS_ACTIVE {
|
||||||
return true
|
return true
|
||||||
@ -103,12 +99,12 @@ func (r *Runner) IsOnline() bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// AllLabels returns agent and custom labels
|
// AllLabels returns agent and custom labels
|
||||||
func (r *Runner) AllLabels() []string {
|
func (r *BotRunner) AllLabels() []string {
|
||||||
return append(r.AgentLabels, r.CustomLabels...)
|
return append(r.AgentLabels, r.CustomLabels...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Editable checks if the runner is editable by the user
|
// Editable checks if the runner is editable by the user
|
||||||
func (r *Runner) Editable(ownerID, repoID int64) bool {
|
func (r *BotRunner) Editable(ownerID, repoID int64) bool {
|
||||||
if ownerID == 0 && repoID == 0 {
|
if ownerID == 0 && repoID == 0 {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@ -119,7 +115,7 @@ func (r *Runner) Editable(ownerID, repoID int64) bool {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LoadAttributes loads the attributes of the runner
|
// LoadAttributes loads the attributes of the runner
|
||||||
func (r *Runner) LoadAttributes(ctx context.Context) error {
|
func (r *BotRunner) LoadAttributes(ctx context.Context) error {
|
||||||
if r.OwnerID > 0 {
|
if r.OwnerID > 0 {
|
||||||
var user user_model.User
|
var user user_model.User
|
||||||
has, err := db.GetEngine(ctx).ID(r.OwnerID).Get(&user)
|
has, err := db.GetEngine(ctx).ID(r.OwnerID).Get(&user)
|
||||||
@ -143,13 +139,13 @@ func (r *Runner) LoadAttributes(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r *Runner) GenerateToken() (err error) {
|
func (r *BotRunner) GenerateToken() (err error) {
|
||||||
r.Token, r.TokenSalt, r.TokenHash, _, err = generateSaltedToken()
|
r.Token, r.TokenSalt, r.TokenHash, _, err = generateSaltedToken()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
db.RegisterModel(&Runner{})
|
db.RegisterModel(&BotRunner{})
|
||||||
}
|
}
|
||||||
|
|
||||||
type FindRunnerOptions struct {
|
type FindRunnerOptions struct {
|
||||||
@ -217,8 +213,8 @@ func FindRunners(opts FindRunnerOptions) (runners RunnerList, err error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetUsableRunner returns the usable runner
|
// GetUsableRunner returns the usable runner
|
||||||
func GetUsableRunner(opts FindRunnerOptions) (*Runner, error) {
|
func GetUsableRunner(opts FindRunnerOptions) (*BotRunner, error) {
|
||||||
var runner Runner
|
var runner BotRunner
|
||||||
has, err := db.GetEngine(db.DefaultContext).
|
has, err := db.GetEngine(db.DefaultContext).
|
||||||
Where(opts.toCond()).
|
Where(opts.toCond()).
|
||||||
Asc("last_online").
|
Asc("last_online").
|
||||||
@ -234,8 +230,8 @@ func GetUsableRunner(opts FindRunnerOptions) (*Runner, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetRunnerByUUID returns a bot runner via uuid
|
// GetRunnerByUUID returns a bot runner via uuid
|
||||||
func GetRunnerByUUID(uuid string) (*Runner, error) {
|
func GetRunnerByUUID(uuid string) (*BotRunner, error) {
|
||||||
var runner Runner
|
var runner BotRunner
|
||||||
has, err := db.GetEngine(db.DefaultContext).Where("uuid=?", uuid).Get(&runner)
|
has, err := db.GetEngine(db.DefaultContext).Where("uuid=?", uuid).Get(&runner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -248,8 +244,8 @@ func GetRunnerByUUID(uuid string) (*Runner, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetRunnerByID returns a bot runner via id
|
// GetRunnerByID returns a bot runner via id
|
||||||
func GetRunnerByID(id int64) (*Runner, error) {
|
func GetRunnerByID(id int64) (*BotRunner, error) {
|
||||||
var runner Runner
|
var runner BotRunner
|
||||||
has, err := db.GetEngine(db.DefaultContext).Where("id=?", id).Get(&runner)
|
has, err := db.GetEngine(db.DefaultContext).Where("id=?", id).Get(&runner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -262,7 +258,7 @@ func GetRunnerByID(id int64) (*Runner, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRunner updates runner's information.
|
// UpdateRunner updates runner's information.
|
||||||
func UpdateRunner(ctx context.Context, r *Runner, cols ...string) error {
|
func UpdateRunner(ctx context.Context, r *BotRunner, cols ...string) error {
|
||||||
e := db.GetEngine(ctx)
|
e := db.GetEngine(ctx)
|
||||||
var err error
|
var err error
|
||||||
if len(cols) == 0 {
|
if len(cols) == 0 {
|
||||||
@ -274,15 +270,15 @@ func UpdateRunner(ctx context.Context, r *Runner, cols ...string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// DeleteRunner deletes a runner by given ID.
|
// DeleteRunner deletes a runner by given ID.
|
||||||
func DeleteRunner(ctx context.Context, r *Runner) error {
|
func DeleteRunner(ctx context.Context, r *BotRunner) error {
|
||||||
e := db.GetEngine(ctx)
|
e := db.GetEngine(ctx)
|
||||||
_, err := e.Delete(r)
|
_, err := e.Delete(r)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
// FindRunnersByRepoID returns all workers for the repository
|
// FindRunnersByRepoID returns all workers for the repository
|
||||||
func FindRunnersByRepoID(repoID int64) ([]*Runner, error) {
|
func FindRunnersByRepoID(repoID int64) ([]*BotRunner, error) {
|
||||||
var runners []*Runner
|
var runners []*BotRunner
|
||||||
err := db.GetEngine(db.DefaultContext).Where("repo_id=? OR repo_id=0", repoID).
|
err := db.GetEngine(db.DefaultContext).Where("repo_id=? OR repo_id=0", repoID).
|
||||||
Find(&runners)
|
Find(&runners)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -293,7 +289,7 @@ func FindRunnersByRepoID(repoID int64) ([]*Runner, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewRunner creates new runner.
|
// NewRunner creates new runner.
|
||||||
func NewRunner(ctx context.Context, t *Runner) error {
|
func NewRunner(ctx context.Context, t *BotRunner) error {
|
||||||
_, err := db.GetEngine(ctx).Insert(t)
|
_, err := db.GetEngine(ctx).Insert(t)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
"code.gitea.io/gitea/modules/container"
|
"code.gitea.io/gitea/modules/container"
|
||||||
)
|
)
|
||||||
|
|
||||||
type RunnerList []*Runner
|
type RunnerList []*BotRunner
|
||||||
|
|
||||||
// GetUserIDs returns a slice of user's id
|
// GetUserIDs returns a slice of user's id
|
||||||
func (runners RunnerList) GetUserIDs() []int64 {
|
func (runners RunnerList) GetUserIDs() []int64 {
|
||||||
|
@ -24,8 +24,8 @@ func (err ErrRunnerTokenNotExist) Error() string {
|
|||||||
return fmt.Sprintf("runner token [%s] is not exist", err.Token)
|
return fmt.Sprintf("runner token [%s] is not exist", err.Token)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunnerToken represents runner tokens
|
// BotRunnerToken represents runner tokens
|
||||||
type RunnerToken struct {
|
type BotRunnerToken struct {
|
||||||
ID int64
|
ID int64
|
||||||
Token string `xorm:"UNIQUE"`
|
Token string `xorm:"UNIQUE"`
|
||||||
OwnerID int64 `xorm:"index"` // org level runner, 0 means system
|
OwnerID int64 `xorm:"index"` // org level runner, 0 means system
|
||||||
@ -39,17 +39,13 @@ type RunnerToken struct {
|
|||||||
Deleted timeutil.TimeStamp `xorm:"deleted"`
|
Deleted timeutil.TimeStamp `xorm:"deleted"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (RunnerToken) TableName() string {
|
|
||||||
return "bots_runner_token"
|
|
||||||
}
|
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
db.RegisterModel(new(RunnerToken))
|
db.RegisterModel(new(BotRunnerToken))
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRunnerToken returns a bot runner via token
|
// GetRunnerToken returns a bot runner via token
|
||||||
func GetRunnerToken(token string) (*RunnerToken, error) {
|
func GetRunnerToken(token string) (*BotRunnerToken, error) {
|
||||||
var runnerToken RunnerToken
|
var runnerToken BotRunnerToken
|
||||||
has, err := db.GetEngine(db.DefaultContext).Where("token=?", token).Get(&runnerToken)
|
has, err := db.GetEngine(db.DefaultContext).Where("token=?", token).Get(&runnerToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -62,7 +58,7 @@ func GetRunnerToken(token string) (*RunnerToken, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// UpdateRunnerToken updates runner token information.
|
// UpdateRunnerToken updates runner token information.
|
||||||
func UpdateRunnerToken(ctx context.Context, r *RunnerToken, cols ...string) (err error) {
|
func UpdateRunnerToken(ctx context.Context, r *BotRunnerToken, cols ...string) (err error) {
|
||||||
e := db.GetEngine(ctx)
|
e := db.GetEngine(ctx)
|
||||||
|
|
||||||
if len(cols) == 0 {
|
if len(cols) == 0 {
|
||||||
@ -74,12 +70,12 @@ func UpdateRunnerToken(ctx context.Context, r *RunnerToken, cols ...string) (err
|
|||||||
}
|
}
|
||||||
|
|
||||||
// NewRunnerToken creates a new runner token
|
// NewRunnerToken creates a new runner token
|
||||||
func NewRunnerToken(ownerID, repoID int64) (*RunnerToken, error) {
|
func NewRunnerToken(ownerID, repoID int64) (*BotRunnerToken, error) {
|
||||||
token, err := util.CryptoRandomString(40)
|
token, err := util.CryptoRandomString(40)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
runnerToken := &RunnerToken{
|
runnerToken := &BotRunnerToken{
|
||||||
OwnerID: ownerID,
|
OwnerID: ownerID,
|
||||||
RepoID: repoID,
|
RepoID: repoID,
|
||||||
IsActive: false,
|
IsActive: false,
|
||||||
@ -90,8 +86,8 @@ func NewRunnerToken(ownerID, repoID int64) (*RunnerToken, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// GetUnactivatedRunnerToken returns a unactivated runner token
|
// GetUnactivatedRunnerToken returns a unactivated runner token
|
||||||
func GetUnactivatedRunnerToken(ownerID, repoID int64) (*RunnerToken, error) {
|
func GetUnactivatedRunnerToken(ownerID, repoID int64) (*BotRunnerToken, error) {
|
||||||
var runnerToken RunnerToken
|
var runnerToken BotRunnerToken
|
||||||
has, err := db.GetEngine(db.DefaultContext).Where("owner_id=? AND repo_id=? AND is_active=?", ownerID, repoID, false).OrderBy("id DESC").Get(&runnerToken)
|
has, err := db.GetEngine(db.DefaultContext).Where("owner_id=? AND repo_id=? AND is_active=?", ownerID, repoID, false).OrderBy("id DESC").Get(&runnerToken)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -6,7 +6,7 @@ package bots
|
|||||||
|
|
||||||
import runnerv1 "code.gitea.io/bots-proto-go/runner/v1"
|
import runnerv1 "code.gitea.io/bots-proto-go/runner/v1"
|
||||||
|
|
||||||
// Status represents the status of Run, RunJob, Task, or TaskStep
|
// Status represents the status of BotRun, BotRunJob, BotTask, or BotTaskStep
|
||||||
type Status int
|
type Status int
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -29,12 +29,12 @@ import (
|
|||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Task represents a distribution of job
|
// BotTask represents a distribution of job
|
||||||
type Task struct {
|
type BotTask struct {
|
||||||
ID int64
|
ID int64
|
||||||
JobID int64
|
JobID int64
|
||||||
Job *RunJob `xorm:"-"`
|
Job *BotRunJob `xorm:"-"`
|
||||||
Steps []*TaskStep `xorm:"-"`
|
Steps []*BotTaskStep `xorm:"-"`
|
||||||
Attempt int64
|
Attempt int64
|
||||||
RunnerID int64 `xorm:"index"`
|
RunnerID int64 `xorm:"index"`
|
||||||
Status Status `xorm:"index"`
|
Status Status `xorm:"index"`
|
||||||
@ -65,7 +65,7 @@ type Task struct {
|
|||||||
var successfulTokenTaskCache *lru.Cache
|
var successfulTokenTaskCache *lru.Cache
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
db.RegisterModel(new(Task), func() error {
|
db.RegisterModel(new(BotTask), func() error {
|
||||||
if setting.SuccessfulTokensCacheSize > 0 {
|
if setting.SuccessfulTokensCacheSize > 0 {
|
||||||
var err error
|
var err error
|
||||||
successfulTokenTaskCache, err = lru.New(setting.SuccessfulTokensCacheSize)
|
successfulTokenTaskCache, err = lru.New(setting.SuccessfulTokensCacheSize)
|
||||||
@ -79,11 +79,7 @@ func init() {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (Task) TableName() string {
|
func (task *BotTask) TakeTime() time.Duration {
|
||||||
return "bots_task"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (task *Task) TakeTime() time.Duration {
|
|
||||||
if task.Started == 0 {
|
if task.Started == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -95,15 +91,15 @@ func (task *Task) TakeTime() time.Duration {
|
|||||||
return time.Since(started).Truncate(time.Second)
|
return time.Since(started).Truncate(time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *Task) IsStopped() bool {
|
func (task *BotTask) IsStopped() bool {
|
||||||
return task.Stopped > 0
|
return task.Stopped > 0
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *Task) GetRepo() string {
|
func (task *BotTask) GetRepo() string {
|
||||||
return "xxxx"
|
return "xxxx"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *Task) GetCommitSHA() string {
|
func (task *BotTask) GetCommitSHA() string {
|
||||||
if task.Job == nil {
|
if task.Job == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -114,7 +110,7 @@ func (task *Task) GetCommitSHA() string {
|
|||||||
return task.Job.Run.CommitSHA
|
return task.Job.Run.CommitSHA
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *Task) GetCommitSHAShort() string {
|
func (task *BotTask) GetCommitSHAShort() string {
|
||||||
commitSHA := task.GetCommitSHA()
|
commitSHA := task.GetCommitSHA()
|
||||||
if len(commitSHA) > 8 {
|
if len(commitSHA) > 8 {
|
||||||
return commitSHA[:8]
|
return commitSHA[:8]
|
||||||
@ -122,14 +118,14 @@ func (task *Task) GetCommitSHAShort() string {
|
|||||||
return commitSHA
|
return commitSHA
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *Task) GetBuildViewLink() string {
|
func (task *BotTask) GetBuildViewLink() string {
|
||||||
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
|
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return task.Job.Run.Repo.Link() + "/bots/runs/" + strconv.FormatInt(task.ID, 10)
|
return task.Job.Run.Repo.Link() + "/bots/runs/" + strconv.FormatInt(task.ID, 10)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *Task) GetCommitLink() string {
|
func (task *BotTask) GetCommitLink() string {
|
||||||
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
|
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
@ -139,21 +135,21 @@ func (task *Task) GetCommitLink() string {
|
|||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *Task) GetRepoName() string {
|
func (task *BotTask) GetRepoName() string {
|
||||||
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
|
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return task.Job.Run.Repo.FullName()
|
return task.Job.Run.Repo.FullName()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *Task) GetRepoLink() string {
|
func (task *BotTask) GetRepoLink() string {
|
||||||
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
|
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
return task.Job.Run.Repo.Link()
|
return task.Job.Run.Repo.Link()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *Task) LoadJob(ctx context.Context) error {
|
func (task *BotTask) LoadJob(ctx context.Context) error {
|
||||||
if task.Job == nil {
|
if task.Job == nil {
|
||||||
job, err := GetRunJobByID(ctx, task.JobID)
|
job, err := GetRunJobByID(ctx, task.JobID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -165,7 +161,7 @@ func (task *Task) LoadJob(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// LoadAttributes load Job Steps if not loaded
|
// LoadAttributes load Job Steps if not loaded
|
||||||
func (task *Task) LoadAttributes(ctx context.Context) error {
|
func (task *BotTask) LoadAttributes(ctx context.Context) error {
|
||||||
if task == nil {
|
if task == nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -188,7 +184,7 @@ func (task *Task) LoadAttributes(ctx context.Context) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (task *Task) GenerateToken() (err error) {
|
func (task *BotTask) GenerateToken() (err error) {
|
||||||
task.Token, task.TokenSalt, task.TokenHash, task.TokenLastEight, err = generateSaltedToken()
|
task.Token, task.TokenSalt, task.TokenHash, task.TokenLastEight, err = generateSaltedToken()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -218,8 +214,8 @@ func (indexes *LogIndexes) ToDB() ([]byte, error) {
|
|||||||
return buf[:i], nil
|
return buf[:i], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTaskByID(ctx context.Context, id int64) (*Task, error) {
|
func GetTaskByID(ctx context.Context, id int64) (*BotTask, error) {
|
||||||
var task Task
|
var task BotTask
|
||||||
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&task)
|
has, err := db.GetEngine(ctx).Where("id=?", id).Get(&task)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -230,7 +226,7 @@ func GetTaskByID(ctx context.Context, id int64) (*Task, error) {
|
|||||||
return &task, nil
|
return &task, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetRunningTaskByToken(ctx context.Context, token string) (*Task, error) {
|
func GetRunningTaskByToken(ctx context.Context, token string) (*BotTask, error) {
|
||||||
errNotExist := fmt.Errorf("task with token %q: %w", token, util.ErrNotExist)
|
errNotExist := fmt.Errorf("task with token %q: %w", token, util.ErrNotExist)
|
||||||
if token == "" {
|
if token == "" {
|
||||||
return nil, errNotExist
|
return nil, errNotExist
|
||||||
@ -248,7 +244,7 @@ func GetRunningTaskByToken(ctx context.Context, token string) (*Task, error) {
|
|||||||
lastEight := token[len(token)-8:]
|
lastEight := token[len(token)-8:]
|
||||||
|
|
||||||
if id := getTaskIDFromCache(token); id > 0 {
|
if id := getTaskIDFromCache(token); id > 0 {
|
||||||
task := &Task{
|
task := &BotTask{
|
||||||
TokenLastEight: lastEight,
|
TokenLastEight: lastEight,
|
||||||
}
|
}
|
||||||
// Re-get the task from the db in case it has been deleted in the intervening period
|
// Re-get the task from the db in case it has been deleted in the intervening period
|
||||||
@ -262,7 +258,7 @@ func GetRunningTaskByToken(ctx context.Context, token string) (*Task, error) {
|
|||||||
successfulTokenTaskCache.Remove(token)
|
successfulTokenTaskCache.Remove(token)
|
||||||
}
|
}
|
||||||
|
|
||||||
var tasks []*Task
|
var tasks []*BotTask
|
||||||
err := db.GetEngine(ctx).Where("token_last_eight = ? AND status = ?", lastEight, StatusRunning).Find(&tasks)
|
err := db.GetEngine(ctx).Where("token_last_eight = ? AND status = ?", lastEight, StatusRunning).Find(&tasks)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -282,7 +278,7 @@ func GetRunningTaskByToken(ctx context.Context, token string) (*Task, error) {
|
|||||||
return nil, errNotExist
|
return nil, errNotExist
|
||||||
}
|
}
|
||||||
|
|
||||||
func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, error) {
|
func CreateTaskForRunner(ctx context.Context, runner *BotRunner) (*BotTask, bool, error) {
|
||||||
dbCtx, commiter, err := db.TxContext(ctx)
|
dbCtx, commiter, err := db.TxContext(ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
@ -299,16 +295,16 @@ func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, erro
|
|||||||
jobCond = builder.In("repo_id", builder.Select("id").From("repository").Where(builder.Eq{"owner_id": runner.OwnerID}))
|
jobCond = builder.In("repo_id", builder.Select("id").From("repository").Where(builder.Eq{"owner_id": runner.OwnerID}))
|
||||||
}
|
}
|
||||||
if jobCond.IsValid() {
|
if jobCond.IsValid() {
|
||||||
jobCond = builder.In("run_id", builder.Select("id").From(Run{}.TableName()).Where(jobCond))
|
jobCond = builder.In("run_id", builder.Select("id").From("bot_run").Where(jobCond))
|
||||||
}
|
}
|
||||||
|
|
||||||
var jobs []*RunJob
|
var jobs []*BotRunJob
|
||||||
if err := e.Where("task_id=? AND status=?", 0, StatusWaiting).And(jobCond).Asc("id").Find(&jobs); err != nil {
|
if err := e.Where("task_id=? AND status=?", 0, StatusWaiting).And(jobCond).Asc("id").Find(&jobs); err != nil {
|
||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: a more efficient way to filter labels
|
// TODO: a more efficient way to filter labels
|
||||||
var job *RunJob
|
var job *BotRunJob
|
||||||
labels := runner.AgentLabels
|
labels := runner.AgentLabels
|
||||||
labels = append(labels, runner.CustomLabels...)
|
labels = append(labels, runner.CustomLabels...)
|
||||||
log.Trace("runner labels: %v", labels)
|
log.Trace("runner labels: %v", labels)
|
||||||
@ -330,7 +326,7 @@ func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, erro
|
|||||||
job.Started = now
|
job.Started = now
|
||||||
job.Status = StatusRunning
|
job.Status = StatusRunning
|
||||||
|
|
||||||
task := &Task{
|
task := &BotTask{
|
||||||
JobID: job.ID,
|
JobID: job.ID,
|
||||||
Attempt: job.Attempt,
|
Attempt: job.Attempt,
|
||||||
RunnerID: runner.ID,
|
RunnerID: runner.ID,
|
||||||
@ -363,9 +359,9 @@ func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, erro
|
|||||||
return nil, false, err
|
return nil, false, err
|
||||||
}
|
}
|
||||||
|
|
||||||
steps := make([]*TaskStep, len(workflowJob.Steps))
|
steps := make([]*BotTaskStep, len(workflowJob.Steps))
|
||||||
for i, v := range workflowJob.Steps {
|
for i, v := range workflowJob.Steps {
|
||||||
steps[i] = &TaskStep{
|
steps[i] = &BotTaskStep{
|
||||||
Name: v.String(),
|
Name: v.String(),
|
||||||
TaskID: task.ID,
|
TaskID: task.ID,
|
||||||
Number: int64(i),
|
Number: int64(i),
|
||||||
@ -401,7 +397,7 @@ func CreateTaskForRunner(ctx context.Context, runner *Runner) (*Task, bool, erro
|
|||||||
return task, true, nil
|
return task, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateTask(ctx context.Context, task *Task, cols ...string) error {
|
func UpdateTask(ctx context.Context, task *BotTask, cols ...string) error {
|
||||||
sess := db.GetEngine(ctx).ID(task.ID)
|
sess := db.GetEngine(ctx).ID(task.ID)
|
||||||
if len(cols) > 0 {
|
if len(cols) > 0 {
|
||||||
sess.Cols(cols...)
|
sess.Cols(cols...)
|
||||||
@ -410,7 +406,7 @@ func UpdateTask(ctx context.Context, task *Task, cols ...string) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
func UpdateTaskByState(state *runnerv1.TaskState) (*Task, error) {
|
func UpdateTaskByState(state *runnerv1.TaskState) (*BotTask, error) {
|
||||||
stepStates := map[int64]*runnerv1.StepState{}
|
stepStates := map[int64]*runnerv1.StepState{}
|
||||||
for _, v := range state.Steps {
|
for _, v := range state.Steps {
|
||||||
stepStates[v.Id] = v
|
stepStates[v.Id] = v
|
||||||
@ -424,7 +420,7 @@ func UpdateTaskByState(state *runnerv1.TaskState) (*Task, error) {
|
|||||||
|
|
||||||
e := db.GetEngine(ctx)
|
e := db.GetEngine(ctx)
|
||||||
|
|
||||||
task := &Task{}
|
task := &BotTask{}
|
||||||
if has, err := e.ID(state.Id).Get(task); err != nil {
|
if has, err := e.ID(state.Id).Get(task); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
} else if !has {
|
} else if !has {
|
||||||
@ -434,7 +430,7 @@ func UpdateTaskByState(state *runnerv1.TaskState) (*Task, error) {
|
|||||||
if state.Result != runnerv1.Result_RESULT_UNSPECIFIED {
|
if state.Result != runnerv1.Result_RESULT_UNSPECIFIED {
|
||||||
task.Status = Status(state.Result)
|
task.Status = Status(state.Result)
|
||||||
task.Stopped = timeutil.TimeStamp(state.StoppedAt.AsTime().Unix())
|
task.Stopped = timeutil.TimeStamp(state.StoppedAt.AsTime().Unix())
|
||||||
if _, err := UpdateRunJob(ctx, &RunJob{
|
if _, err := UpdateRunJob(ctx, &BotRunJob{
|
||||||
ID: task.JobID,
|
ID: task.JobID,
|
||||||
Status: task.Status,
|
Status: task.Status,
|
||||||
Stopped: task.Stopped,
|
Stopped: task.Stopped,
|
||||||
@ -486,7 +482,7 @@ func StopTask(ctx context.Context, taskID int64, status Status) error {
|
|||||||
}
|
}
|
||||||
e := db.GetEngine(ctx)
|
e := db.GetEngine(ctx)
|
||||||
|
|
||||||
task := &Task{}
|
task := &BotTask{}
|
||||||
if has, err := e.ID(taskID).Get(task); err != nil {
|
if has, err := e.ID(taskID).Get(task); err != nil {
|
||||||
return err
|
return err
|
||||||
} else if !has {
|
} else if !has {
|
||||||
@ -499,7 +495,7 @@ func StopTask(ctx context.Context, taskID int64, status Status) error {
|
|||||||
now := timeutil.TimeStampNow()
|
now := timeutil.TimeStampNow()
|
||||||
task.Status = status
|
task.Status = status
|
||||||
task.Stopped = now
|
task.Stopped = now
|
||||||
if _, err := UpdateRunJob(ctx, &RunJob{
|
if _, err := UpdateRunJob(ctx, &BotRunJob{
|
||||||
ID: task.JobID,
|
ID: task.JobID,
|
||||||
Status: task.Status,
|
Status: task.Status,
|
||||||
Stopped: task.Stopped,
|
Stopped: task.Stopped,
|
||||||
|
@ -13,7 +13,7 @@ import (
|
|||||||
"xorm.io/builder"
|
"xorm.io/builder"
|
||||||
)
|
)
|
||||||
|
|
||||||
type TaskList []*Task
|
type TaskList []*BotTask
|
||||||
|
|
||||||
func (tasks TaskList) GetJobIDs() []int64 {
|
func (tasks TaskList) GetJobIDs() []int64 {
|
||||||
jobIDsMap := make(map[int64]struct{})
|
jobIDsMap := make(map[int64]struct{})
|
||||||
@ -32,7 +32,7 @@ func (tasks TaskList) GetJobIDs() []int64 {
|
|||||||
|
|
||||||
func (tasks TaskList) LoadJobs(ctx context.Context) error {
|
func (tasks TaskList) LoadJobs(ctx context.Context) error {
|
||||||
jobIDs := tasks.GetJobIDs()
|
jobIDs := tasks.GetJobIDs()
|
||||||
jobs := make(map[int64]*RunJob, len(jobIDs))
|
jobs := make(map[int64]*BotRunJob, len(jobIDs))
|
||||||
if err := db.GetEngine(ctx).In("id", jobIDs).Find(&jobs); err != nil {
|
if err := db.GetEngine(ctx).In("id", jobIDs).Find(&jobs); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@ -42,7 +42,7 @@ func (tasks TaskList) LoadJobs(ctx context.Context) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var jobsList RunJobList = make([]*RunJob, 0, len(jobs))
|
var jobsList RunJobList = make([]*BotRunJob, 0, len(jobs))
|
||||||
for _, j := range jobs {
|
for _, j := range jobs {
|
||||||
jobsList = append(jobsList, j)
|
jobsList = append(jobsList, j)
|
||||||
}
|
}
|
||||||
@ -105,5 +105,5 @@ func FindTasks(ctx context.Context, opts FindTaskOptions) (TaskList, int64, erro
|
|||||||
}
|
}
|
||||||
|
|
||||||
func CountTasks(ctx context.Context, opts FindTaskOptions) (int64, error) {
|
func CountTasks(ctx context.Context, opts FindTaskOptions) (int64, error) {
|
||||||
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(Task))
|
return db.GetEngine(ctx).Where(opts.toConds()).Count(new(BotTask))
|
||||||
}
|
}
|
||||||
|
@ -12,8 +12,8 @@ import (
|
|||||||
"code.gitea.io/gitea/modules/timeutil"
|
"code.gitea.io/gitea/modules/timeutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TaskStep represents a step of Task
|
// BotTaskStep represents a step of BotTask
|
||||||
type TaskStep struct {
|
type BotTaskStep struct {
|
||||||
ID int64
|
ID int64
|
||||||
Name string
|
Name string
|
||||||
TaskID int64 `xorm:"index unique(task_number)"`
|
TaskID int64 `xorm:"index unique(task_number)"`
|
||||||
@ -27,11 +27,7 @@ type TaskStep struct {
|
|||||||
Updated timeutil.TimeStamp `xorm:"updated"`
|
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (TaskStep) TableName() string {
|
func (step *BotTaskStep) TakeTime() time.Duration {
|
||||||
return "bots_task_step"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (step *TaskStep) TakeTime() time.Duration {
|
|
||||||
if step.Started == 0 {
|
if step.Started == 0 {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
@ -44,10 +40,10 @@ func (step *TaskStep) TakeTime() time.Duration {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
db.RegisterModel(new(TaskStep))
|
db.RegisterModel(new(BotTaskStep))
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTaskStepsByTaskID(ctx context.Context, taskID int64) ([]*TaskStep, error) {
|
func GetTaskStepsByTaskID(ctx context.Context, taskID int64) ([]*BotTaskStep, error) {
|
||||||
var steps []*TaskStep
|
var steps []*BotTaskStep
|
||||||
return steps, db.GetEngine(ctx).Where("task_id=?", taskID).OrderBy("number").Find(&steps)
|
return steps, db.GetEngine(ctx).Where("task_id=?", taskID).OrderBy("number").Find(&steps)
|
||||||
}
|
}
|
||||||
|
@ -12,7 +12,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func addBotTables(x *xorm.Engine) error {
|
func addBotTables(x *xorm.Engine) error {
|
||||||
type BotsRunner struct {
|
type BotRunner struct {
|
||||||
ID int64
|
ID int64
|
||||||
UUID string `xorm:"CHAR(36) UNIQUE"`
|
UUID string `xorm:"CHAR(36) UNIQUE"`
|
||||||
Name string `xorm:"VARCHAR(32)"`
|
Name string `xorm:"VARCHAR(32)"`
|
||||||
@ -40,7 +40,7 @@ func addBotTables(x *xorm.Engine) error {
|
|||||||
Deleted timeutil.TimeStamp `xorm:"deleted"`
|
Deleted timeutil.TimeStamp `xorm:"deleted"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BotsRunnerToken struct {
|
type BotRunnerToken struct {
|
||||||
ID int64
|
ID int64
|
||||||
Token string `xorm:"UNIQUE"`
|
Token string `xorm:"UNIQUE"`
|
||||||
OwnerID int64 `xorm:"index"` // org level runner, 0 means system
|
OwnerID int64 `xorm:"index"` // org level runner, 0 means system
|
||||||
@ -52,7 +52,7 @@ func addBotTables(x *xorm.Engine) error {
|
|||||||
Deleted timeutil.TimeStamp `xorm:"deleted"`
|
Deleted timeutil.TimeStamp `xorm:"deleted"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BotsRun struct {
|
type BotRun struct {
|
||||||
ID int64
|
ID int64
|
||||||
Title string
|
Title string
|
||||||
RepoID int64 `xorm:"index unique(repo_index)"`
|
RepoID int64 `xorm:"index unique(repo_index)"`
|
||||||
@ -72,7 +72,7 @@ func addBotTables(x *xorm.Engine) error {
|
|||||||
Updated timeutil.TimeStamp `xorm:"updated"`
|
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BotsRunJob struct {
|
type BotRunJob struct {
|
||||||
ID int64
|
ID int64
|
||||||
RunID int64 `xorm:"index"`
|
RunID int64 `xorm:"index"`
|
||||||
RepoID int64 `xorm:"index"`
|
RepoID int64 `xorm:"index"`
|
||||||
@ -98,9 +98,9 @@ func addBotTables(x *xorm.Engine) error {
|
|||||||
NumClosedRuns int `xorm:"NOT NULL DEFAULT 0"`
|
NumClosedRuns int `xorm:"NOT NULL DEFAULT 0"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BotsRunIndex db.ResourceIndex
|
type BotRunIndex db.ResourceIndex
|
||||||
|
|
||||||
type BotsTask struct {
|
type BotTask struct {
|
||||||
ID int64
|
ID int64
|
||||||
JobID int64
|
JobID int64
|
||||||
Attempt int64
|
Attempt int64
|
||||||
@ -129,7 +129,7 @@ func addBotTables(x *xorm.Engine) error {
|
|||||||
Updated timeutil.TimeStamp `xorm:"updated index"`
|
Updated timeutil.TimeStamp `xorm:"updated index"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type BotsTaskStep struct {
|
type BotTaskStep struct {
|
||||||
ID int64
|
ID int64
|
||||||
Name string
|
Name string
|
||||||
TaskID int64 `xorm:"index unique(task_number)"`
|
TaskID int64 `xorm:"index unique(task_number)"`
|
||||||
@ -143,14 +143,14 @@ func addBotTables(x *xorm.Engine) error {
|
|||||||
Updated timeutil.TimeStamp `xorm:"updated"`
|
Updated timeutil.TimeStamp `xorm:"updated"`
|
||||||
}
|
}
|
||||||
|
|
||||||
return x.Sync2(
|
return x.Sync(
|
||||||
new(BotsRunner),
|
new(BotRunner),
|
||||||
new(BotsRunnerToken),
|
new(BotRunnerToken),
|
||||||
new(BotsRun),
|
new(BotRun),
|
||||||
new(BotsRunJob),
|
new(BotRunJob),
|
||||||
new(Repository),
|
new(Repository),
|
||||||
new(BotsRunIndex),
|
new(BotRunIndex),
|
||||||
new(BotsTask),
|
new(BotTask),
|
||||||
new(BotsTaskStep),
|
new(BotTaskStep),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -14,7 +14,7 @@ const (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// FullSteps returns steps with "Set up job" and "Complete job"
|
// FullSteps returns steps with "Set up job" and "Complete job"
|
||||||
func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
|
func FullSteps(task *bots_model.BotTask) []*bots_model.BotTaskStep {
|
||||||
if len(task.Steps) == 0 {
|
if len(task.Steps) == 0 {
|
||||||
return fullStepsOfEmptySteps(task)
|
return fullStepsOfEmptySteps(task)
|
||||||
}
|
}
|
||||||
@ -22,7 +22,7 @@ func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
|
|||||||
firstStep := task.Steps[0]
|
firstStep := task.Steps[0]
|
||||||
var logIndex int64
|
var logIndex int64
|
||||||
|
|
||||||
preStep := &bots_model.TaskStep{
|
preStep := &bots_model.BotTaskStep{
|
||||||
Name: preStepName,
|
Name: preStepName,
|
||||||
LogLength: task.LogLength,
|
LogLength: task.LogLength,
|
||||||
Started: task.Started,
|
Started: task.Started,
|
||||||
@ -39,7 +39,7 @@ func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
|
|||||||
}
|
}
|
||||||
logIndex += preStep.LogLength
|
logIndex += preStep.LogLength
|
||||||
|
|
||||||
var lastHasRunStep *bots_model.TaskStep
|
var lastHasRunStep *bots_model.BotTaskStep
|
||||||
for _, step := range task.Steps {
|
for _, step := range task.Steps {
|
||||||
if step.Status.HasRun() {
|
if step.Status.HasRun() {
|
||||||
lastHasRunStep = step
|
lastHasRunStep = step
|
||||||
@ -50,7 +50,7 @@ func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
|
|||||||
lastHasRunStep = preStep
|
lastHasRunStep = preStep
|
||||||
}
|
}
|
||||||
|
|
||||||
postStep := &bots_model.TaskStep{
|
postStep := &bots_model.BotTaskStep{
|
||||||
Name: postStepName,
|
Name: postStepName,
|
||||||
Status: bots_model.StatusWaiting,
|
Status: bots_model.StatusWaiting,
|
||||||
}
|
}
|
||||||
@ -61,7 +61,7 @@ func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
|
|||||||
postStep.Started = lastHasRunStep.Stopped
|
postStep.Started = lastHasRunStep.Stopped
|
||||||
postStep.Stopped = task.Stopped
|
postStep.Stopped = task.Stopped
|
||||||
}
|
}
|
||||||
ret := make([]*bots_model.TaskStep, 0, len(task.Steps)+2)
|
ret := make([]*bots_model.BotTaskStep, 0, len(task.Steps)+2)
|
||||||
ret = append(ret, preStep)
|
ret = append(ret, preStep)
|
||||||
ret = append(ret, task.Steps...)
|
ret = append(ret, task.Steps...)
|
||||||
ret = append(ret, postStep)
|
ret = append(ret, postStep)
|
||||||
@ -69,8 +69,8 @@ func FullSteps(task *bots_model.Task) []*bots_model.TaskStep {
|
|||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
func fullStepsOfEmptySteps(task *bots_model.Task) []*bots_model.TaskStep {
|
func fullStepsOfEmptySteps(task *bots_model.BotTask) []*bots_model.BotTaskStep {
|
||||||
preStep := &bots_model.TaskStep{
|
preStep := &bots_model.BotTaskStep{
|
||||||
Name: preStepName,
|
Name: preStepName,
|
||||||
LogLength: task.LogLength,
|
LogLength: task.LogLength,
|
||||||
Started: task.Started,
|
Started: task.Started,
|
||||||
@ -78,7 +78,7 @@ func fullStepsOfEmptySteps(task *bots_model.Task) []*bots_model.TaskStep {
|
|||||||
Status: bots_model.StatusRunning,
|
Status: bots_model.StatusRunning,
|
||||||
}
|
}
|
||||||
|
|
||||||
postStep := &bots_model.TaskStep{
|
postStep := &bots_model.BotTaskStep{
|
||||||
Name: postStepName,
|
Name: postStepName,
|
||||||
LogIndex: task.LogLength,
|
LogIndex: task.LogLength,
|
||||||
Started: task.Stopped,
|
Started: task.Stopped,
|
||||||
@ -95,7 +95,7 @@ func fullStepsOfEmptySteps(task *bots_model.Task) []*bots_model.TaskStep {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return []*bots_model.TaskStep{
|
return []*bots_model.BotTaskStep{
|
||||||
preStep,
|
preStep,
|
||||||
postStep,
|
postStep,
|
||||||
}
|
}
|
||||||
|
@ -15,13 +15,13 @@ import (
|
|||||||
func TestFullSteps(t *testing.T) {
|
func TestFullSteps(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
name string
|
name string
|
||||||
task *bots_model.Task
|
task *bots_model.BotTask
|
||||||
want []*bots_model.TaskStep
|
want []*bots_model.BotTaskStep
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
name: "regular",
|
name: "regular",
|
||||||
task: &bots_model.Task{
|
task: &bots_model.BotTask{
|
||||||
Steps: []*bots_model.TaskStep{
|
Steps: []*bots_model.BotTaskStep{
|
||||||
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
|
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
|
||||||
},
|
},
|
||||||
Status: bots_model.StatusSuccess,
|
Status: bots_model.StatusSuccess,
|
||||||
@ -29,7 +29,7 @@ func TestFullSteps(t *testing.T) {
|
|||||||
Stopped: 10100,
|
Stopped: 10100,
|
||||||
LogLength: 100,
|
LogLength: 100,
|
||||||
},
|
},
|
||||||
want: []*bots_model.TaskStep{
|
want: []*bots_model.BotTaskStep{
|
||||||
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
|
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
|
||||||
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
|
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 10090},
|
||||||
{Name: postStepName, Status: bots_model.StatusSuccess, LogIndex: 90, LogLength: 10, Started: 10090, Stopped: 10100},
|
{Name: postStepName, Status: bots_model.StatusSuccess, LogIndex: 90, LogLength: 10, Started: 10090, Stopped: 10100},
|
||||||
@ -37,8 +37,8 @@ func TestFullSteps(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "failed step",
|
name: "failed step",
|
||||||
task: &bots_model.Task{
|
task: &bots_model.BotTask{
|
||||||
Steps: []*bots_model.TaskStep{
|
Steps: []*bots_model.BotTaskStep{
|
||||||
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 20, Started: 10010, Stopped: 10020},
|
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 20, Started: 10010, Stopped: 10020},
|
||||||
{Status: bots_model.StatusFailure, LogIndex: 30, LogLength: 60, Started: 10020, Stopped: 10090},
|
{Status: bots_model.StatusFailure, LogIndex: 30, LogLength: 60, Started: 10020, Stopped: 10090},
|
||||||
{Status: bots_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
|
{Status: bots_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
|
||||||
@ -48,7 +48,7 @@ func TestFullSteps(t *testing.T) {
|
|||||||
Stopped: 10100,
|
Stopped: 10100,
|
||||||
LogLength: 100,
|
LogLength: 100,
|
||||||
},
|
},
|
||||||
want: []*bots_model.TaskStep{
|
want: []*bots_model.BotTaskStep{
|
||||||
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
|
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
|
||||||
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 20, Started: 10010, Stopped: 10020},
|
{Status: bots_model.StatusSuccess, LogIndex: 10, LogLength: 20, Started: 10010, Stopped: 10020},
|
||||||
{Status: bots_model.StatusFailure, LogIndex: 30, LogLength: 60, Started: 10020, Stopped: 10090},
|
{Status: bots_model.StatusFailure, LogIndex: 30, LogLength: 60, Started: 10020, Stopped: 10090},
|
||||||
@ -58,8 +58,8 @@ func TestFullSteps(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "first step is running",
|
name: "first step is running",
|
||||||
task: &bots_model.Task{
|
task: &bots_model.BotTask{
|
||||||
Steps: []*bots_model.TaskStep{
|
Steps: []*bots_model.BotTaskStep{
|
||||||
{Status: bots_model.StatusRunning, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 0},
|
{Status: bots_model.StatusRunning, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 0},
|
||||||
},
|
},
|
||||||
Status: bots_model.StatusRunning,
|
Status: bots_model.StatusRunning,
|
||||||
@ -67,7 +67,7 @@ func TestFullSteps(t *testing.T) {
|
|||||||
Stopped: 10100,
|
Stopped: 10100,
|
||||||
LogLength: 100,
|
LogLength: 100,
|
||||||
},
|
},
|
||||||
want: []*bots_model.TaskStep{
|
want: []*bots_model.BotTaskStep{
|
||||||
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
|
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 10, Started: 10000, Stopped: 10010},
|
||||||
{Status: bots_model.StatusRunning, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 0},
|
{Status: bots_model.StatusRunning, LogIndex: 10, LogLength: 80, Started: 10010, Stopped: 0},
|
||||||
{Name: postStepName, Status: bots_model.StatusWaiting, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
|
{Name: postStepName, Status: bots_model.StatusWaiting, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
|
||||||
@ -75,8 +75,8 @@ func TestFullSteps(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "first step has canceled",
|
name: "first step has canceled",
|
||||||
task: &bots_model.Task{
|
task: &bots_model.BotTask{
|
||||||
Steps: []*bots_model.TaskStep{
|
Steps: []*bots_model.BotTaskStep{
|
||||||
{Status: bots_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
|
{Status: bots_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
|
||||||
},
|
},
|
||||||
Status: bots_model.StatusFailure,
|
Status: bots_model.StatusFailure,
|
||||||
@ -84,7 +84,7 @@ func TestFullSteps(t *testing.T) {
|
|||||||
Stopped: 10100,
|
Stopped: 10100,
|
||||||
LogLength: 100,
|
LogLength: 100,
|
||||||
},
|
},
|
||||||
want: []*bots_model.TaskStep{
|
want: []*bots_model.BotTaskStep{
|
||||||
{Name: preStepName, Status: bots_model.StatusFailure, LogIndex: 0, LogLength: 100, Started: 10000, Stopped: 10100},
|
{Name: preStepName, Status: bots_model.StatusFailure, LogIndex: 0, LogLength: 100, Started: 10000, Stopped: 10100},
|
||||||
{Status: bots_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
|
{Status: bots_model.StatusCancelled, LogIndex: 0, LogLength: 0, Started: 0, Stopped: 0},
|
||||||
{Name: postStepName, Status: bots_model.StatusFailure, LogIndex: 100, LogLength: 0, Started: 10100, Stopped: 10100},
|
{Name: postStepName, Status: bots_model.StatusFailure, LogIndex: 100, LogLength: 0, Started: 10100, Stopped: 10100},
|
||||||
@ -92,14 +92,14 @@ func TestFullSteps(t *testing.T) {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "empty steps",
|
name: "empty steps",
|
||||||
task: &bots_model.Task{
|
task: &bots_model.BotTask{
|
||||||
Steps: []*bots_model.TaskStep{},
|
Steps: []*bots_model.BotTaskStep{},
|
||||||
Status: bots_model.StatusSuccess,
|
Status: bots_model.StatusSuccess,
|
||||||
Started: 10000,
|
Started: 10000,
|
||||||
Stopped: 10100,
|
Stopped: 10100,
|
||||||
LogLength: 100,
|
LogLength: 100,
|
||||||
},
|
},
|
||||||
want: []*bots_model.TaskStep{
|
want: []*bots_model.BotTaskStep{
|
||||||
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 100, Started: 10000, Stopped: 10100},
|
{Name: preStepName, Status: bots_model.StatusSuccess, LogIndex: 0, LogLength: 100, Started: 10000, Stopped: 10100},
|
||||||
{Name: postStepName, Status: bots_model.StatusSuccess, LogIndex: 100, LogLength: 0, Started: 10100, Stopped: 10100},
|
{Name: postStepName, Status: bots_model.StatusSuccess, LogIndex: 100, LogLength: 0, Started: 10100, Stopped: 10100},
|
||||||
},
|
},
|
||||||
|
@ -86,7 +86,7 @@ func notifyWithPR(repo *repo_model.Repository, doer *user_model.User, ref string
|
|||||||
}
|
}
|
||||||
|
|
||||||
for id, content := range workflows {
|
for id, content := range workflows {
|
||||||
run := bots_model.Run{
|
run := bots_model.BotRun{
|
||||||
Title: commit.Message(),
|
Title: commit.Message(),
|
||||||
RepoID: repo.ID,
|
RepoID: repo.ID,
|
||||||
OwnerID: repo.OwnerID,
|
OwnerID: repo.OwnerID,
|
||||||
|
@ -56,7 +56,7 @@ func (s *Service) Register(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// create new runner
|
// create new runner
|
||||||
runner := &bots_model.Runner{
|
runner := &bots_model.BotRunner{
|
||||||
UUID: gouuid.New().String(),
|
UUID: gouuid.New().String(),
|
||||||
Name: req.Msg.Name,
|
Name: req.Msg.Name,
|
||||||
OwnerID: runnerToken.OwnerID,
|
OwnerID: runnerToken.OwnerID,
|
||||||
@ -276,7 +276,7 @@ func (s *Service) UpdateLog(
|
|||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func pickTask(ctx context.Context, runner *bots_model.Runner) (*runnerv1.Task, bool, error) {
|
func pickTask(ctx context.Context, runner *bots_model.BotRunner) (*runnerv1.Task, bool, error) {
|
||||||
t, ok, err := bots_model.CreateTaskForRunner(ctx, runner)
|
t, ok, err := bots_model.CreateTaskForRunner(ctx, runner)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, false, fmt.Errorf("CreateTaskForRunner: %w", err)
|
return nil, false, fmt.Errorf("CreateTaskForRunner: %w", err)
|
||||||
@ -294,7 +294,7 @@ func pickTask(ctx context.Context, runner *bots_model.Runner) (*runnerv1.Task, b
|
|||||||
return task, true, nil
|
return task, true, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func getSecretsOfTask(ctx context.Context, task *bots_model.Task) map[string]string {
|
func getSecretsOfTask(ctx context.Context, task *bots_model.BotTask) map[string]string {
|
||||||
// Returning an error is worse than returning empty secrets.
|
// Returning an error is worse than returning empty secrets.
|
||||||
|
|
||||||
secrets := map[string]string{}
|
secrets := map[string]string{}
|
||||||
@ -334,7 +334,7 @@ func getSecretsOfTask(ctx context.Context, task *bots_model.Task) map[string]str
|
|||||||
return secrets
|
return secrets
|
||||||
}
|
}
|
||||||
|
|
||||||
func generateTaskContext(t *bots_model.Task) *structpb.Struct {
|
func generateTaskContext(t *bots_model.BotTask) *structpb.Struct {
|
||||||
event := map[string]interface{}{}
|
event := map[string]interface{}{}
|
||||||
_ = json.Unmarshal([]byte(t.Job.Run.EventPayload), &event)
|
_ = json.Unmarshal([]byte(t.Job.Run.EventPayload), &event)
|
||||||
|
|
||||||
|
@ -68,9 +68,9 @@ func getMethodName(req connect.AnyRequest) string {
|
|||||||
|
|
||||||
type runnerCtxKey struct{}
|
type runnerCtxKey struct{}
|
||||||
|
|
||||||
func GetRunner(ctx context.Context) *bots_model.Runner {
|
func GetRunner(ctx context.Context) *bots_model.BotRunner {
|
||||||
if v := ctx.Value(runnerCtxKey{}); v != nil {
|
if v := ctx.Value(runnerCtxKey{}); v != nil {
|
||||||
if r, ok := v.(*bots_model.Runner); ok {
|
if r, ok := v.(*bots_model.BotRunner); ok {
|
||||||
return r
|
return r
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ func RunnersList(ctx *context.Context, tplName base.TplName, opts bots_model.Fin
|
|||||||
}
|
}
|
||||||
|
|
||||||
// ownid=0,repo_id=0,means this token is used for global
|
// ownid=0,repo_id=0,means this token is used for global
|
||||||
var token *bots_model.RunnerToken
|
var token *bots_model.BotRunnerToken
|
||||||
token, err = bots_model.GetUnactivatedRunnerToken(opts.OwnerID, opts.RepoID)
|
token, err = bots_model.GetUnactivatedRunnerToken(opts.OwnerID, opts.RepoID)
|
||||||
if _, ok := err.(bots_model.ErrRunnerTokenNotExist); ok {
|
if _, ok := err.(bots_model.ErrRunnerTokenNotExist); ok {
|
||||||
token, err = bots_model.NewRunnerToken(opts.OwnerID, opts.RepoID)
|
token, err = bots_model.NewRunnerToken(opts.OwnerID, opts.RepoID)
|
||||||
|
@ -125,7 +125,7 @@ func ViewPost(ctx *context_module.Context) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
var task *bots_model.Task
|
var task *bots_model.BotTask
|
||||||
if current.TaskID > 0 {
|
if current.TaskID > 0 {
|
||||||
var err error
|
var err error
|
||||||
task, err = bots_model.GetTaskByID(ctx, current.TaskID)
|
task, err = bots_model.GetTaskByID(ctx, current.TaskID)
|
||||||
@ -263,7 +263,7 @@ func Cancel(ctx *context_module.Context) {
|
|||||||
// getRunJobs gets the jobs of runIndex, and returns jobs[jobIndex], jobs.
|
// getRunJobs gets the jobs of runIndex, and returns jobs[jobIndex], jobs.
|
||||||
// Any error will be written to the ctx.
|
// Any error will be written to the ctx.
|
||||||
// It never returns a nil job of an empty jobs, if the jobIndex is out of range, it will be treated as 0.
|
// It never returns a nil job of an empty jobs, if the jobIndex is out of range, it will be treated as 0.
|
||||||
func getRunJobs(ctx *context_module.Context, runIndex, jobIndex int64) (*bots_model.RunJob, []*bots_model.RunJob) {
|
func getRunJobs(ctx *context_module.Context, runIndex, jobIndex int64) (*bots_model.BotRunJob, []*bots_model.BotRunJob) {
|
||||||
run, err := bots_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)
|
run, err := bots_model.GetRunByIndex(ctx, ctx.Repo.Repository.ID, runIndex)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if _, ok := err.(bots_model.ErrRunNotExist); ok {
|
if _, ok := err.(bots_model.ErrRunNotExist); ok {
|
||||||
|
@ -51,7 +51,7 @@ func checkJobsOfRun(ctx context.Context, runID int64) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
idToJobs := make(map[string][]*bots_model.RunJob, len(jobs))
|
idToJobs := make(map[string][]*bots_model.BotRunJob, len(jobs))
|
||||||
for _, job := range jobs {
|
for _, job := range jobs {
|
||||||
idToJobs[job.JobID] = append(idToJobs[job.JobID], job)
|
idToJobs[job.JobID] = append(idToJobs[job.JobID], job)
|
||||||
}
|
}
|
||||||
@ -77,7 +77,7 @@ type jobStatusResolver struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func newJobStatusResolver(jobs bots_model.RunJobList) *jobStatusResolver {
|
func newJobStatusResolver(jobs bots_model.RunJobList) *jobStatusResolver {
|
||||||
idToJobs := make(map[string][]*bots_model.RunJob, len(jobs))
|
idToJobs := make(map[string][]*bots_model.BotRunJob, len(jobs))
|
||||||
for _, job := range jobs {
|
for _, job := range jobs {
|
||||||
idToJobs[job.JobID] = append(idToJobs[job.JobID], job)
|
idToJobs[job.JobID] = append(idToJobs[job.JobID], job)
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user