Search code examples
c#github-apioctokit.net

How do I make the repository search run within the topic?


The code to search for "github.com" repositories:

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;
        }                   
    }
}

There is a theme github.com/topics/chrome-extensions

Question
How to make the code look inside github.com/topics/chrome-extensions?

I will add.
"Chrome extension" (github.com/topics/chrome-extensions) contains 95 pieces of repositories.

How do I search for the words I need in "README.MD" in 95 pieces of repositories that contain the "Chrome extension" github.com/topics/chrome-extensions?


Solution

  • Unfortunately, it does not look like Octokit.NET currently supports topics at all. So there doesn't appear to be a nice & simple way.

    But, there is a way... Access to topics via the API is currently available as a preview only. See List all topics for a repository and especially note the warnings & limitations of the preview.

    So you can construct the HTTP request yourself like this:

    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;
    }
    

    I am using the Newtonsoft.Json library to deserialize the response and my Topics class looks like this:

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

    Then in your loop you can use like this:

    var topics = await GetTopics(vr.FullName);
    
    if (!topics.names.Contains("chrome-extensions"))
    {
         continue;
    }
    

    You may also want to check for the singular topic chrome-extension. It seems it's more commonly used as there is currently over 3000 repositories in that one!