...

Source file src/edge-infra.dev/pkg/f8n/devinfra/jack/plugin/triage_issue/handler_test.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/jack/plugin/triage_issue

     1  package triageissue
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"github.com/google/go-github/v47/github"
     9  
    10  	"edge-infra.dev/pkg/f8n/devinfra/jack/constants"
    11  	"edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
    12  	"edge-infra.dev/pkg/lib/logging"
    13  )
    14  
    15  var (
    16  	prBodyIncorrect = "## Purpose\r\n<!-- What and why? Link to external context such as issues, documentation, logs, etc -->\r\n\r\n## Approach\r\n<!-- How? -->\r\n\r\n<!-- Text between ==COMMIT_MSG== is added to the actual commit message when \r\nBulldozer merges. \r\n\r\nProvide information that would be useful for someone looking through the repo \r\nhistory. \r\n\r\nThis should probably be most/all of your Approach section, but there may be \r\nsome things, e.g., \"tested locally by foo\" that don't make sense to go into\r\nthe commit message. -->\r\n==COMMIT_MSG==\r\n\r\n\r\n\r\n==COMMIT_MSG==\r\n\r\n## Issue\r\n<!-- Link the relevant github.com/ncrvoyix-swt-retail/edge-roadmap issue here: \r\nhttps://docs.edge-infra.dev/process/#associating-a-pull-request-with-an-issue \r\n\r\nTo simply link the issue to the PR, provide the ISSUE_NUMBER_HERE below\r\n\r\nTo close the issue when this PR is merged, add \"closes\" to the beginning of the \r\nreference, e.g. `closes ncrvoyix-swt-retail/edge-roadmap#3029` \r\n\r\nDon't be afraid to link multiple issues!-->\r\n\r\nncrvoyix-swt-retail/edge-roadmap#ISSUE_NUMBER_HERE"
    17  	prBodyCorrect   = "## Purpose\r\n<!-- What and why? Link to external context such as issues, documentation, logs, etc -->\r\n\r\n## Approach\r\n<!-- How? -->\r\n\r\n<!-- Text between ==COMMIT_MSG== is added to the actual commit message when \r\nBulldozer merges. \r\n\r\nProvide information that would be useful for someone looking through the repo \r\nhistory. \r\n\r\nThis should probably be most/all of your Approach section, but there may be \r\nsome things, e.g., \"tested locally by foo\" that don't make sense to go into\r\nthe commit message. -->\r\n==COMMIT_MSG==\r\n\r\n\r\n\r\n==COMMIT_MSG==\r\n\r\n## Issue\r\n<!-- Link the relevant github.com/ncrvoyix-swt-retail/edge-roadmap issue here: \r\nhttps://docs.edge-infra.dev/process/#associating-a-pull-request-with-an-issue \r\n\r\nTo simply link the issue to the PR, provide the ISSUE_NUMBER_HERE below\r\n\r\nTo close the issue when this PR is merged, add \"closes\" to the beginning of the \r\nreference, e.g. `closes ncrvoyix-swt-retail/edge-roadmap#3029` \r\n\r\nDon't be afraid to link multiple issues!-->\r\n\r\nncrvoyix-swt-retail/edge-roadmap#2850"
    18  	pr              = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}}
    19  	hp              = &plugin.HandlerParams{
    20  		Ctx:    context.Background(),
    21  		Client: pr,
    22  		Org:    "test-org",
    23  		Repo:   "test-repo",
    24  		Log:    *logging.NewLogger(),
    25  	}
    26  )
    27  
    28  func TestOnCreation(t *testing.T) {
    29  	// Test if body on creation is incorrect -> add label
    30  	pre := github.PullRequestEvent{
    31  		Action: github.String("opened"),
    32  		Number: github.Int(999),
    33  		Repo: &github.Repository{
    34  			Owner: &github.User{Login: github.String(hp.Org)},
    35  			Name:  github.String(hp.Repo),
    36  			ID:    github.Int64(1234)},
    37  		PullRequest: &github.PullRequest{
    38  			Body:   &prBodyIncorrect,
    39  			Title:  github.String("Test PR creation -> addLabel"),
    40  			Number: github.Int(1),
    41  			Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}},
    42  	}
    43  
    44  	handlePR(*hp, pre)
    45  
    46  	ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.TriageIssueLabel}}}
    47  	if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) {
    48  		t.Error("Failed: TestOnCreation -> Test if there is an incorrect issue link on creation.")
    49  	}
    50  
    51  	// Test if body on creation is correct -> do nothing
    52  	pre = github.PullRequestEvent{
    53  		Action: github.String("opened"),
    54  		Number: github.Int(999),
    55  		Repo: &github.Repository{
    56  			Owner: &github.User{Login: github.String(hp.Org)},
    57  			Name:  github.String(hp.Repo),
    58  			ID:    github.Int64(1234)},
    59  		PullRequest: &github.PullRequest{
    60  			Body:   &prBodyCorrect,
    61  			Title:  github.String("Test PR creation -> removeLabel"),
    62  			Number: github.Int(1),
    63  			Labels: []*github.Label{{Name: github.String(constants.TriageIssueLabel)}}},
    64  	}
    65  
    66  	handlePR(*hp, pre)
    67  
    68  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}}
    69  	if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) {
    70  		t.Error("Failed: TestOnCreation -> Test if there is a correct issue link on creation.")
    71  	}
    72  
    73  	// Test if body on creation is correct -> update title
    74  	pre = github.PullRequestEvent{
    75  		Action: github.String("opened"),
    76  		Number: github.Int(999),
    77  		Repo: &github.Repository{
    78  			Owner: &github.User{Login: github.String(hp.Org)},
    79  			Name:  github.String(hp.Repo),
    80  			ID:    github.Int64(1234)},
    81  		PullRequest: &github.PullRequest{
    82  			Body:   &prBodyCorrect,
    83  			Title:  github.String("Test PR creation -> update title"),
    84  			Number: github.Int(1),
    85  			Labels: []*github.Label{}},
    86  	}
    87  
    88  	handlePR(*hp, pre)
    89  
    90  	// Updated title using correct body
    91  	pre = github.PullRequestEvent{
    92  		Action: github.String("opened"),
    93  		Number: github.Int(999),
    94  		Repo: &github.Repository{
    95  			Owner: &github.User{Login: github.String(hp.Org)},
    96  			Name:  github.String(hp.Repo),
    97  			ID:    github.Int64(1234)},
    98  		PullRequest: &github.PullRequest{
    99  			Body:   &prBodyCorrect,
   100  			Title:  github.String("2850: Test PR creation -> update title"),
   101  			Number: github.Int(1),
   102  			Labels: []*github.Label{}},
   103  	}
   104  
   105  	handlePR(*hp, pre)
   106  
   107  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}}
   108  
   109  	if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) {
   110  		t.Error("Failed: TestOnCreation -> Test if there is a correct issue link on creation and update the title.")
   111  	}
   112  }
   113  
   114  func TestEditBody(t *testing.T) {
   115  	pre := github.PullRequestEvent{
   116  		Action: github.String("opened"),
   117  		Number: github.Int(999),
   118  		Repo: &github.Repository{
   119  			Owner: &github.User{Login: github.String(hp.Org)},
   120  			Name:  github.String(hp.Repo),
   121  			ID:    github.Int64(1234)},
   122  		PullRequest: &github.PullRequest{
   123  			Body:   &prBodyIncorrect,
   124  			Title:  github.String("Test PR creation -> addLabel"),
   125  			Number: github.Int(1),
   126  			Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}},
   127  	}
   128  
   129  	handlePR(*hp, pre)
   130  
   131  	// Remove a label using edited correct body
   132  	pre = github.PullRequestEvent{
   133  		Action: github.String("edited"),
   134  		Number: github.Int(999),
   135  		Repo: &github.Repository{
   136  			Owner: &github.User{Login: github.String(hp.Org)},
   137  			Name:  github.String(hp.Repo),
   138  			ID:    github.Int64(1234)},
   139  		PullRequest: &github.PullRequest{
   140  			Body:   &prBodyCorrect,
   141  			Title:  github.String("Test edit body with new correct body"),
   142  			Number: github.Int(1),
   143  			Labels: []*github.Label{{Name: github.String(constants.TriageIssueLabel)}}},
   144  	}
   145  
   146  	handlePR(*hp, pre)
   147  
   148  	ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}}
   149  	if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) {
   150  		t.Error("Failed: TestEditBody -> Test editing a body from correct to incorrect.")
   151  	}
   152  
   153  	// Add a label back using edited incorrect body
   154  	pre = github.PullRequestEvent{
   155  		Action: github.String("edited"),
   156  		Number: github.Int(999),
   157  		Repo: &github.Repository{
   158  			Owner: &github.User{Login: github.String(hp.Org)},
   159  			Name:  github.String(hp.Repo),
   160  			ID:    github.Int64(1234)},
   161  		PullRequest: &github.PullRequest{
   162  			Body:   &prBodyIncorrect,
   163  			Title:  github.String("Test edit body by replacing with incorrect body"),
   164  			Number: github.Int(1),
   165  			Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}},
   166  	}
   167  
   168  	handlePR(*hp, pre)
   169  
   170  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.TriageIssueLabel}}}
   171  	if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) {
   172  		t.Error("Failed: TestEditBody -> Test editing a body from correct to incorrect.")
   173  	}
   174  }
   175  
   176  func TestPRComments(t *testing.T) {
   177  	prceCorrect := github.IssueCommentEvent{
   178  		Action: github.String("created"),
   179  		Issue: &github.Issue{
   180  			Body:   &prBodyIncorrect,
   181  			Title:  github.String("PR comment creation -> removeLabel"),
   182  			Number: github.Int(1),
   183  			PullRequestLinks: &github.PullRequestLinks{
   184  				URL: github.String("testurl.com/unitTestNeedsIssue")},
   185  			Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}},
   186  		Comment: &github.IssueComment{Body: github.String("/link #2850")},
   187  	}
   188  
   189  	handlePRComment(*hp, prceCorrect)
   190  
   191  	ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Body: prBodyCorrect, Labels: []string{}}}
   192  	if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) {
   193  		t.Error("Failed: TestPRComments -> Test adding correct comment link.")
   194  	}
   195  
   196  	prceIncorrect := github.IssueCommentEvent{
   197  		Action: github.String("created"),
   198  		Issue: &github.Issue{
   199  			Body:   &prBodyCorrect,
   200  			Title:  github.String("PR comment creation -> addLabel"),
   201  			Number: github.Int(1),
   202  			PullRequestLinks: &github.PullRequestLinks{
   203  				URL: github.String("testurl.com/unitTestNeedsIssue")},
   204  			Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}},
   205  		Comment: &github.IssueComment{Body: github.String("/link - #2850")},
   206  	}
   207  
   208  	handlePRComment(*hp, prceIncorrect)
   209  
   210  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Body: prBodyIncorrect, Labels: []string{constants.TriageIssueLabel}}}
   211  	if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) {
   212  		t.Error("Failed: TestPRComments -> Test removing correct comment link.")
   213  	}
   214  }
   215  
   216  func TestManualAddRemove(t *testing.T) {
   217  	pr = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}}
   218  	hp = &plugin.HandlerParams{
   219  		Ctx:    context.Background(),
   220  		Client: pr,
   221  		Org:    "test-org",
   222  		Repo:   "test-repo",
   223  		Log:    *logging.NewLogger(),
   224  	}
   225  	pre := github.PullRequestEvent{
   226  		Action: github.String("labeled"),
   227  		Number: github.Int(999),
   228  		Repo: &github.Repository{
   229  			Owner: &github.User{Login: github.String(hp.Org)},
   230  			Name:  github.String(hp.Repo),
   231  			ID:    github.Int64(1234)},
   232  		PullRequest: &github.PullRequest{
   233  			Body:   &prBodyIncorrect,
   234  			Title:  github.String("Test adding label with incorrect body -> addLabel"),
   235  			Number: github.Int(1),
   236  			Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}},
   237  	}
   238  
   239  	handlePR(*hp, pre)
   240  
   241  	ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.TriageIssueLabel}}}
   242  	if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) {
   243  		t.Error("Failed: TestManualAddRemove -> Test adding label with correct body.")
   244  	}
   245  
   246  	// Test add label on correct PR body -> removeLabel
   247  	pre = github.PullRequestEvent{
   248  		Action: github.String("labeled"),
   249  		Number: github.Int(999),
   250  		Repo: &github.Repository{
   251  			Owner: &github.User{Login: github.String(hp.Org)},
   252  			Name:  github.String(hp.Repo),
   253  			ID:    github.Int64(1234)},
   254  		PullRequest: &github.PullRequest{
   255  			Body:   &prBodyCorrect,
   256  			Title:  github.String("Test adding label with correct body -> removeLabel"),
   257  			Number: github.Int(1),
   258  			Labels: []*github.Label{{Name: github.String(constants.TriageIssueLabel)}}},
   259  	}
   260  
   261  	handlePR(*hp, pre)
   262  
   263  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}}
   264  	if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) {
   265  		t.Error("Failed: TestManualAddRemove -> Test adding label with correct body.")
   266  	}
   267  
   268  	pre = github.PullRequestEvent{
   269  		Action: github.String("unlabeled"),
   270  		Number: github.Int(999),
   271  		Repo: &github.Repository{
   272  			Owner: &github.User{Login: github.String(hp.Org)},
   273  			Name:  github.String(hp.Repo),
   274  			ID:    github.Int64(1234)},
   275  		PullRequest: &github.PullRequest{
   276  			Body:   &prBodyIncorrect,
   277  			Title:  github.String("Test removing label with incorrect body -> addLabel"),
   278  			Number: github.Int(1),
   279  			Labels: []*github.Label{{Name: github.String(constants.PluginPRIssue)}}},
   280  	}
   281  
   282  	handlePR(*hp, pre)
   283  
   284  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.TriageIssueLabel}}}
   285  	if !reflect.DeepEqual(ct.GithubIssues, pr.GithubIssues) {
   286  		t.Error("Failed: TestManualAddRemove -> Test adding label with correct body.")
   287  	}
   288  }
   289  

View as plain text