Skip to content

Commit 45f337b

Browse files
committed
Improve PushEvent handling and add commit comparison
Add `getReposCompare` in `Address` and `getReposCompareRequest` in `ReposRepository` to support fetching commit comparison data. Update `EventUtils` to enhance `PushEvent` interaction: * Implement logic in `ActionUtils` to compare `before` and `head` SHAs. * Fetch comparison data to accurately determine the list of commits. * Show a loading dialog during the fetch and a selection dialog if multiple commits are returned. * Refactor `getActionAndDes` to improve event description generation and handle fallbacks for empty commit messages. * Add helper methods `_shortSha` and `_isInvalidCompareBase`.
1 parent fbd6c09 commit 45f337b

File tree

3 files changed

+175
-16
lines changed

3 files changed

+175
-16
lines changed

lib/common/net/address.dart

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ class Address {
7171
return "${host}repos/$reposOwner/$reposName/commits/$sha";
7272
}
7373

74+
///仓库提交比较 get
75+
static getReposCompare(reposOwner, reposName, base, head) {
76+
return "${host}repos/$reposOwner/$reposName/compare/$base...$head";
77+
}
78+
7479
///仓库Issue get
7580
static getReposIssue(String reposOwner, String reposName, state, sort, direction) {
7681
state ??= 'all';

lib/common/repositories/repos_repository.dart

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ import 'package:gsy_github_app_flutter/db/provider/user/user_stared_db_provider.
2424
import 'package:gsy_github_app_flutter/common/config/config.dart';
2525
import 'package:gsy_github_app_flutter/common/repositories/data_result.dart';
2626
import 'package:gsy_github_app_flutter/model/branch.dart';
27+
import 'package:gsy_github_app_flutter/model/commits_comparison.dart';
2728
import 'package:gsy_github_app_flutter/model/event.dart';
2829
import 'package:gsy_github_app_flutter/model/file_model.dart';
2930
import 'package:gsy_github_app_flutter/model/push_commit.dart';
@@ -641,6 +642,19 @@ class ReposRepository {
641642
}
642643
}
643644

