Skip to content

Commit 6adf7cc

Browse files
committed
Implement create repository from template
Implement the service to create a new repo using a template according https://docs.github.com/en/rest/repos/repos#create-a-repository-using-a-template specs (endpoint: /repos/{template_owner}/{template_repo}/generate)
1 parent 546abc2 commit 6adf7cc

File tree

3 files changed

+68
-0
lines changed

3 files changed

+68
-0
lines changed

lib/src/common/model/repos.dart

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,39 @@ class CreateRepository {
379379
Map<String, dynamic> toJson() => _$CreateRepositoryToJson(this);
380380
}
381381

382+
/// Model class for a new repository to be created using a template.
383+
@JsonSerializable()
384+
class CreateRepositoryFromTemplate {
385+
CreateRepositoryFromTemplate(
386+
this.name, {
387+
this.owner,
388+
this.description,
389+
this.includeAllBranches,
390+
this.private,
391+
});
392+
393+
/// Repository Name
394+
final String name;
395+
396+
/// Owner Name
397+
final String? owner;
398+
399+
/// Repository Description
400+
String? description;
401+
402+
/// Include the directory structure and files from all branches in the
403+
/// template repository, and not just the default branch. Default: false.
404+
@JsonKey(name: 'include_all_branches')
405+
bool? includeAllBranches = false;
406+
407+
/// If the repository should be private or not.
408+
bool? private = false;
409+
410+
factory CreateRepositoryFromTemplate.fromJson(Map<String, dynamic> input) =>
411+
_$CreateRepositoryFromTemplateFromJson(input);
412+
Map<String, dynamic> toJson() => _$CreateRepositoryFromTemplateToJson(this);
413+
}
414+
382415
/// Model class for a branch.
383416
@JsonSerializable()
384417
class Branch {

lib/src/common/model/repos.g.dart

Lines changed: 20 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/src/common/repos_service.dart

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,21 @@ class RepositoriesService extends Service {
118118
}
119119
}
120120

121+
/// Creates a repository with [repository] using a template. If an [org] is
122+
/// specified, the new repository will be created under that organization. If
123+
/// no [org] is specified, it will be created for the authenticated user.
124+
///
125+
/// API docs: https://developer.github.com/v3/repos/#create
126+
Future<Repository> createRepositoryFromTemplate(
127+
RepositorySlug template, CreateRepositoryFromTemplate repository) async {
128+
ArgumentError.checkNotNull(repository);
129+
return github.postJSON<Map<String, dynamic>, Repository>(
130+
'/repos/${template.fullName}/generate',
131+
body: GitHubJson.encode(repository),
132+
convert: (i) => Repository.fromJson(i),
133+
);
134+
}
135+
121136
Future<LicenseDetails> getLicense(RepositorySlug slug) async {
122137
ArgumentError.checkNotNull(slug);
123138
return github.getJSON<Map<String, dynamic>, LicenseDetails>(

0 commit comments

Comments
 (0)