[skcq] Abandon previous attempt if the votes changed

To make change attempts UI more accurate.

Bug: skia:12270
Change-Id: Iff44ff81858014a5784cdb498b5f7b7f471c8a83
Reviewed-on: https://skia-review.googlesource.com/c/buildbot/+/432819
Commit-Queue: Ravi Mistry <rmistry@google.com>
Reviewed-by: Eric Boren <borenet@google.com>
diff --git a/skcq/go/caches/BUILD.bazel b/skcq/go/caches/BUILD.bazel
index 2f66d67..2dd6b0b 100644
--- a/skcq/go/caches/BUILD.bazel
+++ b/skcq/go/caches/BUILD.bazel
@@ -21,6 +21,7 @@
     deps = [
         "//go/now",
         "//go/testutils/unittest",
+        "//skcq/go/db",
         "//skcq/go/db/mocks",
         "//skcq/go/types",
         "@com_github_stretchr_testify//require",
diff --git a/skcq/go/caches/current_changes_cache.go b/skcq/go/caches/current_changes_cache.go
index 64e37f0..8840df6 100644
--- a/skcq/go/caches/current_changes_cache.go
+++ b/skcq/go/caches/current_changes_cache.go
@@ -49,7 +49,14 @@
 	cqStartTime := now.Now(ctx).Unix()
 	// Add to the changes cache if it is already not there.
 	cqRecord, ok := c.currentChangesCache[changeEquivalentPatchset]
-	if !ok || cqRecord.DryRun != dryRun {
+	if ok && cqRecord.DryRun != dryRun {
+		// Abandon the previous attempt before we put the new one in.
+		if err := c.dbClient.UpdateChangeAttemptAsAbandoned(ctx, cqRecord.ChangeID, cqRecord.LatestPatchsetID, db.GetChangesCol(internal), cqRecord.StartTs); err != nil {
+			return -1, false, skerr.Wrapf(err, "Error abandoning change attempt")
+		}
+		ok = false
+	}
+	if !ok {
 		cqRecord = &types.CurrentlyProcessingChange{
 			ChangeID:         changeID,
 			LatestPatchsetID: latestPatchsetID,
diff --git a/skcq/go/caches/current_changes_cache_test.go b/skcq/go/caches/current_changes_cache_test.go
index e55c529..8b9fe65 100644
--- a/skcq/go/caches/current_changes_cache_test.go
+++ b/skcq/go/caches/current_changes_cache_test.go
@@ -9,6 +9,7 @@
 
 	"go.skia.org/infra/go/now"
 	"go.skia.org/infra/go/testutils/unittest"
+	"go.skia.org/infra/skcq/go/db"
 	db_mocks "go.skia.org/infra/skcq/go/db/mocks"
 	"go.skia.org/infra/skcq/go/types"
 )
@@ -29,6 +30,7 @@
 	dbClient := &db_mocks.DB{}
 	dbClient.On("GetCurrentChanges", ctx).Return(cacheMap, nil).Twice()
 	dbClient.On("PutCurrentChanges", ctx, cacheMap).Return(nil).Times(3)
+	dbClient.On("UpdateChangeAttemptAsAbandoned", ctx, int64(123), int64(5), db.GetChangesCol(false), now.Now(ctx).Unix()).Return(nil).Times(3)
 
 	// Test GetCurrentChangesCache.
 	ccCache, err := GetCurrentChangesCache(ctx, dbClient)