Skip to content

Commit e5a5c48

Browse files
authored
When using revert-bot, it applies the right base-ref/branch-name. (#4689)
Closes flutter/flutter#168926.
1 parent 154bc47 commit e5a5c48

3 files changed

Lines changed: 173 additions & 5 deletions

File tree

auto_submit/lib/action/git_cli_revert_method.dart

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import 'dart:io';
66

77
import 'package:cocoon_server/logging.dart';
88
import 'package:github/github.dart' as github;
9+
import 'package:meta/meta.dart';
910
import 'package:retry/retry.dart';
1011

1112
import '../git/cli_command.dart';
@@ -19,6 +20,11 @@ import '../service/github_service.dart';
1920
import 'revert_method.dart';
2021

2122
class GitCliRevertMethod implements RevertMethod {
23+
GitCliRevertMethod({
24+
@visibleForTesting GitCli? gitCli, //
25+
}) : _gitCli = gitCli ?? GitCli(GitAccessMethod.HTTP, CliCommand());
26+
final GitCli _gitCli;
27+
2228
@override
2329
Future<github.PullRequest?> createRevert(
2430
Config config,
@@ -30,17 +36,14 @@ class GitCliRevertMethod implements RevertMethod {
3036
final commitSha = pullRequestToRevert.mergeCommitSha!;
3137
// we will need to collect the pr number after the revert request is generated.
3238

33-
final repositoryConfiguration = await config.getRepositoryConfiguration(
34-
slug,
35-
);
36-
final baseBranch = repositoryConfiguration.defaultBranch;
39+
final baseBranch = pullRequestToRevert.base!.ref!;
3740

3841
final cloneToDirectory = '${slug.name}_$commitSha';
3942
final gitRepositoryManager = GitRepositoryManager(
4043
slug: slug,
4144
workingDirectory: Directory.current.path,
4245
cloneToDirectory: cloneToDirectory,
43-
gitCli: GitCli(GitAccessMethod.HTTP, CliCommand()),
46+
gitCli: _gitCli,
4447
);
4548

4649
// The exception is caught by the thrower.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
// Copyright 2023 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:io';
6+
7+
import 'package:auto_submit/action/git_cli_revert_method.dart';
8+
import 'package:auto_submit/git/git_cli.dart';
9+
import 'package:auto_submit/service/config.dart';
10+
import 'package:auto_submit/service/github_service.dart';
11+
import 'package:cocoon_server_test/test_logging.dart';
12+
import 'package:github/src/common/model/pulls.dart';
13+
import 'package:github/src/common/model/repos.dart';
14+
import 'package:test/fake.dart';
15+
import 'package:test/test.dart';
16+
17+
import '../requests/github_webhook_test_data.dart';
18+
19+
void main() {
20+
useTestLoggerPerTest();
21+
22+
late _FakeGitCli gitCli;
23+
24+
setUp(() {
25+
gitCli = _FakeGitCli();
26+
});
27+
28+
test('createRevert uses the branch from the originating PR', () async {
29+
final config = _FakeConfig();
30+
final method = GitCliRevertMethod(gitCli: gitCli);
31+
final revert = await method.createRevert(
32+
config,
33+
'matanlurey',
34+
'I said so',
35+
generatePullRequest(
36+
baseRef: 'flutter-3.32-candidate.0',
37+
mergeCommitSha: 'abc123',
38+
),
39+
);
40+
expect(gitCli.createBranch$newBranchName, 'revert_abc123');
41+
expect(gitCli.setUpstream$branchName, 'revert_abc123');
42+
expect(gitCli.pushBranch$branchName, 'revert_abc123');
43+
expect(revert?.base?.ref, 'flutter-3.32-candidate.0');
44+
});
45+
}
46+
47+
final class _FakeConfig extends Fake implements Config {
48+
@override
49+
Future<String> generateGithubToken(RepositorySlug slug) async {
50+
return 'a_github_token';
51+
}
52+
53+
@override
54+
Future<GithubService> createGithubService(RepositorySlug slug) async {
55+
return _FakeGithubService();
56+
}
57+
}
58+
59+
final class _FakeGithubService extends Fake implements GithubService {
60+
@override
61+
Future<Branch> getBranch(RepositorySlug slug, String branchName) async {
62+
return Branch(branchName, null);
63+
}
64+
65+
@override
66+
Future<List<PullRequestReview>> getPullRequestReviews(
67+
RepositorySlug slug,
68+
int pullRequestNumber,
69+
) async {
70+
return [];
71+
}
72+
73+
@override
74+
Future<PullRequest> createPullRequest({
75+
required RepositorySlug slug,
76+
String? title,
77+
String? head,
78+
required String base,
79+
bool draft = false,
80+
String? body,
81+
}) async {
82+
return PullRequest(base: PullRequestHead(ref: base));
83+
}
84+
}
85+
86+
final class _FakeGitCli extends Fake implements GitCli {
87+
@override
88+
Future<ProcessResult> cloneRepository({
89+
required RepositorySlug slug,
90+
required String workingDirectory,
91+
required String targetDirectory,
92+
List<String>? options,
93+
bool throwOnError = true,
94+
}) async {
95+
return ProcessResult(0, 0, '', '');
96+
}
97+
98+
@override
99+
Future<ProcessResult> setupUserConfig({
100+
required RepositorySlug slug,
101+
required String workingDirectory,
102+
bool throwOnError = true,
103+
}) async {
104+
return ProcessResult(0, 0, '', '');
105+
}
106+
107+
@override
108+
Future<ProcessResult> setupUserEmailConfig({
109+
required RepositorySlug slug,
110+
required String workingDirectory,
111+
bool throwOnError = true,
112+
}) async {
113+
return ProcessResult(0, 0, '', '');
114+
}
115+
116+
late String createBranch$newBranchName;
117+
118+
@override
119+
Future<ProcessResult> createBranch({
120+
required String newBranchName,
121+
required String workingDirectory,
122+
bool useCheckout = false,
123+
bool throwOnError = true,
124+
}) async {
125+
createBranch$newBranchName = newBranchName;
126+
return ProcessResult(0, 0, '', '');
127+
}
128+
129+
late String setUpstream$branchName;
130+
131+
@override
132+
Future<ProcessResult> setUpstream({
133+
required RepositorySlug slug,
134+
required String workingDirectory,
135+
required String branchName,
136+
required String token,
137+
bool throwOnError = true,
138+
}) async {
139+
setUpstream$branchName = branchName;
140+
return ProcessResult(0, 0, '', '');
141+
}
142+
143+
@override
144+
Future<ProcessResult> revertChange({
145+
required String commitSha,
146+
required String workingDirectory,
147+
bool throwOnError = true,
148+
}) async {
149+
return ProcessResult(0, 0, '', '');
150+
}
151+
152+
late String pushBranch$branchName;
153+
154+
@override
155+
Future<ProcessResult> pushBranch({
156+
required String branchName,
157+
required String workingDirectory,
158+
bool throwOnError = true,
159+
}) async {
160+
pushBranch$branchName = branchName;
161+
return ProcessResult(0, 0, '', '');
162+
}
163+
}

auto_submit/test/requests/github_webhook_test_data.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ PullRequest generatePullRequest({
9494
bool? mergeable = true,
9595
String baseRef = 'main',
9696
DateTime? mergedAt,
97+
String mergeCommitSha = 'abc123',
9798
}) {
9899
return PullRequest.fromJson(
99100
json.decode('''{
@@ -119,6 +120,7 @@ PullRequest generatePullRequest({
119120
],
120121
"created_at": "2011-01-26T19:01:12Z",
121122
"merged_at": "${mergedAt ?? DateTime.now().subtract(const Duration(hours: 12))}",
123+
"merge_commit_sha": "$mergeCommitSha",
122124
"closed_at": "2011-01-26T19:10:12Z",
123125
"head": {
124126
"label": "octocat:new-topic",

0 commit comments

Comments
 (0)