Esempio n. 1
0
func TestQueueOrder(t *testing.T) {
	timeBase := time.Now()
	time2 := timeBase.Add(6 * time.Minute).Unix()
	time3 := timeBase.Add(5 * time.Minute).Unix()
	time4 := timeBase.Add(4 * time.Minute).Unix()
	time5 := timeBase.Add(3 * time.Minute).Unix()
	time6 := timeBase.Add(2 * time.Minute).Unix()
	labelEvents := map[int][]github_test.LabelTime{
		2: {{"me", lgtmLabel, time2}},
		3: {{"me", lgtmLabel, time3}},
		4: {{"me", lgtmLabel, time4}},
		5: {{"me", lgtmLabel, time5}},
		6: {{"me", lgtmLabel, time6}},
	}

	tests := []struct {
		name          string
		issues        []*github.Issue
		issueToEvents map[int][]github_test.LabelTime
		expected      []int
	}{
		{
			name: "Just prNum",
			issues: []*github.Issue{
				github_test.Issue(someUserName, 2, nil, true),
				github_test.Issue(someUserName, 3, nil, true),
				github_test.Issue(someUserName, 4, nil, true),
				github_test.Issue(someUserName, 5, nil, true),
			},
			issueToEvents: labelEvents,
			expected:      []int{5, 4, 3, 2},
		},
		{
			name: "With a priority label",
			issues: []*github.Issue{
				github_test.Issue(someUserName, 2, []string{"priority/P1"}, true),
				github_test.Issue(someUserName, 3, []string{"priority/P1"}, true),
				github_test.Issue(someUserName, 4, []string{"priority/P0"}, true),
				github_test.Issue(someUserName, 5, nil, true),
			},
			issueToEvents: labelEvents,
			expected:      []int{4, 3, 2, 5},
		},
		{
			name: "With two priority labels",
			issues: []*github.Issue{
				github_test.Issue(someUserName, 2, []string{"priority/P1", "priority/P0"}, true),
				github_test.Issue(someUserName, 3, []string{"priority/P1"}, true),
				github_test.Issue(someUserName, 4, []string{"priority/P0"}, true),
				github_test.Issue(someUserName, 5, nil, true),
			},
			issueToEvents: labelEvents,
			expected:      []int{4, 2, 3, 5},
		},
		{
			name: "With unrelated labels",
			issues: []*github.Issue{
				github_test.Issue(someUserName, 2, []string{"priority/P1", "priority/P0"}, true),
				github_test.Issue(someUserName, 3, []string{"priority/P1", "kind/design"}, true),
				github_test.Issue(someUserName, 4, []string{"priority/P0"}, true),
				github_test.Issue(someUserName, 5, []string{lgtmLabel, "kind/new-api"}, true),
			},
			issueToEvents: labelEvents,
			expected:      []int{4, 2, 3, 5},
		},
		{
			name: "With invalid priority label",
			issues: []*github.Issue{
				github_test.Issue(someUserName, 2, []string{"priority/P1", "priority/P0"}, true),
				github_test.Issue(someUserName, 3, []string{"priority/P1", "kind/design", "priority/high"}, true),
				github_test.Issue(someUserName, 4, []string{"priority/P0", "priorty/bob"}, true),
				github_test.Issue(someUserName, 5, nil, true),
			},
			issueToEvents: labelEvents,
			expected:      []int{4, 2, 3, 5},
		},
		{
			name: "Unlabeled counts as P3",
			issues: []*github.Issue{
				github_test.Issue(someUserName, 2, nil, true),
				github_test.Issue(someUserName, 3, []string{"priority/P3"}, true),
				github_test.Issue(someUserName, 4, []string{"priority/P2"}, true),
				github_test.Issue(someUserName, 5, nil, true),
			},
			issueToEvents: labelEvents,
			expected:      []int{4, 5, 3, 2},
		},
		{
			name: "retestNotRequiredLabel counts as P-negative 1",
			issues: []*github.Issue{
				github_test.Issue(someUserName, 2, nil, true),
				github_test.Issue(someUserName, 3, []string{"priority/P3"}, true),
				github_test.Issue(someUserName, 4, []string{"priority/P0"}, true),
				github_test.Issue(someUserName, 5, nil, true),
				github_test.Issue(someUserName, 6, []string{"priority/P3", retestNotRequiredLabel}, true),
			},
			issueToEvents: labelEvents,
			expected:      []int{6, 4, 5, 3, 2},
		},
	}
	for testNum, test := range tests {
		config := &github_util.Config{}
		client, server, mux := github_test.InitServer(t, nil, nil, github_test.MultiIssueEvents(test.issueToEvents), nil, nil, nil, nil)
		config.Org = "o"
		config.Project = "r"
		config.SetClient(client)
		sq := getTestSQ(false, config, server)
		for i := range test.issues {
			issue := test.issues[i]
			github_test.ServeIssue(t, mux, issue)

			issueNum := *issue.Number
			obj, err := config.GetObject(issueNum)
			if err != nil {
				t.Fatalf("%d:%q unable to get issue: %v", testNum, test.name, err)
			}
			sq.githubE2EQueue[issueNum] = obj
		}
		actual := sq.orderedE2EQueue()
		if len(actual) != len(test.expected) {
			t.Fatalf("%d:%q len(actual):%v != len(expected):%v", testNum, test.name, actual, test.expected)
		}
		for i, a := range actual {
			e := test.expected[i]
			if a != e {
				t.Errorf("%d:%q a[%d]:%d != e[%d]:%d", testNum, test.name, i, a, i, e)
			}
		}
		server.Close()
	}
}
Esempio n. 2
0
func TestAddLGTMIfCommented(t *testing.T) {
	runtime.GOMAXPROCS(runtime.NumCPU())

	tests := []struct {
		name        string
		comments    []*github.IssueComment
		events      []*github.IssueEvent
		issue       *github.Issue
		assignees   mungerutil.UserSet
		mustHave    []string
		mustNotHave []string
	}{
		{
			name:  "Other comments should not add LGTM.",
			issue: prWithoutLGTM,
			comments: []*github.IssueComment{
				github_test.IssueComment(1, "/comment 1", "user 1", 0),
				github_test.IssueComment(2, "/comment 2 //comment3", "user 2", 1),
			},
			assignees:   mungerutil.UserSet(sets.NewString("user 1")),
			mustHave:    []string{},
			mustNotHave: []string{lgtmLabel},
		},
		{
			name:  "/lgtm by non-assignee should not add LGTM label",
			issue: prWithoutLGTM,
			comments: []*github.IssueComment{
				github_test.IssueComment(1, "/lgtm", "user 1", 0),
				github_test.IssueComment(2, "comment 2", "user 2", 1),
			},
			assignees:   mungerutil.UserSet(sets.NewString("user 2")),
			mustHave:    []string{},
			mustNotHave: []string{lgtmLabel},
		},
		{
			name:  "/lgtm by assignee should add LGTM label",
			issue: prWithoutLGTM,
			comments: []*github.IssueComment{
				github_test.IssueComment(1, "/lgtm", "user 1", 0),
				github_test.IssueComment(2, "comment 2", "user 2", 1),
			},
			assignees:   mungerutil.UserSet(sets.NewString("user 1")),
			mustHave:    []string{lgtmLabel},
			mustNotHave: []string{},
		},
		{
			name:  "/lgtm by assignee followed by cancellation by non-assignee should add lgtm",
			issue: prWithoutLGTM,
			comments: []*github.IssueComment{
				github_test.IssueComment(1, "/lgtm", "user 1", 0),
				github_test.IssueComment(2, "/lgtm cancel", "user 2", 1),
			},
			assignees:   mungerutil.UserSet(sets.NewString("user 1")),
			mustHave:    []string{lgtmLabel},
			mustNotHave: []string{},
		},
		{
			name:  "/lgtm by assignee followed by /lgtm cancel should not add lgtm",
			issue: prWithoutLGTM,
			comments: []*github.IssueComment{
				github_test.IssueComment(1, "/lgtm", "user 1", 0),
				github_test.IssueComment(2, "/lgtm cancel", "user 2", 1),
			},
			assignees:   mungerutil.UserSet(sets.NewString("user 1", "user 2")),
			mustHave:    []string{},
			mustNotHave: []string{lgtmLabel},
		},
		{
			name:  "/lgtm followed by comment should be honored",
			issue: prWithoutLGTM,
			comments: []*github.IssueComment{
				github_test.IssueComment(1, "/lgtm //this is a comment", "user 1", 0),
			},
			assignees:   mungerutil.UserSet(sets.NewString("user 1")),
			mustHave:    []string{lgtmLabel},
			mustNotHave: []string{},
		},
		{
			name:  "/lgtm cancel by bot should be honored",
			issue: prWithoutLGTM,
			comments: []*github.IssueComment{
				github_test.IssueComment(1, "/lgtm //this is a comment", "user 1", 0),
				github_test.IssueComment(1, "/lgtm cancel //this is a bot", botName, 0),
			},
			assignees:   mungerutil.UserSet(sets.NewString("user 1")),
			mustHave:    []string{},
			mustNotHave: []string{lgtmLabel},
		},
		{
			name:  "manual label removal after /lgtm should be honored",
			issue: prWithoutLGTM,
			comments: []*github.IssueComment{
				github_test.IssueComment(1, "/lgtm //this is a comment", "user 1", 0),
			},
			events: github_test.MultiIssueEvents(
				map[int][]github_test.LabelTime{
					1: {{"me", lgtmLabel, 1}},
				}, "unlabeled"),
			assignees:   mungerutil.UserSet(sets.NewString("user 1")),
			mustHave:    []string{},
			mustNotHave: []string{lgtmLabel},
		},
	}

	for testNum, test := range tests {
		pr := ValidPR()
		client, server, mux := github_test.InitServer(t, test.issue, pr, test.events, nil, nil, nil, nil)
		path := fmt.Sprintf("/repos/o/r/issue/%s/labels", *test.issue.Number)
		mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) {
			w.WriteHeader(http.StatusOK)
			out := []github.Label{{}}
			data, err := json.Marshal(out)
			if err != nil {
				t.Errorf("Unexpected error: %v", err)
			}
			w.Write(data)
		})

		config := &github_util.Config{}
		config.Org = "o"
		config.Project = "r"
		config.SetClient(client)

		l := LGTMHandler{}
		obj, err := config.GetObject(*test.issue.Number)
		if err != nil {
			t.Fatalf("%v", err)
		}

		l.addLGTMIfCommented(obj, test.comments, test.events, test.assignees)
		for _, lab := range test.mustHave {
			if !obj.HasLabel(lab) {
				t.Errorf("%s:%d: Did not find label %q, labels: %v", test.name, testNum, lab, obj.Issue.Labels)
			}
		}
		for _, lab := range test.mustNotHave {
			if obj.HasLabel(lab) {
				t.Errorf("%s:%d: Found label %q and should not have, labels: %v", test.name, testNum, lab, obj.Issue.Labels)
			}
		}
		server.Close()
	}
}