|
1 | 1 | #!/usr/bin/env python |
2 | 2 |
|
3 | | -import argparse |
4 | | -import os |
5 | | -import http.client |
6 | | -import json |
7 | | -from github_secrets import GITHUB_API_TOKEN, USERNAME |
| 3 | +''' |
| 4 | +This script is used to create a local repository linked with a remote repository. |
| 5 | +''' |
8 | 6 |
|
| 7 | +# Imports the required modules |
| 8 | +import argparse # required for parsing the command line arguments passed to the script |
| 9 | +import os # required for creating the directory and changing the directory and running the git commands # noqa: E501 |
| 10 | +import http.client # required for making the http request |
| 11 | +import json # required for parsing the response received from the http request |
| 12 | +from github_secrets import GITHUB_API_TOKEN, USERNAME # required for authenticating the http request and setting the user agent # noqa: E501 |
| 13 | + |
| 14 | + |
| 15 | +# Checks if the environment variables are set or not. If not, raises an error as ValueError. # noqa: E501 |
| 16 | +# Environment variables are set in the github_secrets.py file which contains the GITHUB_API_TOKEN and USERNAME # noqa: E501 |
| 17 | +# The GITHUB_API_TOKEN is the personal access token generated from the GitHub account. # noqa: E501 |
9 | 18 | if not GITHUB_API_TOKEN: |
10 | 19 | raise ValueError("Please set the environment variable GITHUB_API_TOKEN in the github_secrets.py file") # noqa: E501 |
11 | 20 |
|
| 21 | +# Base URL for the GitHub API which is used to create a new repository # noqa: E501 |
12 | 22 | URL = "https://api.github.com/user/repos" |
13 | 23 |
|
| 24 | +# Creates a parser object of the argparse class |
| 25 | +# The parser object is used to parse the command line arguments passed to the script # noqa: E501 |
| 26 | +# The parser object is used to create the help text for the script # noqa: E501 |
14 | 27 | parser = argparse.ArgumentParser(description='creates a local repository linked with a remote repository') # noqa: E501 |
15 | 28 |
|
16 | | -parser.add_argument('path', |
| 29 | +# Adds the arguments to the parser object |
| 30 | +parser.add_argument('path', # This argument can be accessed using the `path` variable # noqa: E501 |
17 | 31 | metavar='PATH', |
18 | 32 | type=str, |
19 | 33 | help='Enter the path for the new repository') |
20 | | -parser.add_argument('name', |
| 34 | +parser.add_argument('name', # This argument can be accessed using the `name` variable # noqa: E501 |
21 | 35 | metavar='NAME', |
22 | 36 | type=str, |
23 | 37 | help='Enter a name for the new repository') |
24 | | -args = parser.parse_args() |
| 38 | +args = parser.parse_args() # parses the arguments passed to the script. The arguments are stored in the `args` variable # noqa: E501 |
| 39 | + |
| 40 | +name = args.name # stores the name of the repository from `args` in the name variable |
| 41 | +path = args.path # stores the path of the repository from `args` in the path variable |
25 | 42 |
|
26 | | -name = args.name |
27 | | -path = args.path |
28 | 43 |
|
29 | | -os.chdir(path) |
30 | | -os.mkdir(name) |
31 | | -os.chdir(name) |
32 | | -os.system('git init -b main') |
33 | | -os.system('touch README.md') |
34 | | -os.system('git add . && git commit -m "initial commit"') |
35 | | -# os.system('git status') |
| 44 | +# The following codes creates a new directory with the name of the repository and initializes it with git using the `os` module # noqa: E501 |
| 45 | +os.chdir(path) # changes the directory to the path specified in the `path` variable # noqa: E501 |
| 46 | +os.mkdir(name) # creates a new directory with the name specified in the `name` variable # noqa: E501 |
| 47 | +os.chdir(name) # changes the directory to the newly created directory # noqa: E501 |
| 48 | +os.system('git init -b main') # This executes 'git init -b main' as a system command as if it were written in git bash. It initializes the directory with git and sets the default branch to `main` # noqa: E501 |
| 49 | +os.system('touch README.md') # creates a README.md file # noqa: E501 |
| 50 | +os.system('git add . && git commit -m "initial commit"') # adds the newly created README.md file to the staging area and commits it with the message# noqa: E501 |
36 | 51 |
|
37 | | -conn = http.client.HTTPSConnection("api.github.com") |
| 52 | + |
| 53 | +# The following code makes a POST request to the GitHub API to create a new repository # noqa: E501 |
| 54 | +conn = http.client.HTTPSConnection("api.github.com") # creates a connection object |
| 55 | +# The payload is the data that is sent |
38 | 56 | payload = json.dumps({ |
39 | 57 | "name": name, |
40 | 58 | "description": "made with the GitHub API" |
41 | 59 | }) |
| 60 | + |
| 61 | +# Metadata that is sent along with the request |
| 62 | +# The metadata contains the authorization token, the content type and the user agent # noqa: E501 |
42 | 63 | headers = { |
43 | 64 | 'Authorization': f'Bearer {GITHUB_API_TOKEN}', |
44 | 65 | 'Content-Type': 'application/json', |
45 | 66 | 'User-Agent': f'{USERNAME}' |
46 | 67 | } |
47 | 68 |
|
| 69 | +# The request is made to the URL with the payload and the headers |
| 70 | +# The response received is stored in the `res` variable |
| 71 | +# The response is read and decoded using utf-8 encoding and stored in the `data` variable |
| 72 | +# The `data` variable is parsed using the json module and stored in the `response` variable # noqa: E501 |
| 73 | +# The `response` variable contains the response received from the GitHub API, which is a JSON object # noqa: E501 |
48 | 74 | conn.request("POST", "/user/repos", payload, headers) |
49 | 75 | res = conn.getresponse() |
50 | 76 | data = res.read().decode("utf-8") |
51 | 77 | response = json.loads(data) |
52 | 78 |
|
53 | 79 | print(response) |
54 | | -remote_url = response['svn_url'] |
| 80 | +remote_url = response['svn_url'] # stores the remoteurl using the key `svn_url` in the `response` variable # noqa: E501 |
55 | 81 |
|
56 | | -os.system(f'git remote add origin {remote_url}') |
57 | | -os.system('git push origin main') |
58 | | -print(f"\nREMOTE URL FOR \"{name}\" is: {remote_url}") |
| 82 | +# Runs the git commands as system commands # noqa: E501 |
| 83 | +os.system(f'git remote add origin {remote_url}') # adds the remote url to the local repository |
| 84 | +os.system('git push origin main') # Pushes the local repository to the remote repository |
| 85 | +print(f"\nREMOTE URL FOR \"{name}\" is: {remote_url}") # Prints the remote url |
0 commit comments