From 06dc62e967c7254ab68553740b98d44d90c0d384 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Thu, 14 Feb 2019 11:06:49 +0100 Subject: [PATCH] GitSetup: Use the 'journal' repo if it already exists --- lib/apis/githost.dart | 9 +++++- lib/apis/github.dart | 34 +++++++++++++++++++-- lib/apis/gitlab.dart | 33 ++++++++++++++++++-- lib/screens/githostsetup_autoconfigure.dart | 14 +++++++-- 4 files changed, 82 insertions(+), 8 deletions(-) diff --git a/lib/apis/githost.dart b/lib/apis/githost.dart index c5d3b201..dc91a68a 100644 --- a/lib/apis/githost.dart +++ b/lib/apis/githost.dart @@ -9,14 +9,20 @@ abstract class GitHost { Future getUserInfo(); Future> listRepos(); Future createRepo(String name); + Future getRepo(String name); Future addDeployKey(String sshPublicKey, String repo); } class UserInfo { String name; String email; + String username; - UserInfo({@required this.name, @required this.email}); + UserInfo({ + @required this.name, + @required this.email, + @required this.username, + }); } class GitRepo { @@ -37,6 +43,7 @@ class GitHostException implements Exception { static const RepoExists = const GitHostException("RepoExists"); static const CreateRepoFailed = const GitHostException("CreateRepoFailed"); static const DeployKeyFailed = const GitHostException("DeployKeyFailed"); + static const GetRepoFailed = const GitHostException("GetRepoFailed"); final String cause; const GitHostException(this.cause); diff --git a/lib/apis/github.dart b/lib/apis/github.dart index 513a75dd..e70bb9c1 100644 --- a/lib/apis/github.dart +++ b/lib/apis/github.dart @@ -106,7 +106,6 @@ class GitHub implements GitHost { @override Future createRepo(String name) async { - // FIXME: Proper error when the repo exists! if (_accessCode.isEmpty) { throw GitHostException.MissingAccessCode; } @@ -143,7 +142,32 @@ class GitHub implements GitHost { return _repoFromJson(map); } - // FIXME: Proper error when the repo exists! + @override + Future getRepo(String name) async { + if (_accessCode.isEmpty) { + throw GitHostException.MissingAccessCode; + } + + var userInfo = await getUserInfo(); + var owner = userInfo.username; + var url = + "https://api.github.com/repos/$owner/$name?access_token=$_accessCode"; + + var response = await http.get(url); + if (response.statusCode != 200) { + print("Github getRepo: Invalid response " + + response.statusCode.toString() + + ": " + + response.body); + + throw GitHostException.GetRepoFailed; + } + + print("GitHub getRepo: " + response.body); + Map map = json.decode(response.body); + return _repoFromJson(map); + } + @override Future addDeployKey(String sshPublicKey, String repo) async { if (_accessCode.isEmpty) { @@ -210,6 +234,10 @@ class GitHub implements GitHost { return null; } - return UserInfo(name: map['name'], email: map['email']); + return UserInfo( + name: map['name'], + email: map['email'], + username: map['login'], + ); } } diff --git a/lib/apis/gitlab.dart b/lib/apis/gitlab.dart index 4226d43b..19675d52 100644 --- a/lib/apis/gitlab.dart +++ b/lib/apis/gitlab.dart @@ -95,7 +95,6 @@ class GitLab implements GitHost { @override Future createRepo(String name) async { - // FIXME: Proper error when the repo exists! if (_accessCode.isEmpty) { throw GitHostException.MissingAccessCode; } @@ -132,6 +131,32 @@ class GitLab implements GitHost { return _repoFromJson(map); } + @override + Future getRepo(String name) async { + if (_accessCode.isEmpty) { + throw GitHostException.MissingAccessCode; + } + + var userInfo = await getUserInfo(); + var repo = userInfo.username + '%2F' + name; + var url = + "https://gitlab.com/api/v4/projects/$repo?access_token=$_accessCode"; + + var response = await http.get(url); + if (response.statusCode != 200) { + print("GitLab getRepo: Invalid response " + + response.statusCode.toString() + + ": " + + response.body); + + throw GitHostException.GetRepoFailed; + } + + print("GitLab getRepo: " + response.body); + Map map = json.decode(response.body); + return _repoFromJson(map); + } + @override Future addDeployKey(String sshPublicKey, String repo) async { if (_accessCode.isEmpty) { @@ -199,7 +224,11 @@ class GitLab implements GitHost { return null; } - return UserInfo(name: map['name'], email: map['email']); + return UserInfo( + name: map['name'], + email: map['email'], + username: map['username'], + ); } } diff --git a/lib/screens/githostsetup_autoconfigure.dart b/lib/screens/githostsetup_autoconfigure.dart index 4a70b7d2..c952a3b0 100644 --- a/lib/screens/githostsetup_autoconfigure.dart +++ b/lib/screens/githostsetup_autoconfigure.dart @@ -54,8 +54,18 @@ class GitHostSetupAutoConfigureState extends State { _message = "Creating private repo"; }); - // FIXME: What if repo already exists? - repo = await gitHost.createRepo("journal"); + try { + repo = await gitHost.createRepo("journal"); + } on GitHostException catch (e) { + if (e.cause != GitHostException.RepoExists.cause) { + rethrow; + } + + this.setState(() { + _message = "Using existing repo"; + }); + repo = await gitHost.getRepo("journal"); + } this.setState(() { _message = "Generating SSH Key";