diff --git a/lib/src/common/model/repos.dart b/lib/src/common/model/repos.dart index 3612aa34..28f5be49 100644 --- a/lib/src/common/model/repos.dart +++ b/lib/src/common/model/repos.dart @@ -380,6 +380,39 @@ class CreateRepository { Map 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 input) => + _$CreateRepositoryFromTemplateFromJson(input); + Map toJson() => _$CreateRepositoryFromTemplateToJson(this); +} + /// Model class for a branch. @JsonSerializable() class Branch { diff --git a/lib/src/common/model/repos.g.dart b/lib/src/common/model/repos.g.dart index b2e395bd..bbf6b53b 100644 --- a/lib/src/common/model/repos.g.dart +++ b/lib/src/common/model/repos.g.dart @@ -264,6 +264,26 @@ Map _$CreateRepositoryToJson(CreateRepository instance) => 'license_template': instance.licenseTemplate, }; +CreateRepositoryFromTemplate _$CreateRepositoryFromTemplateFromJson( + Map json) => + CreateRepositoryFromTemplate( + json['name'] as String, + owner: json['owner'] as String?, + description: json['description'] as String?, + includeAllBranches: json['include_all_branches'] as bool?, + private: json['private'] as bool?, + ); + +Map _$CreateRepositoryFromTemplateToJson( + CreateRepositoryFromTemplate instance) => + { + 'name': instance.name, + 'owner': instance.owner, + 'description': instance.description, + 'include_all_branches': instance.includeAllBranches, + 'private': instance.private, + }; + Branch _$BranchFromJson(Map json) => Branch( json['name'] as String?, json['commit'] == null diff --git a/lib/src/common/model/repos_forks.dart b/lib/src/common/model/repos_forks.dart index cbf4b68c..70361138 100644 --- a/lib/src/common/model/repos_forks.dart +++ b/lib/src/common/model/repos_forks.dart @@ -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 input) => _$CreateForkFromJson(input); diff --git a/lib/src/common/model/repos_forks.g.dart b/lib/src/common/model/repos_forks.g.dart index 0d41f4e9..9944f67e 100644 --- a/lib/src/common/model/repos_forks.g.dart +++ b/lib/src/common/model/repos_forks.g.dart @@ -7,10 +7,12 @@ part of 'repos_forks.dart'; // ************************************************************************** CreateFork _$CreateForkFromJson(Map json) => CreateFork( - json['organization'] as String?, + organization: json['organization'] as String?, + name: json['name'] as String?, ); Map _$CreateForkToJson(CreateFork instance) => { 'organization': instance.organization, + 'name': instance.name, }; diff --git a/lib/src/common/repos_service.dart b/lib/src/common/repos_service.dart index a3a73531..dd8012c0 100644 --- a/lib/src/common/repos_service.dart +++ b/lib/src/common/repos_service.dart @@ -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 createRepositoryFromTemplate( + RepositorySlug template, CreateRepositoryFromTemplate repository) async { + ArgumentError.checkNotNull(repository); + return github.postJSON, Repository>( + '/repos/${template.fullName}/generate', + body: GitHubJson.encode(repository), + convert: (i) => Repository.fromJson(i), + ); + } + Future getLicense(RepositorySlug slug) async { ArgumentError.checkNotNull(slug); return github.getJSON, LicenseDetails>( @@ -153,29 +168,34 @@ class RepositoriesService extends Service { /// Edit a Repository. /// - /// API docs: https://developer.github.com/v3/repos/#edit - Future 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 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, Repository>( '/repos/${slug.fullName}', body: GitHubJson.encode(data), + convert: (i) => Repository.fromJson(i), statusCode: 200, ); } @@ -309,6 +329,7 @@ class RepositoriesService extends Service { return false; } + /// https://docs.github.com/en/rest/collaborators/collaborators#add-a-repository-collaborator Future addCollaborator(RepositorySlug slug, String user) async { ArgumentError.checkNotNull(slug); ArgumentError.checkNotNull(user); @@ -316,9 +337,8 @@ class RepositoriesService extends Service { .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 removeCollaborator(RepositorySlug slug, String user) async {