Skip to content

Commit afc4fa3

Browse files
2 parents 0078986 + 0943e7b commit afc4fa3

File tree

3 files changed

+171
-22
lines changed

3 files changed

+171
-22
lines changed

.github/workflows/codeql.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL"
13+
14+
on:
15+
push:
16+
branches: [ "main" ]
17+
pull_request:
18+
# The branches below must be a subset of the branches above
19+
branches: [ "main" ]
20+
schedule:
21+
- cron: '43 3 * * 6'
22+
23+
jobs:
24+
analyze:
25+
name: Analyze
26+
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
27+
timeout-minutes: ${{ (matrix.language == 'swift' && 120) || 360 }}
28+
permissions:
29+
actions: read
30+
contents: read
31+
security-events: write
32+
33+
strategy:
34+
fail-fast: false
35+
matrix:
36+
language: [ 'python' ]
37+
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby', 'swift' ]
38+
# Use only 'java' to analyze code written in Java, Kotlin or both
39+
# Use only 'javascript' to analyze code written in JavaScript, TypeScript or both
40+
# Learn more about CodeQL language support at https://aka.ms/codeql-docs/language-support
41+
42+
steps:
43+
- name: Checkout repository
44+
uses: actions/checkout@v3
45+
46+
# Initializes the CodeQL tools for scanning.
47+
- name: Initialize CodeQL
48+
uses: github/codeql-action/init@v2
49+
with:
50+
languages: ${{ matrix.language }}
51+
# If you wish to specify custom queries, you can do so here or in a config file.
52+
# By default, queries listed here will override any specified in a config file.
53+
# Prefix the list here with "+" to use these queries and those in the config file.
54+
55+
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
56+
# queries: security-extended,security-and-quality
57+
58+
59+
# Autobuild attempts to build any compiled languages (C/C++, C#, Go, Java, or Swift).
60+
# If this step fails, then you should remove it and run the build manually (see below)
61+
- name: Autobuild
62+
uses: github/codeql-action/autobuild@v2
63+
64+
# ℹ️ Command-line programs to run using the OS shell.
65+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
66+
67+
# If the Autobuild fails above, remove it and uncomment the following three lines.
68+
# modify them (or add more) to build your code if your project, please refer to the EXAMPLE below for guidance.
69+
70+
# - run: |
71+
# echo "Run, Build Application using script"
72+
# ./location_of_script_within_repo/buildscript.sh
73+
74+
- name: Perform CodeQL Analysis
75+
uses: github/codeql-action/analyze@v2
76+
with:
77+
category: "/language:${{matrix.language}}"

scripts/create_repo.py

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,85 @@
11
#!/usr/bin/env python
22

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+
'''
86

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
918
if not GITHUB_API_TOKEN:
1019
raise ValueError("Please set the environment variable GITHUB_API_TOKEN in the github_secrets.py file") # noqa: E501
1120

21+
# Base URL for the GitHub API which is used to create a new repository # noqa: E501
1222
URL = "https://api.github.com/user/repos"
1323

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
1427
parser = argparse.ArgumentParser(description='creates a local repository linked with a remote repository') # noqa: E501
1528

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
1731
metavar='PATH',
1832
type=str,
1933
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
2135
metavar='NAME',
2236
type=str,
2337
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
2542

26-
name = args.name
27-
path = args.path
2843

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
3651

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
3856
payload = json.dumps({
3957
"name": name,
4058
"description": "made with the GitHub API"
4159
})
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
4263
headers = {
4364
'Authorization': f'Bearer {GITHUB_API_TOKEN}',
4465
'Content-Type': 'application/json',
4566
'User-Agent': f'{USERNAME}'
4667
}
4768

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
4874
conn.request("POST", "/user/repos", payload, headers)
4975
res = conn.getresponse()
5076
data = res.read().decode("utf-8")
5177
response = json.loads(data)
5278

5379
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
5581

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

scripts/push_repo.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env python
2+
3+
import argparse
4+
import os
5+
from github_secrets import GITHUB_API_TOKEN
6+
7+
8+
def parseArgs():
9+
parser = argparse.ArgumentParser(description='Push an existing local repository to a newly created remote repository of the same name') # noqa: E501
10+
parser.add_argument('path',
11+
metavar='PATH',
12+
type=str,
13+
help='Enter the path for existing local repository')
14+
parser.add_argument('url',
15+
metavar='URL',
16+
type=str,
17+
help='Enter the newly created remote repository url (.git)')
18+
parser.add_argument('description',
19+
metavar='DESCRIPTION',
20+
type=str,
21+
help='Enter the description for remote repository')
22+
args = parser.parse_args()
23+
return args
24+
25+
26+
def pushRepo(remote_url):
27+
origin = remote_url[:8] + GITHUB_API_TOKEN + "@" + remote_url[8:]
28+
os.system(f'git push {origin} --mirror')
29+
30+
31+
def main():
32+
path = args.path
33+
remote_url = args.url
34+
35+
os.chdir(path)
36+
if os.path.isdir('./.git') is False:
37+
print("Not in a git directory")
38+
exit()
39+
40+
pushRepo(remote_url)
41+
42+
43+
if __name__ == "__main__":
44+
args = parseArgs()
45+
main()

0 commit comments

Comments
 (0)