Skip to content

Commit 95e85e5

Browse files
[hp-populate-remote] Implement hands on hp-populate-remote (#78)
## Exercise Discussion This PR will close #56. Below is a more detailed description of the changes along with implementation notes: 1. Added an `add_origin` function in `exercise_utils/git.py`. This might be helpful in the future. I noticed that there is already a `remove_remote` implementation, and `add_origin` is intended to be used in a similar way. 2. To help ensure that the remote repository `gitmastery-things` is created successfully across multiple attempts, I added a check to delete any existing remote repository with the same name. Please note that this requires setting up delete access for the GitHub CLI via `gh auth refresh -h github.com -s delete_repo`. I also realised that the GitHub CLI needs to be configured beforehand for this part of the hands-on to work. **Note:** The download script tested via `test-download.sh` works fine. [Here](https://github.com/oadultradeepfield/gitmastery-things) is the remote repo I managed to create. ## Checklist - [ ] If you require a new remote repository on the `Git-Mastery` organization, have you [created a request](https://github.com/git-mastery/exercises/issues/new?template=request_exercise_repository.md) for it? - [ ] Have you written unit tests using [`repo-smith`](https://github.com/git-mastery/repo-smith) to validate the exercise grading scheme? - [X] Have you tested the download script using `test-download.sh`? - [X] Have you verified that this exercise does not already exist or is not currently in review? - [ ] Did you introduce a new grading mechanism that should belong to [`git-autograder`](https://github.com/git-mastery/git-autograder)? - [ ] Did you introduce a new dependency that should belong to [`app`](https://github.com/git-mastery/app)?
1 parent 3623032 commit 95e85e5

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

hands_on/populate_remote.py

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
3+
from exercise_utils.file import append_to_file, create_or_update_file
4+
from exercise_utils.git import add, add_remote, commit, init
5+
from exercise_utils.github_cli import (
6+
create_repo,
7+
delete_repo,
8+
get_github_username,
9+
has_repo,
10+
)
11+
12+
__requires_git__ = True
13+
__requires_github__ = True
14+
15+
REPO_NAME = "gitmastery-things"
16+
17+
18+
def download(verbose: bool):
19+
_setup_local_repository(verbose)
20+
_create_things_repository(verbose)
21+
_link_repositories(verbose)
22+
23+
24+
def _setup_local_repository(verbose: bool):
25+
os.makedirs("things")
26+
os.chdir("things")
27+
init(verbose)
28+
29+
create_or_update_file(
30+
"fruits.txt",
31+
"""
32+
apples
33+
bananas
34+
cherries
35+
dragon fruits
36+
""",
37+
)
38+
add(["fruits.txt"], verbose)
39+
40+
append_to_file("fruits.txt", "figs")
41+
add(["fruits.txt"], verbose)
42+
commit("Insert figs into fruits.txt", verbose)
43+
44+
create_or_update_file("colours.txt", "a file for colours")
45+
create_or_update_file("shapes.txt", "a file for shapes")
46+
add(["colours.txt", "shapes.txt"], verbose)
47+
commit("Add colours.txt, shapes.txt", verbose)
48+
49+
50+
def _create_things_repository(verbose: bool):
51+
"""Create the gitmastery-things repository, deleting any existing ones."""
52+
full_repo_name = _get_full_repo_name(verbose)
53+
54+
if has_repo(full_repo_name, False, verbose):
55+
delete_repo(full_repo_name, verbose)
56+
57+
create_repo(REPO_NAME, verbose)
58+
59+
60+
def _link_repositories(verbose: bool):
61+
full_repo_name = _get_full_repo_name(verbose)
62+
add_remote("origin", f"https://github.com/{full_repo_name}", verbose)
63+
64+
65+
def _get_full_repo_name(verbose: bool) -> str:
66+
username = get_github_username(verbose)
67+
return f"{username}/{REPO_NAME}"

0 commit comments

Comments
 (0)