Merge pull request 'Enable switch check in linter' (#4555) from thefox/reenable-switch-check into forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/4555
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Reviewed-by: Otto <otto@codeberg.org>
This commit is contained in:
Otto 2024-08-18 15:56:35 +00:00
commit 2cbc9cec73
21 changed files with 128 additions and 125 deletions

View file

@ -43,7 +43,6 @@ linters-settings:
gocritic: gocritic:
disabled-checks: disabled-checks:
- ifElseChain - ifElseChain
- singleCaseSwitch # Every time this occurred in the code, there was no other way.
revive: revive:
severity: error severity: error
rules: rules:

View file

@ -58,6 +58,7 @@ func Cell2Int64(val xorm.Cell) int64 {
v, _ := strconv.ParseInt(string((*val).([]uint8)), 10, 64) v, _ := strconv.ParseInt(string((*val).([]uint8)), 10, 64)
return v return v
default:
return (*val).(int64)
} }
return (*val).(int64)
} }

View file

@ -235,8 +235,7 @@ func (cfg *ActionsConfig) ToDB() ([]byte, error) {
// BeforeSet is invoked from XORM before setting the value of a field of this object. // BeforeSet is invoked from XORM before setting the value of a field of this object.
func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) { func (r *RepoUnit) BeforeSet(colName string, val xorm.Cell) {
switch colName { if colName == "type" {
case "type":
switch unit.Type(db.Cell2Int64(val)) { switch unit.Type(db.Cell2Int64(val)) {
case unit.TypeExternalWiki: case unit.TypeExternalWiki:
r.Config = new(ExternalWikiConfig) r.Config = new(ExternalWikiConfig)

View file

@ -649,8 +649,7 @@ func matchReleaseEvent(payload *api.ReleasePayload, evt *jobparser.Event) bool {
// unpublished, created, deleted, prereleased, released // unpublished, created, deleted, prereleased, released
action := payload.Action action := payload.Action
switch action { if action == api.HookReleaseUpdated {
case api.HookReleaseUpdated:
action = "edited" action = "edited"
} }
for _, val := range vals { for _, val := range vals {
@ -686,8 +685,7 @@ func matchPackageEvent(payload *api.PackagePayload, evt *jobparser.Event) bool {
// updated // updated
action := payload.Action action := payload.Action
switch action { if action == api.HookPackageCreated {
case api.HookPackageCreated:
action = "published" action = "published"
} }
for _, val := range vals { for _, val := range vals {

View file

@ -16,58 +16,67 @@ import (
func TestDetectMatched(t *testing.T) { func TestDetectMatched(t *testing.T) {
testCases := []struct { testCases := []struct {
desc string desc string
commit *git.Commit commit *git.Commit
triggedEvent webhook_module.HookEventType triggeredEvent webhook_module.HookEventType
payload api.Payloader payload api.Payloader
yamlOn string yamlOn string
expected bool expected bool
}{ }{
{ {
desc: "HookEventCreate(create) matches GithubEventCreate(create)", desc: "HookEventCreate(create) matches GithubEventCreate(create)",
triggedEvent: webhook_module.HookEventCreate, triggeredEvent: webhook_module.HookEventCreate,
payload: nil, payload: nil,
yamlOn: "on: create", yamlOn: "on: create",
expected: true, expected: true,
}, },
{ {
desc: "HookEventIssues(issues) `opened` action matches GithubEventIssues(issues)", desc: "HookEventIssues(issues) `opened` action matches GithubEventIssues(issues)",
triggedEvent: webhook_module.HookEventIssues, triggeredEvent: webhook_module.HookEventIssues,
payload: &api.IssuePayload{Action: api.HookIssueOpened}, payload: &api.IssuePayload{Action: api.HookIssueOpened},
yamlOn: "on: issues", yamlOn: "on: issues",
expected: true, expected: true,
}, },
{ {
desc: "HookEventIssues(issues) `milestoned` action matches GithubEventIssues(issues)", desc: "HookEventIssueComment(issue_comment) `created` action matches GithubEventIssueComment(issue_comment)",
triggedEvent: webhook_module.HookEventIssues, triggeredEvent: webhook_module.HookEventIssueComment,
payload: &api.IssuePayload{Action: api.HookIssueMilestoned}, payload: &api.IssueCommentPayload{Action: api.HookIssueCommentCreated},
yamlOn: "on: issues", yamlOn: "on:\n issue_comment:\n types: [created]",
expected: true, expected: true,
},
{
desc: "HookEventIssues(issues) `milestoned` action matches GithubEventIssues(issues)",
triggeredEvent: webhook_module.HookEventIssues,
payload: &api.IssuePayload{Action: api.HookIssueMilestoned},
yamlOn: "on: issues",
expected: true,
},
{
desc: "HookEventPullRequestSync(pull_request_sync) matches GithubEventPullRequest(pull_request)",
triggeredEvent: webhook_module.HookEventPullRequestSync,
payload: &api.PullRequestPayload{Action: api.HookIssueSynchronized},
yamlOn: "on: pull_request",
expected: true,
}, },
{ {
desc: "HookEventPullRequestSync(pull_request_sync) matches GithubEventPullRequest(pull_request)", desc: "HookEventPullRequest(pull_request) `label_updated` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
triggedEvent: webhook_module.HookEventPullRequestSync, triggeredEvent: webhook_module.HookEventPullRequest,
payload: &api.PullRequestPayload{Action: api.HookIssueSynchronized}, payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
yamlOn: "on: pull_request", yamlOn: "on: pull_request",
expected: true, expected: false,
}, },
{ {
desc: "HookEventPullRequest(pull_request) `label_updated` action doesn't match GithubEventPullRequest(pull_request) with no activity type", desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with no activity type",
triggedEvent: webhook_module.HookEventPullRequest, triggeredEvent: webhook_module.HookEventPullRequest,
payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated}, payload: &api.PullRequestPayload{Action: api.HookIssueClosed},
yamlOn: "on: pull_request", yamlOn: "on: pull_request",
expected: false, expected: false,
}, },
{ {
desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with no activity type", desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with branches",
triggedEvent: webhook_module.HookEventPullRequest, triggeredEvent: webhook_module.HookEventPullRequest,
payload: &api.PullRequestPayload{Action: api.HookIssueClosed},
yamlOn: "on: pull_request",
expected: false,
},
{
desc: "HookEventPullRequest(pull_request) `closed` action doesn't match GithubEventPullRequest(pull_request) with branches",
triggedEvent: webhook_module.HookEventPullRequest,
payload: &api.PullRequestPayload{ payload: &api.PullRequestPayload{
Action: api.HookIssueClosed, Action: api.HookIssueClosed,
PullRequest: &api.PullRequest{ PullRequest: &api.PullRequest{
@ -78,60 +87,68 @@ func TestDetectMatched(t *testing.T) {
expected: false, expected: false,
}, },
{ {
desc: "HookEventPullRequest(pull_request) `label_updated` action matches GithubEventPullRequest(pull_request) with `label` activity type", desc: "HookEventPullRequest(pull_request) `label_updated` action matches GithubEventPullRequest(pull_request) with `label` activity type",
triggedEvent: webhook_module.HookEventPullRequest, triggeredEvent: webhook_module.HookEventPullRequest,
payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated}, payload: &api.PullRequestPayload{Action: api.HookIssueLabelUpdated},
yamlOn: "on:\n pull_request:\n types: [labeled]", yamlOn: "on:\n pull_request:\n types: [labeled]",
expected: true, expected: true,
}, },
{ {
desc: "HookEventPullRequestReviewComment(pull_request_review_comment) matches GithubEventPullRequestReviewComment(pull_request_review_comment)", desc: "HookEventPullRequestReviewComment(pull_request_review_comment) matches GithubEventPullRequestReviewComment(pull_request_review_comment)",
triggedEvent: webhook_module.HookEventPullRequestReviewComment, triggeredEvent: webhook_module.HookEventPullRequestReviewComment,
payload: &api.PullRequestPayload{Action: api.HookIssueReviewed}, payload: &api.PullRequestPayload{Action: api.HookIssueReviewed},
yamlOn: "on:\n pull_request_review_comment:\n types: [created]", yamlOn: "on:\n pull_request_review_comment:\n types: [created]",
expected: true, expected: true,
}, },
{ {
desc: "HookEventPullRequestReviewRejected(pull_request_review_rejected) doesn't match GithubEventPullRequestReview(pull_request_review) with `dismissed` activity type (we don't support `dismissed` at present)", desc: "HookEventPullRequestReviewRejected(pull_request_review_rejected) doesn't match GithubEventPullRequestReview(pull_request_review) with `dismissed` activity type (we don't support `dismissed` at present)",
triggedEvent: webhook_module.HookEventPullRequestReviewRejected, triggeredEvent: webhook_module.HookEventPullRequestReviewRejected,
payload: &api.PullRequestPayload{Action: api.HookIssueReviewed}, payload: &api.PullRequestPayload{Action: api.HookIssueReviewed},
yamlOn: "on:\n pull_request_review:\n types: [dismissed]", yamlOn: "on:\n pull_request_review:\n types: [dismissed]",
expected: false, expected: false,
}, },
{ {
desc: "HookEventRelease(release) `published` action matches GithubEventRelease(release) with `published` activity type", desc: "HookEventRelease(release) `published` action matches GithubEventRelease(release) with `published` activity type",
triggedEvent: webhook_module.HookEventRelease, triggeredEvent: webhook_module.HookEventRelease,
payload: &api.ReleasePayload{Action: api.HookReleasePublished}, payload: &api.ReleasePayload{Action: api.HookReleasePublished},
yamlOn: "on:\n release:\n types: [published]", yamlOn: "on:\n release:\n types: [published]",
expected: true, expected: true,
}, },
{ {
desc: "HookEventPackage(package) `created` action doesn't match GithubEventRegistryPackage(registry_package) with `updated` activity type", desc: "HookEventRelease(updated) `updated` action matches GithubEventRelease(edited) with `edited` activity type",
triggedEvent: webhook_module.HookEventPackage, triggeredEvent: webhook_module.HookEventRelease,
payload: &api.PackagePayload{Action: api.HookPackageCreated}, payload: &api.ReleasePayload{Action: api.HookReleaseUpdated},
yamlOn: "on:\n registry_package:\n types: [updated]", yamlOn: "on:\n release:\n types: [edited]",
expected: false, expected: true,
},
{
desc: "HookEventPackage(package) `created` action doesn't match GithubEventRegistryPackage(registry_package) with `updated` activity type",
triggeredEvent: webhook_module.HookEventPackage,
payload: &api.PackagePayload{Action: api.HookPackageCreated},
yamlOn: "on:\n registry_package:\n types: [updated]",
expected: false,
}, },
{ {
desc: "HookEventWiki(wiki) matches GithubEventGollum(gollum)", desc: "HookEventWiki(wiki) matches GithubEventGollum(gollum)",
triggedEvent: webhook_module.HookEventWiki, triggeredEvent: webhook_module.HookEventWiki,
payload: nil, payload: nil,
yamlOn: "on: gollum", yamlOn: "on: gollum",
expected: true, expected: true,
}, },
{ {
desc: "HookEventSchedue(schedule) matches GithubEventSchedule(schedule)", desc: "HookEventSchedule(schedule) matches GithubEventSchedule(schedule)",
triggedEvent: webhook_module.HookEventSchedule, triggeredEvent: webhook_module.HookEventSchedule,
payload: nil, payload: nil,
yamlOn: "on: schedule", yamlOn: "on: schedule",
expected: true, expected: true,
}, },
{ {
desc: "HookEventWorkflowDispatch(workflow_dispatch) matches GithubEventWorkflowDispatch(workflow_dispatch)", desc: "HookEventWorkflowDispatch(workflow_dispatch) matches GithubEventWorkflowDispatch(workflow_dispatch)",
triggedEvent: webhook_module.HookEventWorkflowDispatch, triggeredEvent: webhook_module.HookEventWorkflowDispatch,
payload: nil, payload: nil,
yamlOn: "on: workflow_dispatch", yamlOn: "on: workflow_dispatch",
expected: true, expected: true,
}, },
} }
@ -140,7 +157,7 @@ func TestDetectMatched(t *testing.T) {
evts, err := GetEventsFromContent([]byte(tc.yamlOn)) evts, err := GetEventsFromContent([]byte(tc.yamlOn))
require.NoError(t, err) require.NoError(t, err)
assert.Len(t, evts, 1) assert.Len(t, evts, 1)
assert.Equal(t, tc.expected, detectMatched(nil, tc.commit, tc.triggedEvent, tc.payload, evts[0])) assert.Equal(t, tc.expected, detectMatched(nil, tc.commit, tc.triggeredEvent, tc.payload, evts[0]))
}) })
} }
} }

View file

@ -16,8 +16,9 @@ func GetItemByType(typ ap.ActivityVocabularyType) (ap.Item, error) {
switch typ { switch typ {
case RepositoryType: case RepositoryType:
return RepositoryNew(""), nil return RepositoryNew(""), nil
default:
return ap.GetItemByType(typ)
} }
return ap.GetItemByType(typ)
} }
// JSONUnmarshalerFn is the function that will load the data from a fastjson.Value into an Item // JSONUnmarshalerFn is the function that will load the data from a fastjson.Value into an Item
@ -28,8 +29,9 @@ func JSONUnmarshalerFn(typ ap.ActivityVocabularyType, val *fastjson.Value, i ap.
return OnRepository(i, func(r *Repository) error { return OnRepository(i, func(r *Repository) error {
return JSONLoadRepository(val, r) return JSONLoadRepository(val, r)
}) })
default:
return nil
} }
return nil
} }
// NotEmpty is the function that checks if an object is empty // NotEmpty is the function that checks if an object is empty
@ -44,6 +46,7 @@ func NotEmpty(i ap.Item) bool {
return false return false
} }
return ap.NotEmpty(r.Actor) return ap.NotEmpty(r.Actor)
default:
return ap.NotEmpty(i)
} }
return ap.NotEmpty(i)
} }

View file

@ -34,8 +34,7 @@ func (g *GitHubCalloutTransformer) Transform(node *ast.Document, reader text.Rea
return ast.WalkContinue, nil return ast.WalkContinue, nil
} }
switch v := n.(type) { if v, ok := n.(*ast.Blockquote); ok {
case *ast.Blockquote:
if v.ChildCount() == 0 { if v.ChildCount() == 0 {
return ast.WalkContinue, nil return ast.WalkContinue, nil
} }

View file

@ -23,8 +23,7 @@ func (g *GitHubLegacyCalloutTransformer) Transform(node *ast.Document, reader te
return ast.WalkContinue, nil return ast.WalkContinue, nil
} }
switch v := n.(type) { if v, ok := n.(*ast.Blockquote); ok {
case *ast.Blockquote:
if v.ChildCount() == 0 { if v.ChildCount() == 0 {
return ast.WalkContinue, nil return ast.WalkContinue, nil
} }

View file

@ -98,8 +98,7 @@ func getLicensePlaceholder(name string) *licensePlaceholder {
// Some special placeholders for specific licenses. // Some special placeholders for specific licenses.
// It's unsafe to apply them to all licenses. // It's unsafe to apply them to all licenses.
switch name { if name == "0BSD" {
case "0BSD":
return &licensePlaceholder{ return &licensePlaceholder{
Owner: []string{"AUTHOR"}, Owner: []string{"AUTHOR"},
Email: []string{"EMAIL"}, Email: []string{"EMAIL"},

View file

@ -122,8 +122,7 @@ func (p *RedisProvider) Init(maxlifetime int64, configs string) (err error) {
uri := nosql.ToRedisURI(configs) uri := nosql.ToRedisURI(configs)
for k, v := range uri.Query() { for k, v := range uri.Query() {
switch k { if k == "prefix" {
case "prefix":
p.prefix = v[0] p.prefix = v[0]
} }
} }

View file

@ -13,8 +13,9 @@ func (taskType TaskType) Name() string {
switch taskType { switch taskType {
case TaskTypeMigrateRepo: case TaskTypeMigrateRepo:
return "Migrate Repository" return "Migrate Repository"
default:
return ""
} }
return ""
} }
// TaskStatus defines task status // TaskStatus defines task status

View file

@ -208,8 +208,7 @@ func ViewPackageVersion(ctx *context.Context) {
groups := make(container.Set[string]) groups := make(container.Set[string])
for _, f := range pd.Files { for _, f := range pd.Files {
for _, pp := range f.Properties { for _, pp := range f.Properties {
switch pp.Name { if pp.Name == arch_model.PropertyDistribution {
case arch_model.PropertyDistribution:
groups.Add(pp.Value) groups.Add(pp.Value)
} }
} }

View file

@ -83,12 +83,9 @@ func (entry *Workflow) Dispatch(ctx context.Context, inputGetter InputValueGette
} }
continue continue
} }
} else { } else if input.Type == "boolean" {
switch input.Type { // Since "boolean" inputs are rendered as a checkbox in html, the value inside the form is "on"
case "boolean": val = strconv.FormatBool(val == "on")
// Since "boolean" inputs are rendered as a checkbox in html, the value inside the form is "on"
val = strconv.FormatBool(val == "on")
}
} }
inputs[key] = val inputs[key] = val
} }

View file

@ -181,7 +181,7 @@ func (h *UnsubscribeHandler) Handle(ctx context.Context, _ *MailContent, doer *u
} }
return issues_model.CreateOrUpdateIssueWatch(ctx, doer.ID, issue.ID, false) return issues_model.CreateOrUpdateIssueWatch(ctx, doer.ID, issue.ID, false)
default:
return fmt.Errorf("unsupported unsubscribe reference: %v", ref)
} }
return fmt.Errorf("unsupported unsubscribe reference: %v", ref)
} }

