Last active
April 4, 2023 08:35
-
-
Save lexisother/164b4a4d246efc20ba25a2476f117864 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env zsh | |
setopt ERR_EXIT ERR_RETURN NO_UNSET PIPE_FAIL | |
ansi_reset="$(tput sgr0 || true)" | |
ansi_red="$(tput setaf 1 || true)" | |
ansi_yellow="$(tput setaf 3 || true)" | |
ansi_blue="$(tput setaf 4 || true)" | |
log_info() { echo >&2 "${ansi_blue}[info]${ansi_reset}" "$@"; } | |
log_warn() { echo >&2 "${ansi_yellow}[warn]${ansi_reset}" "$@"; } | |
log_error() { echo >&2 "${ansi_red}[ERROR]${ansi_reset}" "$@"; } | |
trap 'log_error line $LINENO' ERR | |
NETWORK_TIMEOUT=10 | |
GITHUB_API_ORG_REPOS_URL="https://api.github.com/orgs/{}/repos?per_page=1000" | |
PROJECTS_DB_URL="https://gist.githubusercontent.com/lexisother/f86a34f64af23624ed3c5040d0dac817/raw/data.json" | |
PROJECTS_DB_FILE="projects.json" | |
BACKUP_DIR="backup" | |
export GIT_TERMINAL_PROMPT=0 GIT_ASKPASS=0 SSH_ASKPASS=0 | |
mkdir -pv "$BACKUP_DIR" | |
curl() { | |
command curl --location --fail --max-time "$NETWORK_TIMEOUT" "$@" | |
} | |
if [ -z ${DEBUG+x} ]; then | |
log_info "fetching $PROJECTS_DB_URL" | |
curl "$PROJECTS_DB_URL" --output "$PROJECTS_DB_FILE" | |
else | |
PROJECTS_DB_FILE="data.json" | |
fi | |
log_info "reading $PROJECTS_DB_FILE" | |
projects=() | |
projects_db_version="$(jq '.version' $PROJECTS_DB_FILE)" | |
if [[ "$projects_db_version" == 1 ]]; then | |
# https://unix.stackexchange.com/a/136216/411555 | |
projects+=("${(@f)$( | |
jq --compact-output ' | |
(.people[], .organizations[] | | |
select( | |
.name != "Cumcord" or | |
.name != "vendetta-mod" | |
).projects[].home | |
) | |
' "$PROJECTS_DB_FILE" | |
)}") | |
else | |
log_error "unsupported $PROJECTS_DB_FILE version '$projects_db_version'" | |
fi | |
add_github_org_repos() { | |
local org_name="$1" | |
local api_url="${GITHUB_API_ORG_REPOS_URL/\{\}/"${org_name}"}" | |
log_info "fetching repos from $api_url" | |
projects+=("${(@f)$( | |
curl "$api_url" | jq --compact-output --arg org_name "$org_name" '.[] | { type: "github", user: $org_name, repo: .name }' | |
)}") | |
} | |
add_github_org_repos Cumcord | |
add_github_org_repos vendetta-mod | |
add_github_org_repos uwu | |
cd "$BACKUP_DIR" | |
for (( i = 1, len = ${#projects[@]}; i <= len; i++ )); do | |
project="${projects[$i]}" | |
log_info "(${i}/${len}) $project" | |
() { | |
project_type="$(jq --raw-output '.type' <<< "$project")" | |
case "$project_type" in | |
github|gitlab) ;; | |
*) | |
log_warn "unsupported project type '$project_type'" | |
false | |
;; | |
esac | |
tmp_file_path="$(jq --raw-output '"\(.type)/\(.user)/\(.repo).git"' <<< "$project")" | |
git_clone_url="$(jq --raw-output '"https://\($project_type).com/\(.user)/\(.repo).git"' --arg project_type "$project_type" <<< "$project")" | |
tmp_file_dir="$(dirname "$tmp_file_path")" | |
tmp_file_name="$(basename "$tmp_file_path")" | |
mkdir -pv "$tmp_file_dir" | |
if [[ -e "$tmp_file_path" ]]; then | |
log_info "deleting $tmp_file_path" | |
rm -rf "$tmp_file_path" | |
fi | |
git clone --bare "$git_clone_url" "$tmp_file_path" | |
archive_file_path="${tmp_file_path}.tar" | |
log_info "creating archive $archive_file_path" | |
tar --create --force-local --file="$archive_file_path" --directory="$tmp_file_dir" "$tmp_file_name" | |
log_info "deleting $tmp_file_path" | |
rm -rf "$tmp_file_path" | |
} || log_error "failed to backup!" | |
done |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"version": 1, | |
"people": [ | |
{ | |
"name": "lexisother", | |
"url": "https://github.com/lexisother", | |
"projects": [ | |
{ | |
"home": { | |
"type": "github", | |
"user": "lexisother", | |
"repo": "Cumcord-Plugins" | |
}, | |
"tags": ["plugin-repo"] | |
}, | |
{ | |
"home": { | |
"type": "github", | |
"user": "lexisother", | |
"repo": "vendetta-plugins" | |
}, | |
"tags": ["plugin-repo"] | |
}, | |
{ | |
"home": { | |
"type": "github", | |
"user": "lexisother", | |
"repo": "replugged-plugins" | |
}, | |
"tags": ["plugin-repo"] | |
} | |
] | |
} | |
], | |
"organizations": [] | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import time | |
import json | |
with open("data.json") as f: | |
data = json.load(f) | |
print( | |
"# All projects related to [Vendetta](https://vendetta.vercel.app) (as of {})".format( | |
time.strftime("%Y-%m-%d", time.gmtime()) | |
) | |
) | |
print() | |
print( | |
"Entries are sorted alphabetically. The exact meaning of the tags (especially `{{dead}}`) is open to interpretation." | |
) | |
print() | |
def generate_list(entries): | |
for entry in entries: | |
print("### [{}]({})".format(entry["name"], entry["url"])) | |
print() | |
for project in entry["projects"]: | |
home = project["home"] | |
home_type = home["type"] | |
url = None | |
# TODO: Maybe add support for Gitlab / Gitea in the future | |
if home_type == "github": | |
url = "https://github.com/{}/{}".format(home["user"], home["repo"]) | |
elif home_type == "gitlab": | |
url = "https://gitlab.com/{}/{}".format(home["user"], home["repo"]) | |
else: | |
raise Exception("Unknown project home type: " + home_type) | |
print("- {}".format(url)) | |
tags = project["tags"] | |
if tags: | |
print(" {}".format(" ".join(["`{{{}}}`".format(tag) for tag in tags]))) | |
print() | |
print("## People and personal projects") | |
print() | |
generate_list(data["people"]) | |
print("## Organizations") | |
print() | |
generate_list(data["organizations"]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment