Skip to content

Commit

Permalink
Merge pull request #2 from wxiaoguang/improve-runer-type-display
Browse files Browse the repository at this point in the history
introduce OwnerType
  • Loading branch information
yp05327 authored May 11, 2023
2 parents 9c9716e + 20b6817 commit 1a71427
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 39 deletions.
47 changes: 11 additions & 36 deletions models/actions/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (

"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/types"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/translation"
Expand All @@ -28,7 +29,7 @@ type ActionRunner struct {
Version string `xorm:"VARCHAR(64)"`
OwnerID int64 `xorm:"index"` // org level runner, 0 means system
Owner *user_model.User `xorm:"-"`
RepoID int64 `xorm:"index"` // repo level runner, if orgid also is zero, then it's a global
RepoID int64 `xorm:"index"` // repo level runner, if OwnerID also is zero, then it's a global
Repo *repo_model.Repository `xorm:"-"`
Description string `xorm:"TEXT"`
Base int // 0 native 1 docker 2 virtual machine
Expand All @@ -52,51 +53,25 @@ type ActionRunner struct {
Deleted timeutil.TimeStamp `xorm:"deleted"`
}

// RunnerType defines the runner type
type RunnerType int

const (
// RunnerTypeGlobal defines a global runner
RunnerTypeGlobal RunnerType = iota

// RunnerTypeOrganization defines an organization runner
RunnerTypeOrganization

// RunnerTypeRepository defines a repository runner
RunnerTypeRepository
)

func (r *ActionRunner) Type() RunnerType {
// BelongsToOwnerName before calling, should guarantee that all attributes are loaded
func (r *ActionRunner) BelongsToOwnerName() string {
if r.RepoID != 0 {
return RunnerTypeRepository
return r.Repo.FullName()
}
if r.OwnerID != 0 {
return RunnerTypeOrganization
}
return RunnerTypeGlobal
}

func (rt RunnerType) LocaleString(locale translation.Locale) string {
switch rt {
case RunnerTypeGlobal:
return locale.Tr("actions.runners.global_type")
case RunnerTypeOrganization:
return locale.Tr("organization")
case RunnerTypeRepository:
return locale.Tr("repository")
return r.Owner.Name
}
return locale.Tr("unknown")
return ""
}

// Should guarantee that all attributes are loaded
func (r *ActionRunner) BelongsTo() string {
func (r *ActionRunner) BelongsToOwnerType() types.OwnerType {
if r.RepoID != 0 {
return r.Repo.FullName()
return types.OwnerTypeRepository
}
if r.OwnerID != 0 {
return r.Owner.Name
return types.OwnerTypeOrganization
}
return ""
return types.OwnerTypeSystemGlobal
}

func (r *ActionRunner) Status() runnerv1.RunnerStatus {
Expand Down
26 changes: 26 additions & 0 deletions models/types/ownertype.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package types

import "code.gitea.io/gitea/modules/translation"

type OwnerType string

const (
OwnerTypeSystemGlobal = "system-global"
OwnerTypeIndividual = "individual"
OwnerTypeRepository = "repository"
OwnerTypeOrganization = "organization"
)

func (o OwnerType) LocaleString(locale translation.Locale) string {
switch o {
case OwnerTypeSystemGlobal:
return locale.Tr("concept_system_global")
case OwnerTypeIndividual:
return locale.Tr("concept_person_individual")
case OwnerTypeRepository:
return locale.Tr("concept_code_repository")
case OwnerTypeOrganization:
return locale.Tr("concept_user_organization")
}
return locale.Tr("unknown")
}
6 changes: 5 additions & 1 deletion options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,11 @@ unknown = Unknown

rss_feed = RSS Feed

concept_system_global = Global
concept_person_individual = Individual
concept_code_repository = Repository
concept_user_organization = Organization

[aria]
navbar = Navigation Bar
footer = Footer
Expand Down Expand Up @@ -3409,7 +3414,6 @@ runners.status = Status
runners.id = ID
runners.name = Name
runners.owner_type = Type
runners.type_global = Global
runners.description = Description
runners.labels = Labels
runners.last_online = Last Online Time
Expand Down
2 changes: 1 addition & 1 deletion templates/shared/actions/runner_edit.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</div>
<div class="field gt-dib gt-mr-4">
<label>{{.locale.Tr "actions.runners.owner_type"}}</label>
<span data-tooltip-content="{{.Runner.BelongsTo}}">{{.Runner.Type.LocaleString $.locale}}</span>
<span data-tooltip-content="{{.Runner.BelongsToOwnerName}}">{{.Runner.BelongsToOwnerType.LocaleString $.locale}}</span>
</div>
</div>

Expand Down
2 changes: 1 addition & 1 deletion templates/shared/actions/runner_list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
<td>{{.ID}}</td>
<td><p data-tooltip-content="{{.Description}}">{{.Name}}</p></td>
<td>{{if .Version}}{{.Version}}{{else}}{{$.locale.Tr "unknown"}}{{end}}</td>
<td><span data-tooltip-content="{{.BelongsTo}}">{{.Type.LocaleString $.locale}}<span></td>
<td><span data-tooltip-content="{{.BelongsToOwnerName}}">{{.BelongsToOwnerType.LocaleString $.locale}}<span></td>
<td class="runner-tags">
{{range .AllLabels}}<span class="ui label">{{.}}</span>{{end}}
</td>
Expand Down

0 comments on commit 1a71427

Please sign in to comment.