如何使用 Python 檢索 Github 儲存庫數據

Patricia Arquette
發布: 2024-09-29 06:12:30
原創
188 人瀏覽過

How to retrieve Github Repository Data using Python

您的組織是否擁有太多github 儲存庫,並且您需要一種簡單的方法來總結和記錄每個儲存庫的內容以用於報告、儀表板或審計目的?下面是一個使用 Github API 完成該操作的快速腳本。

功能:

  1. get_repo_info(所有者,回購)

    • 取得 GitHub 儲存庫擁有者的使用者名稱 (owner) 和儲存庫名稱 (repo)。
    • 向 GitHub 的 API 發送請求以取得儲存庫資訊。
    • 如果成功,則以 JSON 物件的形式傳回儲存庫的訊息,如果出現錯誤,則傳回 None。
  2. get_collaborators(collaborators_url):

    • 取得儲存庫協作者清單的 URL。
    • 發送請求以獲取協作者清單。
    • 傳回協作者使用者名稱列表,如果發生錯誤則傳回空列表。
  3. get_languages(languages_url):

    • 取得儲存庫語言資料的 URL。
    • 發送請求以檢索儲存庫中使用的程式語言。
    • 傳回語言列表,如果發生錯誤,則傳回空列表。
  4. get_open_issues(所有者,回購)

    • 取得儲存庫擁有者的使用者名稱 (owner) 和儲存庫名稱 (repo)。
    • 發送請求以檢索儲存庫中未解決問題的清單。
    • 以 JSON 格式傳回未解決的問題,或如果出現問題則列印錯誤訊息。
  5. get_repo_data(repo_url):

    • 取得儲存庫 URL,解析它以取得擁有者和儲存庫值,然後呼叫其他函數來收集有關儲存庫的各種資訊。
    • 編譯儲存庫訊息,包括其名稱、所有者、可見性、協作者、語言、未解決的問題和最後的活動,並以結構化格式(字典)返回。
import json
import requests
from pymongo import MongoClient

# MongoDB setup (replace with your actual connection details)
client = MongoClient("mongodb://localhost:27017/")
db = client["github_repos"]  # Database name
collection = db["repos"]     # Collection name

def get_repo_info(owner, repo):
    url = f"https://api.github.com/repos/{owner}/{repo}"
    headers = {"Accept": "application/vnd.github+json"}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        print(f"Error: {response.status_code}")
        return None

def get_collaborators(collaborators_url):
    response = requests.get(collaborators_url)
    if response.status_code == 200:
        return [collaborator["login"] for collaborator in response.json()]
    else:
        return []

def get_languages(languages_url):
    response = requests.get(languages_url)
    if response.status_code == 200:
        return list(response.json().keys())
    else:
        return []

def get_open_issues(owner, repo):
    url = f"https://api.github.com/repos/{owner}/{repo}/issues?state=open"
    headers = {"Accept": "application/vnd.github+json"}
    response = requests.get(url, headers=headers)
    if response.status_code == 200:
        return response.json()
    else:
        print(f"Error: {response.status_code}")
        return []

def get_repo_data(repo_url):
    owner, repo = repo_url.split("/")[-2:]
    repo_info = get_repo_info(owner, repo)

    if repo_info:
        data = {
            "Github URL": repo_url,
            "Project name": repo_info["name"],
            "Project owner": repo_info["owner"]["login"],
            "List users with access": get_collaborators(repo_info["collaborators_url"].split("{")[0]),  # remove template part of URL
            "Programming languages used": get_languages(repo_info["languages_url"]),
            "Security/visibility level": repo_info["visibility"],
            "Summary": repo_info["description"],
            "Last maintained": repo_info["pushed_at"],
            "Last release": repo_info["default_branch"],
            "Open issues": get_open_issues(owner, repo),
        }

        # Insert the data into MongoDB
        collection.insert_one(data)
        print("Data inserted into MongoDB successfully.")

        return data
    else:
        return None

# Example usage
repo_url = "https://github.com/URL"
repo_data = get_repo_data(repo_url)

if repo_data:
    print(json.dumps(repo_data, indent=4))
登入後複製

以上是如何使用 Python 檢索 Github 儲存庫數據的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!