package triageissue import ( "context" "reflect" "testing" "github.com/google/go-github/v47/github" "edge-infra.dev/pkg/f8n/devinfra/jack/constants" "edge-infra.dev/pkg/f8n/devinfra/jack/plugin" "edge-infra.dev/pkg/lib/logging" ) var ( prBodyIncorrect = "## Purpose\r\n\r\n\r\n## Approach\r\n\r\n\r\n\r\n==COMMIT_MSG==\r\n\r\n\r\n\r\n==COMMIT_MSG==\r\n\r\n## Issue\r\n\r\n\r\nncrvoyix-swt-retail/edge-roadmap#ISSUE_NUMBER_HERE" prBodyCorrect = "## Purpose\r\n\r\n\r\n## Approach\r\n\r\n\r\n\r\n==COMMIT_MSG==\r\n\r\n\r\n\r\n==COMMIT_MSG==\r\n\r\n## Issue\r\n\r\n\r\nncrvoyix-swt-retail/edge-roadmap#2850" pr = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}} hp = &plugin.HandlerParams{ Ctx: context.Background(), Client: pr, Org: "test-org", Repo: "test-repo", Log: *logging.NewLogger(), } ) func TestOnCreation(t *testing.T) { // Test if body on creation is incorrect -> add label pre := github.PullRequestEvent{ Action: github.String("opened"), Number: github.Int(999), Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234)}, PullRequest: &github.PullRequest{ Body: &prBodyIncorrect, Title: github.String("Test PR creation -> addLabel"), Number: github.Int(1), Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}}, } handlePR(*hp, pre) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.TriageIssueLabel}}} if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) { t.Error("Failed: TestOnCreation -> Test if there is an incorrect issue link on creation.") } // Test if body on creation is correct -> do nothing pre = github.PullRequestEvent{ Action: github.String("opened"), Number: github.Int(999), Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234)}, PullRequest: &github.PullRequest{ Body: &prBodyCorrect, Title: github.String("Test PR creation -> removeLabel"), Number: github.Int(1), Labels: []*github.Label{{Name: github.String(constants.TriageIssueLabel)}}}, } handlePR(*hp, pre) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}} if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) { t.Error("Failed: TestOnCreation -> Test if there is a correct issue link on creation.") } // Test if body on creation is correct -> update title pre = github.PullRequestEvent{ Action: github.String("opened"), Number: github.Int(999), Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234)}, PullRequest: &github.PullRequest{ Body: &prBodyCorrect, Title: github.String("Test PR creation -> update title"), Number: github.Int(1), Labels: []*github.Label{}}, } handlePR(*hp, pre) // Updated title using correct body pre = github.PullRequestEvent{ Action: github.String("opened"), Number: github.Int(999), Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234)}, PullRequest: &github.PullRequest{ Body: &prBodyCorrect, Title: github.String("2850: Test PR creation -> update title"), Number: github.Int(1), Labels: []*github.Label{}}, } handlePR(*hp, pre) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}} if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) { t.Error("Failed: TestOnCreation -> Test if there is a correct issue link on creation and update the title.") } } func TestEditBody(t *testing.T) { pre := github.PullRequestEvent{ Action: github.String("opened"), Number: github.Int(999), Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234)}, PullRequest: &github.PullRequest{ Body: &prBodyIncorrect, Title: github.String("Test PR creation -> addLabel"), Number: github.Int(1), Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}}, } handlePR(*hp, pre) // Remove a label using edited correct body pre = github.PullRequestEvent{ Action: github.String("edited"), Number: github.Int(999), Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234)}, PullRequest: &github.PullRequest{ Body: &prBodyCorrect, Title: github.String("Test edit body with new correct body"), Number: github.Int(1), Labels: []*github.Label{{Name: github.String(constants.TriageIssueLabel)}}}, } handlePR(*hp, pre) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}} if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) { t.Error("Failed: TestEditBody -> Test editing a body from correct to incorrect.") } // Add a label back using edited incorrect body pre = github.PullRequestEvent{ Action: github.String("edited"), Number: github.Int(999), Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234)}, PullRequest: &github.PullRequest{ Body: &prBodyIncorrect, Title: github.String("Test edit body by replacing with incorrect body"), Number: github.Int(1), Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}}, } handlePR(*hp, pre) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.TriageIssueLabel}}} if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) { t.Error("Failed: TestEditBody -> Test editing a body from correct to incorrect.") } } func TestPRComments(t *testing.T) { prceCorrect := github.IssueCommentEvent{ Action: github.String("created"), Issue: &github.Issue{ Body: &prBodyIncorrect, Title: github.String("PR comment creation -> removeLabel"), Number: github.Int(1), PullRequestLinks: &github.PullRequestLinks{ URL: github.String("testurl.com/unitTestNeedsIssue")}, Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}}, Comment: &github.IssueComment{Body: github.String("/link #2850")}, } handlePRComment(*hp, prceCorrect) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Body: prBodyCorrect, Labels: []string{}}} if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) { t.Error("Failed: TestPRComments -> Test adding correct comment link.") } prceIncorrect := github.IssueCommentEvent{ Action: github.String("created"), Issue: &github.Issue{ Body: &prBodyCorrect, Title: github.String("PR comment creation -> addLabel"), Number: github.Int(1), PullRequestLinks: &github.PullRequestLinks{ URL: github.String("testurl.com/unitTestNeedsIssue")}, Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}}, Comment: &github.IssueComment{Body: github.String("/link - #2850")}, } handlePRComment(*hp, prceIncorrect) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Body: prBodyIncorrect, Labels: []string{constants.TriageIssueLabel}}} if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) { t.Error("Failed: TestPRComments -> Test removing correct comment link.") } } func TestManualAddRemove(t *testing.T) { pr = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}} hp = &plugin.HandlerParams{ Ctx: context.Background(), Client: pr, Org: "test-org", Repo: "test-repo", Log: *logging.NewLogger(), } pre := github.PullRequestEvent{ Action: github.String("labeled"), Number: github.Int(999), Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234)}, PullRequest: &github.PullRequest{ Body: &prBodyIncorrect, Title: github.String("Test adding label with incorrect body -> addLabel"), Number: github.Int(1), Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}}, } handlePR(*hp, pre) ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.TriageIssueLabel}}} if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) { t.Error("Failed: TestManualAddRemove -> Test adding label with correct body.") } // Test add label on correct PR body -> removeLabel pre = github.PullRequestEvent{ Action: github.String("labeled"), Number: github.Int(999), Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234)}, PullRequest: &github.PullRequest{ Body: &prBodyCorrect, Title: github.String("Test adding label with correct body -> removeLabel"), Number: github.Int(1), Labels: []*github.Label{{Name: github.String(constants.TriageIssueLabel)}}}, } handlePR(*hp, pre) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}} if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) { t.Error("Failed: TestManualAddRemove -> Test adding label with correct body.") } pre = github.PullRequestEvent{ Action: github.String("unlabeled"), Number: github.Int(999), Repo: &github.Repository{ Owner: &github.User{Login: github.String(hp.Org)}, Name: github.String(hp.Repo), ID: github.Int64(1234)}, PullRequest: &github.PullRequest{ Body: &prBodyIncorrect, Title: github.String("Test removing label with incorrect body -> addLabel"), Number: github.Int(1), Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}}, } handlePR(*hp, pre) ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.TriageIssueLabel}}} if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) { t.Error("Failed: TestManualAddRemove -> Test adding label with correct body.") } }