Skip to content

Commit

Permalink
Test single check suite with empty check run scenario
Browse files Browse the repository at this point in the history
  • Loading branch information
benbp committed Dec 29, 2022
1 parent d84a5ee commit 7172462
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 27 deletions.
63 changes: 37 additions & 26 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Payloads struct {
CheckSuiteResponse []byte
MultipleCheckSuiteResponse []byte
MultipleWithEmptyCheckSuiteResponse []byte
SingleWithEmptyCheckSuiteResponse []byte
StatusResponse []byte
NewCommentResponse []byte
NoPipelinesComment []byte
Expand Down Expand Up @@ -57,6 +58,10 @@ func getPayloads() (Payloads, error) {
if err != nil {
return Payloads{}, err
}
payloads.SingleWithEmptyCheckSuiteResponse, err = ioutil.ReadFile("./testpayloads/single_with_empty_check_suite_response.json")
if err != nil {
return Payloads{}, err
}
payloads.StatusResponse, err = ioutil.ReadFile("./testpayloads/status_response.json")
if err != nil {
return Payloads{}, err
Expand Down Expand Up @@ -86,20 +91,20 @@ func getStatusBody(assert *assert.Assertions, req *http.Request) StatusBody {
}

type TestCheckSuiteCase struct {
Description string
AppTargets []string
InjectState1 CommitState
InjectState2 CommitState
ShouldPostStatus bool
ExpectedStatus CommitState
Event []byte
Description string
AppTargets []string
InjectConclusion1 CheckSuiteConclusion
InjectConclusion2 CheckSuiteConclusion
ShouldPostStatus bool
ExpectedState CommitState
Event []byte
}

func NewCheckSuiteTestServer(
assert *assert.Assertions,
payloads Payloads,
state1 CommitState,
state2 CommitState,
conclusion1 CheckSuiteConclusion,
conclusion2 CheckSuiteConclusion,
postedState *CommitState,
postedStatus *bool,
description string,
Expand All @@ -114,10 +119,10 @@ func NewCheckSuiteTestServer(
assert.Contains(req.URL.Path, checkSuite.CheckSuite.HeadSha, description)
response = payloads.MultipleCheckSuiteResponse
response = []byte(strings.Replace(string(response),
`"conclusion": "neutral"`, fmt.Sprintf("\"conclusion\": \"%s\"", state1), 1))
if state2 != "" {
`"conclusion": "neutral"`, fmt.Sprintf("\"conclusion\": \"%s\"", conclusion1), 1))
if conclusion2 != "" {
response = []byte(strings.Replace(string(response),
`"conclusion": "neutral"`, fmt.Sprintf("\"conclusion\": \"%s\"", state2), 1))
`"conclusion": "neutral"`, fmt.Sprintf("\"conclusion\": \"%s\"", conclusion2), 1))
}
} else if strings.Contains(checkSuite.GetStatusesUrl(), req.URL.String()) && req.Method == "POST" {
assert.Contains(req.URL.Path, checkSuite.CheckSuite.HeadSha)
Expand Down Expand Up @@ -152,15 +157,20 @@ func TestCheckSuite(t *testing.T) {
[]byte(strings.ReplaceAll(string(payloads.CheckSuiteEvent), `"conclusion": "success"`, fmt.Sprintf("\"conclusion\": \"%s\"", CommitStateFailure)))},
{"POST success for single suite", singleAppTarget, "", "", true, CommitStateSuccess, payloads.CheckSuiteEvent},
{"POST pending for no match, single suite", noMatchAppTarget, "", "", true, CommitStatePending, payloads.CheckSuiteEvent},
{"POST pending for multiple suites pending", multiAppTarget, CommitStateSuccess, CommitStatePending, true, CommitStatePending, payloads.CheckSuiteEvent},
{"POST pending for multiple suites pending 2", multiAppTarget, CommitStatePending, CommitStateSuccess, true, CommitStatePending, payloads.CheckSuiteEvent},
{"POST pending for multiple suites failure", multiAppTarget, CommitStateSuccess, CommitStateFailure, true, CommitStatePending, payloads.CheckSuiteEvent},
{"POST success for multiple suites", multiAppTarget, CommitStateSuccess, CommitStateSuccess, true, CommitStateSuccess, payloads.CheckSuiteEvent},
{"skip for main branch", singleAppTarget, CommitStateSuccess, "", false, zeroCommitState, []byte(strings.ReplaceAll(string(payloads.CheckSuiteEvent), `"head_branch": "changes"`, `"head_branch": "main"`))},
{"POST pending for multiple suites pending", multiAppTarget, CheckSuiteConclusionSuccess, CheckSuiteConclusionEmpty,
true, CommitStatePending, payloads.CheckSuiteEvent},
{"POST pending for multiple suites pending 2", multiAppTarget, CheckSuiteConclusionEmpty, CheckSuiteConclusionSuccess,
true, CommitStatePending, payloads.CheckSuiteEvent},
{"POST pending for multiple suites failure", multiAppTarget, CheckSuiteConclusionSuccess, CheckSuiteConclusionFailure,
true, CommitStatePending, payloads.CheckSuiteEvent},
{"POST success for multiple suites", multiAppTarget, CheckSuiteConclusionSuccess, CheckSuiteConclusionSuccess,
true, CommitStateSuccess, payloads.CheckSuiteEvent},
{"skip for main branch", singleAppTarget, CheckSuiteConclusionSuccess, "", false, zeroCommitState,
[]byte(strings.ReplaceAll(string(payloads.CheckSuiteEvent), `"head_branch": "changes"`, `"head_branch": "main"`))},
} {
var postedStatus bool
var postedState CommitState
server := NewCheckSuiteTestServer(assert, payloads, tc.InjectState1, tc.InjectState2, &postedState, &postedStatus, tc.Description)
server := NewCheckSuiteTestServer(assert, payloads, tc.InjectConclusion1, tc.InjectConclusion2, &postedState, &postedStatus, tc.Description)
gh, err := NewGithubClient(server.URL, "", tc.AppTargets...)
assert.NoError(err, tc.Description)
servers = append(servers, server)
Expand All @@ -169,7 +179,7 @@ func TestCheckSuite(t *testing.T) {
err = handleEvent(gh, tc.Event)
assert.NoError(err, tc.Description)
assert.Equal(tc.ShouldPostStatus, postedStatus, tc.Description)
assert.Equal(tc.ExpectedStatus, postedState, tc.Description)
assert.Equal(tc.ExpectedState, postedState, tc.Description)
}
}

Expand Down Expand Up @@ -332,24 +342,25 @@ func TestWorkflowRun(t *testing.T) {
payloads, err := getPayloads()
assert.NoError(err)
servers := []*httptest.Server{}
apps := []string{"Octocat App"}

singleCheckSuiteResponse := []byte(strings.ReplaceAll(
string(payloads.CheckSuiteResponse), `"conclusion": "neutral"`, fmt.Sprintf("\"conclusion\": \"%s\"", CommitStateSuccess)))

multipleCheckSuiteResponse := []byte(strings.ReplaceAll(string(payloads.MultipleCheckSuiteResponse),
`"conclusion": "neutral"`, fmt.Sprintf("\"conclusion\": \"%s\"", CommitStateSuccess)))
multipleCheckSuiteResponsePending := []byte(strings.Replace(string(multipleCheckSuiteResponse),
fmt.Sprintf("\"conclusion\": \"%s\"", CommitStateSuccess), fmt.Sprintf("\"conclusion\": \"%s\"", CommitStatePending), 1))

for i, tc := range []WorkflowRunCase{
{"Workflow run success", payloads.WorkflowRunEvent, singleCheckSuiteResponse, CommitStateSuccess, apps},
{"Workflow run multiple success", payloads.WorkflowRunEvent, multipleCheckSuiteResponse, CommitStateSuccess, apps},
{"Workflow run multiple pending", payloads.WorkflowRunEvent, multipleCheckSuiteResponsePending, CommitStatePending, apps},
{"Workflow run success", payloads.WorkflowRunEvent, singleCheckSuiteResponse, CommitStateSuccess, []string{"Octocat App"}},
{"Workflow run multiple success", payloads.WorkflowRunEvent, multipleCheckSuiteResponse, CommitStateSuccess, []string{"Octocat App"}},
{"Workflow run multiple pending", payloads.WorkflowRunEvent, multipleCheckSuiteResponsePending, CommitStatePending, []string{"Octocat App"}},
{"Workflow run with empty check run suite", payloads.WorkflowRunEvent, payloads.MultipleWithEmptyCheckSuiteResponse,
CommitStateSuccess, []string{"GitHub Actions", "Azure Pipelines"}},
{"Workflow run push event", []byte(strings.ReplaceAll(string(payloads.WorkflowRunEvent), `"event": "pull_request"`, `"event": "push"`)),
singleCheckSuiteResponse, CommitStatePending, apps},
{"Workflow run single pending", payloads.WorkflowRunEvent, payloads.SingleWithEmptyCheckSuiteResponse,
CommitStatePending, []string{"Azure Pipelines"}},
{"Workflow run push event",
[]byte(strings.ReplaceAll(string(payloads.WorkflowRunEvent), `"event": "pull_request"`, `"event": "push"`)),
singleCheckSuiteResponse, CommitStatePending, []string{"Octocat App"}},
} {
var postedState CommitState

Expand Down
177 changes: 177 additions & 0 deletions testpayloads/single_with_empty_check_suite_response.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
{
"total_count": 1,
"check_suites": [
{
"id": 10092247692,
"node_id": "CS_kwDOFqc1sc8AAAACWYt6jA",
"head_branch": "benbp/ci-pr",
"head_sha": "1f3db168a3ae9d8aad1b7e65184565efb3018713",
"status": "queued",
"conclusion": null,
"url": "https://api.github.com/repos/benbp/azure-sdk-tools/check-suites/10092247692",
"before": "b22924091189779aeca71beec21d79f9d79dc6a2",
"after": "1f3db168a3ae9d8aad1b7e65184565efb3018713",
"pull_requests": [
{
"url": "https://api.github.com/repos/benbp/azure-sdk-tools/pulls/26",
"id": 1174568198,
"number": 26,
"head": {
"ref": "benbp/ci-pr",
"sha": "1f3db168a3ae9d8aad1b7e65184565efb3018713",
"repo": {
"id": 380057009,
"url": "https://api.github.com/repos/benbp/azure-sdk-tools",
"name": "azure-sdk-tools"
}
},
"base": {
"ref": "main",
"sha": "050e02490337f660730af4edbdf5a8b23d34759f",
"repo": {
"id": 380057009,
"url": "https://api.github.com/repos/benbp/azure-sdk-tools",
"name": "azure-sdk-tools"
}
}
}
],
"app": {
"id": 9426,
"slug": "azure-pipelines",
"node_id": "MDM6QXBwOTQyNg==",
"owner": {
"login": "AzurePipelines",
"id": 39924718,
"node_id": "MDQ6VXNlcjM5OTI0NzE4",
"avatar_url": "https://avatars.githubusercontent.com/u/39924718?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/AzurePipelines",
"html_url": "https://github.com/AzurePipelines",
"followers_url": "https://api.github.com/users/AzurePipelines/followers",
"following_url": "https://api.github.com/users/AzurePipelines/following{/other_user}",
"gists_url": "https://api.github.com/users/AzurePipelines/gists{/gist_id}",
"starred_url": "https://api.github.com/users/AzurePipelines/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/AzurePipelines/subscriptions",
"organizations_url": "https://api.github.com/users/AzurePipelines/orgs",
"repos_url": "https://api.github.com/users/AzurePipelines/repos",
"events_url": "https://api.github.com/users/AzurePipelines/events{/privacy}",
"received_events_url": "https://api.github.com/users/AzurePipelines/received_events",
"type": "User",
"site_admin": false
},
"name": "Azure Pipelines",
"description": "### Continuously build, test, and deploy to any platform and cloud\r\n\r\n[Azure Pipelines](https://azure.microsoft.com/services/devops/pipelines/) offers cloud-hosted pipelines for Linux, macOS, and Windows with 10 free parallel jobs and unlimited minutes for open source projects.\r\n\r\n:v: **Any language, platform, and cloud**\r\nBuild, test, and deploy Node.js, Python, 
Java, PHP, Ruby, Go, C/C++, C#, Android, and iOS apps. Run in parallel on Linux, macOS, and Windows. Deploy to cloud providers like Azure, AWS, and GCP. Distribute mobile apps through beta channels and app stores.\r\n\r\n:whale2: **Native container support**\r\nCreate new containers with ease and push them to any registry. Deploy containers to independent hosts or Kubernetes.\r\n\r\n:muscle: **Advanced workflows and features**\r\nEasy build chaining and multi-phased builds. Support for YAML, test integration, release gates, reporting, and more.\r\n\r\n:wrench: **Extensible**\r\nUse a range of build, test, and deployment tasks built by the community – hundreds of extensions from Slack to SonarCloud. You can even deploy from other CI systems, like Jenkins. Webhooks and REST APIs help you integrate.\r\n\r\n:blue_heart: **Free, to you from Azure Pipelines**\r\nFree cloud-hosted builds for public and private repositories.\r\n",
"external_url": "https://azure.microsoft.com/services/devops/pipelines/",
"html_url": "https://github.com/apps/azure-pipelines",
"created_at": "2018-02-23T17:14:32Z",
"updated_at": "2018-10-25T18:51:25Z",
"permissions": {
"checks": "write",
"contents": "write",
"deployments": "write",
"issues": "write",
"metadata": "read",
"pull_requests": "write",
"statuses": "write"
},
"events": [
"check_suite",
"create",
"issue_comment",
"pull_request",
"push",
"release",
"repository"
]
},
"created_at": "2022-12-28T23:52:49Z",
"updated_at": "2022-12-28T23:52:49Z",
"rerequestable": true,
"runs_rerequestable": true,
"latest_check_runs_count": 0,
"check_runs_url": "https://api.github.com/repos/benbp/azure-sdk-tools/check-suites/10092247692/check-runs",
"head_commit": {
"id": "1f3db168a3ae9d8aad1b7e65184565efb3018713",
"tree_id": "d5c30619027c5dd2b2b7f86d452e46bf13353add",
"message": "9",
"timestamp": "2022-12-28T23:52:47Z",
"author": {
"name": "Ben Broderick Phillips",
"email": "[email protected]"
},
"committer": {
"name": "Ben Broderick Phillips",
"email": "[email protected]"
}
},
"repository": {
"id": 380057009,
"node_id": "MDEwOlJlcG9zaXRvcnkzODAwNTcwMDk=",
"name": "azure-sdk-tools",
"full_name": "benbp/azure-sdk-tools",
"private": false,
"owner": {
"login": "benbp",
"id": 1020379,
"node_id": "MDQ6VXNlcjEwMjAzNzk=",
"avatar_url": "https://avatars.githubusercontent.com/u/1020379?v=4",
"gravatar_id": "",
"url": "https://api.github.com/users/benbp",
"html_url": "https://github.com/benbp",
"followers_url": "https://api.github.com/users/benbp/followers",
"following_url": "https://api.github.com/users/benbp/following{/other_user}",
"gists_url": "https://api.github.com/users/benbp/gists{/gist_id}",
"starred_url": "https://api.github.com/users/benbp/starred{/owner}{/repo}",
"subscriptions_url": "https://api.github.com/users/benbp/subscriptions",
"organizations_url": "https://api.github.com/users/benbp/orgs",
"repos_url": "https://api.github.com/users/benbp/repos",
"events_url": "https://api.github.com/users/benbp/events{/privacy}",
"received_events_url": "https://api.github.com/users/benbp/received_events",
"type": "User",
"site_admin": false
},
"html_url": "https://github.com/benbp/azure-sdk-tools",
"description": "Tools repository leveraged by the Azure SDK team.",
"fork": true,
"url": "https://api.github.com/repos/benbp/azure-sdk-tools",
"forks_url": "https://api.github.com/repos/benbp/azure-sdk-tools/forks",
"keys_url": "https://api.github.com/repos/benbp/azure-sdk-tools/keys{/key_id}",
"collaborators_url": "https://api.github.com/repos/benbp/azure-sdk-tools/collaborators{/collaborator}",
"teams_url": "https://api.github.com/repos/benbp/azure-sdk-tools/teams",
"hooks_url": "https://api.github.com/repos/benbp/azure-sdk-tools/hooks",
"issue_events_url": "https://api.github.com/repos/benbp/azure-sdk-tools/issues/events{/number}",
"events_url": "https://api.github.com/repos/benbp/azure-sdk-tools/events",
"assignees_url": "https://api.github.com/repos/benbp/azure-sdk-tools/assignees{/user}",
"branches_url": "https://api.github.com/repos/benbp/azure-sdk-tools/branches{/branch}",
"tags_url": "https://api.github.com/repos/benbp/azure-sdk-tools/tags",
"blobs_url": "https://api.github.com/repos/benbp/azure-sdk-tools/git/blobs{/sha}",
"git_tags_url": "https://api.github.com/repos/benbp/azure-sdk-tools/git/tags{/sha}",
"git_refs_url": "https://api.github.com/repos/benbp/azure-sdk-tools/git/refs{/sha}",
"trees_url": "https://api.github.com/repos/benbp/azure-sdk-tools/git/trees{/sha}",
"statuses_url": "https://api.github.com/repos/benbp/azure-sdk-tools/statuses/{sha}",
"languages_url": "https://api.github.com/repos/benbp/azure-sdk-tools/languages",
"stargazers_url": "https://api.github.com/repos/benbp/azure-sdk-tools/stargazers",
"contributors_url": "https://api.github.com/repos/benbp/azure-sdk-tools/contributors",
"subscribers_url": "https://api.github.com/repos/benbp/azure-sdk-tools/subscribers",
"subscription_url": "https://api.github.com/repos/benbp/azure-sdk-tools/subscription",
"commits_url": "https://api.github.com/repos/benbp/azure-sdk-tools/commits{/sha}",
"git_commits_url": "https://api.github.com/repos/benbp/azure-sdk-tools/git/commits{/sha}",
"comments_url": "https://api.github.com/repos/benbp/azure-sdk-tools/comments{/number}",
"issue_comment_url": "https://api.github.com/repos/benbp/azure-sdk-tools/issues/comments{/number}",
"contents_url": "https://api.github.com/repos/benbp/azure-sdk-tools/contents/{+path}",
"compare_url": "https://api.github.com/repos/benbp/azure-sdk-tools/compare/{base}...{head}",
"merges_url": "https://api.github.com/repos/benbp/azure-sdk-tools/merges",
"archive_url": "https://api.github.com/repos/benbp/azure-sdk-tools/{archive_format}{/ref}",
"downloads_url": "https://api.github.com/repos/benbp/azure-sdk-tools/downloads",
"issues_url": "https://api.github.com/repos/benbp/azure-sdk-tools/issues{/number}",
"pulls_url": "https://api.github.com/repos/benbp/azure-sdk-tools/pulls{/number}",
"milestones_url": "https://api.github.com/repos/benbp/azure-sdk-tools/milestones{/number}",
"notifications_url": "https://api.github.com/repos/benbp/azure-sdk-tools/notifications{?since,all,participating}",
"labels_url": "https://api.github.com/repos/benbp/azure-sdk-tools/labels{/name}",
"releases_url": "https://api.github.com/repos/benbp/azure-sdk-tools/releases{/id}",
"deployments_url": "https://api.github.com/repos/benbp/azure-sdk-tools/deployments"
}
}
]
}
3 changes: 2 additions & 1 deletion types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (

IssueCommentActionCreated ActionType = "created"

CheckSuiteStatusRequested CheckSuiteStatus = "requested"
CheckSuiteStatusQueued CheckSuiteStatus = "queued"
CheckSuiteStatusInProgress CheckSuiteStatus = "in_progress"
CheckSuiteStatusCompleted CheckSuiteStatus = "completed"

Expand All @@ -27,6 +27,7 @@ const (
CheckSuiteConclusionTimedOut CheckSuiteConclusion = "timed_out"
CheckSuiteConclusionActionRequired CheckSuiteConclusion = "action_required"
CheckSuiteConclusionStale CheckSuiteConclusion = "stale"
CheckSuiteConclusionEmpty CheckSuiteConclusion = ""
)

type ActionType string
Expand Down

0 comments on commit 7172462

Please sign in to comment.