Skip to content

Commit

Permalink
some more tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
djeebus committed Dec 13, 2023
1 parent 97386c6 commit b6af2e8
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
8 changes: 8 additions & 0 deletions pkg/commitState.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ func (s CommitState) Emoji() string {
}
}

func (s CommitState) BareString() string {
text, ok := stateString[s]
if !ok {
text = defaultString
}
return text
}

func (s CommitState) String() string {
text, ok := stateString[s]
if !ok {
Expand Down
8 changes: 4 additions & 4 deletions pkg/events/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ func NewCheckEvent(repo *repo.Repo, client pkg.Client, cfg *config.ServerConfig)
return ce
}

// GetRepo gets the repo from a CheckEvent. In normal operations a CheckEvent can only be made by the VCSHookHandler
// getRepo gets the repo from a CheckEvent. In normal operations a CheckEvent can only be made by the VCSHookHandler
// As the Repo is built from a webhook payload via the VCSClient, it should always be present. If not, error
func (ce *CheckEvent) GetRepo(ctx context.Context) (*repo.Repo, error) {
func (ce *CheckEvent) getRepo(ctx context.Context) (*repo.Repo, error) {
_, span := otel.Tracer("Kubechecks").Start(ctx, "CheckEventGetRepo")
defer span.End()
var err error
Expand Down Expand Up @@ -113,7 +113,7 @@ func (ce *CheckEvent) CloneRepoLocal(ctx context.Context) error {
func (ce *CheckEvent) MergeIntoTarget(ctx context.Context) error {
ctx, span := otel.Tracer("Kubechecks").Start(ctx, "MergeIntoTarget")
defer span.End()
gitRepo, err := ce.GetRepo(ctx)
gitRepo, err := ce.getRepo(ctx)
if err != nil {
return err
}
Expand All @@ -125,7 +125,7 @@ func (ce *CheckEvent) GetListOfChangedFiles(ctx context.Context) ([]string, erro
ctx, span := otel.Tracer("Kubechecks").Start(ctx, "CheckEventGetListOfChangedFiles")
defer span.End()

gitRepo, err := ce.GetRepo(ctx)
gitRepo, err := ce.getRepo(ctx)
if err != nil {
return nil, err
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/github_client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (c *Client) CommitStatus(ctx context.Context, repo *repo.Repo, status pkg.C
log.Info().Str("repo", repo.Name).Str("sha", repo.SHA).Str("status", status.String()).Msg("setting Github commit status")
repoStatus, _, err := c.Repositories.CreateStatus(ctx, repo.Owner, repo.Name, repo.SHA, &github.RepoStatus{
State: toGithubCommitStatus(status),
Description: pkg.Pointer(status.String()),
Description: pkg.Pointer(status.BareString()),
ID: pkg.Pointer(int64(repo.CheckID)),
Context: pkg.Pointer("kubechecks"),
})
Expand Down
2 changes: 1 addition & 1 deletion pkg/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func (m *Message) IsSuccess() bool {
for _, r := range m.apps {
for _, result := range r.results {
switch result.State {
case StateSuccess, StateWarning, StateNone:
case StateError, StateFailure, StatePanic, StateWarning:
isSuccess = false
}
}
Expand Down
59 changes: 59 additions & 0 deletions pkg/message_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,62 @@ func TestBuildComment(t *testing.T) {
should add some important details here
</details></details>`, comment)
}

func TestMessageIsSuccess(t *testing.T) {
t.Run("logic works", func(t *testing.T) {
var (
message = NewMessage("name", 1, 2)
ctx = context.TODO()
)

// no apps mean success
assert.True(t, message.IsSuccess())

// one app, no checks = success
message.AddNewApp(ctx, "some-app")
assert.True(t, message.IsSuccess())

// one app, one success = success
message.AddToAppMessage(ctx, "some-app", CheckResult{State: StateSuccess})
assert.True(t, message.IsSuccess())

// one app, one success, one failure = failure
message.AddToAppMessage(ctx, "some-app", CheckResult{State: StateFailure})
assert.False(t, message.IsSuccess())

// one app, two successes, one failure = failure
message.AddToAppMessage(ctx, "some-app", CheckResult{State: StateSuccess})
assert.False(t, message.IsSuccess())

// one app, two successes, one failure = failure
message.AddToAppMessage(ctx, "some-app", CheckResult{State: StateSuccess})
assert.False(t, message.IsSuccess())

// two apps: second app's success does not override first app's failure
message.AddNewApp(ctx, "some-other-app")
message.AddToAppMessage(ctx, "some-other-app", CheckResult{State: StateSuccess})
assert.False(t, message.IsSuccess())
})

testcases := map[CommitState]bool{
StateNone: true,
StateSuccess: true,
StateRunning: true,
StateWarning: false,
StateFailure: false,
StateError: false,
StatePanic: false,
}

for state, expected := range testcases {
t.Run(state.BareString(), func(t *testing.T) {
var (
message = NewMessage("name", 1, 2)
ctx = context.TODO()
)
message.AddNewApp(ctx, "some-app")
message.AddToAppMessage(ctx, "some-app", CheckResult{State: state})
assert.Equal(t, expected, message.IsSuccess())
})
}
}

0 comments on commit b6af2e8

Please sign in to comment.