跳至頁尾內容
.NET 幫助

Octokit .NET(開發者使用指南)

開始使用 Octokit.NET

在 .NET 專案中設定 Octokit.NET

要開始在您的專案中使用 Octokit.NET,您首先需要安裝套件。 您可以透過 NuGet 加入,這是最簡單的方法。 在 Visual Studio 中,您可以使用 NuGet Package Manager。 搜尋 Octokit 並將其安裝到您的專案中。

基本程式碼範例:存取 GitHub 使用者資訊

以下是一個簡單的範例,說明如何使用 Octokit.NET 擷取 GitHub 使用者的相關資訊。 本範例假設您已經使用 Octokit.NET 設定專案。

using Octokit;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // Create a new instance of the GitHubClient class with your application name
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));

        // Retrieve user information for the specified GitHub username
        var user = await client.User.Get("octocat");

        // Output the user's name to the console
        Console.WriteLine("User Name: " + user.Name);
    }
}
using Octokit;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        // Create a new instance of the GitHubClient class with your application name
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));

        // Retrieve user information for the specified GitHub username
        var user = await client.User.Get("octocat");

        // Output the user's name to the console
        Console.WriteLine("User Name: " + user.Name);
    }
}
$vbLabelText   $csharpLabel

此程式碼片段會建立一個新的 GitHub 用戶端,並擷取特定使用者 octocat 的資訊。 然後將使用者的姓名列印到控制台。 請注意,這演示的是在未認證公開使用者資訊的情況下存取 GitHub 的 API。

實現 Octokit.NET 的功能。

搜尋儲存庫

您可以使用 Octokit.NET 依據條件搜尋 GitHub 套件庫。 以下是如何執行搜尋:

using Octokit;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));

        // Define search criteria: topic 'machine learning' and language 'C#'
        var searchRepositoriesRequest = new SearchRepositoriesRequest("machine learning")
        {
            Language = Language.CSharp
        };

        // Execute the search and retrieve the results
        var result = await client.Search.SearchRepo(searchRepositoriesRequest);

        // Iterate and print each repository's full name
        foreach (var repo in result.Items)
        {
            Console.WriteLine(repo.FullName);
        }
    }
}
using Octokit;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));

        // Define search criteria: topic 'machine learning' and language 'C#'
        var searchRepositoriesRequest = new SearchRepositoriesRequest("machine learning")
        {
            Language = Language.CSharp
        };

        // Execute the search and retrieve the results
        var result = await client.Search.SearchRepo(searchRepositoriesRequest);

        // Iterate and print each repository's full name
        foreach (var repo in result.Items)
        {
            Console.WriteLine(repo.FullName);
        }
    }
}
$vbLabelText   $csharpLabel

本代碼會搜尋以 C# 撰寫、與"機器學習"相關的資料庫。 它會輸出儲存庫的全名。

管理分叉的套件庫

若要管理分叉套件庫,您可以列出並建立分叉。 以下是如何列出儲存庫的分叉:

using Octokit;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));

        // List all forks for the 'Hello-World' repository owned by 'octocat'
        var forks = await client.Repository.Forks.GetAll("octocat", "Hello-World");

        // Print each fork's ID and owner login
        foreach (var fork in forks)
        {
            Console.WriteLine("Fork ID: " + fork.Id + " - Owner: " + fork.Owner.Login);
        }
    }
}
using Octokit;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));

        // List all forks for the 'Hello-World' repository owned by 'octocat'
        var forks = await client.Repository.Forks.GetAll("octocat", "Hello-World");

        // Print each fork's ID and owner login
        foreach (var fork in forks)
        {
            Console.WriteLine("Fork ID: " + fork.Id + " - Owner: " + fork.Owner.Login);
        }
    }
}
$vbLabelText   $csharpLabel

本範例列出 octocat 所擁有的 "Hello-World" 套件庫的所有分叉。

處理速率限制

在與 GitHub API 互動時,瞭解和處理速率限制至關重要。 Octokit.NET 提供檢查費率限制的工具:

using Octokit;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));

        // Retrieve the current rate limits for your GitHub API usage
        var rateLimit = await client.Miscellaneous.GetRateLimits();

        // Display the core API usage limit information
        Console.WriteLine("Core Limit: " + rateLimit.Resources.Core.Limit);
    }
}
using Octokit;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));

        // Retrieve the current rate limits for your GitHub API usage
        var rateLimit = await client.Miscellaneous.GetRateLimits();

        // Display the core API usage limit information
        Console.WriteLine("Core Limit: " + rateLimit.Resources.Core.Limit);
    }
}
$vbLabelText   $csharpLabel

此片段會檢查並顯示 GitHub API 使用量的核心限制,協助您在不超出速率限制的情況下管理請求。

主動式擴充支援

Octokit.NET 支援 Reactive Extensions (Rx),用於反應式程式設計。 以下是一個基本範例:

using Octokit.Reactive;
using System;

