0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Giteaで組織(Group)が持つリポジトリをまとめてクローンするスクリプト

Posted at

環境

  • Windows 10
  • Python 3.9.7

準備

モジュールのインストール

$ pip install requests

アクセストークンの取得

Giteaのブラウザ上でサインインし、[設定] -> [アプリケーション]からトークンを生成する。

スクリーンショット 2025-03-04 103201.png

作成したPythonスクリプト

gitea_clone.py

import requests
import os
import subprocess

GITEA_URL = ""
ORG_NAME = ""
TOKEN = ""  # ここにアクセストークンを設定
CLONE_DIR = "./"

# 保存ディレクトリを作成
os.makedirs(CLONE_DIR, exist_ok=True)

def get_all_repos():
    repos = []
    page = 1
    while True:
        url = f"{GITEA_URL}/api/v1/orgs/{ORG_NAME}/repos?limit=50&page={page}"
        headers = {"Authorization": f"token {TOKEN}"}
        response = requests.get(url, headers=headers)

        if response.status_code != 200:
            print(f"Error: {response.status_code}, {response.text}")
            break

        data = response.json()
        if not data:
            break  # データが空なら終了

        repos.extend(data)
        page += 1  # 次のページへ

    return repos

def clone_repos():
    repos = get_all_repos()
    for repo in repos:
        clone_url = repo["clone_url"]
        repo_name = repo["name"]
        repo_path = os.path.join(CLONE_DIR, repo_name)

        if os.path.exists(repo_path):
            print(f"Skipping {repo_name}, already cloned.")
        else:
            print(f"Cloning {repo_name}...")
            subprocess.run(["git", "clone", clone_url, repo_path])

if __name__ == "__main__":
    clone_repos()

GiteaのURL, グループ名、アクセストークン、保存ディレクトリを適宜入力し、実行する。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?