loading up the forgejo repo on tangled to test page performance
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

refactor improve NoBetterThan (#26126)

- The `NoBetterThan` function can only handle comparisons between
"pending," "success," "error," and "failure." For any other comparison,
we directly return false. This prevents logic errors like the one in
#26121.
- The callers of the `NoBetterThan` function should also avoid making
incomparable calls.

---------

Co-authored-by: yp05327 <576951401@qq.com>
Co-authored-by: puni9869 <80308335+puni9869@users.noreply.github.com>

authored by

caicandong
yp05327
puni9869
and committed by
GitHub
13359581 df9afe3a

+193 -8
+17 -6
modules/structs/commit_status.go
··· 4 4 package structs 5 5 6 6 // CommitStatusState holds the state of a CommitStatus 7 - // It can be "pending", "success", "error", "failure", and "warning" 7 + // It can be "pending", "success", "error" and "failure" 8 8 type CommitStatusState string 9 9 10 10 const ( ··· 18 18 CommitStatusFailure CommitStatusState = "failure" 19 19 ) 20 20 21 + var commitStatusPriorities = map[CommitStatusState]int{ 22 + CommitStatusError: 0, 23 + CommitStatusFailure: 1, 24 + CommitStatusPending: 2, 25 + CommitStatusSuccess: 3, 26 + } 27 + 21 28 // NoBetterThan returns true if this State is no better than the given State 29 + // This function only handles the states defined in CommitStatusPriorities 22 30 func (css CommitStatusState) NoBetterThan(css2 CommitStatusState) bool { 23 - commitStatusPriorities := map[CommitStatusState]int{ 24 - CommitStatusError: 0, 25 - CommitStatusFailure: 1, 26 - CommitStatusPending: 2, 27 - CommitStatusSuccess: 3, 31 + // NoBetterThan only handles the 4 states above 32 + if _, exist := commitStatusPriorities[css]; !exist { 33 + return false 34 + } 35 + 36 + if _, exist := commitStatusPriorities[css2]; !exist { 37 + return false 28 38 } 39 + 29 40 return commitStatusPriorities[css] <= commitStatusPriorities[css2] 30 41 } 31 42
+174
modules/structs/commit_status_test.go
··· 1 + // Copyright 2023 The Gitea Authors. All rights reserved. 2 + // SPDX-License-Identifier: MIT 3 + 4 + package structs 5 + 6 + import ( 7 + "testing" 8 + ) 9 + 10 + func TestNoBetterThan(t *testing.T) { 11 + type args struct { 12 + css CommitStatusState 13 + css2 CommitStatusState 14 + } 15 + var unExpectedState CommitStatusState 16 + tests := []struct { 17 + name string 18 + args args 19 + want bool 20 + }{ 21 + { 22 + name: "success is no better than success", 23 + args: args{ 24 + css: CommitStatusSuccess, 25 + css2: CommitStatusSuccess, 26 + }, 27 + want: true, 28 + }, 29 + { 30 + name: "success is no better than pending", 31 + args: args{ 32 + css: CommitStatusSuccess, 33 + css2: CommitStatusPending, 34 + }, 35 + want: false, 36 + }, 37 + { 38 + name: "success is no better than failure", 39 + args: args{ 40 + css: CommitStatusSuccess, 41 + css2: CommitStatusFailure, 42 + }, 43 + want: false, 44 + }, 45 + { 46 + name: "success is no better than error", 47 + args: args{ 48 + css: CommitStatusSuccess, 49 + css2: CommitStatusError, 50 + }, 51 + want: false, 52 + }, 53 + { 54 + name: "pending is no better than success", 55 + args: args{ 56 + css: CommitStatusPending, 57 + css2: CommitStatusSuccess, 58 + }, 59 + want: true, 60 + }, 61 + { 62 + name: "pending is no better than pending", 63 + args: args{ 64 + css: CommitStatusPending, 65 + css2: CommitStatusPending, 66 + }, 67 + want: true, 68 + }, 69 + { 70 + name: "pending is no better than failure", 71 + args: args{ 72 + css: CommitStatusPending, 73 + css2: CommitStatusFailure, 74 + }, 75 + want: false, 76 + }, 77 + { 78 + name: "pending is no better than error", 79 + args: args{ 80 + css: CommitStatusPending, 81 + css2: CommitStatusError, 82 + }, 83 + want: false, 84 + }, 85 + { 86 + name: "failure is no better than success", 87 + args: args{ 88 + css: CommitStatusFailure, 89 + css2: CommitStatusSuccess, 90 + }, 91 + want: true, 92 + }, 93 + { 94 + name: "failure is no better than pending", 95 + args: args{ 96 + css: CommitStatusFailure, 97 + css2: CommitStatusPending, 98 + }, 99 + want: true, 100 + }, 101 + { 102 + name: "failure is no better than failure", 103 + args: args{ 104 + css: CommitStatusFailure, 105 + css2: CommitStatusFailure, 106 + }, 107 + want: true, 108 + }, 109 + { 110 + name: "failure is no better than error", 111 + args: args{ 112 + css: CommitStatusFailure, 113 + css2: CommitStatusError, 114 + }, 115 + want: false, 116 + }, 117 + { 118 + name: "error is no better than success", 119 + args: args{ 120 + css: CommitStatusError, 121 + css2: CommitStatusSuccess, 122 + }, 123 + want: true, 124 + }, 125 + { 126 + name: "error is no better than pending", 127 + args: args{ 128 + css: CommitStatusError, 129 + css2: CommitStatusPending, 130 + }, 131 + want: true, 132 + }, 133 + { 134 + name: "error is no better than failure", 135 + args: args{ 136 + css: CommitStatusError, 137 + css2: CommitStatusFailure, 138 + }, 139 + want: true, 140 + }, 141 + { 142 + name: "error is no better than error", 143 + args: args{ 144 + css: CommitStatusError, 145 + css2: CommitStatusError, 146 + }, 147 + want: true, 148 + }, 149 + { 150 + name: "unExpectedState is no better than success", 151 + args: args{ 152 + css: unExpectedState, 153 + css2: CommitStatusSuccess, 154 + }, 155 + want: false, 156 + }, 157 + { 158 + name: "unExpectedState is no better than unExpectedState", 159 + args: args{ 160 + css: unExpectedState, 161 + css2: unExpectedState, 162 + }, 163 + want: false, 164 + }, 165 + } 166 + for _, tt := range tests { 167 + t.Run(tt.name, func(t *testing.T) { 168 + result := tt.args.css.NoBetterThan(tt.args.css2) 169 + if result != tt.want { 170 + t.Errorf("NoBetterThan() = %v, want %v", result, tt.want) 171 + } 172 + }) 173 + } 174 + }
+1 -1
services/convert/status.go
··· 48 48 retStatus.Statuses = make([]*api.CommitStatus, 0, len(statuses)) 49 49 for _, status := range statuses { 50 50 retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(ctx, status)) 51 - if status.State.NoBetterThan(retStatus.State) { 51 + if retStatus.State == "" || status.State.NoBetterThan(retStatus.State) { 52 52 retStatus.State = status.State 53 53 } 54 54 }
+1 -1
templates/swagger/v1_json.tmpl
··· 16514 16514 "x-go-package": "code.gitea.io/gitea/modules/structs" 16515 16515 }, 16516 16516 "CommitStatusState": { 16517 - "description": "CommitStatusState holds the state of a CommitStatus\nIt can be \"pending\", \"success\", \"error\", \"failure\", and \"warning\"", 16517 + "description": "CommitStatusState holds the state of a CommitStatus\nIt can be \"pending\", \"success\", \"error\" and \"failure\"", 16518 16518 "type": "string", 16519 16519 "x-go-package": "code.gitea.io/gitea/modules/structs" 16520 16520 },