class ReactiveExample
{
    static void Main(string[] args)
    {
        var client = new ObservableGitHubClient(new ProductHeaderValue("YourAppName"));

        // Subscribe to retrieve user information and handle data/error reactively
        var subscription = client.User.Get("octocat").Subscribe(
            user => Console.WriteLine("User Name: " + user.Name),
            error => Console.WriteLine("Error: " + error.Message)
        );

        // Unsubscribe when done to avoid memory leaks
        subscription.Dispose();
    }
}
using Octokit.Reactive;
using System;

class ReactiveExample
{
    static void Main(string[] args)
    {
        var client = new ObservableGitHubClient(new ProductHeaderValue("YourAppName"));

        // Subscribe to retrieve user information and handle data/error reactively
        var subscription = client.User.Get("octocat").Subscribe(
            user => Console.WriteLine("User Name: " + user.Name),
            error => Console.WriteLine("Error: " + error.Message)
        );

        // Unsubscribe when done to avoid memory leaks
        subscription.Dispose();
    }
}
$vbLabelText   $csharpLabel

本範例示範如何以異步方式擷取使用者資訊,並作出反應式處理。

使用標籤工作

若要透過 Octokit.NET 使用 Git 標籤,您可以從儲存庫擷取標籤:

using Octokit;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));

        // Retrieve all tags for the 'Hello-World' repository owned by 'octocat'
        var tags = await client.Repository.GetAllTags("octocat", "Hello-World");

        // Print each tag's name
        foreach (var tag in tags)
        {
            Console.WriteLine("Tag Name: " + tag.Name);
        }
    }
}
using Octokit;
using System;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));

        // Retrieve all tags for the 'Hello-World' repository owned by 'octocat'
        var tags = await client.Repository.GetAllTags("octocat", "Hello-World");

        // Print each tag's name
        foreach (var tag in tags)
        {
            Console.WriteLine("Tag Name: " + tag.Name);
        }
    }
}
$vbLabelText   $csharpLabel

此代碼列出 octocat 所擁有的 "Hello-World" 套件庫的所有標籤。

將 Octokit.NET 與 IronPDF 整合。

Octokit .NET (How It Works For Developers):圖 1 - IronPdf

IronPDF for .NET 是一個廣受歡迎的 .NET 函式庫,可讓開發人員直接在 C# 和 .NET 應用程式中建立、處理和渲染 PDF。 這是一款功能強大的工具,可從 HTML、發票或任何需要固定版面格式的文件產生 PDF 報告。 當與與 GitHub API 互動的 Octokit.NET 結合時,自動化文件流程(尤其是涉及程式碼儲存庫)的潛力將大幅提升。

探索 IronPDF 圖書館

若要進一步瞭解 IronPdf 及其功能,請造訪 IronPDF 官方網站。 他們的網站提供全面的資源和文件,以支援您的開發流程。

將 IronPDF 與 Octokit.NET 合併的使用案例。

IronPDF for .NET 與 Octokit.NET 整合的一個實際用例是自動產生儲存於 GitHub 儲存庫的專案文件 PDF 報告。 例如,您可以從特定儲存庫取得所有 markdown 檔案,將它們轉換成 PDF 文件,然後將此文件分發給可能偏好編譯版本的文件或發行說明的利害關係人或客戶。

使用個案的程式碼範例

讓我們來建立一個簡單的應用程式來展示這個整合。 應用程式將執行下列任務:

1.使用 Octokit.NET 驗證並連線至 GitHub。 2.從指定的儲存庫擷取檔案。 3.使用 IronPDF 將這些檔案從 Markdown 轉換成 PDF。 4.將 PDF 儲存至本機。

以下是您可以使用 C# 來撰寫的方式:

using Octokit;
using IronPdf;
using System;
using System.Threading.Tasks;
using System.Linq;

class Program
{
    static async Task Main(string[] args)
    {
        // GitHub client setup
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));
        var tokenAuth = new Credentials("your_github_token"); // Replace with your GitHub token
        client.Credentials = tokenAuth;

        // Repository details
        var owner = "repository_owner";
        var repo = "repository_name";

        // Fetch repository content
        var contents = await client.Repository.Content.GetAllContents(owner, repo);

        // Initialize the PDF builder
        var pdf = new ChromePdfRenderer();

        // Convert each markdown file to PDF
        foreach (var content in contents.Where(c => c.Name.EndsWith(".md")))
        {
            pdf.RenderHtmlAsPdf(content.Content).SaveAs($"{content.Name}.pdf");
            Console.WriteLine($"Created PDF for: {content.Name}");
        }
    }
}
using Octokit;
using IronPdf;
using System;
using System.Threading.Tasks;
using System.Linq;

