1
0
mirror of https://github.com/go-gitea/gitea.git synced 2025-04-18 00:47:48 -04:00

feat(runner-view): add task list in runnder details page, in admin

This commit is contained in:
fuxiaohei 2022-11-04 21:11:59 +08:00 committed by Jason Song
parent 8a8214113b
commit 8c3ed11ed9
8 changed files with 245 additions and 33 deletions

View File

@ -14,6 +14,49 @@ import (
type RunJobList []*RunJob
func (jobs RunJobList) GetRunIDs() []int64 {
var runIDsMap = make(map[int64]struct{})
for _, j := range jobs {
if j.RunID == 0 {
continue
}
runIDsMap[j.RunID] = struct{}{}
}
var runIDs = make([]int64, 0, len(runIDsMap))
for runID := range runIDsMap {
runIDs = append(runIDs, runID)
}
return runIDs
}
func (jobs RunJobList) LoadRuns(ctx context.Context, withRepo bool) error {
runIDs := jobs.GetRunIDs()
runs := make(map[int64]*Run, len(runIDs))
if err := db.GetEngine(ctx).In("id", runIDs).Find(&runs); err != nil {
return err
}
for _, j := range jobs {
if j.RunID > 0 && j.Run == nil {
j.Run = runs[j.RunID]
}
}
if withRepo {
var runsList RunList = make([]*Run, 0, len(runs))
for _, r := range runs {
runsList = append(runsList, r)
}
return runsList.LoadRepos()
}
return nil
}
func (jobs RunJobList) LoadAttributes(ctx context.Context, withRepo bool) error {
if err := jobs.LoadRuns(ctx, withRepo); err != nil {
return err
}
return nil
}
type FindRunJobOptions struct {
db.ListOptions
RunID int64

View File

@ -8,6 +8,7 @@ import (
"context"
"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/modules/util"
"xorm.io/builder"
@ -28,6 +29,18 @@ func (runs RunList) GetUserIDs() []int64 {
return userIDs
}
func (runs RunList) GetRepoIDs() []int64 {
repoIDsMap := make(map[int64]struct{})
for _, run := range runs {
repoIDsMap[run.RepoID] = struct{}{}
}
repoIDs := make([]int64, 0, len(repoIDsMap))
for repoID := range repoIDsMap {
repoIDs = append(repoIDs, repoID)
}
return repoIDs
}
func (runs RunList) LoadTriggerUser() error {
userIDs := runs.GetUserIDs()
users := make(map[int64]*user_model.User, len(userIDs))
@ -40,6 +53,18 @@ func (runs RunList) LoadTriggerUser() error {
return nil
}
func (runs RunList) LoadRepos() error {
repoIDs := runs.GetRepoIDs()
repos, err := repo_model.GetRepositoriesMapByIDs(repoIDs)
if err != nil {
return err
}
for _, run := range runs {
run.Repo = repos[run.RepoID]
}
return nil
}
type FindRunOptions struct {
db.ListOptions
RepoID int64

View File

@ -12,6 +12,7 @@ import (
"errors"
"fmt"
"io"
"strconv"
"time"
auth_model "code.gitea.io/gitea/models/auth"
@ -92,6 +93,64 @@ func (task *Task) TakeTime() time.Duration {
return time.Since(started).Truncate(time.Second)
}
func (task *Task) IsStopped() bool {
return task.Stopped > 0
}
func (task *Task) GetRepo() string {
return "xxxx"
}
func (task *Task) GetCommitSHA() string {
if task.Job == nil {
return ""
}
if task.Job.Run == nil {
return ""
}
return task.Job.Run.CommitSHA
}
func (task *Task) GetCommitSHAShort() string {
commitSHA := task.GetCommitSHA()
if len(commitSHA) > 8 {
return commitSHA[:8]
}
return commitSHA
}
func (task *Task) GetBuildViewLink() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
return task.Job.Run.Repo.Link() + "/builds/runs/" + strconv.FormatInt(task.ID, 10)
}
func (task *Task) GetCommitLink() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
if commitSHA := task.GetCommitSHA(); commitSHA != "" {
return task.Job.Run.Repo.CommitLink(commitSHA)
}
return ""
}
func (task *Task) GetRepoName() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
return task.Job.Run.Repo.FullName()
}
func (task *Task) GetRepoLink() string {
if task.Job == nil || task.Job.Run == nil || task.Job.Run.Repo == nil {
return ""
}
return task.Job.Run.Repo.Link()
}
func (task *Task) LoadJob(ctx context.Context) error {
if task.Job == nil {
job, err := GetRunJobByID(ctx, task.JobID)

View File

@ -14,11 +14,54 @@ import (
type TaskList []*Task
func (tasks TaskList) GetJobIDs() []int64 {
var jobIDsMap = make(map[int64]struct{})
for _, t := range tasks {
if t.JobID == 0 {
continue
}
jobIDsMap[t.JobID] = struct{}{}
}
var jobIDs = make([]int64, 0, len(jobIDsMap))
for jobID := range jobIDsMap {
jobIDs = append(jobIDs, jobID)
}
return jobIDs
}
func (tasks TaskList) LoadJobs(ctx context.Context) error {
jobIDs := tasks.GetJobIDs()
jobs := make(map[int64]*RunJob, len(jobIDs))
if err := db.GetEngine(ctx).In("id", jobIDs).Find(&jobs); err != nil {
return err
}
for _, t := range tasks {
if t.JobID > 0 && t.Job == nil {
t.Job = jobs[t.JobID]
}
}
var jobsList RunJobList = make([]*RunJob, 0, len(jobs))
for _, j := range jobs {
jobsList = append(jobsList, j)
}
return jobsList.LoadAttributes(ctx, true)
}
func (tasks TaskList) LoadAttributes(ctx context.Context) error {
if err := tasks.LoadJobs(ctx); err != nil {
return err
}
return nil
}
type FindTaskOptions struct {
db.ListOptions
Status Status
UpdatedBefore timeutil.TimeStamp
StartedBefore timeutil.TimeStamp
RunnerID int64
IDOrderDesc bool
}
func (opts FindTaskOptions) toConds() builder.Cond {
@ -32,6 +75,9 @@ func (opts FindTaskOptions) toConds() builder.Cond {
if opts.StartedBefore > 0 {
cond = cond.And(builder.Lt{"started": opts.StartedBefore})
}
if opts.RunnerID > 0 {
cond = cond.And(builder.Eq{"runner_id": opts.RunnerID})
}
return cond
}
@ -40,6 +86,9 @@ func FindTasks(ctx context.Context, opts FindTaskOptions) (TaskList, int64, erro
if opts.PageSize > 0 && opts.Page >= 1 {
e.Limit(opts.PageSize, (opts.Page-1)*opts.PageSize)
}
if opts.IDOrderDesc {
e.OrderBy("id DESC")
}
var tasks TaskList
total, err := e.FindAndCount(&tasks)
return tasks, total, err

View File

@ -80,6 +80,21 @@ func RunnerDetails(ctx *context.Context, tplName base.TplName, runnerID int64, o
ctx.Data["Runner"] = runner
// TODO: get task list for this runner
tasks, _, err := bots_model.FindTasks(ctx, bots_model.FindTaskOptions{
Status: bots_model.StatusUnknown, // Unknown means all
IDOrderDesc: true,
RunnerID: runner.ID,
})
if err != nil {
ctx.ServerError("FindTasks", err)
return
}
if err = tasks.LoadAttributes(ctx); err != nil {
ctx.ServerError("TasksLoadAttributes", err)
return
}
ctx.Data["Tasks"] = tasks
ctx.HTML(http.StatusOK, tplName)
}

View File

@ -93,30 +93,3 @@ func DeleteRunnerPost(ctx *context.Context) {
func ResetRunnerRegistrationToken(ctx *context.Context) {
common.RunnerResetRegistrationToken(ctx, 0, 0, setting.AppSubURL+"/admin/runners/")
}
/**
// NewRunner render adding a new runner page
func NewRunner(ctx *context.Context) {
ctx.Data["Title"] = ctx.Tr("admin.runners.new")
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminRunners"] = true
ctx.HTML(http.StatusOK, tplRunnerNew)
}
// NewRunnerPost response for adding a new runner
func NewRunnerPost(ctx *context.Context) {
// form := web.GetForm(ctx).(*forms.AdminCreateRunnerForm)
ctx.Data["Title"] = ctx.Tr("admin.runners.new")
ctx.Data["PageIsAdmin"] = true
ctx.Data["PageIsAdminRunners"] = true
if ctx.HasError() {
ctx.HTML(http.StatusOK, tplRunnerNew)
return
}
// ctx.Flash.Success(ctx.Tr("admin.runners.new_success", u.Name))
// ctx.Redirect(setting.AppSubURL + "/admin/users/" + strconv.FormatInt(u.ID, 10))
}
**/

View File

@ -56,6 +56,37 @@
{{.locale.Tr "admin.runners.task_list"}}
</h4>
<div class="ui attached segment">
Comming soon
<table class="ui very basic striped table unstackable">
<thead>
<tr>
<th>{{.locale.Tr "runners.task_list.job"}}</th>
<th>{{.locale.Tr "runners.task_list.status"}}</th>
<th>{{.locale.Tr "runners.task_list.repository"}}</th>
<th>{{.locale.Tr "runners.task_list.commit"}}</th>
<th>{{.locale.Tr "runners.task_list.finish_at"}}</th>
</tr>
</thead>
<tbody>
{{range .Tasks}}
<tr>
<td><a href="{{.GetBuildViewLink}}" target="_blank">#{{.ID}}</a></td>
<td><span class="ui label task-status-{{.Status.String}}">{{.Status.String}}</span></td>
<td>{{.GetRepoName}}</td>
<td><strong>
<a href="{{.GetCommitLink}}" target="_blank">{{.GetCommitSHAShort}}</a>
</strong> </td>
<td>{{if .IsStopped}}
<span>{{TimeSinceUnix .Stopped $.locale}}</span>
{{else}}-{{end}}</td>
</tr>
{{end}}
{{if not .Tasks}}
<tr>
<td colspan="5">{{.locale.Tr "runners.task_list.no_tasks"}}</td>
</tr>
{{end}}
</tbody>
</table>
{{template "base/paginate" .}}
</div>
</div>

View File

@ -1,6 +1,7 @@
@import "variables.less";
.runner-container {
padding-bottom: 30px;
.runner-ops > a {
margin-left: 0.5em;
}
@ -10,18 +11,34 @@
.runner-basic-info .dib {
margin-right: 1em;
}
.runner-status-online{
.runner-status-online {
.ui.label;
background-color: var(--color-green);
color: var(--color-white);
}
.runner-new-text{
color: var(--color-white)
.runner-new-text {
color: var(--color-white);
}
#runner-new:hover .runner-new-text{
#runner-new:hover .runner-new-text {
color: var(--color-white) !important;
}
.runner-new-menu{
.runner-new-menu {
width: 300px;
}
.task-status-success {
background-color: var(--color-green);
color: var(--color-white);
}
.task-status-failure {
background-color: var(--color-red-light);
color: var(--color-white);
}
.task-status-running {
background-color: var(--color-blue);
color: var(--color-white);
}
.task-status-cancelled, .task-status-blocked {
background-color: var(--color-yellow);
color: var(--color-white);
}
}