Add versioning and error handling

This commit is contained in:
Aaron Fischer 2019-07-07 22:31:06 +02:00
parent 27d93e53ff
commit 7cf3237ccb
3 changed files with 80 additions and 26 deletions

View file

@ -41,17 +41,37 @@ class GitHubAdapter {
"+filename:mordheim.yml+path:\"" + "+filename:mordheim.yml+path:\"" +
path + path +
"\""); "\"");
// GitHub is not reachable
if (response.statusCode != 200) {
syncErrors.add('Could not find any warband roaster files');
yield {};
return;
}
// No valid response from GitHub
dynamic searchResults;
try {
searchResults = jsonDecode(response.body);
} on FormatException catch (e) {
syncErrors.add('Could not parse GitHub response.');
yield {};
return;
}
// Find suitable files for examination
RegExp fileRegex = new RegExp(r"[a-zA-Z]+\.mordheim\.ya?ml"); RegExp fileRegex = new RegExp(r"[a-zA-Z]+\.mordheim\.ya?ml");
var resp = jsonDecode(response.body); for (dynamic searchResult in searchResults['items']) {
for (var searchResult in resp['items']) {
if (fileRegex.hasMatch(searchResult['name'])) { if (fileRegex.hasMatch(searchResult['name'])) {
// We try to get the name of the player from the name of the folder
// in which the file resists
String completePath = searchResult['path']; String completePath = searchResult['path'];
String playerName = List<String> pathParts =
completePath.substring(path.length + 1).split('/').first; completePath.substring(path.length + 1).split('/');
if (playerName == '') { String playerName;
playerName = 'Lonely Recluse'; if (pathParts.length >= 2) {
playerName = pathParts.first;
} }
// Fetch last change and some metainformation of the file // Fetch last change and some metainformation of the file
@ -60,18 +80,34 @@ class GitHubAdapter {
repository + repository +
"/commits?path=" + "/commits?path=" +
completePath); completePath);
var resp = jsonDecode(response.body);
var lastCommit = resp.first;
// TODO: Add some error handling if (response.statusCode != 200) {
syncErrors.add('Could not load the warband metadata from GitHub.');
continue;
}
// No valid response from GitHub
dynamic commits;
try {
commits = jsonDecode(response.body);
} on FormatException catch (e) {
syncErrors.add('Could not parse GitHub response.');
continue;
}
// No commits available
if (commits.length == 0) {
continue;
}
dynamic latestCommit = commits.first;
yield { yield {
'filePath': completePath.toString(), 'filePath': completePath.toString(),
'shaHash': lastCommit['sha'], 'shaHash': latestCommit['sha'],
'player': playerName.toString(), 'player': playerName.toString(),
'author': lastCommit['commit']['author']['name'], 'author': latestCommit['commit']['author']['name'],
'date': lastCommit['commit']['committer']['date'], 'date': latestCommit['commit']['committer']['date'],
'message': lastCommit['commit']['message'] 'message': latestCommit['commit']['message']
}; };
} }
} }
@ -89,11 +125,16 @@ class GitHubAdapter {
try { try {
YamlMap yamlObject = loadYaml(response.body); YamlMap yamlObject = loadYaml(response.body);
WarbandRoaster roaster = WarbandRoaster.fromJson(yamlObject); WarbandRoaster roaster = WarbandRoaster.fromJson(yamlObject);
if (player['player'] != '') {
roaster.playerName = player['player']; roaster.playerName = player['player'];
roaster.author = player['author']; }
roaster.commitDate = player['date'];
roaster.commitMessage = player['message']; roaster.currentVersion = new Version(player['shaHash'], player['date'],
roaster.commitHash = player['shaHash'] player['author'], player['message']);
// On a search, we drop all previous information about the warbands,
// Sp, lastSyncVersion is equal to the currentVersion.
roaster.lastSyncVersion = roaster.currentVersion;
roasters.add(roaster); roasters.add(roaster);
} catch (e) { } catch (e) {

View file

@ -57,7 +57,7 @@ class _RoasterWidgetState extends State<RoasterWidget> {
roasters.forEach((roaster) { roasters.forEach((roaster) {
tiles.add(ListTile( tiles.add(ListTile(
title: Text(roaster.name + ' (' + roaster.playerName + ')'), title: Text(roaster.name + ' (' + roaster.playerName + ')'),
subtitle: Text(roaster.commitMessage), subtitle: Text(roaster.currentVersion.message),
isThreeLine: true, isThreeLine: true,
trailing: Chip( trailing: Chip(
label: Text( label: Text(
@ -83,6 +83,10 @@ class _RoasterWidgetState extends State<RoasterWidget> {
case ConnectionState.done: case ConnectionState.done:
List<WarbandRoaster> roasters = snapshot.data; List<WarbandRoaster> roasters = snapshot.data;
if (roasters.length == 0) {
return Text('No warbands found');
}
// TODO: Replace with router // TODO: Replace with router
WarbandRoaster warband = roasters.first; WarbandRoaster warband = roasters.first;

View file

@ -153,6 +153,16 @@ class Stats {
this.save); this.save);
} }
class Version {
String gitHash;
String date;
String author;
String message;
Version(this.gitHash, this.date, this.author, this.message);
}
@JsonSerializable(nullable: true, anyMap: true, createToJson: false) @JsonSerializable(nullable: true, anyMap: true, createToJson: false)
class WarbandRoaster { class WarbandRoaster {
/// Store the complete string of name and race. This will split up into the /// Store the complete string of name and race. This will split up into the
@ -180,11 +190,10 @@ class WarbandRoaster {
/// The players name is not defined in the yml file. This will be added later /// The players name is not defined in the yml file. This will be added later
/// from the GitHubAdapter. /// from the GitHubAdapter.
String playerName = ''; String playerName = 'Lonely Recluse';
String author = '';
String commitDate = ''; Version lastSyncVersion;
String commitMessage = ''; Version currentVersion;
String commitHash = '';
WarbandRoaster( WarbandRoaster(
this.nameAndRace, this.nameAndRace,