645+
/// 获取两个提交之间的比较信息
646+
static getReposCompareRequest(
647+
String userName, String reposName, String base, String head) async {
648+
String url = Address.getReposCompare(userName, reposName, base, head);
649+
var res = await httpManager.netFetch(url, null, null, null, noTip: true);
650+
if (res != null && res.result) {
651+
CommitsComparison comparison = CommitsComparison.fromJson(res.data);
652+
return DataResult(comparison, true);
653+
} else {
654+
return DataResult(null, false);
655+
}
656+
}
657+
644658
/// 获取仓库的release列表
645659
static getRepositoryReleaseRequest(String userName, String reposName, page,
646660
{needHtml = true, release = true}) async {

lib/common/utils/event_utils.dart

Lines changed: 156 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,32 @@
11
import 'package:flutter/material.dart';
2+
import 'package:gsy_github_app_flutter/common/repositories/repos_repository.dart';
23
import 'package:gsy_github_app_flutter/model/event.dart';
34
import 'package:gsy_github_app_flutter/model/push_event_commit.dart';
5+
import 'package:gsy_github_app_flutter/model/repo_commit.dart';
46
import 'package:gsy_github_app_flutter/common/utils/common_utils.dart';
57
import 'package:gsy_github_app_flutter/common/utils/navigator_utils.dart';
68

79
/// 事件逻辑
810
/// Created by guoshuyu
911
/// Date: 2018-07-16
1012
class EventUtils {
13+
static bool _isInvalidCompareBase(String? sha) {
14+
if (sha == null || sha.isEmpty) {
15+
return true;
16+
}
17+
return RegExp(r'^0+$').hasMatch(sha);
18+
}
19+
20+
static String _shortSha(String? sha, [int length = 7]) {
21+
if (sha == null || sha.isEmpty) {
22+
return "";
23+
}
24+
if (sha.length <= length) {
25+
return sha;
26+
}
27+
return sha.substring(0, length);
28+
}
29+
1130
///事件描述与动作
1231
static ({String? actionStr, String? des}) getActionAndDes(Event event) {
1332
String? actionStr;
@@ -96,26 +115,37 @@ class EventUtils {
96115
ref = ref.substring(ref.lastIndexOf("/") + 1);
97116
actionStr = "Push to $ref at ${event.repo!.name!}";
98117

99-
des = '';
100-
String descSpan = '';
101-
102-
int count = event.payload?.commits?.length ?? 0;
118+
String descSpan = "";
119+
List<PushEventCommit> commits = event.payload?.commits ?? [];
120+
int count = commits.length;
103121
int maxLines = 4;
104122
int max = count > maxLines ? maxLines - 1 : count;
105123

106124
for (int i = 0; i < max; i++) {
107-
PushEventCommit commit = event.payload!.commits![i];
125+
PushEventCommit commit = commits[i];
108126
if (i != 0) {
109127
descSpan += ("\n");
110128
}
111-
String sha = commit.sha!.substring(0, 7);
129+
String sha = _shortSha(commit.sha);
112130
descSpan += sha;
113131
descSpan += " ";
114-
descSpan += commit.message!;
132+
descSpan += (commit.message ?? "Commit");
115133
}
116134
if (count > maxLines) {
117135
descSpan = "$descSpan\n...";
118136
}
137+
if (descSpan.trim().isNotEmpty) {
138+
des = descSpan;
139+
} else if (event.payload?.description != null &&
140+
event.payload!.description!.trim().isNotEmpty) {
141+
des = event.payload!.description;
142+
} else if (event.payload?.head != null &&
143+
event.payload!.head!.trim().isNotEmpty) {
144+
String head = _shortSha(event.payload!.head);
145+
des = "head: $head";
146+
} else {
147+
des = "";
148+
}
119149
} else {
120150
actionStr = "";
121151
}
@@ -133,7 +163,11 @@ class EventUtils {
133163
}
134164

135165
///跳转
136-
static ActionUtils(BuildContext context, Event event, currentRepository) {
166+
static Future<void> ActionUtils(
167+
BuildContext context,
168+
Event event,
169+
currentRepository,
170+
) async {
137171
if (event.repo == null) {
138172
NavigatorUtils.goPerson(context, event.actor!.login);
139173
return;
@@ -153,32 +187,138 @@ class EventUtils {
153187
);
154188
break;
155189
case 'PushEvent':
156-
if (event.payload!.commits == null) {
190+
List<PushEventCommit> commits = event.payload?.commits ?? [];
191+
String? beforeSha = event.payload?.before;
192+
String? headSha = event.payload?.head;
193+
List<RepoCommit> compareCommits = [];
194+
195+
if (!_isInvalidCompareBase(beforeSha) &&
196+
headSha != null &&
197+
headSha.isNotEmpty &&
198+
beforeSha != headSha) {
199+
CommonUtils.showLoadingDialog(context);
200+
try {
201+
var compareRes = await ReposRepository.getReposCompareRequest(
202+
owner,
203+
repositoryName,
204+
beforeSha!,
205+
headSha,
206+
);
207+
if (compareRes != null &&
208+
compareRes.result &&
209+
compareRes.data != null) {
210+
compareCommits = compareRes.data.commits ?? [];
211+
}
212+
} finally {
213+
if (context.mounted) {
214+
Navigator.pop(context);
215+
}
216+
}
217+
}
218+
if (!context.mounted) {
219+
return;
220+
}
221+
222+
if (compareCommits.length == 1 && compareCommits.first.sha != null) {
223+
NavigatorUtils.goPushDetailPage(
224+
context,
225+
owner,
226+
repositoryName,
227+
compareCommits.first.sha,
228+
true,
229+
);
230+
return;
231+
}
232+
233+
if (compareCommits.length > 1) {
234+
StringList list = [];
235+
for (int i = 0; i < compareCommits.length; i++) {
236+
RepoCommit commit = compareCommits[i];
237+
String message =
238+
commit.commit?.message?.split('\n').first.trim() ?? "Commit";
239+
if (message.isEmpty) {
240+
message = "Commit";
241+
}
242+
list.add("$message ${_shortSha(commit.sha, 4)}");
243+
}
244+
CommonUtils.showCommitOptionDialog(context, list, (index) {
245+
NavigatorUtils.goPushDetailPage(
246+
context,
247+
owner,
248+
repositoryName,
249+
compareCommits[index].sha,
250+
true,
251+
);
252+
});
253+
return;
254+
}
255+
256+
if (commits.isEmpty) {
257+
if (headSha != null && headSha.isNotEmpty) {
258+
NavigatorUtils.goPushDetailPage(
259+
context,
260+
owner,
261+
repositoryName,
262+
headSha,
263+
true,
264+
);
265+
return;
266+
}
157267
if (fullName.toLowerCase() == currentRepository.toLowerCase()) {
158268
return;
159269
}
160270
NavigatorUtils.goReposDetail(context, owner, repositoryName);
161-
} else if (event.payload!.commits!.length == 1) {
271+
} else if (commits.length == 1 && commits.first.sha != null) {
162272
NavigatorUtils.goPushDetailPage(
163273
context,
164274
owner,
165275
repositoryName,
166-
event.payload!.commits![0].sha,
276+
commits.first.sha,
167277
true,
168278
);
169279
} else {
170-
StringList list = [];
171-
for (int i = 0; i < event.payload!.commits!.length; i++) {
172-
list.add(
173-
"${event.payload!.commits![i].message!} ${event.payload!.commits![i].sha!.substring(0, 4)}",
280+
List<PushEventCommit> validCommits = commits
281+
.where((item) => item.sha != null && item.sha!.isNotEmpty)
282+
.toList();
283+
if (validCommits.isEmpty) {
284+
if (headSha != null && headSha.isNotEmpty) {
285+
NavigatorUtils.goPushDetailPage(
286+
context,
287+
owner,
288+
repositoryName,
289+
headSha,
290+
true,
291+
);
292+
} else {
293+
NavigatorUtils.goReposDetail(context, owner, repositoryName);
294+
}
295+
return;
296+
}
297+
if (validCommits.length == 1) {
298+
NavigatorUtils.goPushDetailPage(
299+
context,
300+
owner,
301+
repositoryName,
302+
validCommits.first.sha,
303+
true,
174304
);
305+
return;
306+
}
307+
StringList list = [];
308+
for (int i = 0; i < validCommits.length; i++) {
309+
PushEventCommit commit = validCommits[i];
310+
String shaShort = _shortSha(commit.sha, 4);
311+
String message = (commit.message == null || commit.message!.isEmpty)
312+
? "Commit"
313+
: commit.message!;
314+
list.add("$message $shaShort");
175315
}
176316
CommonUtils.showCommitOptionDialog(context, list, (index) {
177317
NavigatorUtils.goPushDetailPage(
178318
context,
179319
owner,
180320
repositoryName,
181-
event.payload!.commits![index].sha,
321+
validCommits[index].sha,
182322
true,
183323
);
184324
});

0 commit comments

Comments
 (0)