package size import ( "context" "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(), } func TestAddSizeToNewIssue(t *testing.T) { // Test if issue is an epic. c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}} ie := github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Epic))}}}, } handleIssue(*hp, ie) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddSizeToNewIssue -> Test if issue is an epic.") } // Test if a size label already exists. c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.SmallLabel}} ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Task))}, {Name: github.String(constants.SmallLabel)}}}, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.SmallLabel}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddSizeToNewIssue -> Test if a size label already exists.") } // Test if a size command exists in the issue body c.GithubIssues = &plugin.MockIssueService{ Labels: []string{string(constants.Task)}, Body: "No relevant command here", } ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Labels: []*github.Label{{Name: github.String(string(constants.Task))}}, Body: github.String("No relevant command here"), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{ GithubIssues: &plugin.MockIssueService{ Labels: []string{string(constants.Task), constants.TriageSizeLabel}, Body: "No relevant command here", }, } if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddSizeToNewIssue -> Test if a size command exists in the issue body.") } // Test if the size command has "extra" in it, but is an invalid command c.GithubIssues = &plugin.MockIssueService{ Labels: []string{string(constants.Task)}, Body: "This issue is of size extra medismell", } ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Labels: []*github.Label{{Name: github.String(string(constants.Task))}}, Body: github.String("This issue is of size extra medismell"), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{ GithubIssues: &plugin.MockIssueService{ Labels: []string{string(constants.Task), constants.TriageSizeLabel}, Body: "This issue is of size extra medismell", }, } if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddSizeToNewIssue -> Test if the size command has 'extra' in it, but is an invalid command.") } // Test if the size command is invalid c.GithubIssues = &plugin.MockIssueService{ Labels: []string{string(constants.Task)}, Body: "This issue is of size medismell", } ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Labels: []*github.Label{{Name: github.String(string(constants.Task))}}, Body: github.String("This issue is of size medismell"), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{ GithubIssues: &plugin.MockIssueService{ Labels: []string{string(constants.Task), constants.TriageSizeLabel}, Body: "This issue is of size medismell", }, } if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddSizeToNewIssue -> Test if the size command is invalid.") } // Test if the size command with "extra" is valid c.GithubIssues = &plugin.MockIssueService{ Labels: []string{string(constants.Task)}, Body: "This issue is of size extra large", } ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Labels: []*github.Label{{Name: github.String(string(constants.Task))}}, Body: github.String("This issue is of size extra large"), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{ GithubIssues: &plugin.MockIssueService{ Labels: []string{string(constants.Task), constants.ExtraLargeLabel}, Body: "This issue is of size extra large", }, } if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddSizeToNewIssue -> Test if the size command with 'extra' is valid.") } // Test if the size command is valid c.GithubIssues = &plugin.MockIssueService{ Labels: []string{string(constants.Task)}, Body: "This issue is of size large", } ie = github.IssuesEvent{ Action: github.String("opened"), Issue: &github.Issue{ Labels: []*github.Label{{Name: github.String(string(constants.Task))}}, Body: github.String("This issue is of size large"), }, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{ GithubIssues: &plugin.MockIssueService{ Labels: []string{string(constants.Task), constants.LargeLabel}, Body: "This issue is of size large", }, } if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddSizeToNewIssue -> // Test if the size command is valid.") } } func TestAddLabel(t *testing.T) { // Test if an issue has an existing parent label c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}} ie := github.IssuesEvent{ Action: github.String("labeled"), Label: &github.Label{Name: github.String(string(constants.Feature))}, Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Epic))}}}, } handleIssue(*hp, ie) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddLabel -> Test if an issue has an existing parent label.") } // Test if the added label is a parent label and no parent label currently exists // This action should clear an existing size label c.GithubIssues = &plugin.MockIssueService{Labels: []string{constants.SmallLabel}} ie = github.IssuesEvent{ Action: github.String("labeled"), Label: &github.Label{Name: github.String(string(constants.Feature))}, Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(constants.SmallLabel)}}}, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddLabel -> Test if the added label is a parent label and no parent label currently exists.") } // Test if the new label is an invalid size label c.GithubIssues = &plugin.MockIssueService{Labels: []string{}} ie = github.IssuesEvent{ Action: github.String("labeled"), Label: &github.Label{Name: github.String("size/1234")}, Issue: &github.Issue{Labels: []*github.Label{}}, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddLabel -> Test if the new label is not a size label.") } // Test if a valid size label is added but a parent label exists c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}} ie = github.IssuesEvent{ Action: github.String("labeled"), Label: &github.Label{Name: github.String(constants.SmallLabel)}, Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Epic))}}}, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddLabel -> Test if a valid size label is added but a parent label exists.") } // Test adding a valid size label (normal behavior) c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Task)}} ie = github.IssuesEvent{ Action: github.String("labeled"), Label: &github.Label{Name: github.String(constants.SmallLabel)}, Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Task))}}}, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.SmallLabel}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestAddLabel -> Test adding a valid size label (normal behavior).") } } func TestRemoveLabel(t *testing.T) { // Test if issue has a parent label c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Feature), constants.MediumLabel}} ie := github.IssuesEvent{ Action: github.String("unlabeled"), Label: &github.Label{Name: github.String(constants.MediumLabel)}, Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Feature))}, {Name: github.String(constants.MediumLabel)}}}, } handleIssue(*hp, ie) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Feature), constants.MediumLabel}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestRemoveLabel -> Test if issue has a parent label.") } // Test if an epic label is being removed c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Feature)}} ie = github.IssuesEvent{ Action: github.String("unlabeled"), Label: &github.Label{Name: github.String(string(constants.Feature))}, Issue: &github.Issue{Labels: []*github.Label{}}, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.TriageSizeLabel}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestRemoveLabel -> // Test if an epic label is being removed.") } // Test if the label is not a valid size c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.MediumLabel}} ie = github.IssuesEvent{ Action: github.String("unlabeled"), Label: &github.Label{Name: github.String(string(constants.Task))}, Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Task))}, {Name: github.String(constants.MediumLabel)}}}, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.MediumLabel}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestRemoveLabel -> Test if the label is not a valid size.") } // Test removal of valid size label c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.MediumLabel}} ie = github.IssuesEvent{ Action: github.String("unlabeled"), Label: &github.Label{Name: github.String(constants.MediumLabel)}, Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Task))}}}, } handleIssue(*hp, ie) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.TriageSizeLabel}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestRemoveLabel -> Test removal of valid size label.") } } // Check Issue Comment events for handleIssueComment func TestCommentEvents(t *testing.T) { // Test if no size commands exist in the comment c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Feature)}} ce := github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("No valid comment")}, Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}}, } handleIssueComment(*hp, ce) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Feature)}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestCommentEvents -> Test if no size commands exist in the comment.") } // Test if the size command applies to an epic // A warning comment should be added to the issue c.GithubIssues = &plugin.MockIssueService{ Labels: []string{string(constants.Feature)}, Comments: 0, ID: 1, } ce = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("size small")}, Issue: &github.Issue{ Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}, Number: github.Int(1), ID: github.Int64(1), }, Repo: &github.Repository{ Name: github.String(hp.Repo), Owner: &github.User{Login: github.String(hp.Org)}, }, } handleIssueComment(*hp, ce) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{ Labels: []string{string(constants.Feature)}, Comments: 1, ID: 1, }} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestCommentEvents -> Test if the size command applies to an epic.") } // Test if the command is an invalid extra size command c.GithubIssues = &plugin.MockIssueService{Labels: []string{}} ce = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("size extra medismell")}, Issue: &github.Issue{Labels: []*github.Label{}}, } handleIssueComment(*hp, ce) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestCommentEvents -> Test if the command is an invalid extra size command.") } // Test if the command in the comment already exists c.GithubIssues = &plugin.MockIssueService{Labels: []string{constants.ExtraSmallLabel}} ce = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("size extra small")}, Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(constants.ExtraSmallLabel)}}}, } handleIssueComment(*hp, ce) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.ExtraSmallLabel}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestCommentEvents -> Test if the command in the comment already exists.") } // Test if the size command is added as a label (normal behavior) c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.ExtraSmallLabel}} ce = github.IssueCommentEvent{ Action: github.String("created"), Comment: &github.IssueComment{Body: github.String("size small")}, Issue: &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Task))}, {Name: github.String(constants.ExtraSmallLabel)}}}, } handleIssueComment(*hp, ce) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.SmallLabel}}} if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) { t.Error("Failed: TestCommentEvents -> Test if the size command is added as a label (normal behavior).") } }