From 887d6a30d0648b4771aa4412574c5a9a68ca4cc4 Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 25 Jul 2023 17:49:11 +0800 Subject: [PATCH 1/9] improve noBetterThan --- modules/structs/commit_status.go | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/modules/structs/commit_status.go b/modules/structs/commit_status.go index ff31f2d2ac3de..44213e32473c8 100644 --- a/modules/structs/commit_status.go +++ b/modules/structs/commit_status.go @@ -4,7 +4,7 @@ package structs // CommitStatusState holds the state of a CommitStatus -// It can be "pending", "success", "error", "failure", and "warning" +// It can be "pending", "success", "error", "failure" type CommitStatusState string const ( @@ -19,6 +19,7 @@ const ( ) // NoBetterThan returns true if this State is no better than the given State +// the function only handles the 4 states above func (css CommitStatusState) NoBetterThan(css2 CommitStatusState) bool { commitStatusPriorities := map[CommitStatusState]int{ CommitStatusError: 0, @@ -26,6 +27,15 @@ func (css CommitStatusState) NoBetterThan(css2 CommitStatusState) bool { CommitStatusPending: 2, CommitStatusSuccess: 3, } + // NoBetterThan only handles the 4 states above + if _, exist := commitStatusPriorities[css]; !exist { + return false + } + + if _, exist := commitStatusPriorities[css2]; !exist { + return false + } + return commitStatusPriorities[css] <= commitStatusPriorities[css2] } From ffcaef691e09c2b38b737f09d8262deaf1f7d07a Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 25 Jul 2023 17:52:55 +0800 Subject: [PATCH 2/9] avoid meaningless comparisons --- services/convert/status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/services/convert/status.go b/services/convert/status.go index c7b6450e272d3..6cef63c1cdadf 100644 --- a/services/convert/status.go +++ b/services/convert/status.go @@ -48,7 +48,7 @@ func ToCombinedStatus(ctx context.Context, statuses []*git_model.CommitStatus, r retStatus.Statuses = make([]*api.CommitStatus, 0, len(statuses)) for _, status := range statuses { retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(ctx, status)) - if status.State.NoBetterThan(retStatus.State) { + if retStatus.State == "" || status.State.NoBetterThan(retStatus.State) { retStatus.State = status.State } } From cc07f3a346ccdd98b7423e4d3091595e1f64c893 Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 25 Jul 2023 20:42:37 +0800 Subject: [PATCH 3/9] update swagger doc --- templates/swagger/v1_json.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 69874bdc75f04..d89b254f10dce 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -16514,7 +16514,7 @@ "x-go-package": "code.gitea.io/gitea/modules/structs" }, "CommitStatusState": { - "description": "CommitStatusState holds the state of a CommitStatus\nIt can be \"pending\", \"success\", \"error\", \"failure\", and \"warning\"", + "description": "CommitStatusState holds the state of a CommitStatus\nIt can be \"pending\", \"success\", \"error\", \"failure\"", "type": "string", "x-go-package": "code.gitea.io/gitea/modules/structs" }, From f465eb8450ec2bf4f1ed1a42a7b1093622c40859 Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 25 Jul 2023 20:44:53 +0800 Subject: [PATCH 4/9] add unit test --- modules/structs/commit_status_test.go | 171 ++++++++++++++++++++++++++ 1 file changed, 171 insertions(+) create mode 100644 modules/structs/commit_status_test.go diff --git a/modules/structs/commit_status_test.go b/modules/structs/commit_status_test.go new file mode 100644 index 0000000000000..aac0faa55065d --- /dev/null +++ b/modules/structs/commit_status_test.go @@ -0,0 +1,171 @@ +package structs + +import ( + "testing" +) + +func TestNoBetterThan(t *testing.T) { + type args struct { + css CommitStatusState + css2 CommitStatusState + } + var unExpectedState CommitStatusState + tests := []struct { + name string + args args + want bool + }{ + { + name: "success is no better than success", + args: args{ + css: CommitStatusSuccess, + css2: CommitStatusSuccess, + }, + want: true, + }, + { + name: "success is no better than pending", + args: args{ + css: CommitStatusSuccess, + css2: CommitStatusPending, + }, + want: false, + }, + { + name: "success is no better than failure", + args: args{ + css: CommitStatusSuccess, + css2: CommitStatusFailure, + }, + want: false, + }, + { + name: "success is no better than error", + args: args{ + css: CommitStatusSuccess, + css2: CommitStatusError, + }, + want: false, + }, + { + name: "pending is no better than success", + args: args{ + css: CommitStatusPending, + css2: CommitStatusSuccess, + }, + want: true, + }, + { + name: "pending is no better than pending", + args: args{ + css: CommitStatusPending, + css2: CommitStatusPending, + }, + want: true, + }, + { + name: "pending is no better than failure", + args: args{ + css: CommitStatusPending, + css2: CommitStatusFailure, + }, + want: false, + }, + { + name: "pending is no better than error", + args: args{ + css: CommitStatusPending, + css2: CommitStatusError, + }, + want: false, + }, + { + name: "failure is no better than success", + args: args{ + css: CommitStatusFailure, + css2: CommitStatusSuccess, + }, + want: true, + }, + { + name: "failure is no better than pending", + args: args{ + css: CommitStatusFailure, + css2: CommitStatusPending, + }, + want: true, + }, + { + name: "failure is no better than failure", + args: args{ + css: CommitStatusFailure, + css2: CommitStatusFailure, + }, + want: true, + }, + { + name: "failure is no better than error", + args: args{ + css: CommitStatusFailure, + css2: CommitStatusError, + }, + want: false, + }, + { + name: "error is no better than success", + args: args{ + css: CommitStatusError, + css2: CommitStatusSuccess, + }, + want: true, + }, + { + name: "error is no better than pending", + args: args{ + css: CommitStatusError, + css2: CommitStatusPending, + }, + want: true, + }, + { + name: "error is no better than failure", + args: args{ + css: CommitStatusError, + css2: CommitStatusFailure, + }, + want: true, + }, + { + name: "error is no better than error", + args: args{ + css: CommitStatusError, + css2: CommitStatusError, + }, + want: true, + }, + { + name: "unExpectedState is no better than success", + args: args{ + css: unExpectedState, + css2: CommitStatusSuccess, + }, + want: false, + }, + { + name: "unExpectedState is no better than unExpectedState", + args: args{ + css: unExpectedState, + css2: unExpectedState, + }, + want: false, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := tt.args.css.NoBetterThan(tt.args.css2) + if result != tt.want { + t.Errorf("NoBetterThan() = %v, want %v", result, tt.want) + } + }) + } +} From 9dd8d4d31104a8ca3939371e1e871c1ecebf1d67 Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 25 Jul 2023 21:17:34 +0800 Subject: [PATCH 5/9] fix lint --- modules/structs/commit_status_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/modules/structs/commit_status_test.go b/modules/structs/commit_status_test.go index aac0faa55065d..f06808534c408 100644 --- a/modules/structs/commit_status_test.go +++ b/modules/structs/commit_status_test.go @@ -1,3 +1,6 @@ +// Copyright 2023 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + package structs import ( From 02e39dbbdefdc08abbf1b53a98165712282b2bd6 Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Tue, 25 Jul 2023 22:36:31 +0800 Subject: [PATCH 6/9] move commitStatusPriorities to global --- modules/structs/commit_status.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/modules/structs/commit_status.go b/modules/structs/commit_status.go index 44213e32473c8..b829243398f47 100644 --- a/modules/structs/commit_status.go +++ b/modules/structs/commit_status.go @@ -18,15 +18,16 @@ const ( CommitStatusFailure CommitStatusState = "failure" ) +var commitStatusPriorities = map[CommitStatusState]int{ + CommitStatusError: 0, + CommitStatusFailure: 1, + CommitStatusPending: 2, + CommitStatusSuccess: 3, +} + // NoBetterThan returns true if this State is no better than the given State // the function only handles the 4 states above func (css CommitStatusState) NoBetterThan(css2 CommitStatusState) bool { - commitStatusPriorities := map[CommitStatusState]int{ - CommitStatusError: 0, - CommitStatusFailure: 1, - CommitStatusPending: 2, - CommitStatusSuccess: 3, - } // NoBetterThan only handles the 4 states above if _, exist := commitStatusPriorities[css]; !exist { return false From 5adb38f5af362fa6bbbfa3ed7aebe0a8239f00ad Mon Sep 17 00:00:00 2001 From: caicandong <50507092+CaiCandong@users.noreply.github.com> Date: Wed, 26 Jul 2023 09:34:39 +0800 Subject: [PATCH 7/9] Update modules/structs/commit_status.go Co-authored-by: yp05327 <576951401@qq.com> --- modules/structs/commit_status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/structs/commit_status.go b/modules/structs/commit_status.go index b829243398f47..f7b6b1fcc7045 100644 --- a/modules/structs/commit_status.go +++ b/modules/structs/commit_status.go @@ -26,7 +26,7 @@ var commitStatusPriorities = map[CommitStatusState]int{ } // NoBetterThan returns true if this State is no better than the given State -// the function only handles the 4 states above +// This function only handles the states defined in CommitStatusPriorities func (css CommitStatusState) NoBetterThan(css2 CommitStatusState) bool { // NoBetterThan only handles the 4 states above if _, exist := commitStatusPriorities[css]; !exist { From 578141db9c1eff7ba8cead1b7a146b70cfb6c010 Mon Sep 17 00:00:00 2001 From: caicandong <50507092+CaiCandong@users.noreply.github.com> Date: Wed, 26 Jul 2023 13:07:55 +0800 Subject: [PATCH 8/9] Update modules/structs/commit_status.go Co-authored-by: puni9869 <80308335+puni9869@users.noreply.github.com> --- modules/structs/commit_status.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/structs/commit_status.go b/modules/structs/commit_status.go index f7b6b1fcc7045..de1d8fa566cd0 100644 --- a/modules/structs/commit_status.go +++ b/modules/structs/commit_status.go @@ -4,7 +4,7 @@ package structs // CommitStatusState holds the state of a CommitStatus -// It can be "pending", "success", "error", "failure" +// It can be "pending", "success", "error" and "failure" type CommitStatusState string const ( From 659eba5a40f4f8f304117f8ae9c34392a9651b4f Mon Sep 17 00:00:00 2001 From: CaiCandong <1290147055@qq.com> Date: Wed, 26 Jul 2023 13:40:52 +0800 Subject: [PATCH 9/9] fix lint --- templates/swagger/v1_json.tmpl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index fb79c56013ee1..10ad8f555caf4 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -16514,7 +16514,7 @@ "x-go-package": "code.gitea.io/gitea/modules/structs" }, "CommitStatusState": { - "description": "CommitStatusState holds the state of a CommitStatus\nIt can be \"pending\", \"success\", \"error\", \"failure\"", + "description": "CommitStatusState holds the state of a CommitStatus\nIt can be \"pending\", \"success\", \"error\" and \"failure\"", "type": "string", "x-go-package": "code.gitea.io/gitea/modules/structs" },