From 79fe174b1052dbeac5f4053bc6f4d44e9f5a2842 Mon Sep 17 00:00:00 2001 From: Rongjian Zhang Date: Sun, 2 Feb 2020 10:58:42 +0800 Subject: [PATCH] fix(gitlab): use default branch instead of master r #28 --- lib/models/gitlab.dart | 1 + lib/models/gitlab.g.dart | 4 +++- lib/router.dart | 10 ++++++---- lib/screens/gitlab_blob.dart | 5 +++-- lib/screens/gitlab_project.dart | 2 +- lib/screens/gitlab_tree.dart | 20 ++++++++++++-------- 6 files changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/models/gitlab.dart b/lib/models/gitlab.dart index 096e25b..baff444 100644 --- a/lib/models/gitlab.dart +++ b/lib/models/gitlab.dart @@ -92,6 +92,7 @@ class GitlabProject { GitlabProjectStatistics statistics; DateTime lastActivityAt; DateTime createdAt; + String defaultBranch; GitlabProject(); factory GitlabProject.fromJson(Map json) => _$GitlabProjectFromJson(json); diff --git a/lib/models/gitlab.g.dart b/lib/models/gitlab.g.dart index 32aac01..1b55158 100644 --- a/lib/models/gitlab.g.dart +++ b/lib/models/gitlab.g.dart @@ -162,7 +162,8 @@ GitlabProject _$GitlabProjectFromJson(Map json) { : DateTime.parse(json['last_activity_at'] as String) ..createdAt = json['created_at'] == null ? null - : DateTime.parse(json['created_at'] as String); + : DateTime.parse(json['created_at'] as String) + ..defaultBranch = json['default_branch'] as String; } Map _$GitlabProjectToJson(GitlabProject instance) => @@ -184,6 +185,7 @@ Map _$GitlabProjectToJson(GitlabProject instance) => 'statistics': instance.statistics, 'last_activity_at': instance.lastActivityAt?.toIso8601String(), 'created_at': instance.createdAt?.toIso8601String(), + 'default_branch': instance.defaultBranch, }; GitlabProjectBadge _$GitlabProjectBadgeFromJson(Map json) { diff --git a/lib/router.dart b/lib/router.dart index 40e373a..61b789e 100644 --- a/lib/router.dart +++ b/lib/router.dart @@ -148,12 +148,14 @@ class GitlabRouter { static final group = RouterScreen('/group/:id', (context, p) => GitlabGroupScreen(int.parse(p['id'].first))); static final blob = RouterScreen( - '/projects/:id/blob', - (context, params) => GitlabBlobScreen(int.parse(params['id'].first), + '/projects/:id/blob/:ref', + (context, params) => GitlabBlobScreen( + int.parse(params['id'].first), params['ref'].first, path: params['path']?.first)); static final tree = RouterScreen( - '/projects/:id/tree', - (context, params) => GitlabTreeScreen(int.parse(params['id'].first), + '/projects/:id/tree/:ref', + (context, params) => GitlabTreeScreen( + int.parse(params['id'].first), params['ref'].first, path: params['path']?.first)); static final project = RouterScreen('/projects/:id', (context, params) => GitlabProjectScreen(int.parse(params['id'].first))); diff --git a/lib/screens/gitlab_blob.dart b/lib/screens/gitlab_blob.dart index 852aad3..23330bd 100644 --- a/lib/screens/gitlab_blob.dart +++ b/lib/screens/gitlab_blob.dart @@ -10,8 +10,9 @@ import 'package:provider/provider.dart'; class GitlabBlobScreen extends StatelessWidget { final int id; + final String ref; final String path; - GitlabBlobScreen(this.id, {this.path}); + GitlabBlobScreen(this.id, this.ref, {this.path}); @override Widget build(BuildContext context) { @@ -19,7 +20,7 @@ class GitlabBlobScreen extends StatelessWidget { title: AppBarTitle(path ?? ''), fetchData: () async { final res = await Provider.of(context).fetchGitlab( - '/projects/$id/repository/files/${path.urlencode}?ref=master'); // TODO: + '/projects/$id/repository/files/${path.urlencode}?ref=$ref'); return GitlabBlob.fromJson(res); }, action: ActionEntry(iconData: Icons.settings, url: '/choose-code-theme'), diff --git a/lib/screens/gitlab_project.dart b/lib/screens/gitlab_project.dart index 1db1a58..85a9ad0 100644 --- a/lib/screens/gitlab_project.dart +++ b/lib/screens/gitlab_project.dart @@ -115,7 +115,7 @@ class GitlabProjectScreen extends StatelessWidget { leftIconData: Octicons.code, text: Text(langs.keys.isEmpty ? 'Code' : langs.keys.first), rightWidget: Text(filesize(p.statistics.repositorySize)), - url: '/gitlab/projects/$id/tree', + url: '/gitlab/projects/$id/tree/${p.defaultBranch}', ), if (p.issuesEnabled) TableViewItem( diff --git a/lib/screens/gitlab_tree.dart b/lib/screens/gitlab_tree.dart index aac1395..06bb8f5 100644 --- a/lib/screens/gitlab_tree.dart +++ b/lib/screens/gitlab_tree.dart @@ -10,8 +10,9 @@ import 'package:git_touch/utils/utils.dart'; class GitlabTreeScreen extends StatelessWidget { final int id; + final String ref; final String path; - GitlabTreeScreen(this.id, {this.path}); + GitlabTreeScreen(this.id, this.ref, {this.path}); @override Widget build(BuildContext context) { @@ -19,11 +20,14 @@ class GitlabTreeScreen extends StatelessWidget { return RefreshStatefulScaffold>( title: AppBarTitle(path ?? 'Files'), fetchData: () async { - var url = '/projects/$id/repository/tree'; - if (path != null) { - url += '?path=' + path; - } - final res = await auth.fetchGitlab(url); + final uri = Uri( + path: '/projects/$id/repository/tree', + queryParameters: { + 'ref': ref, + ...(path == null ? {} : {'path': path}) + }, + ); + final res = await auth.fetchGitlab(uri.toString()); return (res as List).map((v) => GitlabTreeItem.fromJson(v)); }, bodyBuilder: (data, _) { @@ -37,9 +41,9 @@ class GitlabTreeScreen extends StatelessWidget { url: (() { switch (item.type) { case 'tree': - return '/gitlab/projects/$id/tree?path=${item.path.urlencode}'; + return '/gitlab/projects/$id/tree/$ref?path=${item.path.urlencode}'; case 'blob': - return '/gitlab/projects/$id/blob?path=${item.path.urlencode}'; + return '/gitlab/projects/$id/blob/$ref?path=${item.path.urlencode}'; default: return null; }