GitSetup: Use the 'journal' repo if it already exists

This commit is contained in:
Vishesh Handa
2019-02-14 11:06:49 +01:00
parent 22b022be82
commit 06dc62e967
4 changed files with 82 additions and 8 deletions

View File

@ -9,14 +9,20 @@ abstract class GitHost {
Future<UserInfo> getUserInfo();
Future<List<GitRepo>> listRepos();
Future<GitRepo> createRepo(String name);
Future<GitRepo> 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);

View File

@ -106,7 +106,6 @@ class GitHub implements GitHost {
@override
Future<GitRepo> 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<GitRepo> 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<String, dynamic> 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'],
);
}
}

View File

@ -95,7 +95,6 @@ class GitLab implements GitHost {
@override
Future<GitRepo> 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<GitRepo> 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<String, dynamic> 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'],
);
}
}

View File

@ -54,8 +54,18 @@ class GitHostSetupAutoConfigureState extends State<GitHostSetupAutoConfigure> {
_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";