func createCommitMessage(message string) github.RepositoryCommit {
	repoCommit := new(github.RepositoryCommit)
	commit := new(github.Commit)
	commit.Message = &message
	repoCommit.Commit = commit
	return *repoCommit
}
func createGHCommit(message string, sha string) *github.RepositoryCommit {
	repoCommit := new(github.RepositoryCommit)
	commit := new(github.Commit)
	commit.Message = &message
	commit.SHA = &sha
	repoCommit.SHA = &sha
	repoCommit.Commit = commit
	return repoCommit
}
Esempio n. 3
0
// Commit returns a filled out github.Commit which happened at time.Unix(t, 0)
func pathsCommit(path []string) []github.RepositoryCommit {
	c := github.Commit{
		SHA: stringPtr("mysha"),
		Committer: &github.CommitAuthor{
			Date: timePtr(time.Unix(10, 0)),
		},
	}
	rc := github.RepositoryCommit{
		SHA:    stringPtr("mysha"),
		Commit: &c,
	}
	files := []github.CommitFile{}
	for _, p := range path {
		f := github.CommitFile{
			Filename: stringPtr(p),
		}
		files = append(files, f)
	}
	rc.Files = files
	return []github.RepositoryCommit{rc}
}
func TestBatchMatchesCommits(t *testing.T) {
	makeCommits := func(spec []string) []*githubapi.RepositoryCommit {
		out := []*githubapi.RepositoryCommit{}
		for _, s := range spec {
			i := strings.Index(s, " ")
			refs := s[:i]
			msg := s[i+1:]
			split := strings.Split(refs, ":")
			commit := githubapi.RepositoryCommit{
				SHA:    &split[0],
				Commit: &githubapi.Commit{Message: &msg},
			}
			for _, parent := range strings.Split(split[1], ",") {
				p := string(parent) // thanks, Go!
				commit.Parents = append(commit.Parents, githubapi.Commit{SHA: &p})
			}
			out = append(out, &commit)
		}
		return out
	}

	for _, test := range []struct {
		pulls    []batchPull
		commits  []string
		expected int
		err      string
	}{
		// no commits
		{nil, []string{}, 0, "no commits"},
		// base matches
		{nil, []string{"a:0 blah"}, 0, ""},
		// base doesn't match
		{nil, []string{"b:0 blaga"}, 0, "Unknown non-merge commit b"},
		// PR could apply
		{[]batchPull{{1, "c"}}, []string{"a:0 blah"}, 0, ""},
		// PR already applied
		{[]batchPull{{1, "c"}}, []string{"d:a,c Merge #1", "c:a fix stuff", "a:0 blah"}, 1, ""},
		// unknown merge
		{[]batchPull{{2, "d"}}, []string{"d:a,c Merge #1", "c:a fix stuff", "a:0 blah"}, 0, "Merge of something not in batch"},
		// unknown commit
		{[]batchPull{{2, "d"}}, []string{"c:a fix stuff", "a:0 blah"}, 0, "Unknown non-merge commit c"},
		// PRs could apply
		{[]batchPull{{1, "c"}, {2, "e"}}, []string{"a:0 blah"}, 0, ""},
		// 1 PR applied
		{[]batchPull{{1, "c"}, {2, "e"}}, []string{"d:a,c Merge #1", "c:a fix stuff", "a:0 blah"}, 1, ""},
		// other PR merged
		{[]batchPull{{1, "c"}, {2, "e"}}, []string{"d:a,g Merge #3", "g:a add feature", "a:0 blah"}, 0, "Merge of something not in batch"},
		// both PRs already merged
		{[]batchPull{{1, "c"}, {2, "e"}},
			[]string{"f:d,e Merge #2", "e:a fix bug", "d:a,c Merge #1", "c:a fix stuff", "a:0 blah"}, 2, ""},
		// PRs merged in wrong order
		{[]batchPull{{1, "c"}, {2, "e"}},
			[]string{"f:d,c Merge #1", "d:a,e Merge #2", "e:a fix bug", "c:a fix stuff", "a:0 blah"}, 0, "Batch PRs merged out-of-order"},
	} {
		batch := Batch{"m", "a", test.pulls}
		commits := makeCommits(test.commits)
		actual, err := batch.matchesCommits(commits)
		if err == nil {
			err = errors.New("")
		}
		expectEqual(t, "batch.matchesCommits", actual, test.expected)
		expectEqual(t, "batch.matchesCommits err", err.Error(), test.err)
	}
}