Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement missing endpoints and small fixes #334

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
33 changes: 33 additions & 0 deletions lib/src/common/model/repos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,39 @@ class CreateRepository {
Map<String, dynamic> toJson() => _$CreateRepositoryToJson(this);
}

/// Model class for a new repository to be created using a template.
@JsonSerializable()
class CreateRepositoryFromTemplate {
CreateRepositoryFromTemplate(
this.name, {
this.owner,
this.description,
this.includeAllBranches,
this.private,
});

/// Repository Name
final String name;

/// Owner Name
final String? owner;

/// Repository Description
String? description;

/// Include the directory structure and files from all branches in the
/// template repository, and not just the default branch. Default: false.
@JsonKey(name: 'include_all_branches')
bool? includeAllBranches = false;

/// If the repository should be private or not.
bool? private = false;

factory CreateRepositoryFromTemplate.fromJson(Map<String, dynamic> input) =>
_$CreateRepositoryFromTemplateFromJson(input);
Map<String, dynamic> toJson() => _$CreateRepositoryFromTemplateToJson(this);
}

/// Model class for a branch.
@JsonSerializable()
class Branch {
Expand Down
20 changes: 20 additions & 0 deletions lib/src/common/model/repos.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion lib/src/common/model/repos_forks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import 'package:json_annotation/json_annotation.dart';
part 'repos_forks.g.dart';

/// Model class for a new fork to be created.
/// https://docs.github.com/en/rest/repos/forks#create-a-fork
@JsonSerializable()
class CreateFork {
CreateFork([this.organization]);
CreateFork({this.organization, this.name});

String? organization;
String? name;

factory CreateFork.fromJson(Map<String, dynamic> input) =>
_$CreateForkFromJson(input);
Expand Down
4 changes: 3 additions & 1 deletion lib/src/common/model/repos_forks.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 40 additions & 20 deletions lib/src/common/repos_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,21 @@ class RepositoriesService extends Service {
}
}

/// Creates a repository with [repository] using a template. If an [org] is
/// specified, the new repository will be created under that organization. If
/// no [org] is specified, it will be created for the authenticated user.
///
/// API docs: https://developer.github.com/v3/repos/#create
Future<Repository> createRepositoryFromTemplate(
RepositorySlug template, CreateRepositoryFromTemplate repository) async {
ArgumentError.checkNotNull(repository);
return github.postJSON<Map<String, dynamic>, Repository>(
'/repos/${template.fullName}/generate',
body: GitHubJson.encode(repository),
convert: (i) => Repository.fromJson(i),
);
}

Future<LicenseDetails> getLicense(RepositorySlug slug) async {
ArgumentError.checkNotNull(slug);
return github.getJSON<Map<String, dynamic>, LicenseDetails>(
Expand Down Expand Up @@ -153,29 +168,34 @@ class RepositoriesService extends Service {

/// Edit a Repository.
///
/// API docs: https://developer.github.com/v3/repos/#edit
Future<Repository> editRepository(RepositorySlug slug,
{String? name,
String? description,
String? homepage,
bool? private,
bool? hasIssues,
bool? hasWiki,
bool? hasDownloads}) async {
/// API docs: https://docs.github.com/en/rest/repos/repos#update-a-repository
Future<Repository> editRepository(
RepositorySlug slug, {
String? name,
String? description,
String? homepage,
bool? private,
bool? hasIssues,
bool? hasWiki,
bool? hasDownloads,
String? defaultBranch,
}) async {
ArgumentError.checkNotNull(slug);

final data = createNonNullMap({
'name': name!,
'description': description!,
'homepage': homepage!,
'private': private!,
'has_issues': hasIssues!,
'has_wiki': hasWiki!,
'has_downloads': hasDownloads!,
'default_branch': 'defaultBranch'
'name': name,
'description': description,
'homepage': homepage,
'private': private,
'has_issues': hasIssues,
'has_wiki': hasWiki,
'has_downloads': hasDownloads,
'default_branch': defaultBranch,
});
return github.postJSON(
return github.postJSON<Map<String, dynamic>, Repository>(
'/repos/${slug.fullName}',
body: GitHubJson.encode(data),
convert: (i) => Repository.fromJson(i),
statusCode: 200,
);
}
Expand Down Expand Up @@ -309,16 +329,16 @@ class RepositoriesService extends Service {
return false;
}

/// https://docs.github.com/en/rest/collaborators/collaborators#add-a-repository-collaborator
Future<bool> addCollaborator(RepositorySlug slug, String user) async {
ArgumentError.checkNotNull(slug);
ArgumentError.checkNotNull(user);
return github
.request(
'PUT',
'/repos/${slug.fullName}/collaborators/$user',
statusCode: StatusCodes.NO_CONTENT,
)
.then((response) => response.statusCode == StatusCodes.NO_CONTENT);
.then((response) => response.statusCode == StatusCodes.CREATED || response.statusCode == StatusCodes. NO_CONTENT);
}

Future<bool> removeCollaborator(RepositorySlug slug, String user) async {
Expand Down