changelog: Added changelog.go and test files to changelog package

Changelog.go contains the Changelog function, which takes 2 build numbers and outputs 2 changelogs for changes that occur between the builds. It uses Gitiles to query Manifest files and commits from GoB instances.

BUG=b/160901711
TEST=unittests, run local

Change-Id: Id9ceef077e95b08ca0291aa502db2c577ff6db05
diff --git a/src/pkg/changelog/changelog.go b/src/pkg/changelog/changelog.go
new file mode 100755
index 0000000..8d25802
--- /dev/null
+++ b/src/pkg/changelog/changelog.go
@@ -0,0 +1,311 @@
+// Copyright 2020 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+// This package generates a changelog based on the commit history between
+// two build numbers. The changelog consists of two outputs - the commits
+// added to the target build that aren't present in the source build, and the
+// commits in the source build that aren't present in the target build. This
+// package uses concurrency to improve performance.
+//
+// This packages uses Gitiles to request information from a Git on Borg instance.
+// To generate a changelog, the package first retrieves the the manifest files for
+// the two requested builds using the provided manifest GoB instance and repository.
+// The package then parses the XML files and retrieves the committish and instance
+// URL. A request is sent on a seperate thread for each repository, asking for a list
+// of commits that occurred between the source committish and the target committish.
+// Finally, the resulting git.Commit objects are converted to Commit objects, and
+// consolidated into a mapping of repositoryName -> []*Commit.
+
+package changelog
+
+import (
+	"context"
+	"errors"
+	"fmt"
+	"strings"
+	"time"
+
+	"github.com/beevik/etree"
+	"github.com/google/martian/log"
+	"go.chromium.org/luci/auth"
+	gitilesApi "go.chromium.org/luci/common/api/gitiles"
+	gitilesProto "go.chromium.org/luci/common/proto/gitiles"
+)
+
+const (
+	manifestFileName string = "snapshot.xml"
+
+	// These constants are used for exponential increase in Gitiles request size.
+	defaultPageSize          int32 = 1000
+	pageSizeGrowthMultiplier int32 = 5
+	maxPageSize              int32 = 10000
+)
+
+type repo struct {
+	// The Git on Borg instance to query from.
+	InstanceURL string
+	// A value that points to the last commit for a build on a given repo.
+	// Acceptable values:
+	// - A commit SHA
+	// - A ref, ex. "refs/heads/branch"
+	// - A ref defined as n-th parent of R in the form "R-n".
+	//   ex. "master-2" or "deadbeef-1".
+	// Source: https://pkg.go.dev/go.chromium.org/luci/common/proto/gitiles?tab=doc#LogRequest
+	Committish string
+}
+
+type commitsResult struct {
+	RepoURL string
+	Commits []*Commit
+	Err     error
+}
+
+type additionsResult struct {
+	Additions map[string][]*Commit
+	Err       error
+}
+
+func client(authenticator *auth.Authenticator, remoteURL string) (gitilesProto.GitilesClient, error) {
+	authedClient, err := authenticator.Client()
+	cl, err := gitilesApi.NewRESTClient(authedClient, remoteURL, true)
+	if err != nil {
+		return nil, errors.New("changelog: Failed to establish client to remote url: " + remoteURL)
+	}
+	return cl, nil
+}
+
+func createClients(clients map[string]gitilesProto.GitilesClient, authenticator *auth.Authenticator, repoMap map[string]*repo) error {
+	for _, repoData := range repoMap {
+		remoteURL := repoData.InstanceURL
+		if _, ok := clients[remoteURL]; ok {
+			continue
+		}
+		client, err := client(authenticator, remoteURL)
+		if err != nil {
+			return fmt.Errorf("createClients: error creating client mapping:\n%v", err)
+		}
+		clients[remoteURL] = client
+	}
+	return nil
+}
+
+// repoMap generates a mapping of repo name to instance URL and committish.
+// This eliminates the need to track remote names and allows lookup
+// of source committish when generating changelog.
+func repoMap(manifest string) (map[string]*repo, error) {
+	doc := etree.NewDocument()
+	if err := doc.ReadFromString(manifest); err != nil {
+		return nil, fmt.Errorf("repoMap: error parsing manifest xml:\n%v", err)
+	}
+	root := doc.SelectElement("manifest")
+
+	// Parse each <remote fetch=X name=Y> tag in the manifest xml file.
+	// Extract the "fetch" and "name" attributes from each remote tag, and map the name to the fetch URL.
+	remoteMap := make(map[string]string)
+	for _, remote := range root.SelectElements("remote") {
+		url := strings.Replace(remote.SelectAttr("fetch").Value, "https://", "", 1)
+		remoteMap[remote.SelectAttr("name").Value] = url
+	}
+
+	// Parse each <project name=X remote=Y revision=Z> tag in the manifest xml file.
+	// Extract the "name", "remote", and "revision" attributes from each project tag.
+	// Some projects do not have a "remote" attribute.
+	// If this is the case, they should use the default remoteURL.
+	remoteMap[""] = remoteMap[root.SelectElement("default").SelectAttr("remote").Value]
+	repos := make(map[string]*repo)
+	for _, project := range root.SelectElements("project") {
+		repos[project.SelectAttr("name").Value] = &repo{
+			InstanceURL: remoteMap[project.SelectAttrValue("remote", "")],
+			Committish:  project.SelectAttr("revision").Value,
+		}
+	}
+	return repos, nil
+}
+
+// mappedManifest retrieves a Manifest file from GoB and unmarshals XML.
+func mappedManifest(client gitilesProto.GitilesClient, repo string, buildNum string) (map[string]*repo, error) {
+	request := gitilesProto.DownloadFileRequest{
+		Project:    repo,
+		Committish: "refs/tags/" + buildNum,
+		Path:       manifestFileName,
+		Format:     1,
+	}
+	response, err := client.DownloadFile(context.Background(), &request)
+	if err != nil {
+		return nil, fmt.Errorf("mappedManifest: error downloading manifest file from repo %s:\n%v",
+			repo, err)
+	}
+	mappedManifest, err := repoMap(response.Contents)
+	if err != nil {
+		return nil, fmt.Errorf("mappedManifest: error parsing manifest contents from repo %s:\n%v",
+			repo, err)
+	}
+	return mappedManifest, nil
+}
+
+// commits get all commits that occur between committish and ancestor for a specific repo.
+func commits(client gitilesProto.GitilesClient, repo string, committish string, ancestor string, outputChan chan commitsResult) {
+	start := time.Now()
+	pageSize := defaultPageSize
+	request := gitilesProto.LogRequest{
+		Project:            repo,
+		Committish:         committish,
+		ExcludeAncestorsOf: ancestor,
+		PageSize:           pageSize,
+	}
+	response, err := client.Log(context.Background(), &request)
+	if err != nil {
+		outputChan <- commitsResult{Err: fmt.Errorf("commits: Error retrieving log for repo: %s with committish: %s and ancestor %s:\n%v",
+			repo, committish, ancestor, err)}
+		return
+	}
+	// No nextPageToken means there were less than <defaultPageSize> commits total.
+	// We can immediately return.
+	if response.NextPageToken == "" {
+		log.Infof("Retrieved %d commits from %s in %s\n", len(response.Log), repo, time.Since(start))
+		parsedCommits, err := ParseGitCommitLog(response.Log)
+		if err != nil {
+			outputChan <- commitsResult{Err: fmt.Errorf("commits: Error parsing log response for repo: %s with committish: %s and ancestor %s:\n%v",
+				repo, committish, ancestor, err)}
+			return
+		}
+		outputChan <- commitsResult{RepoURL: repo, Commits: parsedCommits}
+		return
+	}
+	// Retrieve remaining commits using exponential increase in pageSize.
+	allCommits := response.Log
+	for response.NextPageToken != "" {
+		if pageSize < maxPageSize {
+			pageSize *= pageSizeGrowthMultiplier
+		}
+		request := gitilesProto.LogRequest{
+			Project:            repo,
+			Committish:         committish,
+			ExcludeAncestorsOf: ancestor,
+			PageToken:          response.NextPageToken,
+			PageSize:           pageSize,
+		}
+		response, err = client.Log(context.Background(), &request)
+		if err != nil {
+			outputChan <- commitsResult{Err: fmt.Errorf("commits: Error retrieving log for repo: %s with committish: %s and ancestor %s:\n%v",
+				repo, committish, ancestor, err)}
+			return
+		}
+		allCommits = append(allCommits, response.Log...)
+	}
+	log.Infof("Retrieved %d commits from %s in %s\n", len(allCommits), repo, time.Since(start))
+	parsedCommits, err := ParseGitCommitLog(allCommits)
+	if err != nil {
+		outputChan <- commitsResult{Err: fmt.Errorf("commits: Error parsing log response for repo: %s with committish: %s and ancestor %s:\n%v",
+			repo, committish, ancestor, err)}
+		return
+	}
+	outputChan <- commitsResult{RepoURL: repo, Commits: parsedCommits}
+}
+
+// additions retrieves all commits that occured between 2 parsed manifest files for each repo.
+// Returns a map of repo name -> list of commits.
+func additions(clients map[string]gitilesProto.GitilesClient, sourceRepos map[string]*repo, targetRepos map[string]*repo, outputChan chan additionsResult) {
+	repoCommits := make(map[string][]*Commit)
+	commitsChan := make(chan commitsResult, len(targetRepos))
+	for repoURL, targetRepoInfo := range targetRepos {
+		cl := clients[targetRepoInfo.InstanceURL]
+		// If the source Manifest file does not contain a target repo,
+		// count every commit since target repo creation as an addition
+		ancestorCommittish := ""
+		if sourceRepoInfo, ok := sourceRepos[repoURL]; ok {
+			ancestorCommittish = sourceRepoInfo.Committish
+		}
+		go commits(cl, repoURL, targetRepoInfo.Committish, ancestorCommittish, commitsChan)
+	}
+	for i := 0; i < len(targetRepos); i++ {
+		res := <-commitsChan
+		if res.Err != nil {
+			outputChan <- additionsResult{Err: res.Err}
+			return
+		}
+		if len(res.Commits) > 0 {
+			repoCommits[res.RepoURL] = res.Commits
+		}
+	}
+	outputChan <- additionsResult{Additions: repoCommits}
+	return
+}
+
+// Changelog generates a changelog between 2 build numbers
+//
+// authenticator is an auth.Authenticator object that is used to build authenticated
+// Gitiles clients
+//
+// sourceBuildNum and targetBuildNum should be build numbers. It should match
+// a tag that links directly to snapshot.xml
+// Ex. For /refs/tags/15049.0.0, the argument should be 15049.0.0
+//
+// The host should be the GoB instance that Manifest files are hosted in
+// ex. "cos.googlesource.com"
+//
+// The repo should be the repository that build manifest files
+// are located, ex. "cos/manifest-snapshots"
+//
+// Outputs two changelogs
+// The first changelog contains new commits that were added to the target
+// build starting from the source build number
+//
+// The second changelog contains all commits that are present in the source build
+// but not present in the target build
+func Changelog(authenticator *auth.Authenticator, sourceBuildNum string, targetBuildNum string, host string, repo string) (map[string][]*Commit, map[string][]*Commit, error) {
+	clients := make(map[string]gitilesProto.GitilesClient)
+
+	// Since the manifest file is always in the cos instance, add cos client
+	// so that client knows what URL to use
+	manifestClient, err := client(authenticator, host)
+	if err != nil {
+		return nil, nil, fmt.Errorf("Changelog: error creating client for GoB instance: %s:\n%v", host, err)
+	}
+	sourceRepos, err := mappedManifest(manifestClient, repo, sourceBuildNum)
+	if err != nil {
+		return nil, nil, fmt.Errorf("Changelog: error retrieving mapped manifest for source build number: %s using manifest repository: %s:\n%v",
+			sourceBuildNum, repo, err)
+	}
+	targetRepos, err := mappedManifest(manifestClient, repo, targetBuildNum)
+	if err != nil {
+		return nil, nil, fmt.Errorf("Changelog: error retrieving mapped manifest for target build number: %s using manifest repository: %s:\n%v",
+			targetBuildNum, repo, err)
+	}
+
+	clients[host] = manifestClient
+	err = createClients(clients, authenticator, sourceRepos)
+	if err != nil {
+		return nil, nil, fmt.Errorf("Changelog: error creating source clients:\n%v", err)
+	}
+	err = createClients(clients, authenticator, targetRepos)
+	if err != nil {
+		return nil, nil, fmt.Errorf("Changelog: error creating target clients:\n%v", err)
+	}
+
+	addChan := make(chan additionsResult, 1)
+	missChan := make(chan additionsResult, 1)
+	go additions(clients, sourceRepos, targetRepos, addChan)
+	go additions(clients, targetRepos, sourceRepos, missChan)
+	addRes := <-addChan
+	if addRes.Err != nil {
+		return nil, nil, fmt.Errorf("Changelog: failure when retrieving commit additions:\n%v", err)
+	}
+	missRes := <-missChan
+	if missRes.Err != nil {
+		return nil, nil, fmt.Errorf("Changelog: failure when retrieving missed commits:\n%v", err)
+	}
+
+	return addRes.Additions, missRes.Additions, nil
+}
diff --git a/src/pkg/changelog/changelog_test.go b/src/pkg/changelog/changelog_test.go
new file mode 100644
index 0000000..361750e
--- /dev/null
+++ b/src/pkg/changelog/changelog_test.go
@@ -0,0 +1,273 @@
+// Copyright 2020 Google Inc. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+//     http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package changelog
+
+import (
+	"context"
+	"testing"
+
+	"go.chromium.org/luci/auth"
+	"go.chromium.org/luci/common/api/gerrit"
+	"go.chromium.org/luci/hardcoded/chromeinfra"
+)
+
+const cosInstance = "cos.googlesource.com"
+const defaultManifestRepo = "cos/manifest-snapshots"
+
+func getAuthenticator() *auth.Authenticator {
+	opts := chromeinfra.DefaultAuthOptions()
+	opts.Scopes = []string{gerrit.OAuthScope, auth.OAuthScopeEmail}
+	return auth.NewAuthenticator(context.Background(), auth.InteractiveLogin, opts)
+}
+
+func commitsMatch(commits []*Commit, expectedCommits []string) bool {
+	if len(commits) != len(expectedCommits) {
+		return false
+	}
+	for i, commit := range commits {
+		if commit == nil {
+			return false
+		}
+		if commit.SHA != expectedCommits[i] {
+			return false
+		}
+	}
+	return true
+}
+
+func mappingInLog(log map[string][]*Commit, check []string) bool {
+	for _, check := range check {
+		if log, ok := log[check]; !ok || len(log) == 0 {
+			return false
+		}
+	}
+	return true
+}
+
+func TestChangelog(t *testing.T) {
+	authenticator := getAuthenticator()
+
+	// Test invalid source
+	additions, misses, err := Changelog(authenticator, "15", "15043.0.0", cosInstance, defaultManifestRepo)
+	if additions != nil {
+		t.Errorf("Changelog failed, expected nil additions, got %v", additions)
+	} else if misses != nil {
+		t.Errorf("Changelog failed, expected nil misses, got %v", misses)
+	} else if err == nil {
+		t.Errorf("Changelog failed, expected error, got nil")
+	}
+
+	// Test invalid target
+	additions, misses, err = Changelog(authenticator, "15043.0.0", "abx", cosInstance, defaultManifestRepo)
+	if additions != nil {
+		t.Errorf("Changelog failed, expected nil additions, got %v", additions)
+	} else if misses != nil {
+		t.Errorf("Changelog failed, expected nil misses, got %v", misses)
+	} else if err == nil {
+		t.Errorf("Changelog failed, expected error, got nil")
+	}
+
+	// Test invalid instance
+	additions, misses, err = Changelog(authenticator, "15036.0.0", "15041.0.0", "com", defaultManifestRepo)
+	if additions != nil {
+		t.Errorf("Changelog failed, expected nil additions, got %v", additions)
+	} else if misses != nil {
+		t.Errorf("Changelog failed, expected nil misses, got %v", misses)
+	} else if err == nil {
+		t.Errorf("Changelog failed, expected error, got nil")
+	}
+
+	// Test invalid manifest repo
+	additions, misses, err = Changelog(authenticator, "15036.0.0", "15041.0.0", cosInstance, "cos/not-a-repo")
+	if additions != nil {
+		t.Errorf("Changelog failed, expected nil additions, got %v", additions)
+	} else if misses != nil {
+		t.Errorf("Changelog failed, expected nil misses, got %v", misses)
+	} else if err == nil {
+		t.Errorf("Changelog failed, expected error, got nil")
+	}
+
+	// Test build number higher than latest release
+	additions, misses, err = Changelog(authenticator, "15036.0.0", "99999.0.0", cosInstance, defaultManifestRepo)
+	if additions != nil {
+		t.Errorf("Changelog failed, expected nil additions, got %v", additions)
+	} else if misses != nil {
+		t.Errorf("Changelog failed, expected nil misses, got %v", misses)
+	} else if err == nil {
+		t.Errorf("Changelog failed, expected error, got nil")
+	}
+
+	// Test 1 build number difference with only 1 repo change between them
+	// Ensure that commits are correctly inserted in proper order
+	source := "15050.0.0"
+	target := "15051.0.0"
+	expectedCommits := []string{
+		"6201c49afe667c8fa7796608a4d7162bb3f7f4f4",
+		"a8bcf0feaa0e3c0131a888fcd9d0dcbbe8c3850c",
+		"5e3ef32e062fb227aaa6b47138950557ec91d23e",
+		"654ed08e8a349e7199eb3a80b6d7704a20ff8ec4",
+		"d5c0e74fbb2a50517a1249cbbec4dcee3d049883",
+		"cd226061776dad6c0e35323f407eaa138795f4cc",
+		"4351d0dc5480e941fac96cb0ec898a87171eadda",
+		"cdbcf507749a86acad3e8787ffb3c3356ed76b3a",
+		"4fdd7f397bc09924e91f475d3ed55bb5a302bdaf",
+		"3adae69de78875a8d33061205357388a513ea51d",
+		"5fd85ec937d362984e5108762e8b5e20105a4219",
+		"03b6099c920c1b3cb4cbda2172089e80b4d4be6e",
+		"1febb203aaf99f00e5d9d80d965726458ba8348f",
+		"2de610687308b6ea00d9ac6190d83f0edb2a46b4",
+		"db3083c438442ea6ab34e84404b4602618d2e07b",
+		"13eb9486f2bf43d56ce58695df8461099fd7c314",
+		"12b8a449ef93289674d93f437c19a06530c2c966",
+		"6d9752b0abeeaf7438ab08ea7ff5b0f76c2dacca",
+		"8555ba160a5eee0be464b25a07abc6031dc9159c",
+		"a8c1c3c2971acc03f4246c20b1ddd5bb5376ded3",
+		"762495e014eaa74e3aa4d83caaaa778fcfb968a2",
+		"784782cd8c1d846c17541a3e527ad56857fe2e91",
+		"7c6916858860715db25eaadc2b3ec81865304095",
+		"76cc8bf290a133ee821a8a2b14207150de9a7803",
+		"dc07ec7806f249fdb0b7bda68c687a87b311c952",
+		"f18ad3b35466354d5a0e166008070f54a06759a6",
+		"34f008f664e11b6df2f06735b6db6d6a42804d25",
+		"a24eee7a6b6caed0448365e548e92724069a8448",
+		"64ddf2924656f07bd63269524ed1731a2357b82f",
+		"e40d4ce60313cd28ebf1c376860402f9b3d373cd",
+		"3018e2531a1f0f22c4d053ed0b8a5cc86ad81319",
+		"668cd418350d03e1535c7862ebe93801ace0b1c6",
+		"fabf26e3eab2af24371c48e19062d7c8df34bd9f",
+		"7b38982caecbeb16520b4dd84422ecad0edaf772",
+		"658380877ca2eedc3cd80d3b6daafa24ab96a261",
+		"63dee6c8cd318dfa20cfddf2e72243873e816046",
+		"bc194a3ce16407015da5bc8d46df55231cf4d625",
+		"ff75e90067c7c535116cb5566ebc14451785b36a",
+		"c64b1cc6b930024e77425fd105716ade26d0524c",
+		"d5123111900fd70d85b7acf5809df701da24f1ea",
+		"c617b261c68b52b0abefc0635c1ea03c4cb0cb11",
+		"a2619465e4eca49692d832b593cb205118042bc9",
+		"6f6451dd56a7fad25b2e8b31a053275adb2008a4",
+		"68d5d3901d5c3df44e3be8c3fac0c6b1e90d780c",
+		"308882e4e837f231e3ad0f37fd143cee419d816f",
+		"1cb20f5aa5a82a412d97fad7b9c13c87c9381f14",
+		"f6c0c6f1618676519efd74c8f946e191472b6a4e",
+		"dea6ca48a629e80cc2ffbf203c9cc1855a28a47e",
+		"fa0115b220b3471a1542b3b66463f9ec80c8c7f0",
+		"b815d624f7715ab51379e8a913c280cac1eafde4",
+		"39fe5d201b87e02baedf4da8b02523571c4ccbcc",
+		"58aff81e0829100cc9d3239791573300e2d2398d",
+		"cd570b8e278aca36f166eb84b5003eaee3c03ecc",
+		"50f9936fe8ab106d2716e007a342860c695f7822",
+		"2a1e98d6c3dca9b52bcb7b02c7a242c10c0a0de9",
+		"b9fe6cc174f215d576954e6b2c93bc4de8ba2c34",
+		"f78d275ec9d0c4061f75ae2f97f958657a71ebd1",
+		"315ea4a344e3f8b300e8c3e48fafc21eaee767fe",
+		"1c9392eb35c68ca38a1f0178cd191f07d387f52d",
+		"9cd44834d383b5414bd9bac873e9c620a67eff1d",
+		"e0f3f79316591affedeaf2702a350d3512bd6a69",
+		"148bba54f3762b23a79057825a763c1132bd1d55",
+		"48ce30dd18de40852cea15dccaaa833b4017ae10",
+		"474e61f82f79d9779b0e2c3bc63d920d9f75b5b6",
+		"b93f0e4f3edbe3e64b0128db38ee231a737f06c9",
+		"714065afa108556b6ff43ff312b731c239d6e551",
+		"45a780a84daa27307addd836df94afa2c70dccb6",
+		"0df346778d142f9c6bf221d67bdac96d9d636408",
+		"6ad098080fb6437da98511e56026476fa71cce87",
+		"3f2915159ab1e42b258ee78d2a71f2dc59d51d35",
+		"1d5a9ebc23d1455966963a042bd610fdb38cd705",
+		"e31b072bbc2d83db107d913a3f32d907de119ca2",
+		"6da63745bd4318577ab8937100871e654df04cb3",
+		"d5a54c19f7bf1f8250bc5ac779f80450764e836e",
+		"54c59bdcf9965dbb77a6dd9682f255e21e4821a1",
+		"67b538de711500bfb1ed5d322e916e8cd3f74700",
+		"2814ccbb44a3d19cb4d696705794ced3beb31ef3",
+		"deb92542c03e9096fe37d8833532a50a6bb1df3c",
+		"d2b9b62c2ad5440005b72826bb55a36dfc115ac2",
+		"da9cd84436f716c3c7a6d90e820afb87a9a218b9",
+		"d0937f57cd2904df1af7449f32c75aaadaeac2a2",
+		"65441913baef06967e59158f3848e41dce18b43a",
+		"7cc03e836eba4d13526969b84aaa8dd61d8b6216",
+		"dff08d118cab7f8416b8f171aac91b8ca3f6b44d",
+		"aedb933f853499a0c736deb2d2ab899b607aacee",
+		"aa592bf7b0b7b13eee2b20fa54fd81e11e96cf56",
+		"f495c107eefc879b10fdf2e3a2a0155259210dba",
+		"7e4e0964a1426d46cdbcbccd861cee7a106a9430",
+		"d0ca437a1ed89e2adbd6b2d1bd572b475cd1d8ec",
+		"0dce9e5070718b7ba950f0b6575bb3bbd0e362bc",
+		"ddd73889c36e93c6128a4d791b6d673cd655447e",
+		"04e70ee7abbb702e4939fef98d50b5e6cc018ccd",
+		"86da591dd3d8515ebf4d1eebc68a61092ad13e95",
+		"8676fbad9fa41e0d0f69dafb2b4f8bd4b5a3b3cc",
+		"b8b3a8cc67fcdf58d495489c19e5d3aa23d22563",
+		"7441c2cf859b84f7cedff8946dbd0c3dc7ef956b",
+		"7f3e0778e212c8a22f8262e2819a6aebfca8b879",
+		"a82b808965dbe304e0a95cb9534b09b3b5c0486a",
+		"0388f30783e2454ea9f0c3978f92c797fc0bdf20",
+		"67f6e97cee8a5b33f8e27b4d2426fb009c0ae435",
+		"094bef7b6bd0c034ea19aa3cb9744ca35998ecc8",
+		"ec07a4f7eb15d867e453c8c8991656b361a29882",
+		"0a304d6481d01d774fe97f31c9574c970fdb532f",
+		"3f77b91ad1abb2d2074286635927fa6472eb0a2e",
+		"ca721a37ec8edc8f1b8aeb4c143aa936dc032ac1",
+		"c0b7d2df81ae29869f9d7a1874b741eeec0d5d18",
+		"9bc12bb411f357188d008864f80dfba43210b9d8",
+		"bf0dd3757826b9bc9d7082f5f749ff7615d4bcb3",
+	}
+	additions, misses, err = Changelog(authenticator, source, target, cosInstance, defaultManifestRepo)
+	if err != nil {
+		t.Errorf("Changelog failed, expected no error, got %v", err)
+	} else if len(misses) != 0 {
+		t.Errorf("Changelog failed, expected empty misses list, got %v", misses)
+	} else if len(additions) != 1 {
+		t.Errorf("Changelog failed, expected only 1 repo in additions, got %v", additions)
+	} else if _, ok := additions["cos/overlays/board-overlays"]; !ok {
+		t.Errorf("Changelog failed, expected \"cos/overlays/board-overlays\" in additions, got %v", additions)
+	} else if changes, _ := additions["cos/overlays/board-overlays"]; len(changes) != 108 {
+		t.Errorf("Changelog failed, expected 108 changes for \"cos/overlays/board-overlays\", got %d", len(changes))
+	} else if !commitsMatch(additions["cos/overlays/board-overlays"], expectedCommits) {
+		t.Errorf("Changelog failed, Changelog output does not match expected commits or is not sorted")
+	}
+
+	// Test build numbers further apart from each other with multiple repo differences
+	// Also ensures that misses are correctly populated
+	source = "15020.0.0"
+	target = "15056.0.0"
+	additionRepos := []string{
+		"mirrors/cros/chromiumos/platform/crosutils",
+		"cos/manifest",
+		"mirrors/cros/chromiumos/platform/vboot_reference",
+		"mirrors/cros/chromiumos/platform/dev-util",
+		"mirrors/cros/chromiumos/platform/crostestutils",
+		"mirrors/cros/chromiumos/infra/proto",
+		"mirrors/cros/chromiumos/third_party/toolchain-utils",
+		"mirrors/cros/chromiumos/third_party/coreboot",
+		"cos/overlays/board-overlays",
+		"mirrors/cros/chromiumos/platform2",
+		"mirrors/cros/chromiumos/overlays/eclass-overlay",
+		"mirrors/cros/chromiumos/chromite",
+		"mirrors/cros/chromiumos/third_party/autotest",
+		"mirrors/cros/chromiumos/overlays/chromiumos-overlay",
+		"third_party/kernel",
+		"mirrors/cros/chromium/tools/depot_tools",
+		"mirrors/cros/chromiumos/repohooks",
+		"mirrors/cros/chromiumos/overlays/portage-stable",
+	}
+	additions, misses, err = Changelog(authenticator, source, target, cosInstance, defaultManifestRepo)
+	if err != nil {
+		t.Errorf("Changelog failed, expected no error, got %v", err)
+	} else if _, ok := misses["third_party/kernel"]; len(misses) != 1 && ok {
+		t.Errorf("Changelog failed, expected miss list containing only \"third_party/kernel\", got %v", misses)
+	} else if !mappingInLog(additions, additionRepos) {
+		t.Errorf("Changelog failed, additions repo output does not match expected repos %v", additionRepos)
+	}
+}