Add archive_github.com
This commit is contained in:
parent
ccc7b83e72
commit
b43944a5b8
2 changed files with 73 additions and 0 deletions
|
@ -61,3 +61,10 @@ Run the script to migrate repositories:
|
||||||
```bash
|
```bash
|
||||||
pipenv run python migrate_github_to_forgejo.py
|
pipenv run python migrate_github_to_forgejo.py
|
||||||
```
|
```
|
||||||
|
|
||||||
|
After that you can add a notice where they moved and archive
|
||||||
|
those repositories with:
|
||||||
|
|
||||||
|
```bash
|
||||||
|
pipenv run python archive_github.py
|
||||||
|
```
|
||||||
|
|
66
archive_github.py
Normal file
66
archive_github.py
Normal file
|
@ -0,0 +1,66 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
import os
|
||||||
|
import requests
|
||||||
|
from base64 import b64decode, b64encode
|
||||||
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
|
# Load credentials from .env
|
||||||
|
load_dotenv()
|
||||||
|
|
||||||
|
GITHUB_USER = os.getenv("GITHUB_USERNAME")
|
||||||
|
TOKEN = os.getenv("GITHUB_TOKEN")
|
||||||
|
FORGEJO_USER = os.getenv("FORGEJO_USERNAME")
|
||||||
|
FORGEJO_URL = os.getenv("FORGEJO_URL")
|
||||||
|
|
||||||
|
headers = {"Authorization": f"token {TOKEN}"}
|
||||||
|
|
||||||
|
# Get list of repos
|
||||||
|
repos_url = f"https://api.github.com/users/{GITHUB_USER}/repos?per_page=100"
|
||||||
|
repos = requests.get(repos_url, headers=headers).json()
|
||||||
|
|
||||||
|
for repo in repos:
|
||||||
|
repo_name = repo["name"]
|
||||||
|
print(f"Processing {repo_name}...")
|
||||||
|
|
||||||
|
# Skip forks if desired
|
||||||
|
if repo["fork"]:
|
||||||
|
print(" Skipping fork")
|
||||||
|
continue
|
||||||
|
|
||||||
|
# 1. Update or create README.md
|
||||||
|
readme_url = f"https://api.github.com/repos/{GITHUB_USER}/{repo_name}/contents/README.md"
|
||||||
|
r = requests.get(readme_url, headers=headers)
|
||||||
|
|
||||||
|
notice = f"⚠️ This repository has moved to {FORGEJO_URL}/{FORGEJO_USER}/{repo_name}\n\n"
|
||||||
|
|
||||||
|
if r.status_code == 200:
|
||||||
|
readme_info = r.json()
|
||||||
|
sha = readme_info["sha"]
|
||||||
|
content = b64decode(readme_info["content"]).decode("utf-8")
|
||||||
|
|
||||||
|
if not content.startswith(notice):
|
||||||
|
updated_content = notice + content
|
||||||
|
data = {
|
||||||
|
"message": "Add notice about migration to Forgejo",
|
||||||
|
"content": b64encode(updated_content.encode("utf-8")).decode("utf-8"),
|
||||||
|
"sha": sha
|
||||||
|
}
|
||||||
|
r = requests.put(readme_url, headers=headers, json=data)
|
||||||
|
print(" README.md updated" if r.status_code == 200 else f" Failed to update README.md: {r.text}")
|
||||||
|
else:
|
||||||
|
print(" README.md already contains notice")
|
||||||
|
elif r.status_code == 404:
|
||||||
|
data = {
|
||||||
|
"message": "Create README with migration notice",
|
||||||
|
"content": b64encode(notice.encode("utf-8")).decode("utf-8")
|
||||||
|
}
|
||||||
|
r = requests.put(readme_url, headers=headers, json=data)
|
||||||
|
print(" README.md created" if r.status_code == 201 else f" Failed to create README.md: {r.text}")
|
||||||
|
else:
|
||||||
|
print(f" Failed to get README.md: {r.text}")
|
||||||
|
|
||||||
|
# 2. Archive the repo
|
||||||
|
archive_url = f"https://api.github.com/repos/{GITHUB_USER}/{repo_name}"
|
||||||
|
data = {"archived": True}
|
||||||
|
r = requests.patch(archive_url, headers=headers, json=data)
|
||||||
|
print(" Repository archived" if r.status_code == 200 else f" Failed to archive repo: {r.text}")
|
Loading…
Add table
Add a link
Reference in a new issue