mirror of
https://github.com/GitJournal/GitJournal.git
synced 2025-07-01 04:07:53 +08:00
GitSetup: Use the 'journal' repo if it already exists
This commit is contained in:
@ -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);
|
||||
|
@ -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'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -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'],
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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";
|
||||
|
Reference in New Issue
Block a user