Skip to content

Commit cbb42b2

Browse files
authored
Do not add/update a commit if the SHA already exists. (#4673)
Closes flutter/flutter#168684.
1 parent 2ae6ae3 commit cbb42b2

2 files changed

Lines changed: 47 additions & 11 deletions

File tree

app_dart/lib/src/service/commit_service.dart

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'dart:async';
66

7+
import 'package:cocoon_server/logging.dart';
78
import 'package:github/github.dart' as gh;
89
import 'package:github/hooks.dart' as gh;
910
import 'package:googleapis/firestore/v1.dart' as fs;
@@ -44,23 +45,38 @@ interface class CommitService {
4445
);
4546

4647
// Convert into the format the rest of the service uses.
47-
await _insertFirestore(
48+
await _insertFirestoreIfNewSha(
4849
_Commit.fromBranchEvent(
4950
repository: slug,
5051
branch: branch,
5152
commit: ghCommit,
5253
now: _now(),
5354
),
55+
debugOrigin: 'GitHub Branch Event',
5456
);
5557
}
5658

5759
/// Add a commit based on a Push event to the Firestore.
5860
/// https://docs.github.com/en/webhooks/webhook-events-and-payloads#push
5961
Future<void> handlePushGithubRequest(Map<String, Object?> pushEvent) async {
60-
await _insertFirestore(_Commit.fromPushEventJson(pushEvent));
62+
await _insertFirestoreIfNewSha(
63+
_Commit.fromPushEventJson(pushEvent),
64+
debugOrigin: 'GitHub Push Event',
65+
);
6166
}
6267

63-
Future<void> _insertFirestore(_Commit commit) async {
68+
Future<void> _insertFirestoreIfNewSha(
69+
_Commit commit, {
70+
required String debugOrigin,
71+
}) async {
72+
if (await fs.Commit.tryFromFirestoreBySha(_firestore, sha: commit.sha) !=
73+
null) {
74+
log.info(
75+
'Skipping commit ${commit.sha} (from $debugOrigin): already exists in '
76+
'Firestore',
77+
);
78+
return;
79+
}
6480
final fsCommit = fs.Commit(
6581
createTimestamp: commit.createdOn.millisecondsSinceEpoch,
6682
repositoryPath: commit.repository.fullName,

app_dart/test/service/commit_service_test.dart

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import 'package:test/test.dart';
1212

1313
import '../src/fake_config.dart';
1414
import '../src/service/fake_firestore_service.dart';
15+
import '../src/utilities/entity_generators.dart';
1516
import '../src/utilities/mocks.mocks.dart';
1617
import '../src/utilities/webhook_generators.dart';
1718

@@ -90,15 +91,23 @@ void main() {
9091
);
9192
});
9293

93-
test('does not add commit to db if it exists in Firestore', () async {
94+
// Regression test for https://github.com/flutter/flutter/issues/168684.
95+
test('does not update commit in db if SHA exists in Firestore', () async {
96+
firestore.putDocument(
97+
generateFirestoreCommit(1, branch: branch, sha: sha),
98+
);
99+
94100
when(
95101
githubService.getReference(
96102
RepositorySlug(owner, repository),
97-
'heads/$branch',
103+
'heads/$branch-different',
98104
),
99105
).thenAnswer((Invocation invocation) {
100106
return Future<GitReference>.value(
101-
GitReference(ref: 'refs/$branch', object: GitObject('', sha, '')),
107+
GitReference(
108+
ref: 'refs/$branch-different',
109+
object: GitObject('', sha, ''),
110+
),
102111
);
103112
});
104113

@@ -119,12 +128,15 @@ void main() {
119128
});
120129

121130
final createEvent = generateCreateBranchEvent(
122-
branch,
131+
'$branch-different',
123132
'$owner/$repository',
124133
);
125134
await commitService.handleCreateGithubRequest(createEvent);
126135

127-
expect(firestore, existsInStorage(fs.Commit.metadata, hasLength(1)));
136+
expect(
137+
firestore,
138+
existsInStorage(fs.Commit.metadata, [isCommit.hasBranch(branch)]),
139+
);
128140
});
129141
});
130142

@@ -155,9 +167,14 @@ void main() {
155167
);
156168
});
157169

158-
test('does not add commit to db if it exists in Firestore', () async {
170+
// Regression test for https://github.com/flutter/flutter/issues/168684.
171+
test('does not update commit in db if SHA exists in Firestore', () async {
172+
firestore.putDocument(
173+
generateFirestoreCommit(1, branch: branch, sha: sha),
174+
);
175+
159176
final pushEvent = generatePushEvent(
160-
branch,
177+
'$branch-different',
161178
owner,
162179
repository,
163180
message: message,
@@ -167,7 +184,10 @@ void main() {
167184
);
168185
await commitService.handlePushGithubRequest(pushEvent);
169186

170-
expect(firestore, existsInStorage(fs.Commit.metadata, hasLength(1)));
187+
expect(
188+
firestore,
189+
existsInStorage(fs.Commit.metadata, [isCommit.hasBranch(branch)]),
190+
);
171191
});
172192
});
173193
}

0 commit comments

Comments
 (0)