Skip to content

Commit

Permalink
check sdk: return {Create,Update}CheckRunOptions (#662)
Browse files Browse the repository at this point in the history
Callers don't tend to work in `CheckRun`s themselves, but rather
`CreateCheckRunOptions`es (to create runs) or `UpdateCheckRunOptions`es
(to update them).

There's no built-in method to convert a `CheckRun` into either of those,
so we'll just do it ourselves.

To avoid duplication, `UpdateCheckRun()` just calls `CreateCheckRun()`
and twiddles its values.

---------

Signed-off-by: Jason Hall <[email protected]>
  • Loading branch information
imjasonh authored Dec 12, 2024
1 parent 885e96d commit 6192fe9
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 15 deletions.
22 changes: 18 additions & 4 deletions modules/github-bots/sdk/check/check.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,13 @@ func (b *Builder) Writef(format string, args ...any) {
//
// If the Summary field is empty, it will be set to the name field.
// If the Conclusion field is set, the CheckRun will be marked as completed.
func (b *Builder) CheckRun() *github.CheckRun {
func (b *Builder) CheckRunCreate() *github.CreateCheckRunOptions {
if b.Summary == "" {
b.Summary = b.name
}
cr := &github.CheckRun{
Name: &b.name,
HeadSHA: &b.headSHA,
cr := &github.CreateCheckRunOptions{
Name: b.name,
HeadSHA: b.headSHA,
Output: &github.CheckRunOutput{
Title: &b.Summary,
Summary: &b.Summary,
Expand All @@ -91,3 +91,17 @@ func (b *Builder) CheckRun() *github.CheckRun {
}
return cr
}

func (b *Builder) CheckRunUpdate() *github.UpdateCheckRunOptions {
create := b.CheckRunCreate()
return &github.UpdateCheckRunOptions{
Name: create.Name,
Status: create.Status,
Conclusion: create.Conclusion,
Output: &github.CheckRunOutput{
Title: create.GetOutput().Title,
Summary: create.GetOutput().Summary,
Text: create.GetOutput().Text,
},
}
}
44 changes: 33 additions & 11 deletions modules/github-bots/sdk/check/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,46 @@ func TestCheckRun(t *testing.T) {
b := NewBuilder("name", "headSHA")
b.Writef("test %d", 123)

if diff := cmp.Diff(b.CheckRun(), &github.CheckRun{
Name: github.String("name"),
HeadSHA: github.String("headSHA"),
if diff := cmp.Diff(b.CheckRunCreate(), &github.CreateCheckRunOptions{
Name: "name",
HeadSHA: "headSHA",
Output: &github.CheckRunOutput{
Title: github.String("name"),
Summary: github.String("name"),
Text: github.String("test 123\n"),
},
}); diff != "" {
t.Errorf("CheckRun() mismatch (-want +got):\n%s", diff)
t.Errorf("CheckRunCreate() mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(b.CheckRunUpdate(), &github.UpdateCheckRunOptions{
Name: "name",
Output: &github.CheckRunOutput{
Title: github.String("name"),
Summary: github.String("name"),
Text: github.String("test 123\n"),
},
}); diff != "" {
t.Errorf("CheckRunUpdate() mismatch (-want +got):\n%s", diff)
}

b.Summary = "summary"
b.Conclusion = ConclusionSuccess
b.Writef("test %t", true)
if diff := cmp.Diff(b.CheckRun(), &github.CheckRun{
Name: github.String("name"),
HeadSHA: github.String("headSHA"),
if diff := cmp.Diff(b.CheckRunCreate(), &github.CreateCheckRunOptions{
Name: "name",
HeadSHA: "headSHA",
Status: github.String("completed"),
Conclusion: github.String("success"),
Output: &github.CheckRunOutput{
Title: github.String("summary"),
Summary: github.String("summary"),
Text: github.String("test 123\ntest true\n"),
},
}); diff != "" {
t.Errorf("CheckRunCreate() mismatch (-want +got):\n%s", diff)
}
if diff := cmp.Diff(b.CheckRunUpdate(), &github.UpdateCheckRunOptions{
Name: "name",
Status: github.String("completed"),
Conclusion: github.String("success"),
Output: &github.CheckRunOutput{
Expand All @@ -38,7 +60,7 @@ func TestCheckRun(t *testing.T) {
Text: github.String("test 123\ntest true\n"),
},
}); diff != "" {
t.Errorf("CheckRun() mismatch (-want +got):\n%s", diff)
t.Errorf("CheckRunCreate() mismatch (-want +got):\n%s", diff)
}
}

Expand All @@ -55,13 +77,13 @@ func TestWritef(t *testing.T) {
}
}

gotText := b.CheckRun().GetOutput().GetText()
gotText := b.CheckRunCreate().GetOutput().GetText()
wantLength := maxCheckOutputLength
if len(gotText) != wantLength {
t.Fatalf("CheckRun().Output.Text length = %d, want %d", len(gotText), wantLength)
t.Fatalf("CheckRunCreate().Output.Text length = %d, want %d", len(gotText), wantLength)
}
if !strings.HasSuffix(gotText, truncationMessage) {
last100 := gotText[len(gotText)-100:]
t.Errorf("CheckRun().Output.Text does not have truncation message, ends with %q", last100)
t.Errorf("CheckRunCreate().Output.Text does not have truncation message, ends with %q", last100)
}
}

0 comments on commit 6192fe9

Please sign in to comment.