Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@colinshum
Copy link
Contributor

@colinshum colinshum commented Aug 27, 2020

Summary

Closes #1578

In this PR, I added functionality to use the following command to create a new repo from a template:

gh repo create <new-repo-name> --template="<link-to-template-repo> OR <owner/template-repo>"

Changes

I used the cloneTemplateRepository mutation from the public GraphQL API to achieve this functionality. You can read more about it here: GraphQL Docs

A limitation of using this mutation is that it does not fully support all of the inputs that the createRepository mutation does. Therefore, I had to add some ad-hoc input processing to warn the user about passing incompatible flags with --template.

This seemed to be the most straightforward approach, as we do not want to modify the createRepository mutation to support this existing functionality.

Example

$ gh repo create hello_world --template username/template-example --homepage="example.com"
the '--template' option is not supported with '--homepage, --team, --enable-issues or --enable-wiki'

Note: --enable-wiki="true" or --enable-issues="true" will not throw an error, as those are repo defaults. However, if you pass false, it will raise this FlagError.

Demo

ScreenFlow

Checklist

  • Basic Functionality
  • Error Handling
  • Tests

@colinshum colinshum marked this pull request as ready for review August 27, 2020 21:58
@colinshum
Copy link
Contributor Author

/cc @mislav @vilmibm ready for review!

Copy link
Contributor

@vilmibm vilmibm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for taking this on! It's really close, just asking for some cosmetic and minor code structure changes.

Visibility: visibility,
OwnerID: repoToCreate.RepoOwner(),
TeamID: opts.Team,
RepositoryID: opts.Template,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this type is intended to pass as input for the create repo mutation I'm not in love with using it to signal the desire to clone from a template.

I'd rather see:

  • a new helper, repoCreateFromTemplate
  • a change to repoCreate's function signature to accept a potentially empty template argument

Copy link
Contributor Author

@colinshum colinshum Aug 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in f465b07

@vilmibm I've refactored the changes so that repoCreate is a variadic function:

func repoCreate(client *http.Client, hostname string, input repoCreateInput, template ...string) (*api.Repository, error)

As a result, I was able to remove RepositoryID from the repoCreateInput block, and passed it as an argument instead whenever repoCreate was called 😄

@StefanXhunga
Copy link

Thank you for this improvement

@colinshum colinshum force-pushed the colinshum/template-repo branch 2 times, most recently from 957c31f to f465b07 Compare August 27, 2020 23:15
@colinshum
Copy link
Contributor Author

@vilmibm @mislav: I've refactored the function to be variadic as Go doesn't support optional arguments. Please feel free to merge if it's good to ship! :shipit:

Copy link
Contributor

@vilmibm vilmibm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one last tiny nitpick then we're good!

@colinshum colinshum force-pushed the colinshum/template-repo branch from 10d25fc to 34c3718 Compare August 28, 2020 18:58
@colinshum colinshum force-pushed the colinshum/template-repo branch from 34c3718 to 99372f0 Compare August 28, 2020 19:02
@vilmibm vilmibm merged commit fd31007 into cli:trunk Aug 28, 2020
@colinshum colinshum deleted the colinshum/template-repo branch August 28, 2020 19:23
@colinshum colinshum restored the colinshum/template-repo branch August 28, 2020 19:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Create repositories from template repo

3 participants