func TestPRGetFixesList(t *testing.T) { tests := []struct { issue *github.Issue body string expected []int }{ { issue: github_test.Issue("", 1, []string{"label1"}, false), body: `bla resolve this pr closes #45545 and also fixes #679 bla, some more bla with close here and there. some suggest that it resolved #5643`, expected: []int{45545, 679, 5643}, }, { issue: github_test.Issue("", 2, []string{"label1"}, false), body: `bla resolve 345 some suggest that it also closes #892`, expected: []int{892}, }, { issue: github_test.Issue("", 3, []string{"label1"}, false), body: `bla resolve this pr closes and fixes nothing`, expected: nil, }, { issue: github_test.Issue("", 4, []string{"label1"}, false), body: `bla bla this pr Fixes #23 and FIXES #45 but not fixxx #99`, expected: []int{23, 45}, }, } for testNum, test := range tests { client, server, _ := github_test.InitServer(t, test.issue, nil, nil, nil, nil, nil, nil) config := &Config{} config.Org = "o" config.Project = "r" config.SetClient(client) obj, err := config.GetObject(*test.issue.Number) if err != nil { t.Fatalf("%d: unable to get issue: %v", testNum, *test.issue.Number) } obj.Issue.Body = &test.body fixes := obj.GetPRFixesList() if len(test.expected) != len(fixes) { t.Errorf("%d: len(fixes) not equal, expected: %v but got: %v", testNum, test.expected, fixes) return } for i, n := range test.expected { if n != fixes[i] { t.Errorf("%d: expected fixes: %v but got fixes: %v", testNum, test.expected, fixes) } } server.Close() } }
func TestRemoveLabel(t *testing.T) { tests := []struct { issue *github.Issue remove string expected []string }{ { issue: github_test.Issue("", 1, []string{"label1"}, false), remove: "label1", expected: []string{}, }, { issue: github_test.Issue("", 1, []string{"label2", "label1"}, false), remove: "label1", expected: []string{"label2"}, }, { issue: github_test.Issue("", 1, []string{"label2"}, false), remove: "label1", expected: []string{"label2"}, }, { issue: github_test.Issue("", 1, []string{}, false), remove: "label1", expected: []string{}, }, } for testNum, test := range tests { client, server, mux := github_test.InitServer(t, test.issue, nil, nil, nil, nil, nil) config := &Config{} config.Org = "o" config.Project = "r" config.SetClient(client) mux.HandleFunc(fmt.Sprintf("/repos/o/r/issues/1/labels/%s", test.remove), func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) }) obj, err := config.GetObject(*test.issue.Number) if err != nil { t.Fatalf("%d: unable to get issue: %v", testNum, *test.issue.Number) } obj.RemoveLabel(test.remove) if len(test.expected) != len(obj.Issue.Labels) { t.Errorf("%d: len(labels) not equal, expected labels: %v but got labels: %v", testNum, test.expected, obj.Issue.Labels) return } for i, l := range test.expected { if l != *obj.Issue.Labels[i].Name { t.Errorf("%d: expected labels: %v but got labels: %v", testNum, test.expected, obj.Issue.Labels) } } server.Close() } }
func TestHasLabels(t *testing.T) { tests := []struct { obj MungeObject seekLabels []string hasLabel bool }{ { obj: MungeObject{ Issue: github_test.Issue("", 1, []string{"foo"}, true), }, seekLabels: []string{"foo"}, hasLabel: true, }, { obj: MungeObject{ Issue: github_test.Issue("", 1, []string{"bar"}, true), }, seekLabels: []string{"foo"}, hasLabel: false, }, { obj: MungeObject{ Issue: github_test.Issue("", 1, []string{"bar", "foo"}, true), }, seekLabels: []string{"foo"}, hasLabel: true, }, { obj: MungeObject{ Issue: github_test.Issue("", 1, []string{"bar", "baz"}, true), }, seekLabels: []string{"foo"}, hasLabel: false, }, { obj: MungeObject{ Issue: github_test.Issue("", 1, []string{"foo"}, true), }, seekLabels: []string{"foo", "bar"}, hasLabel: false, }, } for _, test := range tests { if test.hasLabel != test.obj.HasLabels(test.seekLabels) { t.Errorf("Unexpected output: %v", test) } } }
func DoNotMergeMilestoneIssue() *github.Issue { issue := github_test.Issue(someUserName, 1, []string{claYesLabel, lgtmLabel, doNotMergeLabel}, true) milestone := &github.Milestone{ Title: stringPtr(doNotMergeMilestone), } issue.Milestone = milestone return issue }
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() } }
func NoCLAIssue() *github.Issue { return github_test.Issue(someUserName, 1, []string{lgtmLabel}, true) }
func NoRetestIssue() *github.Issue { return github_test.Issue(someUserName, 1, []string{claYesLabel, lgtmLabel, retestNotRequiredLabel}, true) }
func OnlyApprovedIssue() *github.Issue { return github_test.Issue(someUserName, 1, []string{claYesLabel, approvedLabel}, true) }
func TestAssignFixes(t *testing.T) { runtime.GOMAXPROCS(runtime.NumCPU()) tests := []struct { name string assignee string pr *github.PullRequest prIssue *github.Issue prBody string fixesIssue *github.Issue }{ { name: "fixes an issue", assignee: "dev45", pr: github_test.PullRequest("dev45", false, true, true), prIssue: github_test.Issue("fred", 7779, []string{}, true), prBody: "does stuff and fixes #8889.", fixesIssue: github_test.Issue("jill", 8889, []string{}, true), }, } for _, test := range tests { test.prIssue.Body = &test.prBody client, server, mux := github_test.InitServer(t, test.prIssue, test.pr, nil, nil, nil, nil) path := fmt.Sprintf("/repos/o/r/issues/%d", *test.fixesIssue.Number) mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { data, err := json.Marshal(test.fixesIssue) if err != nil { t.Errorf("%v", err) } if r.Method != "PATCH" && r.Method != "GET" { t.Errorf("Unexpected method: expected: GET/PATCH got: %s", r.Method) } if r.Method == "PATCH" { body, _ := ioutil.ReadAll(r.Body) type IssuePatch struct { Assignee string } var ip IssuePatch err := json.Unmarshal(body, &ip) if err != nil { fmt.Println("error:", err) } if ip.Assignee != test.assignee { t.Errorf("Patching the incorrect Assignee %v instead of %v", ip.Assignee, test.assignee) } } w.WriteHeader(http.StatusOK) w.Write(data) }) config := &github_util.Config{} config.Org = "o" config.Project = "r" config.SetClient(client) c := AssignFixesMunger{} err := c.Initialize(config, nil) if err != nil { t.Fatalf("%v", err) } err = c.EachLoop() if err != nil { t.Fatalf("%v", err) } obj, err := config.GetObject(*test.prIssue.Number) if err != nil { t.Fatalf("%v", err) } c.Munge(obj) server.Close() } }
func DoNotMergeIssue() *github.Issue { return github_test.Issue(someUserName, 1, []string{claYesLabel, lgtmLabel, doNotMergeLabel}, true) }
func DoNotMergeIssue() *github.Issue { return github_test.Issue(whitelistUser, 1, []string{"cla: yes", "lgtm", doNotMergeLabel}, true) }
func TestQueueOrder(t *testing.T) { tests := []struct { name string issues []github.Issue expected []int }{ { name: "Just prNum", issues: []github.Issue{ *github_test.Issue(whitelistUser, 2, nil, true), *github_test.Issue(whitelistUser, 3, nil, true), *github_test.Issue(whitelistUser, 4, nil, true), *github_test.Issue(whitelistUser, 5, nil, true), }, expected: []int{2, 3, 4, 5}, }, { name: "With a priority label", issues: []github.Issue{ *github_test.Issue(whitelistUser, 2, []string{"priority/P1"}, true), *github_test.Issue(whitelistUser, 3, []string{"priority/P1"}, true), *github_test.Issue(whitelistUser, 4, []string{"priority/P0"}, true), *github_test.Issue(whitelistUser, 5, nil, true), }, expected: []int{4, 2, 3, 5}, }, { name: "With two priority labels", issues: []github.Issue{ *github_test.Issue(whitelistUser, 2, []string{"priority/P1", "priority/P0"}, true), *github_test.Issue(whitelistUser, 3, []string{"priority/P1"}, true), *github_test.Issue(whitelistUser, 4, []string{"priority/P0"}, true), *github_test.Issue(whitelistUser, 5, nil, true), }, expected: []int{2, 4, 3, 5}, }, { name: "With unrelated labels", issues: []github.Issue{ *github_test.Issue(whitelistUser, 2, []string{"priority/P1", "priority/P0"}, true), *github_test.Issue(whitelistUser, 3, []string{"priority/P1", "kind/design"}, true), *github_test.Issue(whitelistUser, 4, []string{"priority/P0"}, true), *github_test.Issue(whitelistUser, 5, []string{"LGTM", "kind/new-api"}, true), }, expected: []int{2, 4, 3, 5}, }, { name: "With invalid priority label", issues: []github.Issue{ *github_test.Issue(whitelistUser, 2, []string{"priority/P1", "priority/P0"}, true), *github_test.Issue(whitelistUser, 3, []string{"priority/P1", "kind/design", "priority/high"}, true), *github_test.Issue(whitelistUser, 4, []string{"priority/P0", "priorty/bob"}, true), *github_test.Issue(whitelistUser, 5, nil, true), }, expected: []int{2, 4, 3, 5}, }, { name: "Unlabeled counts as P3", issues: []github.Issue{ *github_test.Issue(whitelistUser, 2, nil, true), *github_test.Issue(whitelistUser, 3, []string{"priority/P3"}, true), *github_test.Issue(whitelistUser, 4, []string{"priority/P2"}, true), *github_test.Issue(whitelistUser, 5, nil, true), }, expected: []int{4, 2, 3, 5}, }, { name: "e2e-not-required counts as P-negative 1", issues: []github.Issue{ *github_test.Issue(whitelistUser, 2, nil, true), *github_test.Issue(whitelistUser, 3, []string{"priority/P3"}, true), *github_test.Issue(whitelistUser, 4, []string{"priority/P2"}, true), *github_test.Issue(whitelistUser, 5, nil, true), *github_test.Issue(whitelistUser, 6, []string{"priority/P3", e2eNotRequiredLabel}, true), }, expected: []int{6, 4, 2, 3, 5}, }, } for testNum, test := range tests { config := &github_util.Config{} client, server, mux := github_test.InitServer(t, nil, 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() } }
func BareIssue() *github.Issue { return github_test.Issue(whitelistUser, 1, []string{}, true) }
func TestForEachIssueDo(t *testing.T) { issue1 := github_test.Issue("bob", 1, nil, true) issue5 := github_test.Issue("bob", 5, nil, true) issue6 := github_test.Issue("bob", 6, nil, true) issue7 := github_test.Issue("bob", 7, nil, true) issue20 := github_test.Issue("bob", 20, nil, true) user := github.User{Login: stringPtr("bob")} tests := []struct { Issues [][]github.Issue Pages []int ValidIssues int }{ { Issues: [][]github.Issue{ {*issue5}, }, Pages: []int{0}, ValidIssues: 1, }, { Issues: [][]github.Issue{ {*issue5}, {*issue6}, {*issue7}, { { Number: intPtr(8), // no User, invalid }, }, }, Pages: []int{4, 4, 4, 0}, ValidIssues: 3, }, { Issues: [][]github.Issue{ // Invalid 1 < MinPRNumber // Invalid 20 > MaxPRNumber {*issue1, *issue20}, // two valid issues {*issue5, *issue6}, { { // no Number, invalid User: &user, }, }, }, Pages: []int{3, 3, 0}, ValidIssues: 2, }, } for i, test := range tests { client, server, mux := github_test.InitServer(t, nil, nil, nil, nil, nil, nil) config := &Config{ client: client, Org: "foo", Project: "bar", MinPRNumber: 5, MaxPRNumber: 15, } count := 0 mux.HandleFunc("/repos/foo/bar/issues", func(w http.ResponseWriter, r *http.Request) { if r.Method != "GET" { t.Errorf("Unexpected method: %s", r.Method) } // this means page 0, return page 1 page := r.URL.Query().Get("page") if page == "" { t.Errorf("Should not get page 0, start with page 1") } if page != strconv.Itoa(count+1) { t.Errorf("Unexpected page: %s", r.URL.Query().Get("page")) } if r.URL.Query().Get("sort") != "created" { t.Errorf("Unexpected sort: %s", r.URL.Query().Get("sort")) } if r.URL.Query().Get("per_page") != "100" { t.Errorf("Unexpected per_page: %s", r.URL.Query().Get("per_page")) } w.Header().Add("Link", fmt.Sprintf("<https://api.github.com/?page=%d>; rel=\"last\"", test.Pages[count])) w.WriteHeader(http.StatusOK) data, err := json.Marshal(test.Issues[count]) if err != nil { t.Errorf("Unexpected error: %v", err) } w.Write(data) count++ }) objects := []*MungeObject{} handle := func(obj *MungeObject) error { objects = append(objects, obj) return nil } err := config.ForEachIssueDo(handle) if err != nil { t.Errorf("unexpected error: %v", err) } if len(objects) != test.ValidIssues { t.Errorf("Test: %d Unexpected output %d vs %d", i, len(objects), test.ValidIssues) } if count != len(test.Issues) { t.Errorf("Test: %d Unexpected number of fetches: %d", i, count) } server.Close() } }
func TestGetLastModified(t *testing.T) { tests := []struct { commits []github.RepositoryCommit expectedTime *time.Time }{ { commits: github_test.Commits(1, 10), expectedTime: timePtr(time.Unix(10, 0)), }, { // remember the order of github_test.Commits() is non-deterministic commits: github_test.Commits(3, 10), expectedTime: timePtr(time.Unix(12, 0)), }, { // so this is probably not quite the same test... commits: github_test.Commits(3, 8), expectedTime: timePtr(time.Unix(10, 0)), }, { // We can't represent the same time in 2 commits using github_test.Commits() commits: []github.RepositoryCommit{ { SHA: stringPtr("mysha1"), Commit: &github.Commit{ SHA: stringPtr("mysha1"), Committer: &github.CommitAuthor{ Date: timePtr(time.Unix(9, 0)), }, }, }, { SHA: stringPtr("mysha2"), Commit: &github.Commit{ SHA: stringPtr("mysha2"), Committer: &github.CommitAuthor{ Date: timePtr(time.Unix(10, 0)), }, }, }, { SHA: stringPtr("mysha3"), Commit: &github.Commit{ SHA: stringPtr("mysha3"), Committer: &github.CommitAuthor{ Date: timePtr(time.Unix(9, 0)), }, }, }, }, expectedTime: timePtr(time.Unix(10, 0)), }, } for _, test := range tests { client, server, _ := github_test.InitServer(t, nil, nil, nil, test.commits, nil, nil) config := &Config{} config.Org = "o" config.Project = "r" config.SetClient(client) obj := &MungeObject{ config: config, Issue: github_test.Issue("bob", 1, nil, true), } ts := obj.LastModifiedTime() if !ts.Equal(*test.expectedTime) { t.Errorf("expected: %v, saw: %v for: %v", test.expectedTime, ts, test) } server.Close() } }
func docsProposalIssue() *github.Issue { return github_test.Issue(botName, 1, []string{claYesLabel, "kind/design"}, true) }
func docsProposalIssue() *github.Issue { return github_test.Issue(botName, 1, []string{"cla: yes", "kind/design"}, true) }
func TestCherrypickAuthApprove(t *testing.T) { runtime.GOMAXPROCS(runtime.NumCPU()) tests := []struct { name string issue *github.Issue issueBody string prBranch string parentIssue *github.Issue milestone *github.Milestone shouldHaveLabel string shouldHaveMilestone string shouldNotHaveLabel string shouldNotHaveMile string }{ { name: "Add cpApproved and milestone", issue: github_test.Issue(botName, 1, []string{}, true), issueBody: "Cherry pick of #2 on release-1.2.", prBranch: "release-1.2", parentIssue: github_test.Issue(botName, 2, []string{cpApprovedLabel}, true), milestone: &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)}, shouldHaveLabel: cpApprovedLabel, shouldHaveMilestone: "v1.2", }, { name: "Add milestone", issue: github_test.Issue(botName, 1, []string{cpApprovedLabel}, true), issueBody: "Cherry pick of #2 on release-1.2.", prBranch: "release-1.2", parentIssue: github_test.Issue(botName, 2, []string{cpApprovedLabel}, true), milestone: &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)}, shouldHaveLabel: cpApprovedLabel, shouldHaveMilestone: "v1.2", }, { name: "Do not add because parent not have", issue: github_test.Issue(botName, 1, []string{}, true), issueBody: "Cherry pick of #2 on release-1.2.", prBranch: "release-1.2", parentIssue: github_test.Issue(botName, 2, []string{}, true), milestone: &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)}, shouldNotHaveLabel: cpApprovedLabel, shouldNotHaveMile: "v1.2", }, { name: "PR against wrong branch", issue: github_test.Issue(botName, 1, []string{}, true), issueBody: "Cherry pick of #2 on release-1.2.", prBranch: "release-1.1", parentIssue: github_test.Issue(botName, 2, []string{cpApprovedLabel}, true), milestone: &github.Milestone{Title: stringPtr("v1.2"), Number: intPtr(1)}, shouldNotHaveLabel: cpApprovedLabel, shouldNotHaveMile: "v1.2", }, { name: "Parent milestone against other branch", issue: github_test.Issue(botName, 1, []string{}, true), issueBody: "Cherry pick of #2 on release-1.2.", prBranch: "release-1.2", parentIssue: github_test.Issue(botName, 2, []string{cpApprovedLabel}, true), milestone: &github.Milestone{Title: stringPtr("v1.1"), Number: intPtr(1)}, shouldNotHaveLabel: cpApprovedLabel, shouldNotHaveMile: "v1.1", }, { name: "Parent has no milestone", issue: github_test.Issue(botName, 1, []string{}, true), issueBody: "Cherry pick of #2 on release-1.2.", prBranch: "release-1.2", parentIssue: github_test.Issue(botName, 2, []string{cpApprovedLabel}, true), shouldNotHaveLabel: cpApprovedLabel, shouldNotHaveMile: "v1.2", }, } for testNum, test := range tests { test.issue.Body = &test.issueBody pr := ValidPR() pr.Base.Ref = &test.prBranch client, server, mux := github_test.InitServer(t, test.issue, pr, nil, nil, nil, nil, nil) path := fmt.Sprintf("/repos/o/r/issues/%d/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) }) path = "/repos/o/r/milestones" mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) out := []github.Milestone{} if test.milestone != nil { out = append(out, *test.milestone) } data, err := json.Marshal(out) if err != nil { t.Errorf("Unexpected error: %v", err) } w.Write(data) }) test.parentIssue.Milestone = test.milestone path = fmt.Sprintf("/repos/o/r/issues/%d", *test.parentIssue.Number) mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { data, err := json.Marshal(test.parentIssue) if err != nil { t.Errorf("%v", err) } if r.Method != "GET" { t.Errorf("Unexpected method: expected: GET got: %s", r.Method) } w.WriteHeader(http.StatusOK) w.Write(data) }) config := &github_util.Config{} config.Org = "o" config.Project = "r" config.SetClient(client) c := CherrypickAutoApprove{} err := c.Initialize(config, nil) if err != nil { t.Fatalf("%v", err) } err = c.EachLoop() if err != nil { t.Fatalf("%v", err) } obj, err := config.GetObject(*test.issue.Number) if err != nil { t.Fatalf("%v", err) } c.Munge(obj) if test.shouldHaveLabel != "" && !obj.HasLabel(test.shouldHaveLabel) { t.Errorf("%d:%q: missing label %q", testNum, test.name, test.shouldHaveLabel) } if test.shouldHaveMilestone != "" && obj.ReleaseMilestone() != test.shouldHaveMilestone { t.Errorf("%d:%q: missing milestone %q", testNum, test.name, test.shouldHaveMilestone) } if test.shouldNotHaveLabel != "" && obj.HasLabel(test.shouldNotHaveLabel) { t.Errorf("%d:%q: extra label %q", testNum, test.name, test.shouldNotHaveLabel) } if test.shouldNotHaveMile != "" && obj.ReleaseMilestone() == test.shouldNotHaveMile { t.Errorf("%d:%q: extra milestone %q", testNum, test.name, test.shouldNotHaveMile) } server.Close() } }
func BareIssue() *github.Issue { return github_test.Issue(someUserName, 1, []string{}, true) }
func NoCLAIssue() *github.Issue { return github_test.Issue(whitelistUser, 1, []string{"lgtm", "ok-to-merge"}, true) }
func LGTMIssue() *github.Issue { return github_test.Issue(someUserName, 1, []string{claYesLabel, lgtmLabel}, true) }
func UserNotInWhitelistOKToMergeIssue() *github.Issue { return github_test.Issue(noWhitelistUser, 1, []string{"lgtm", "cla: yes", "ok-to-merge"}, true) }
"encoding/json" "fmt" "net/http" "runtime" "testing" github_util "k8s.io/contrib/mungegithub/github" github_test "k8s.io/contrib/mungegithub/github/testing" "k8s.io/contrib/mungegithub/mungers/mungerutil" "k8s.io/kubernetes/pkg/util/sets" "github.com/google/go-github/github" ) var ( prWithLGTM = github_test.Issue(botName, 1, []string{lgtmLabel}, true) prWithoutLGTM = github_test.Issue(botName, 1, []string{}, true) ) func TestAddLGTMIfCommented(t *testing.T) { runtime.GOMAXPROCS(runtime.NumCPU()) tests := []struct { name string comments []*github.IssueComment issue *github.Issue assignees mungerutil.UserSet mustHave []string mustNotHave []string }{ {
func DontRequireGithubE2EIssue() *github.Issue { return github_test.Issue(whitelistUser, 1, []string{"cla: yes", "lgtm", e2eNotRequiredLabel}, true) }
func NoOKToMergeIssue() *github.Issue { return github_test.Issue(whitelistUser, 1, []string{"cla: yes", "lgtm"}, true) }
func TestCLAMunger(t *testing.T) { runtime.GOMAXPROCS(runtime.NumCPU()) tests := []struct { name string issue *github.Issue status *github.CombinedStatus mustHave []string mustNotHave []string }{ { name: "CLA status success should add cncf/cla:yes label and remove cncf/cla:no label", issue: github_test.Issue("user1", 1, []string{cncfClaNoLabel}, true), status: &github.CombinedStatus{ Statuses: []github.RepoStatus{ { Context: stringPtr(claContext), State: stringPtr(contextSuccess), }, }, }, mustHave: []string{cncfClaYesLabel}, mustNotHave: []string{cncfClaNoLabel}, }, { name: "CLA status failure should add cncf/cla:no label and remove cncf/cla:yes label", issue: github_test.Issue("user1", 1, []string{cncfClaYesLabel}, true), status: &github.CombinedStatus{ Statuses: []github.RepoStatus{ { Context: stringPtr(claContext), State: stringPtr(contextFailure), }, }, }, mustHave: []string{cncfClaNoLabel}, mustNotHave: []string{cncfClaYesLabel}, }, { name: "CLA status error should apply cncf/cla:no label.", issue: github_test.Issue("user1", 1, []string{}, true), status: &github.CombinedStatus{ Statuses: []github.RepoStatus{ { Context: stringPtr(claContext), State: stringPtr(contextError), }, }, }, mustHave: []string{cncfClaNoLabel}, mustNotHave: []string{cncfClaYesLabel}, }, { name: "CLA status pending should not apply labels.", issue: github_test.Issue("user1", 1, []string{}, true), status: &github.CombinedStatus{ Statuses: []github.RepoStatus{ { Context: stringPtr(claContext), State: stringPtr(contextPending), }, }, }, mustHave: []string{}, mustNotHave: []string{cncfClaYesLabel, cncfClaNoLabel}, }, } for testNum, test := range tests { pr := ValidPR() pr.Head = &github.PullRequestBranch{} pr.Head.SHA = stringPtr("0") client, server, mux := github_test.InitServer(t, test.issue, pr, nil, nil, nil, nil, nil) setUpMockFunctions(mux, t, test.issue) path := fmt.Sprintf("/repos/o/r/commits/%s/status", *pr.Head.SHA) mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) out := test.status 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) cla := ClaMunger{ CLAStatusContext: claContext, pinger: c.NewPinger("[fake-ping]").SetDescription(""), } obj, err := config.GetObject(*test.issue.Number) if err != nil { t.Fatalf("%v", err) } cla.Munge(obj) 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() } }
func NoLGTMIssue() *github.Issue { return github_test.Issue(whitelistUser, 1, []string{"cla: yes", "ok-to-merge"}, true) }
func TestReleaseNoteLabel(t *testing.T) { runtime.GOMAXPROCS(runtime.NumCPU()) tests := []struct { name string issue *github.Issue body string branch string secondIssue *github.Issue mustHave []string mustNotHave []string }{ { name: "LGTM with release-note", issue: github_test.Issue(botName, 1, []string{"lgtm", releaseNote}, true), mustHave: []string{"lgtm", releaseNote}, mustNotHave: []string{releaseNoteLabelNeeded}, }, { name: "LGTM with release-note-none", issue: github_test.Issue(botName, 1, []string{"lgtm", releaseNoteNone}, true), mustHave: []string{"lgtm", releaseNoteNone}, mustNotHave: []string{releaseNoteLabelNeeded}, }, { name: "LGTM with release-note-action-required", issue: github_test.Issue(botName, 1, []string{"lgtm", releaseNoteActionRequired}, true), mustHave: []string{"lgtm", releaseNoteActionRequired}, mustNotHave: []string{releaseNoteLabelNeeded}, }, { name: "LGTM with release-note-label-needed", issue: github_test.Issue(botName, 1, []string{"lgtm", releaseNoteLabelNeeded}, true), mustHave: []string{releaseNoteLabelNeeded}, mustNotHave: []string{"lgtm"}, }, { name: "LGTM only", issue: github_test.Issue(botName, 1, []string{"lgtm"}, true), mustHave: []string{releaseNoteLabelNeeded}, mustNotHave: []string{"lgtm"}, }, { name: "No labels", issue: github_test.Issue(botName, 1, []string{}, true), mustHave: []string{releaseNoteLabelNeeded}, }, { name: "release-note", issue: github_test.Issue(botName, 1, []string{releaseNote}, true), mustHave: []string{releaseNote}, }, { name: "release-note-none", issue: github_test.Issue(botName, 1, []string{releaseNoteNone}, true), mustHave: []string{releaseNoteNone}, }, { name: "release-note-action-required", issue: github_test.Issue(botName, 1, []string{releaseNoteActionRequired}, true), mustHave: []string{releaseNoteActionRequired}, }, { name: "release-note and release-note-label-needed", issue: github_test.Issue(botName, 1, []string{releaseNote, releaseNoteLabelNeeded}, true), mustHave: []string{releaseNote}, mustNotHave: []string{releaseNoteLabelNeeded}, }, { name: "release-note-none and release-note-label-needed", issue: github_test.Issue(botName, 1, []string{releaseNoteNone, releaseNoteLabelNeeded}, true), mustHave: []string{releaseNoteNone}, mustNotHave: []string{releaseNoteLabelNeeded}, }, { name: "release-note-action-required and release-note-label-needed", issue: github_test.Issue(botName, 1, []string{releaseNoteActionRequired, releaseNoteLabelNeeded}, true), mustHave: []string{releaseNoteActionRequired}, mustNotHave: []string{releaseNoteLabelNeeded}, }, { name: "do not add needs label when parent PR has releaseNote label", branch: "release-1.2", issue: github_test.Issue(botName, 1, []string{}, true), body: "Cherry pick of #2 on release-1.2.", secondIssue: github_test.Issue(botName, 2, []string{releaseNote}, true), mustNotHave: []string{releaseNoteLabelNeeded}, }, { name: "do not touch LGTM on non-master when parent PR has releaseNote label", branch: "release-1.2", issue: github_test.Issue(botName, 1, []string{"lgtm"}, true), body: "Cherry pick of #2 on release-1.2.", secondIssue: github_test.Issue(botName, 2, []string{releaseNote}, true), mustHave: []string{"lgtm"}, mustNotHave: []string{releaseNoteLabelNeeded}, }, { name: "add needs label when parent PR does not have releaseNote label", branch: "release-1.2", issue: github_test.Issue(botName, 1, []string{}, true), body: "Cherry pick of #2 on release-1.2.", secondIssue: github_test.Issue(botName, 2, []string{releaseNoteNone}, true), mustHave: []string{releaseNoteLabelNeeded}, }, { name: "remove LGTM on non-master when parent PR has releaseNote label", branch: "release-1.2", issue: github_test.Issue(botName, 1, []string{"lgtm"}, true), body: "Cherry pick of #2 on release-1.2.", secondIssue: github_test.Issue(botName, 2, []string{releaseNoteNone}, true), mustHave: []string{releaseNoteLabelNeeded}, mustNotHave: []string{"lgtm"}, }, } for testNum, test := range tests { pr := ValidPR() if test.branch != "" { pr.Base.Ref = &test.branch } test.issue.Body = &test.body client, server, mux := github_test.InitServer(t, test.issue, pr, 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) }) if test.secondIssue != nil { path = fmt.Sprintf("/repos/o/r/issues/%d", *test.secondIssue.Number) mux.HandleFunc(path, func(w http.ResponseWriter, r *http.Request) { data, err := json.Marshal(test.secondIssue) if err != nil { t.Errorf("%v", err) } if r.Method != "GET" { t.Errorf("Unexpected method: expected: GET got: %s", r.Method) } w.WriteHeader(http.StatusOK) w.Write(data) }) } config := &github_util.Config{} config.Org = "o" config.Project = "r" config.SetClient(client) r := ReleaseNoteLabel{} err := r.Initialize(config, nil) if err != nil { t.Fatalf("%v", err) } err = r.EachLoop() if err != nil { t.Fatalf("%v", err) } obj, err := config.GetObject(*test.issue.Number) if err != nil { t.Fatalf("%v", err) } r.Munge(obj) for _, l := range test.mustHave { if !obj.HasLabel(l) { t.Errorf("%s:%d: Did not find label %q, labels: %v", test.name, testNum, l, obj.Issue.Labels) } } for _, l := range test.mustNotHave { if obj.HasLabel(l) { t.Errorf("%s:%d: Found label %q and should not have, labels: %v", test.name, testNum, l, obj.Issue.Labels) } } server.Close() } }
func DontRequireGithubE2EIssue() *github.Issue { return github_test.Issue(whitelistUser, 1, []string{"cla: yes", "lgtm", "e2e-not-required"}, true) }
func DontRequireGithubE2EIssue() *github.Issue { return github_test.Issue(whitelistUser, 1, []string{claYesLabel, lgtmLabel, e2eNotRequiredLabel}, true) }