如何在主题中运行存储库搜索?

时间:2017-11-04 20:06:51

标签: c# github-api octokit.net

搜索“github.com”存储库的代码:

public async void searchReposit_GitHub()        
{   
    var client = new GitHubClient(new ProductHeaderValue("my-cool-app")); 
    var basicAuth = new Credentials("username", "password"); 

    client.Credentials = basicAuth;

    var request = new SearchRepositoriesRequest("mvc client side framework") 
    {   
        // or go all out and search the readme, name or description?
        In = new[] { InQualifier.Readme, InQualifier.Description, InQualifier.Name },             
    };            

    var result = await client.Search.SearchRepo(request);

    for (int s = 0; s < 3; s++)
    {
        var vr = result.Items[s];

        string readme_content;
        string readme_name;
        string readme_descript;
        string readme_Html_url;

        readme_name = vr.Name;
        readme_descript = vr.Description;
        readme_Html_url = vr.HtmlUrl; 

        try
        {
            var readme = await client.Repository.Content.GetReadme(vr.Id);
            var rawText = readme.Content;

            readme_content = rawText.ToString();

            richTextBox1.AppendText(
                " Сontent `readme` *** " + readme_content + " *** \r\n"); 
        }
        catch (Exception)
        {
            richTextBox1.AppendText(
                " Сontent `readme` -  empty ххх" +  " \r\n"); // url
            // return;
            // throw;
        }                   
    }
}

有一个主题github.com/topics/chrome-extensions

问题
如何在github.com/topics/chrome-extensions内查看代码?

我会补充。
“Chrome扩展程序”(github.com/topics/chrome-extensions)包含95个存储库。

如何在95个包含“Chrome扩展程序”github.com/topics/chrome-extensions的存储库中的“README.MD”中搜索我需要的单词?

1 个答案:

答案 0 :(得分:1)

不幸的是,它看起来并不像Octokit.NET目前支持主题。所以似乎没有一个好的&amp;简单的方法。

但是,有一种方法......通过API访问主题目前仅作为预览提供。请参阅List all topics for a repository,特别注意警告和&amp;预览的局限性。

所以你可以像这样自己构建HTTP请求:

private static async Task<Topics> GetTopics(string repoFullName)
{
    var client = new HttpClient();

    client.DefaultRequestHeaders.Accept.Clear();

    client.DefaultRequestHeaders.Accept.Add(
        new MediaTypeWithQualityHeaderValue("application/vnd.github.mercy-preview+json"));

    client.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue(
            "Basic",
            Convert.ToBase64String(
                Encoding.ASCII.GetBytes(
                    string.Format("{0}:{1}", "username", "password"))));

    client.DefaultRequestHeaders.Add("User-Agent", "my-cool-app");

    var stringTask = client.GetStringAsync(
        $"https://api.github.com/repos/{repoFullName}/topics");

    var response = await stringTask;

    var topics = JsonConvert.DeserializeObject<Topics>(response);

    return topics;
}

我正在使用Newtonsoft.Json库来反序列化响应,而我的Topics类看起来像这样:

public class Topics
{
    public List<string> names { get; set; }
}

然后在你的循环中你可以像这样使用:

var topics = await GetTopics(vr.FullName);

if (!topics.names.Contains("chrome-extensions"))
{
     continue;
}

您可能还想检查单数主题chrome-extension。它似乎更常用,因为目前有超过3000个存储库!