2 min read

How to find all the repos that reference a specific contract using GitHub CLI

If you work with a large codebase that spans multiple repositories, you might have encountered the problem of finding all the places where a certain contract or interface is used.
How to find all the repos that reference a specific contract using GitHub CLI
Photo by Lucas Gallone / Unsplash
🤖
This article was written by AI. For more context, see my post Will AI kill factual writing?

If you work with a large codebase that spans multiple repositories, you might have encountered the problem of finding all the places where a certain contract or interface is used. For example, I wanted to find out which other repos reference MyProduct.Business.Contracts.MyContract and the GitHub web UI wasn’t cutting it.

Sure, you can use the web UI to search for code, but it has some limitations. You can only see 10 results per page, and you have to click through each result to see the repository name. Also, you might get some false positives from forks or archived repos that are not relevant.

So I decided to try a different approach: using the GitHub CLI tool. The GitHub CLI is a command-line interface that lets you interact with GitHub from your terminal. You can do things like create issues, pull requests, releases, and more. But you can also use it to query the GitHub API and get JSON data back.

One of the commands that the GitHub CLI supports is gh api, which allows you to make arbitrary requests to the GitHub API. You can pass in any endpoint and parameters, and get the response as JSON. You can also use the --paginate flag to automatically fetch all pages of results.

So I installed the gh command line app, auth’d using gh auth login and then ran this command:

gh api --paginate search/code?q=MyProduct.Business.Contracts.MyContract | jq .items[].repository.full_name | sort | uniq

This command does the following:

  • It uses the search/code endpoint to search for code that matches MyProduct.Business.Contracts.MyContract.
  • It pipes the JSON output to jq, a command-line tool for processing JSON data.
  • It extracts the full_name property of each repository from the items array using jq .items[].repository.full_name.
  • It sorts the results alphabetically using sort.
  • It removes any duplicates using uniq.

The result is a sorted list of repos that mention that contract in the code. Here’s an example output:

arunstephens/MyProduct.Api
arunstephens/MyProduct.Client
arunstephens/MyProduct.Common
arunstephens/MyProduct.Service

This way, I can quickly see all the repos that depend on that contract, and make any changes or updates accordingly. I can also use other filters or modifiers in the query string, such as language, user, org, etc.

I hope you found this tip useful. If you want to learn more about the GitHub CLI, check out their documentation. And if you have any questions or feedback, feel free to leave a comment below or reach out to me on Twitter.

Will AI kill factual writing?
We’ve been living in a post-LLM world for about 6 months now. Some are saying that the hype is dying down. Search trends for ChatGPT are dropping the same way that searches for NFTs dropped off (thankfully). When thinking of topics when as I was starting out this newsletter a
Find out more on how this article came to be