//nolint:goconst // TODO: IDEK package list import ( "context" "fmt" "reflect" "testing" "edge-infra.dev/pkg/lib/logging" "github.com/google/go-github/v47/github" "edge-infra.dev/pkg/f8n/devinfra/jack/constants" "edge-infra.dev/pkg/f8n/devinfra/jack/plugin" ) var c = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}} var hp = &plugin.HandlerParams{ Ctx: context.Background(), Client: c, Org: "some-org", Repo: "some-repo", Log: *logging.NewLogger(), } var relatedIssue = github.Issue{ Assignees: []*github.User{}, Body: github.String("Related Issue"), Number: github.Int(999), Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, Repository: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } const pcBody = "### parents\n\n*No Issues added yet*\n\n### children\n\n*No Issues added yet*\n" func TestHandleNewIssue(t *testing.T) { // Test if the new issue body is empty c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}} ie := github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Body: github.String(""), Labels: []*github.Label{{Name: github.String(string(constants.Epic))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssue(*hp, ie) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleNewIssue -> Test if the new issue body is empty.") } // Test if the new issue body has NO jack commands c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}} ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Body: github.String("Nothing to see here"), Labels: []*github.Label{{Name: github.String(string(constants.Epic))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleNewIssue -> Test if the new issue body has NO jack commands.") } // Test if the new issue body has both /child and /parent commands c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}} ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Body: github.String("/child #1 /parent #1"), Labels: []*github.Label{{Name: github.String(string(constants.Epic))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Comments: 1, Labels: []string{string(constants.Epic)}, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleNewIssue -> Test if the new issue body has both /child and /parent commands.") } // Test if the new issue is a parent c.GithubIssues = &plugin.MockIssueService{ Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, } ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Body: github.String("/child #2"), Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: fmt.Sprintf("/child #2\n%s%s%s", constants.Preamble, "### parents\n\n*No Issues added yet*\n\n### children\n\n- #999\n", constants.Postamble), Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleNewIssue -> Test if the new issue is a parent.") } // Test if the new issue is a child c.GithubIssues = &plugin.MockIssueService{ Labels: []string{string(constants.Task)}, RelatedIssue: relatedIssue, } ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Body: github.String("/parent #2"), Labels: []*github.Label{{Name: github.String(string(constants.Task))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: fmt.Sprintf("/parent #2\n%s%s%s", constants.Preamble, "### parents\n\n- #999\n\n\n\n", constants.Postamble), Labels: []string{string(constants.Task)}, RelatedIssue: relatedIssue, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleNewIssue -> Test if the new issue is a child.") } // Test if the new issue is an epic and has a parent c.GithubIssues = &plugin.MockIssueService{ Labels: []string{string(constants.Epic)}, RelatedIssue: relatedIssue, } ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Body: github.String("/parent #2"), Labels: []*github.Label{{Name: github.String(string(constants.Epic))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Comments: 1, Labels: []string{string(constants.Epic)}, RelatedIssue: relatedIssue, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleNewIssue -> Test if the new issue is an epic and has a parent.") } // Test adding epic to parent c.GithubIssues = &plugin.MockIssueService{Labels: []string{}} ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Body: github.String("/child #12"), Labels: []*github.Label{}, Number: github.Int(1), Title: github.String("Test adding epic"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: fmt.Sprintf("/child #12\n%s%s%s", constants.Preamble, "\n\n\n### children\n\n- #0\n", constants.Postamble), Comments: 0, Labels: []string{string(constants.Epic)}, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed test: handleIssue -> Test adding epic to parent") } } func TestHandleIssueComment(t *testing.T) { // Test if action is NOT created c.GithubIssues = &plugin.MockIssueService{} ice := github.IssueCommentEvent{ Action: github.String("edited"), } handleIssueComment(*hp, ice) ct := plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleIssueComment -> Test if action is NOT created.") } // Test if no commands are found in the comment ice = github.IssueCommentEvent{ Action: github.String("created"), Issue: &github.Issue{}, Comment: &github.IssueComment{Body: github.String("No relevant commands here.")}, } handleIssueComment(*hp, ice) if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleIssueComment -> Test if no commands are found in the comment.") } // Test if comment has child commands while issue is not a parent c.GithubIssues = &plugin.MockIssueService{ Comments: 0, Labels: []string{string(constants.Task)}, } ice = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("/child #2")}, Issue: &github.Issue{ Body: github.String("Nothing to see here"), Labels: []*github.Label{{Name: github.String(string(constants.Task))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssueComment(*hp, ice) ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Comments: 1, Labels: []string{string(constants.Task)}}, } if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleIssueComment -> Test if comment has child commands while issue is not a parent.") } // Test child removal c.GithubIssues = &plugin.MockIssueService{ Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, } ice = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("/child -#2")}, Issue: &github.Issue{ Body: github.String("Nothing to see here"), Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssueComment(*hp, ice) ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, pcBody, constants.Postamble), Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleIssueComment -> Test child removal.") } // Test parent removal c.GithubIssues = &plugin.MockIssueService{ Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, } ice = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("/parent -#2")}, Issue: &github.Issue{ Body: github.String("Nothing to see here"), Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssueComment(*hp, ice) ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ // Body: "Nothing to see here" + pcBody, Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, pcBody, constants.Postamble), Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleIssueComment -> Test parent removal.") } // Test child addition c.GithubIssues = &plugin.MockIssueService{ Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, } ice = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("/child #2")}, Issue: &github.Issue{ Body: github.String("Nothing to see here"), Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssueComment(*hp, ice) ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, "### parents\n\n*No Issues added yet*\n\n### children\n\n- #999\n", constants.Postamble), Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleIssueComment -> Test child addition.") } // Test parent addition c.GithubIssues = &plugin.MockIssueService{ Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, } ice = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("/parent #2")}, Issue: &github.Issue{ Body: github.String("Nothing to see here"), Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssueComment(*hp, ice) ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, "### parents\n\n- #999\n\n### children\n\n*No Issues added yet*\n", constants.Postamble), Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleIssueComment -> Test parent addition.") } // Test adding an issue to itself c.GithubIssues = &plugin.MockIssueService{ Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, } ice = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("/parent #1")}, Issue: &github.Issue{ Body: github.String("Nothing to see here"), Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, Number: github.Int(1), Title: github.String("List Test Issue"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234), }, } handleIssueComment(*hp, ice) ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ // Body: "Nothing to see here" + pcBody, Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, pcBody, constants.Postamble), Comments: 1, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestHandleIssueComment -> Test adding an issue to itself.") } // Test SwapChildToParent // Setting test issue c.GithubIssues = &plugin.MockIssueService{ Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, Body: "Nothing to see here\n### parents\n\n- #999\n\n### children\n\n*No Issues added yet*\n", } // Setting environment // Once passed thru handleIssueCmt, will store in c.GithubIssues and compare to ct.GithubIssues ice = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("/child #999")}, Issue: &github.Issue{ Body: github.String("Nothing to see here\n\n___\n\n\n### parents\n\n- #999\n\n### children\n\n*No Issues added yet*\n\n\n To add to this list use the commands: \n`/child #{child_number}` to add a child or `/parent #{parent_number}` to add a parent.\n[Click here to find out more about jackbot](https://docs.edge-infra.dev/edge-dev/process/bots/jackbot/)\n___\n"), Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, Number: github.Int(1), Title: github.String("Test Swappie"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Org), ID: github.Int64(1234), }, } handleIssueComment(*hp, ice) // Expected result ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: fmt.Sprintf("Nothing to see here\n%s%s%s", constants.Preamble, "### parents\n\n*No Issues added yet*\n\n### children\n\n- #999\n", constants.Postamble), Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, }} if !reflect.DeepEqual(c.GithubIssues, ct.GithubIssues) { t.Error("Failed: TestIssueComment ==> Swap child to parent") } // Test SwapParentToChild c.GithubIssues = &plugin.MockIssueService{ Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, Body: "Nothing to see here\n" + constants.Preamble + "### parents\n\n*No Issues added yet*\n\n### children\n\n- #999\n" + constants.Postamble, } ice = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("/parent #999")}, Issue: &github.Issue{ Body: github.String("Nothing to see here\n" + constants.Preamble + "### parents\n\n*No Issues added yet*\n\n### children\n\n- #999\n" + constants.Postamble), Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, Number: github.Int(1), Title: github.String("Test Swip Swap"), }, Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Org), ID: github.Int64(1234), }, } handleIssueComment(*hp, ice) ct = plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Body: "Nothing to see here\n" + constants.Preamble + "### parents\n\n- #999\n\n### children\n\n*No Issues added yet*\n" + constants.Postamble, Comments: 0, Labels: []string{string(constants.Feature)}, RelatedIssue: relatedIssue, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestIssueComment ==> Swap parent to child") } }