Skip to content

Commit 85b0b29

Browse files
matt-grahampaddyroddydstansby
authored
Renaming github_username to github_owner + generating __repo_name & __repo_url (#409)
Though its not exposed to user now we have longer prompts rather than using variable names directly, I think `github_username` is a bit of a misnomer given that it can be either a user or organization name, hence I think `github_owner` would be a better choice. We also currently manually construct the GitHub repository URL in lots of different bits of the template and also the qualified repository name in a few places. We can use [double underscore prefixed (rendered) private variables](https://cookiecutter.readthedocs.io/en/stable/advanced/private_variables.html) in the cookiecutter config to generate the repository URL and qualified name once and then reuse elsewhere to avoid the repetition. This would also make it simpler to later switch to supporting alternative repository hosting options such as GitLab in future (which is the rational for naming the variables `__repo_name` and `__repo_url` rather than `__github_repo_name` and `__github_repo_url`). --------- Co-authored-by: Patrick J. Roddy <patrickjamesroddy@gmail.com> Co-authored-by: David Stansby <dstansby@gmail.com>
1 parent d5b41c7 commit 85b0b29

File tree

9 files changed

+41
-41
lines changed

9 files changed

+41
-41
lines changed

cookiecutter.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
"project_short_description": "A cookiecutter package with UCL ARC recommendations.",
99
"initialise_git_repository": true,
1010
"deploy_docs_to_github_pages": true,
11-
"github_username": "{{cookiecutter.author_given_names.lower().replace(' ', '-')}}-{{cookiecutter.author_family_names.lower().replace(' ', '-')}}",
11+
"github_owner": "{{cookiecutter.author_given_names.lower().replace(' ', '-')}}-{{cookiecutter.author_family_names.lower().replace(' ', '-')}}",
1212
"min_python_version": ["3.11", "3.12", "3.13"],
1313
"max_python_version": ["3.13", "3.12", "3.11"],
1414
"license": ["MIT", "BSD-3", "GPL-3.0"],
1515
"funder": "",
16+
"__repo_name": "{{cookiecutter.github_owner}}/{{cookiecutter.project_slug}}",
17+
"__repo_url": "https://github.com/{{cookiecutter.__repo_name}}",
1618
"__prompts__": {
1719
"author_given_names": "Given name(s) of package author",
1820
"author_family_names": "Family name(s) of package author",
@@ -23,7 +25,7 @@
2325
"project_short_description": "Short description of the project",
2426
"initialise_git_repository": "Initialise project directory as a Git repository?",
2527
"deploy_docs_to_github_pages": "Automatically deploy HTML docs to GitHub Pages on pushes to main?",
26-
"github_username": "GitHub user or organization name which will be owner of repository",
28+
"github_owner": "GitHub user or organization name which will be owner of repository",
2729
"min_python_version": "Minimum Python version supported by package",
2830
"max_python_version": "Maximum Python version supported by package",
2931
"license": "Which open-source license to release package under",

hooks/post_gen_project.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def main(initialise_git_repository: str, deploy_docs_to_github_pages: str) -> in
6262
print(
6363
"GitHub CLI detected, you can create a repo with the following:\n\n"
6464
"gh repo create "
65-
"{{cookiecutter.github_username}}/{{cookiecutter.project_slug}} "
65+
"{{cookiecutter.__repo_name}} "
6666
'-d "{{cookiecutter.project_short_description}}" '
6767
"--public "
6868
"-r origin "
@@ -73,12 +73,12 @@ def main(initialise_git_repository: str, deploy_docs_to_github_pages: str) -> in
7373
print(
7474
"You now have a local git repository. To sync this to GitHub "
7575
"you need to create an empty GitHub repo with the name "
76-
"{{cookiecutter.github_username}}/{{cookiecutter.project_slug}} "
76+
"{{cookiecutter.__repo_name}} "
7777
"- DO NOT SELECT ANY OTHER OPTION.\n\nSee this link for more detail "
7878
"https://docs.github.com/en/get-started/quickstart/create-a-repo.\n\n"
7979
"Then run:\n\n"
8080
"git remote add origin git@github.com:"
81-
"{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}.git\n"
81+
"{{cookiecutter.__repo_name}}.git\n"
8282
)
8383
except subprocess.CalledProcessError as e:
8484
# some other error
@@ -91,13 +91,11 @@ def main(initialise_git_repository: str, deploy_docs_to_github_pages: str) -> in
9191
"deploying as a GitHub Pages website. To allow the GitHub Actions bot to "
9292
"push to the gh-pages branch you need to enable 'Read and write "
9393
"permissions' under 'Workflow permissions' at\n\n"
94-
"https://github.com/{{cookiecutter.github_username}}/"
95-
"{{cookiecutter.project_slug}}/settings/actions\n\n"
94+
"{{cookiecutter.__repo_url}}/settings/actions\n\n"
9695
"After the 'Documentation' workflow has successfully completed at least "
9796
"once you will also need to configure the repository to deploy a GitHub "
9897
"pages site from the content on the gh-pages branch by going to\n\n"
99-
"https://github.com/{{cookiecutter.github_username}}/"
100-
"{{cookiecutter.project_slug}}/settings/pages\n\n"
98+
"{{cookiecutter.__repo_url}}/settings/pages\n\n"
10199
"and under 'Built and deployment' selecting 'Deploy from a branch' for "
102100
"the 'Source' drop-down and 'gh-pages' for the 'Branch' drop-down, "
103101
"leaving the branch path drop-down with its default value of '/ (root)'."

tests/test_git_init.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ def test_initialisation_of_git_repo(
1515
) -> None:
1616
"""Checks to see if git was correctly initialised if desired."""
1717
test_config = {
18-
"github_username": "test-user",
18+
"github_owner": "test-user",
1919
"project_short_description": "description",
2020
"project_name": "Cookiecutter Test",
2121
"initialise_git_repository": initialise_git_repository,
@@ -57,7 +57,7 @@ def test_initialisation_of_git_repo(
5757
)
5858
assert (
5959
"GitHub CLI detected, you can create a repo with the following:\n\n"
60-
f"gh repo create {test_config['github_username']}/"
60+
f"gh repo create {test_config['github_owner']}/"
6161
f"cookiecutter-test -d "
6262
f"\"{test_config['project_short_description']}\" --public -r "
6363
f"origin --source cookiecutter-test" in result.stdout
@@ -69,11 +69,11 @@ def test_initialisation_of_git_repo(
6969
assert (
7070
"You now have a local git repository. To sync this to GitHub you "
7171
"need to create an empty GitHub repo with the name "
72-
f"{test_config['github_username']}/"
72+
f"{test_config['github_owner']}/"
7373
f"cookiecutter-test - DO NOT SELECT ANY "
7474
"OTHER OPTION.\n\nSee this link for more detail "
7575
"https://docs.github.com/en/get-started/quickstart/create-a-repo"
7676
".\n\nThen run:\n\ngit remote add origin git@github.com:"
77-
f"{test_config['github_username']}/"
77+
f"{test_config['github_owner']}/"
7878
f"cookiecutter-test.git" in result.stdout
7979
)

tests/test_package_generation.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_package_generation(
4040
) -> None:
4141
"""Test package generation."""
4242
test_config = {
43-
"github_username": "test-user",
43+
"github_owner": "test-user",
4444
"project_short_description": "description",
4545
"project_name": "Cookiecutter Test",
4646
# Not having a git repo makes it easier to check in/out reference
@@ -98,7 +98,7 @@ def test_pip_installable(
9898
) -> None:
9999
"""Test generated package is pip installable."""
100100
test_config = {
101-
"github_username": "test-user",
101+
"github_owner": "test-user",
102102
"project_short_description": "description",
103103
"project_name": "Cookiecutter Test",
104104
}
@@ -129,7 +129,7 @@ def test_optional_funder(
129129
) -> None:
130130
"""Test package generation."""
131131
config = {
132-
"github_username": "test-user",
132+
"github_owner": "test-user",
133133
"project_short_description": "description",
134134
"project_name": "Cookiecutter Test",
135135
"funder": funder,

tutorial.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -153,19 +153,19 @@ When you completed setting up the package using the `cookiecutter` command you s
153153
```sh
154154
GitHub CLI detected, you can create a repo with the following:
155155

156-
gh repo create {github_user}/{project_slug} -d "{project_description}" --public -r origin --source {project_slug}
156+
gh repo create {github_owner}/{project_slug} -d "{project_description}" --public -r origin --source {project_slug}
157157
```
158158

159-
where `{github_user}`, `{project_slug}` and `{project_description}` are replaced with the relevant GitHub user name, project slug and description that you entered when setting up the package using `cookiecutter`. You should now copy-paste and run the `gh repo create ...` command that was printed out in the message - if you no longer have access to the message you can use the example above, being careful to subsitute the placeholders in braces `{}` with the relevant values.
159+
where `{github_owner}`, `{project_slug}` and `{project_description}` are replaced with the relevant GitHub repository owner (user name), project slug and description that you entered when setting up the package using `cookiecutter`. You should now copy-paste and run the `gh repo create ...` command that was printed out in the message - if you no longer have access to the message you can use the example above, being careful to subsitute the placeholders in braces `{}` with the relevant values.
160160

161161
If you get an error message at this point it may be because you have not installed the GitHub CLI or set up the authorisation for the GitHub CLI as [described in the setup instructions above](#%EF%B8%8F-setting-up-dependencies-for-using-template).
162162

163163
If the command runs successfully you should see a message of the form
164164

165165
```sh
166-
✓ Created repository {github_user}/{project_slug} on GitHub
167-
https://github.com/{github_user}/{project_slug}
168-
✓ Added remote https://github.com/{github_user}/{project_slug}.git
166+
✓ Created repository {github_owner}/{project_slug} on GitHub
167+
https://github.com/{github_owner}/{project_slug}
168+
✓ Added remote https://github.com/{github_owner}/{project_slug}.git
169169
```
170170

171171
A repository should have been created at the printed URL which you should be able to navigate to in your browser (either copy-paste in to browser or depending on terminal application you may be able to hold `Ctrl` and click on URL directly). Currently both the remote and local repositories are empty as we have not made any commits.
@@ -181,7 +181,7 @@ git push --set-upstream origin main
181181

182182
If you now navigate to the GitHub repository URL in your browser you should see the package files present, with the content of the top-level `README.md` being displayed as the repository landing page.
183183

184-
The push of the initial commit to the default `main` branch will have also triggered runs of the GitHub Actions workflows set up in the repository. You can view the status of the workflow job runs by opening the `Actions` pane from the top navigation bar on the repository landing page, or going to the URL `https://github.com/{github_user}/{project_slug}/actions`.
184+
The push of the initial commit to the default `main` branch will have also triggered runs of the GitHub Actions workflows set up in the repository. You can view the status of the workflow job runs by opening the `Actions` pane from the top navigation bar on the repository landing page, or going to the URL `https://github.com/{github_owner}/{project_slug}/actions`.
185185

186186
Depending on your account settings, you may find that the `Documentation` workflow shows as failed (❌). If this is the case this is likely because the repository has been set by default to only allow GitHub Actions workflows read permissions on the repository, which prevents the step in the `Documentation` workflow which pushes the built HTML documentation to a branch `gh-pages` from completing. In the next section we will look at how to configure the repository to ensure the permissions are set correctly and have the HTML documentation deployed to a GitHub Pages website.
187187

@@ -196,27 +196,27 @@ GitHub Pages website. To allow the GitHub Actions bot to push to the gh-pages
196196
branch you need to enable 'Read and write permissions' under 'Workflow
197197
permissions' at
198198

199-
https://github.com/{github_user}/{project_slug}/settings/actions
199+
https://github.com/{github_owner}/{project_slug}/settings/actions
200200

201201
After the 'Documentation' workflow has successfully completed at least once
202202
you will also need to configure the repository to deploy a GitHub pages site
203203
from the content on the gh-pages branch by going to
204204

205-
https://github.com/{github_user}/{project_slug}/settings/pages
205+
https://github.com/{github_owner}/{project_slug}/settings/pages
206206

207207
and under 'Built and deployment' selecting 'Deploy from a branch' for the
208208
'Source' drop-down and 'gh-pages' for the 'Branch' drop-down, leaving the
209209
branch path drop-down with its default value of '/ (root)'.
210210
```
211211

212-
where as before `{github_user}` and `{project_slug}` are replaced with the relevant GitHub user name and project slug that you entered when setting up the package.
212+
where as before `{github_owner}` and `{project_slug}` are replaced with the relevant GitHub repository owner and project slug that you entered when setting up the package.
213213

214-
The first part of the instructions gives details of how to set the permissions for GitHub Actions workflows in the repository to allow the GitHub Actions bot to push the built HTML documentation to a branch `gh-pages` on updates to the default `main` branch on the repository, so that the documentation is automatically updated whenever changes are merged in to `main`. After going to the Actions settings page at the URL indicated (which can also be reached by going to the `Settings` tab in the top navigation bar of the repository and then selecting `Action > General` in the left hand navigation menu) and ensuring `Workflow permissions` is set to allow `Read and write permissions`, if you have changed the permissions you will also need to re-run the documentation workflow with the updated permissions. This can be done by going to the Actions pane in the repository (accessible from top navigation bar or by going to `https://github.com/{github_user}/{project_slug}/actions`), clicking on the (top / latest if there are multiple) entry for the _Documentation_ workflow, which should be showing as failing, and then from the resulting page click the `Re-run all jobs` button.
214+
The first part of the instructions gives details of how to set the permissions for GitHub Actions workflows in the repository to allow the GitHub Actions bot to push the built HTML documentation to a branch `gh-pages` on updates to the default `main` branch on the repository, so that the documentation is automatically updated whenever changes are merged in to `main`. After going to the Actions settings page at the URL indicated (which can also be reached by going to the `Settings` tab in the top navigation bar of the repository and then selecting `Action > General` in the left hand navigation menu) and ensuring `Workflow permissions` is set to allow `Read and write permissions`, if you have changed the permissions you will also need to re-run the documentation workflow with the updated permissions. This can be done by going to the Actions pane in the repository (accessible from top navigation bar or by going to `https://github.com/{github_owner}/{project_slug}/actions`), clicking on the (top / latest if there are multiple) entry for the _Documentation_ workflow, which should be showing as failing, and then from the resulting page click the `Re-run all jobs` button.
215215

216-
Once the _Documentation_ workflow has successfully completed, a new branch `gh-pages` should have been created in the repository containing the HTML documentation. The second part of the instructions printed in the message output by the `cookiecutter` command indicate to go to a URL `https://github.com/{github_user}/{project_slug}/settings/pages` to set this branch as the source for a GitHub Pages website for the repository. If you now follow those instructions, setting the `Source` to `Deploy from branch` and `Branch` to `gh-pages`, the a new Actions workflow `pages-build-deployment` will be automatically triggered. Once this workflow has completed, you should be able to view the HTML documentation for the repository at a URL
216+
Once the _Documentation_ workflow has successfully completed, a new branch `gh-pages` should have been created in the repository containing the HTML documentation. The second part of the instructions printed in the message output by the `cookiecutter` command indicate to go to a URL `https://github.com/{github_owner}/{project_slug}/settings/pages` to set this branch as the source for a GitHub Pages website for the repository. If you now follow those instructions, setting the `Source` to `Deploy from branch` and `Branch` to `gh-pages`, the a new Actions workflow `pages-build-deployment` will be automatically triggered. Once this workflow has completed, you should be able to view the HTML documentation for the repository at a URL
217217

218218
```sh
219-
https://{github_user}.github.io/{project_slug}
219+
https://{github_owner}.github.io/{project_slug}
220220
```
221221

222222
The index page of the documentation reproduces the content from the repository README file. The documentation site also importantly includes _application programming interface_ (API) reference documentation built from the [docstrings](https://peps.python.org/pep-0257/#what-is-a-docstring) in the package source code; this API documentation is accessible from the `API reference` link in the navigation.

{{cookiecutter.project_slug}}/CITATION.cff

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ authors:
55
- family-names: "{{cookiecutter.author_family_names }}"
66
given-names: "{{ cookiecutter.author_given_names }}"
77
email: "{{ cookiecutter.author_email }}"
8-
repository-code: "https://github.com/{{ cookiecutter.github_username }}/{{ cookiecutter.project_slug }}"
8+
repository-code: "{{ cookiecutter.__repo_url }}"
99
title: "{{ cookiecutter.project_name }}: {{ cookiecutter.project_short_description }}"
1010
license: "{{ cookiecutter.license }}"

{{cookiecutter.project_slug}}/README.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
[![License][license-badge]](./LICENSE.md)
88

99
<!-- prettier-ignore-start -->
10-
[tests-badge]: https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}/actions/workflows/tests.yml/badge.svg
11-
[tests-link]: https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}/actions/workflows/tests.yml
12-
[linting-badge]: https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}/actions/workflows/linting.yml/badge.svg
13-
[linting-link]: https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}/actions/workflows/linting.yml
14-
[documentation-badge]: https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}/actions/workflows/docs.yml/badge.svg
15-
[documentation-link]: https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}/actions/workflows/docs.yml
10+
[tests-badge]: {{cookiecutter.__repo_url}}/actions/workflows/tests.yml/badge.svg
11+
[tests-link]: {{cookiecutter.__repo_url}}/actions/workflows/tests.yml
12+
[linting-badge]: {{cookiecutter.__repo_url}}/actions/workflows/linting.yml/badge.svg
13+
[linting-link]: {{cookiecutter.__repo_url}}/actions/workflows/linting.yml
14+
[documentation-badge]: {{cookiecutter.__repo_url}}/actions/workflows/docs.yml/badge.svg
15+
[documentation-link]: {{cookiecutter.__repo_url}}/actions/workflows/docs.yml
1616
{%- if cookiecutter.license == "MIT" %}
1717
[license-badge]: https://img.shields.io/badge/License-MIT-yellow.svg
1818
{%- elif cookiecutter.license == "BSD-3" %}
@@ -68,13 +68,13 @@ development version of `{{cookiecutter.project_slug}}` using `pip` in the curren
6868
environment run
6969

7070
```sh
71-
pip install git+https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}.git
71+
pip install git+{{cookiecutter.__repo_url}}.git
7272
```
7373

7474
Alternatively create a local clone of the repository with
7575

7676
```sh
77-
git clone https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}.git
77+
git clone {{cookiecutter.__repo_url}}.git
7878
```
7979

8080
and then install in editable mode by running

{{cookiecutter.project_slug}}/mkdocs.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ site_name: "{{cookiecutter.project_name}}"
22
site_description: "Documentation website for {{cookiecutter.project_name}}"
33
site_author: "{{cookiecutter.author_given_names}} {{cookiecutter.author_family_names}}"
44
copyright: "Copyright © {% now 'utc', '%Y' %} {{cookiecutter.author_given_names}} {{cookiecutter.author_family_names}}"
5-
repo_url: "https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}/"
6-
repo_name: "{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}"
5+
repo_url: "{{cookiecutter.__repo_url}}/"
6+
repo_name: "{{cookiecutter.__repo_name}}"
77
edit_uri: edit/main/docs/
88

99
validation:
@@ -60,4 +60,4 @@ plugins:
6060
extra:
6161
social:
6262
- icon: fontawesome/brands/github
63-
link: "https://github.com/{{cookiecutter.github_username}}"
63+
link: "https://github.com/{{cookiecutter.github_owner}}"

{{cookiecutter.project_slug}}/pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ optional-dependencies = {dev = [
5050
readme = "README.md"
5151
requires-python = ">={{cookiecutter.min_python_version}}"
5252
license.file = "LICENSE.md"
53-
urls.homepage = "https://github.com/{{cookiecutter.github_username}}/{{cookiecutter.project_slug}}"
53+
urls.homepage = "{{cookiecutter.__repo_url}}"
5454

5555
[tool.coverage]
5656
report = {sort = "cover"}

0 commit comments

Comments
 (0)