make git repo script more concise and work with default home
parent
93d0c87b30
commit
b3b2adf301
|
@ -0,0 +1,10 @@
|
|||
#!/usr/bin/python3
|
||||
|
||||
# This script is used for initializing a CLion C++ Project
|
||||
|
||||
import requests
|
||||
import json
|
||||
import argparse
|
||||
import os
|
||||
import create_git_repo as repo
|
||||
|
|
@ -4,23 +4,26 @@ import requests
|
|||
import json
|
||||
import argparse
|
||||
import os
|
||||
from pathlib import Path
|
||||
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='Repo Creator',
|
||||
description='Create Git Repos on Gitea and Github',
|
||||
epilog='Hello')
|
||||
class EnvData:
|
||||
def __init__(self, github_token = '', gitea_token = ''):
|
||||
self.github_token = github_token
|
||||
self.gitea_token = gitea_token
|
||||
|
||||
parser.add_argument('repo_name')
|
||||
parser.add_argument('-e', help="environment file", required=False, default="~/.brett_scripts.env")
|
||||
parser.add_argument('-d', '--description', default = "")
|
||||
class RepoData:
|
||||
def __init__(self, repo_name, description, env: EnvData, github_username = "tri11paragon", gitea_username = "tri11paragon", gitea_url = "https://git.tpgc.me"):
|
||||
self.repo_name = repo_name
|
||||
self.description = description
|
||||
self.env = env
|
||||
self.github_username = github_username
|
||||
self.gitea_username = gitea_username
|
||||
self.gitea_url = gitea_url
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
github_token = ''
|
||||
gitea_token = ''
|
||||
|
||||
if (args.e is not None) and not (args.e == 'env'):
|
||||
f = open(args.e, "rt")
|
||||
def get_env_from_file(file):
|
||||
github_token = ''
|
||||
gitea_token = ''
|
||||
f = open(file, "rt")
|
||||
values = {}
|
||||
for line in f:
|
||||
if line.startswith("export"):
|
||||
|
@ -36,49 +39,40 @@ if (args.e is not None) and not (args.e == 'env'):
|
|||
gitea_token = values["gitea_token"]
|
||||
except:
|
||||
print("Failed to parse gitea token!")
|
||||
else:
|
||||
github_token = os.environ["github_token"]
|
||||
gitea_token = os.environ["gitea_token"]
|
||||
return EnvData(github_token=github_token, gitea_token=gitea_token)
|
||||
|
||||
# GitHub credentials
|
||||
github_username = 'tri11paragon'
|
||||
def get_env_from_os():
|
||||
return EnvData(github_token=os.environ["github_token"], gitea_token=os.environ["gitea_token"])
|
||||
|
||||
# Gitea credentials
|
||||
gitea_username = 'tri11paragon'
|
||||
gitea_url = 'https://git.tpgc.me'
|
||||
|
||||
# Repository name
|
||||
repo_name = args.repo_name
|
||||
|
||||
# GitHub API endpoint for creating a repository
|
||||
github_url = f'https://api.github.com/user/repos'
|
||||
github_payload = {
|
||||
'name': repo_name,
|
||||
'description': args.description,
|
||||
def create_repo(data: RepoData):
|
||||
# GitHub API endpoint for creating a repository
|
||||
github_url = f'https://api.github.com/user/repos'
|
||||
github_payload = {
|
||||
'name': data.repo_name,
|
||||
'description': data.description,
|
||||
'private': False # Set to True if you want a private repository
|
||||
}
|
||||
github_headers = {
|
||||
'Authorization': f'token {github_token}',
|
||||
}
|
||||
github_headers = {
|
||||
'Authorization': f'token {data.env.github_token}',
|
||||
'Accept': 'application/vnd.github.v3+json'
|
||||
}
|
||||
}
|
||||
|
||||
# Create repository on GitHub
|
||||
response = requests.post(github_url, json=github_payload, headers=github_headers)
|
||||
if response.status_code == 201:
|
||||
print(f"GitHub repository '{repo_name}' created successfully.")
|
||||
else:
|
||||
# Create repository on GitHub
|
||||
response = requests.post(github_url, json=github_payload, headers=github_headers)
|
||||
if response.status_code == 201:
|
||||
print(f"GitHub repository '{data.repo_name}' created successfully.")
|
||||
else:
|
||||
print(f"Failed to create GitHub repository. Status code: {response.status_code}")
|
||||
print(response.text)
|
||||
#exit()
|
||||
return False
|
||||
|
||||
# Gitea API endpoint for creating a repository
|
||||
#gitea_url = f'{gitea_url}/api/v1/user/repos'
|
||||
gitea_url = f'{gitea_url}/api/v1/repos/migrate'
|
||||
gitea_payload = {
|
||||
'auth_token': github_token,
|
||||
'auth_username': github_username,
|
||||
'clone_addr': f'https://github.com/{github_username}/{repo_name}.git',
|
||||
'description': args.description,
|
||||
# Gitea API endpoint for creating a repository
|
||||
gitea_url = f'{data.gitea_url}/api/v1/repos/migrate'
|
||||
gitea_payload = {
|
||||
'auth_token': data.env.github_token,
|
||||
'auth_username': data.github_username,
|
||||
'clone_addr': f'https://github.com/{data.github_username}/{data.repo_name}.git',
|
||||
'description': data.description,
|
||||
"issues": True,
|
||||
"labels": True,
|
||||
"lfs": False,
|
||||
|
@ -88,151 +82,42 @@ gitea_payload = {
|
|||
"releases": True,
|
||||
"service": "github",
|
||||
"wiki": True,
|
||||
'repo_name': repo_name,
|
||||
'repo_owner': gitea_username,
|
||||
'repo_name': data.repo_name,
|
||||
'repo_owner': data.gitea_username,
|
||||
'private': False # Set to True if you want a private repository
|
||||
}
|
||||
gitea_headers = {
|
||||
'Authorization': f'token {gitea_token}',
|
||||
}
|
||||
gitea_headers = {
|
||||
'Authorization': f'token {data.env.gitea_token}',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
}
|
||||
}
|
||||
|
||||
# Create repository on Gitea
|
||||
response = requests.post(gitea_url, json=gitea_payload, headers=gitea_headers)
|
||||
if response.status_code == 201:
|
||||
print(f"Gitea repository '{repo_name}' created successfully.")
|
||||
else:
|
||||
# Create repository on Gitea
|
||||
response = requests.post(gitea_url, json=gitea_payload, headers=gitea_headers)
|
||||
if response.status_code == 201:
|
||||
print(f"Gitea repository '{data.repo_name}' created successfully.")
|
||||
return True
|
||||
else:
|
||||
print(f"Failed to create Gitea repository. Status code: {response.status_code}")
|
||||
print(response.text)
|
||||
#exit()
|
||||
return False
|
||||
|
||||
# gitea_url_add_mirror = f'{gitea_url}/api/v1/repos/{gitea_username}/{repo_name}/mirror'
|
||||
if __name__ == "__main__":
|
||||
parser = argparse.ArgumentParser(
|
||||
prog='Repo Creator',
|
||||
description='Create Git Repos on Gitea and Github',
|
||||
epilog='Hello')
|
||||
parser.add_argument('repo_name')
|
||||
parser.add_argument('-e', help="environment file", required=False, default=str(Path.home() / ".brett_scripts.env"))
|
||||
parser.add_argument('-d', '--description', default = "")
|
||||
|
||||
# print(gitea_url_add_mirror)
|
||||
args = parser.parse_args()
|
||||
|
||||
# # Payload for adding a mirror
|
||||
# gitea_payload_add_mirror = {
|
||||
# 'name': 'GitHub Mirror',
|
||||
# 'url': f'https://github.com/{github_username}/{repo_name}.git',
|
||||
# 'repo_user': github_username,
|
||||
# 'repo_passwd': github_token,
|
||||
# 'interval': '1h', # Adjust this as needed, e.g., '1h' for every hour
|
||||
# 'external': True,
|
||||
# }
|
||||
env = EnvData('', '')
|
||||
if (args.e is not None) and not (args.e == 'env'):
|
||||
env = get_env_from_file(args.e)
|
||||
else:
|
||||
env = get_env_from_os()
|
||||
|
||||
# # Add mirror on Gitea
|
||||
# response = requests.post(gitea_url_add_mirror, json=gitea_payload_add_mirror, headers=gitea_headers)
|
||||
|
||||
# # Check if the mirror was set up successfully
|
||||
# if response.status_code == 200:
|
||||
# print(f"Gitea repository '{repo_name}' set to mirror GitHub repository successfully.")
|
||||
# else:
|
||||
# print(f"Failed to set up mirror on Gitea repository. Status code: {response.status_code}")
|
||||
# print(response.text)
|
||||
|
||||
# # Set the repository name
|
||||
# repo_name = args.repo_name
|
||||
|
||||
# # GitHub API endpoint for creating a repository
|
||||
# github_url = f'https://api.github.com/user/repos'
|
||||
|
||||
# # JSON payload for the GitHub repository creation request
|
||||
# github_payload = {
|
||||
# 'name': repo_name,
|
||||
# 'private': False, # Set to True if you want a private repository
|
||||
# # You can add more options like description, homepage, etc. if needed
|
||||
# }
|
||||
|
||||
# # Headers containing the GitHub authorization token
|
||||
# github_headers = {
|
||||
# 'Authorization': f'token {github_token}',
|
||||
# 'Accept': 'application/vnd.github.v3+json'
|
||||
# }
|
||||
|
||||
# # Make POST request to create the GitHub repository
|
||||
# response = requests.post(github_url, json=github_payload, headers=github_headers)
|
||||
|
||||
# # Check if the GitHub repository was created successfully
|
||||
# if response.status_code == 201:
|
||||
# print(f"GitHub repository '{repo_name}' created successfully.")
|
||||
# else:
|
||||
# print(f"Failed to create GitHub repository. Status code: {response.status_code}")
|
||||
# print(response.text)
|
||||
# exit()
|
||||
|
||||
# # Gitea API endpoint for creating a repository
|
||||
# gitea_url = f'{gitea_url}/api/v1/user/repos'
|
||||
|
||||
# # JSON payload for the Gitea repository creation request
|
||||
# gitea_payload = {
|
||||
# 'name': repo_name,
|
||||
# 'private': False, # Set to True if you want a private repository
|
||||
# # You can add more options like description, homepage, etc. if needed
|
||||
# }
|
||||
|
||||
# # Headers containing the Gitea authorization token
|
||||
# gitea_headers = {
|
||||
# 'Authorization': f'token {gitea_token}',
|
||||
# 'Accept': 'application/json',
|
||||
# 'Content-Type': 'application/json'
|
||||
# }
|
||||
|
||||
# # Make POST request to create the Gitea repository
|
||||
# response = requests.post(gitea_url, json=gitea_payload, headers=gitea_headers)
|
||||
|
||||
# # Check if the Gitea repository was created successfully
|
||||
# if response.status_code == 201:
|
||||
# print(f"Gitea repository '{repo_name}' created successfully.")
|
||||
# else:
|
||||
# print(f"Failed to create Gitea repository. Status code: {response.status_code}")
|
||||
# print(response.text)
|
||||
# exit()
|
||||
|
||||
# # Set up Gitea repository to push commits to GitHub repository
|
||||
# gitea_repo_full_name = f'{gitea_username}/{repo_name}'
|
||||
# gitea_repo_url = f'{gitea_url}/{gitea_repo_full_name}/git/mirror'
|
||||
# gitea_mirror_payload = {
|
||||
# 'repo_name': f'{github_username}/{repo_name}',
|
||||
# 'repo_user': github_username,
|
||||
# 'repo_pass': github_token,
|
||||
# 'mirror_interval': '1h', # You can adjust this interval as needed
|
||||
# }
|
||||
# response = requests.post(gitea_repo_url, json=gitea_mirror_payload, headers=gitea_headers)
|
||||
|
||||
# # Check if the mirror was set up successfully
|
||||
# if response.status_code == 201:
|
||||
# print(f"Gitea repository '{repo_name}' set to mirror commits to GitHub repository successfully.")
|
||||
# else:
|
||||
# print(f"Failed to set up mirror on Gitea repository. Status code: {response.status_code}")
|
||||
# print(response.text)
|
||||
|
||||
# # Set the repository name
|
||||
# repo_name = args.repo_name
|
||||
|
||||
# # API endpoint for creating a repository
|
||||
# url = f'https://api.github.com/user/repos'
|
||||
|
||||
# # JSON payload for the repository creation request
|
||||
# payload = {
|
||||
# 'name': repo_name,
|
||||
# 'private': False, # Set to True if you want a private repository
|
||||
# # You can add more options like description, homepage, etc. if needed
|
||||
# }
|
||||
|
||||
# # Headers containing the authorization token
|
||||
# headers = {
|
||||
# 'Authorization': f'token {token}',
|
||||
# 'Accept': 'application/vnd.github.v3+json'
|
||||
# }
|
||||
|
||||
# # Make POST request to create the repository
|
||||
# response = requests.post(url, json=payload, headers=headers)
|
||||
|
||||
# # Check if the repository was created successfully
|
||||
# if response.status_code == 201:
|
||||
# print(f"Repository '{repo_name}' created successfully.")
|
||||
# else:
|
||||
# print(f"Failed to create repository. Status code: {response.status_code}")
|
||||
# print(response.text)
|
||||
create_repo(RepoData(repo_name=args.repo_name, description=args.description, env=env))
|
||||
|
Loading…
Reference in New Issue