class Program
{
    static async Task Main(string[] args)
    {
        // GitHub client setup
        var client = new GitHubClient(new ProductHeaderValue("YourAppName"));
        var tokenAuth = new Credentials("your_github_token"); // Replace with your GitHub token
        client.Credentials = tokenAuth;

        // Repository details
        var owner = "repository_owner";
        var repo = "repository_name";

        // Fetch repository content
        var contents = await client.Repository.Content.GetAllContents(owner, repo);

        // Initialize the PDF builder
        var pdf = new ChromePdfRenderer();

        // Convert each markdown file to PDF
        foreach (var content in contents.Where(c => c.Name.EndsWith(".md")))
        {
            pdf.RenderHtmlAsPdf(content.Content).SaveAs($"{content.Name}.pdf");
            Console.WriteLine($"Created PDF for: {content.Name}");
        }
    }
}
$vbLabelText   $csharpLabel

在這個範例中,設定 GitHub 用戶端並指定您的憑證之後,您就可以從儲存庫中取得內容。 對於資源庫中的每個 markdown 檔案,IronPDF 會將內容轉換為 PDF 檔案,然後儲存在本機中。 這個簡單但有效的工作流程可以擴充至包含更複雜的過濾、格式化,甚至針對較大的儲存庫進行檔案批次處理。

結論

Octokit .NET (How It Works For Developers):圖 2 - 授權

將 Octokit.NET 與 IronPDF 整合,可在 GitHub 專案中提供自動化和簡化文件工作流程的無縫方法。 透過利用這些工具,您可以提高處理文件的效率,使文件能以符合各種專業需求的格式輕鬆存取。 尤其是 IronPDF,它提供了一個強大的 PDF 操作平台,值得注意的是,他們提供免費試用,讓您可以快速上手。 如果您決定在您的專案中實作,授權費從 $799 開始。

如需瞭解 Iron Software 產品的更多資訊,包括 IronPDF 以及 IronBarcode、IronOCR、IronWebScraper 等其他函式庫,請造訪 Iron Software 產品函式庫

常見問題解答

如何將 Octokit.NET 與 PDF 產生工具整合?

您可以將 Octokit.NET 與 IronPDF 等庫集成,以自動從 GitHub 程式碼庫內容產生 PDF 報告。透過使用 Octokit.NET 取得 Markdown 文件,並使用 IronPDF 將其轉換為 PDF,您可以簡化文件工作流程。

如何使用 .NET 將 GitHub Markdown 文件轉換為 PDF?

首先,使用 Octokit.NET 存取 GitHub 倉庫中的 Markdown 文件。然後,使用 IronPDF 的ChromePdfRenderer將這些 Markdown 文件轉換為 PDF 格式,以便於分發和存檔。

是否可以使用 Octokit.NET 實現文件工作流程自動化?

是的,透過將 Octokit.NET 與 IronPDF 結合使用,您可以自動執行從 GitHub 儲存庫取得內容並將其轉換為 PDF 文件的流程,從而提高文件工作流程的效率。

如何使用 Octokit.NET 取得儲存庫內容?

若要使用 Octokit.NET 取得儲存庫內容,請初始化一個GitHubClient ,並使用Repository.GetAllContents等方法從指定的儲存庫中擷取檔案或目錄。

PDF 報告為 GitHub 文件帶來了哪些好處?

從 GitHub 文件產生 PDF 報告可確保內容易於分發和離線存取。 IronPDF 等工具可簡化此流程,建立專業且格式統一的文件。

速率限制會如何影響我對 Octokit.NET 的使用?

Octokit.NET 包含用於監控 API 速率限制的方法,例如Miscellaneous.GetRateLimits 。這有助於您有效地管理 API 請求,防止因超出速率限製而導致的中斷。

為什麼要在 Octokit.NET 中使用響應式擴充功能?

Octokit.NET 中的響應式擴充功能可讓您有效率地管理非同步資料流,提供響應式方法來處理資料和錯誤,這對於需要強大資料處理能力的應用程式來說是有益的。

Jacob Mellor,Team Iron 首席技術官
首席技術長

Jacob Mellor 是 Iron Software 的首席技術官,也是一位富有遠見的工程師,率先開發了 C# PDF 技術。作為 Iron Software 核心程式碼庫的最初開發者,他自公司成立之初便參與塑造了其產品架構,並與執行長 Cameron Rimington 一起將其發展成為一家擁有 50 多名員工、服務於 NASA、特斯拉和全球政府機構的公司。

Jacob 於 1998 年至 2001 年在曼徹斯特大學獲得土木工程一級榮譽學士學位。 1999 年,他在倫敦創辦了自己的第一家軟體公司;2005 年,他創建了自己的第一個 .NET 元件。此後,他專注於解決微軟生態系統中的複雜問題。

他的旗艦產品 IronPDF 和 IronSuite .NET 庫在全球 NuGet 上的安裝量已超過 3000 萬次,其基礎程式碼持續為全球開發者工具提供支援。憑藉 25 年的商業經驗和 41 年的程式設計專長,Jacob 始終致力於推動企業級 C#、Java 和 Python PDF 技術的創新,同時指導下一代技術領導者。