View file

@ -160,8 +160,7 @@ func (dc dingtalkConvertor) PullRequest(p *api.PullRequestPayload) (DingtalkPayl
// Review implements PayloadConvertor Review method // Review implements PayloadConvertor Review method
func (dc dingtalkConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (DingtalkPayload, error) { func (dc dingtalkConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (DingtalkPayload, error) {
var text, title string var text, title string
switch p.Action { if p.Action == api.HookIssueReviewed {
case api.HookIssueReviewed:
action, err := parseHookPullRequestEventType(event) action, err := parseHookPullRequestEventType(event)
if err != nil { if err != nil {
return DingtalkPayload{}, err return DingtalkPayload{}, err

View file

@ -215,8 +215,7 @@ func (d discordConvertor) PullRequest(p *api.PullRequestPayload) (DiscordPayload
func (d discordConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (DiscordPayload, error) { func (d discordConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (DiscordPayload, error) {
var text, title string var text, title string
var color int var color int
switch p.Action { if p.Action == api.HookIssueReviewed {
case api.HookIssueReviewed:
action, err := parseHookPullRequestEventType(event) action, err := parseHookPullRequestEventType(event)
if err != nil { if err != nil {
return DiscordPayload{}, err return DiscordPayload{}, err

View file

@ -237,8 +237,7 @@ func (m matrixConvertor) Review(p *api.PullRequestPayload, event webhook_module.
repoLink := htmlLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName) repoLink := htmlLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
var text string var text string
switch p.Action { if p.Action == api.HookIssueReviewed {
case api.HookIssueReviewed:
action, err := parseHookPullRequestEventType(event) action, err := parseHookPullRequestEventType(event)
if err != nil { if err != nil {
return MatrixPayload{}, err return MatrixPayload{}, err

View file

@ -225,8 +225,7 @@ func (m msteamsConvertor) PullRequest(p *api.PullRequestPayload) (MSTeamsPayload
func (m msteamsConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (MSTeamsPayload, error) { func (m msteamsConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (MSTeamsPayload, error) {
var text, title string var text, title string
var color int var color int
switch p.Action { if p.Action == api.HookIssueReviewed {
case api.HookIssueReviewed:
action, err := parseHookPullRequestEventType(event) action, err := parseHookPullRequestEventType(event)
if err != nil { if err != nil {
return MSTeamsPayload{}, err return MSTeamsPayload{}, err

View file

@ -289,8 +289,7 @@ func (s slackConvertor) Review(p *api.PullRequestPayload, event webhook_module.H
repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName) repoLink := SlackLinkFormatter(p.Repository.HTMLURL, p.Repository.FullName)
var text string var text string
switch p.Action { if p.Action == api.HookIssueReviewed {
case api.HookIssueReviewed:
action, err := parseHookPullRequestEventType(event) action, err := parseHookPullRequestEventType(event)
if err != nil { if err != nil {
return SlackPayload{}, err return SlackPayload{}, err

View file

@ -164,8 +164,7 @@ func (t telegramConvertor) PullRequest(p *api.PullRequestPayload) (TelegramPaylo
// Review implements PayloadConvertor Review method // Review implements PayloadConvertor Review method
func (t telegramConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (TelegramPayload, error) { func (t telegramConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (TelegramPayload, error) {
var text, attachmentText string var text, attachmentText string
switch p.Action { if p.Action == api.HookIssueReviewed {
case api.HookIssueReviewed:
action, err := parseHookPullRequestEventType(event) action, err := parseHookPullRequestEventType(event)
if err != nil { if err != nil {
return TelegramPayload{}, err return TelegramPayload{}, err

View file

@ -154,8 +154,7 @@ func (wc wechatworkConvertor) PullRequest(p *api.PullRequestPayload) (Wechatwork
// Review implements PayloadConvertor Review method // Review implements PayloadConvertor Review method
func (wc wechatworkConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (WechatworkPayload, error) { func (wc wechatworkConvertor) Review(p *api.PullRequestPayload, event webhook_module.HookEventType) (WechatworkPayload, error) {
var text, title string var text, title string
switch p.Action { if p.Action == api.HookIssueReviewed {
case api.HookIssueReviewed:
action, err := parseHookPullRequestEventType(event) action, err := parseHookPullRequestEventType(event)
if err != nil { if err != nil {
return WechatworkPayload{}, err return WechatworkPayload{}, err