Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

introduce OwnerType #2

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

About the file path and the package name.
We have many XxxType in different files now, if we add OwnerType into types package in this PR, does this mean we should move the others into this folder/package in the future?

Copy link
Collaborator Author

@wxiaoguang wxiaoguang May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so, if a type is shared by different packages.

But if a type is specialized for one model, then keep it in its own package.

Copy link
Owner

@yp05327 yp05327 May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. How about convert the folder name into shared to make it more clearly to other maintainers?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe sharedtypes? I guess golang doesn't like underline in package name ...

Copy link
Owner

@yp05327 yp05327 May 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I edited my comment.
Maybe models/shared/ownertype.go and package name types is good.
We can add more shared things into this folder if we have.
If there are too many shared types, we can move them into models/shared/types/xxx.go in the future.


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
Comment on lines +116 to +119
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

individual and organization are both user types. So maybe concept_user_individual is better?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, feel free to improve~~

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your kindness.
I will make the change later.


[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