mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-11-04 12:53:43 +01:00 
			
		
		
		
	clean up
This commit is contained in:
		
							parent
							
								
									40bcd57d44
								
							
						
					
					
						commit
						8d78184596
					
				@ -233,7 +233,7 @@ func CalcCommitStatus(statuses []*CommitStatus) *CommitStatus {
 | 
				
			|||||||
	var lastStatus *CommitStatus
 | 
						var lastStatus *CommitStatus
 | 
				
			||||||
	state := api.CommitStatusSuccess
 | 
						state := api.CommitStatusSuccess
 | 
				
			||||||
	for _, status := range statuses {
 | 
						for _, status := range statuses {
 | 
				
			||||||
		if status.State.NoBetterThan(state) {
 | 
							if status.State.HasHigherPriorityThan(state) {
 | 
				
			||||||
			state = status.State
 | 
								state = status.State
 | 
				
			||||||
			lastStatus = status
 | 
								lastStatus = status
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
@ -35,19 +35,10 @@ func (css CommitStatusState) String() string {
 | 
				
			|||||||
	return string(css)
 | 
						return string(css)
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// NoBetterThan returns true if this State is no better than the given State
 | 
					// HasHigherPriorityThan returns true if this state has higher priority than the other
 | 
				
			||||||
// This function only handles the states defined in CommitStatusPriorities
 | 
					// Undefined states are considered to have the highest priority like CommitStatusError(0)
 | 
				
			||||||
func (css CommitStatusState) NoBetterThan(css2 CommitStatusState) bool {
 | 
					func (css CommitStatusState) HasHigherPriorityThan(other CommitStatusState) bool {
 | 
				
			||||||
	// only handle the states with defined priorities above, it always returns false for undefined priorities
 | 
						return commitStatusPriorities[css] < commitStatusPriorities[other]
 | 
				
			||||||
	if _, exist := commitStatusPriorities[css]; !exist {
 | 
					 | 
				
			||||||
		return false
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	if _, exist := commitStatusPriorities[css2]; !exist {
 | 
					 | 
				
			||||||
		return false
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	return commitStatusPriorities[css] <= commitStatusPriorities[css2]
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
// IsPending represents if commit status state is pending
 | 
					// IsPending represents if commit status state is pending
 | 
				
			||||||
 | 
				
			|||||||
@ -10,165 +10,21 @@ import (
 | 
				
			|||||||
)
 | 
					)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func TestNoBetterThan(t *testing.T) {
 | 
					func TestNoBetterThan(t *testing.T) {
 | 
				
			||||||
	type args struct {
 | 
					 | 
				
			||||||
		css  CommitStatusState
 | 
					 | 
				
			||||||
		css2 CommitStatusState
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
	var unExpectedState CommitStatusState
 | 
					 | 
				
			||||||
	tests := []struct {
 | 
						tests := []struct {
 | 
				
			||||||
		name string
 | 
							s1, s2 CommitStatusState
 | 
				
			||||||
		args args
 | 
							higher bool
 | 
				
			||||||
		want bool
 | 
					 | 
				
			||||||
	}{
 | 
						}{
 | 
				
			||||||
		{
 | 
							{CommitStatusError, CommitStatusFailure, true},
 | 
				
			||||||
			name: "success is no better than success",
 | 
							{CommitStatusFailure, CommitStatusWarning, true},
 | 
				
			||||||
			args: args{
 | 
							{CommitStatusWarning, CommitStatusPending, true},
 | 
				
			||||||
				css:  CommitStatusSuccess,
 | 
							{CommitStatusPending, CommitStatusSuccess, true},
 | 
				
			||||||
				css2: CommitStatusSuccess,
 | 
							{CommitStatusSuccess, CommitStatusSkipped, true},
 | 
				
			||||||
			},
 | 
					
 | 
				
			||||||
			want: true,
 | 
							{CommitStatusError, "unknown-xxx", false},
 | 
				
			||||||
		},
 | 
							{"unknown-xxx", CommitStatusFailure, 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 {
 | 
						for _, tt := range tests {
 | 
				
			||||||
		t.Run(tt.name, func(t *testing.T) {
 | 
							assert.Equal(t, tt.higher, tt.s1.HasHigherPriorityThan(tt.s2), "s1=%s, s2=%s, expected=%v", tt.s1, tt.s2, tt.higher)
 | 
				
			||||||
			result := tt.args.css.NoBetterThan(tt.args.css2)
 | 
					 | 
				
			||||||
			assert.Equal(t, tt.want, result)
 | 
					 | 
				
			||||||
		})
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						assert.Equal(t, false, CommitStatusError.HasHigherPriorityThan(CommitStatusError))
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -43,12 +43,13 @@ func ToCombinedStatus(ctx context.Context, statuses []*git_model.CommitStatus, r
 | 
				
			|||||||
		TotalCount: len(statuses),
 | 
							TotalCount: len(statuses),
 | 
				
			||||||
		Repository: repo,
 | 
							Repository: repo,
 | 
				
			||||||
		URL:        "",
 | 
							URL:        "",
 | 
				
			||||||
 | 
							State:      api.CommitStatusSuccess,
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	retStatus.Statuses = make([]*api.CommitStatus, 0, len(statuses))
 | 
						retStatus.Statuses = make([]*api.CommitStatus, 0, len(statuses))
 | 
				
			||||||
	for _, status := range statuses {
 | 
						for _, status := range statuses {
 | 
				
			||||||
		retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(ctx, status))
 | 
							retStatus.Statuses = append(retStatus.Statuses, ToCommitStatus(ctx, status))
 | 
				
			||||||
		if retStatus.State == "" || status.State.NoBetterThan(retStatus.State) {
 | 
							if status.State.HasHigherPriorityThan(retStatus.State) {
 | 
				
			||||||
			retStatus.State = status.State
 | 
								retStatus.State = status.State
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
				
			|||||||
@ -46,13 +46,13 @@ func MergeRequiredContextsCommitStatus(commitStatuses []*git_model.CommitStatus,
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
			// If required rule not match any action, then it is pending
 | 
								// If required rule not match any action, then it is pending
 | 
				
			||||||
			if targetStatus == "" {
 | 
								if targetStatus == "" {
 | 
				
			||||||
				if structs.CommitStatusPending.NoBetterThan(returnedStatus) {
 | 
									if structs.CommitStatusPending.HasHigherPriorityThan(returnedStatus) {
 | 
				
			||||||
					returnedStatus = structs.CommitStatusPending
 | 
										returnedStatus = structs.CommitStatusPending
 | 
				
			||||||
				}
 | 
									}
 | 
				
			||||||
				break
 | 
									break
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			if targetStatus.NoBetterThan(returnedStatus) {
 | 
								if targetStatus.HasHigherPriorityThan(returnedStatus) {
 | 
				
			||||||
				returnedStatus = targetStatus
 | 
									returnedStatus = targetStatus
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user