mirror of
https://github.com/ChristianLempa/scripts.git
synced 2026-11-11 07:32:43 +01:00
update python scripts to use pip libraries
This commit is contained in:
1 parent
32d1c9f9d1
commit
ae3c6ffed0
5 files changed
+41
-165
No files matched your search
@@ -3,7 +3,7 @@
|
||||
# Required parameters:
|
||||
# @raycast.schemaVersion 1
|
||||
# @raycast.title Check Repos for clcreative
|
||||
# @raycast.mode inline
|
||||
# @raycast.mode compact
|
||||
|
||||
# Optional parameters:
|
||||
# @raycast.icon https://avatars.githubusercontent.com/u/97734037?s=200&v=4
|
||||
@@ -15,12 +15,12 @@
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
from pathlib import Path
|
||||
from repos import getRepos
|
||||
from github import Github
|
||||
|
||||
|
||||
load_dotenv()
|
||||
|
||||
github_token = os.getenv("GITHUB_TOKEN")
|
||||
g = Github(os.getenv("GITHUB_TOKEN"))
|
||||
|
||||
ignored_folders = [
|
||||
'.git',
|
||||
@@ -31,53 +31,26 @@ ignored_folders = [
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
# Get all repos for an organization from GitHub
|
||||
# https://docs.github.com/en/rest/reference/repos#list-organization-repositories
|
||||
|
||||
repos = getRepos(github_token, 'clcreative')
|
||||
repos_need_changes = []
|
||||
repos_not_cloned = []
|
||||
repos = g.get_organization('clcreative').get_repos()
|
||||
missing_repos = []
|
||||
|
||||
if repos is not None:
|
||||
for repo in repos:
|
||||
# set repo path on local workstation
|
||||
repo_path = f'{ Path.home() }/projects/{repo["owner"]["login"]}/{repo["name"]}'
|
||||
repo_path = f'{ Path.home() }/projects/{repo.owner.login}/{repo.name}'
|
||||
|
||||
# when repo is existing
|
||||
if os.path.exists(repo_path):
|
||||
try:
|
||||
os.chdir(repo_path)
|
||||
# when repo is not existing
|
||||
if not os.path.exists(repo_path):
|
||||
missing_repos.append(repo.name)
|
||||
|
||||
# check if there are any changes
|
||||
# changes = os.system('git diff --exit-code --quiet')
|
||||
|
||||
# check if there are any changes to the repo, and if something needs to be pulled or committed
|
||||
changes = os.system('git diff --exit-code --quiet')
|
||||
|
||||
if changes != 0:
|
||||
# print(f" {repo['name']} - {repo['description']}")
|
||||
repos_need_changes.append(repo)
|
||||
else:
|
||||
pass
|
||||
# print(f" {repo['name']} - {repo['description']}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"error: {e}")
|
||||
else:
|
||||
# print(f" {repo['name']} - {repo['description']}")
|
||||
repos_not_cloned.append(repo)
|
||||
|
||||
# Check orphaned repos
|
||||
repos_orphaned = []
|
||||
|
||||
# read all folders in projects folder
|
||||
for item in os.listdir(f'{ Path.home() }/projects/clcreative'):
|
||||
# check if item is in repos list, and if item is not in ignored folders
|
||||
if item not in [repo['name'] for repo in repos] and item not in ignored_folders:
|
||||
repos_orphaned.append(item)
|
||||
|
||||
# Get Information about all Repos
|
||||
print(f"Repos: {len(repos)} \033[31m(u: {len(repos_need_changes)}, m: {len(repos_not_cloned)}, o: {len(repos_orphaned)})\033[0m")
|
||||
if len(missing_repos) > 0:
|
||||
print(f"You have {len(missing_repos)} missing repos. Consider cloning...")
|
||||
else:
|
||||
print("All repos are up to date.")
|
||||
else:
|
||||
print("Error: No repos found in organization")
|
||||
exit(1)
|
||||
|
||||
except Exception as e:
|
||||
print(f"error: {e}")
|
||||
print(f"{e.data['message']}")
|
||||
exit(1)
|
||||
@@ -3,7 +3,7 @@
|
||||
# Required parameters:
|
||||
# @raycast.schemaVersion 1
|
||||
# @raycast.title Clone Repos for clcreative
|
||||
# @raycast.mode inline
|
||||
# @raycast.mode compact
|
||||
|
||||
# Optional parameters:
|
||||
# @raycast.icon https://avatars.githubusercontent.com/u/97734037?s=200&v=4
|
||||
@@ -15,12 +15,13 @@
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
from pathlib import Path
|
||||
from repos import getRepos
|
||||
from github import Github
|
||||
from git import Repo
|
||||
|
||||
|
||||
load_dotenv()
|
||||
|
||||
github_token = os.getenv("GITHUB_TOKEN")
|
||||
g = Github(os.getenv("GITHUB_TOKEN"))
|
||||
|
||||
ignored_folders = [
|
||||
'.git',
|
||||
@@ -34,23 +35,28 @@ if __name__ == "__main__":
|
||||
# https://docs.github.com/en/rest/reference/repos#list-organization-repositories
|
||||
# --
|
||||
try:
|
||||
repos = getRepos(github_token, 'clcreative')
|
||||
|
||||
repos = g.get_organization('clcreative').get_repos()
|
||||
cloned_repos = []
|
||||
|
||||
if repos is not None:
|
||||
for repo in repos:
|
||||
# set repo path on local workstation
|
||||
repo_path = f'{ Path.home() }/projects/{repo["owner"]["login"]}/{repo["name"]}'
|
||||
repo_path = f'{ Path.home() }/projects/{repo.owner.login}/{repo.name}'
|
||||
|
||||
# when repo is not existing
|
||||
if not os.path.exists(repo_path):
|
||||
# print(f" {repo['name']} - {repo['description']}")
|
||||
os.system(f'git clone git@github.com:{repo["owner"]["login"]}/{repo["name"]}.git {repo_path}')
|
||||
cloned_repos.append(repo)
|
||||
Repo.clone_from(repo.ssh_url, repo_path)
|
||||
cloned_repos.append(repo.name)
|
||||
|
||||
# Show how many repos are cloned
|
||||
print(f"\033[32mcloned: {len(cloned_repos)}\033[0m")
|
||||
if len(cloned_repos) > 0:
|
||||
print(f"cloned {len(cloned_repos)} repos.")
|
||||
else:
|
||||
print("all repos are already cloned.")
|
||||
else:
|
||||
print("error: no repos found in organization")
|
||||
exit(1)
|
||||
|
||||
except Exception as e:
|
||||
print(f"error: {e}")
|
||||
print(f"{e.data['message']}")
|
||||
exit(1)
|
||||
@@ -14,12 +14,12 @@
|
||||
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
from repos import getRepos
|
||||
from github import Github
|
||||
|
||||
|
||||
load_dotenv()
|
||||
|
||||
github_token = os.getenv("GITHUB_TOKEN")
|
||||
g = Github(os.getenv("GITHUB_TOKEN"))
|
||||
|
||||
ignored_folders = [
|
||||
'.git',
|
||||
@@ -27,21 +27,14 @@ ignored_folders = [
|
||||
'.obsidian',
|
||||
]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
# List all repos for an organization from GitHub
|
||||
# https://docs.github.com/en/rest/reference/repos#list-organization-repositories
|
||||
repos = g.get_organization('clcreative').get_repos()
|
||||
|
||||
repos = getRepos(github_token, 'clcreative')
|
||||
if repos is not None:
|
||||
for repo in repos:
|
||||
print(f"{repo['name']}, {repo['owner']['login']}, {repo['description']}")
|
||||
else:
|
||||
raise Exception("repos is None")
|
||||
for repo in repos:
|
||||
print(repo.name)
|
||||
|
||||
# Sum all repos
|
||||
print(f"---\nTotal: {len(repos)}")
|
||||
print(f"Total repos: {repos.totalCount}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"error: {e}")
|
||||
@@ -1,49 +0,0 @@
|
||||
import requests
|
||||
|
||||
|
||||
def getReposFromPage(github_token: str, organization: str, page: int):
|
||||
# Get all repos from a specific page for an organization from GitHub.
|
||||
# https://docs.github.com/en/rest/reference/repos#list-organization-repositories
|
||||
try:
|
||||
url = f"https://api.github.com/orgs/{organization}/repos"
|
||||
payload = {}
|
||||
headers = {
|
||||
'Authorization': f'token {github_token}'
|
||||
}
|
||||
params = {
|
||||
'per_page': '100',
|
||||
'page': page,
|
||||
'sort': 'full_name'
|
||||
}
|
||||
|
||||
response = requests.request("GET", url, headers=headers, data=payload, params=params)
|
||||
|
||||
if response.status_code == 200:
|
||||
return response.json()
|
||||
else:
|
||||
raise Exception(f"status code {response.status_code}")
|
||||
except Exception as e:
|
||||
print(f"error: {e}")
|
||||
return None
|
||||
|
||||
|
||||
def getRepos(github_token: str, organization: str):
|
||||
# Get all repos for an organization from GitHub by iterating over all pages.
|
||||
# https://docs.github.com/en/rest/reference/repos#list-organization-repositories
|
||||
try:
|
||||
repos = []
|
||||
page = 1
|
||||
while True:
|
||||
repos_page = getReposFromPage(github_token, organization, page)
|
||||
if repos_page is not None:
|
||||
repos.extend(repos_page)
|
||||
if len(repos_page) < 100:
|
||||
break
|
||||
else:
|
||||
page += 1
|
||||
else:
|
||||
raise Exception("repos_page is None")
|
||||
return repos
|
||||
except Exception as e:
|
||||
print(f"error: {e}")
|
||||
return None
|
||||
@@ -1,47 +0,0 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
# Required parameters:
|
||||
# @raycast.schemaVersion 1
|
||||
# @raycast.title test1
|
||||
# @raycast.mode fullOutput
|
||||
|
||||
# Optional parameters:
|
||||
# @raycast.icon https://avatars.githubusercontent.com/u/97734037?s=200&v=4
|
||||
|
||||
# Documentation:
|
||||
# @raycast.author ChristianLempa
|
||||
# @raycast.authorURL https://raycast.com/ChristianLempa
|
||||
|
||||
from dotenv import load_dotenv
|
||||
import os
|
||||
from repos import getRepos
|
||||
|
||||
|
||||
load_dotenv()
|
||||
|
||||
github_token = os.getenv("GITHUB_TOKEN")
|
||||
|
||||
ignored_folders = [
|
||||
'.git',
|
||||
'.DS_Store',
|
||||
'.obsidian',
|
||||
]
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
try:
|
||||
# List all repos for an organization from GitHub
|
||||
# https://docs.github.com/en/rest/reference/repos#list-organization-repositories
|
||||
|
||||
repos = getRepos(github_token, 'clcreative')
|
||||
if repos is not None:
|
||||
for repo in repos:
|
||||
print(f"{repo['name']}, {repo['owner']['login']}, {repo['description']}")
|
||||
else:
|
||||
raise Exception("repos is None")
|
||||
|
||||
# Sum all repos
|
||||
print(f"---\nTotal: {len(repos)}")
|
||||
|
||||
except Exception as e:
|
||||
print(f"error: {e}")
|
||||
Reference in new issue
Block a user