Closed jaredcwhite closed 4 years ago
Yes its possible. After some digging into the Github API I found where to query for a "default_branch".
V3 Rest API:
https://developer.github.com/v3/repos/#get-a-repository
V4 GraphQL API
query DefaultBranchQuery {
repository(owner: "bridgetownrb", name: "bridgetown"){
defaultBranchRef {
name
}
}
}
will return the following data:
{
"data": {
"repository": {
"defaultBranchRef": {
"name": "main"
}
}
}
}
@ParamagicDev Cool! As I think about this, I've run into rate limits in the past using the GH API without a specific key, but that was when pinging the API multiple times a minute from the same machine. So I'm guessing a one-off now and then should be fine, but we'll want to gracefully handle any error conditions.
https://developer.github.com/v3/#rate-limiting You are correct. An unauthenticated user is allowed 60 requests / hour under the REST API. And a similar number under the graphQL api.
There is another way to check the default branch using the git
cli, but it would require cloning the repository.
I guess we could silently pull down the repository into a tempdir
, check the default branch, then request the automation. Although at that point, you could just run the automation directly in the cloned repo....
Edit: the more I think about it, the more I like the idea of pulling a repo down in the background because if a user requests a different branch, you could simply do git checkout
otherwise, you're already on the default branch.
Hmm 🤔
I know we're already manually doing Git checkouts in some of the automations…but that feels a little heavyweight to me to go with for a default approach.
Maybe if no master branch is found, raise an error and prompt the user to provide a branch?
EDIT: Actually, if no master branch is found, we could rescue the error and then we could prompt the user to provide a branch on the command line so they don't have to retype the command.
At the risk of overcomplicating things, I suggest we could try the following:
Honestly I'm fine even with dropping option 3… just prompt them to paste a branch-specific URL instead.
Related to #77, currently we're assuming that if a branch name isn't specified for an automation via a GitHub URL, then look for the automation script in the master branch. Fair assumption.
But we really should be looking at the default branch whatever it is, whether it's
main
or something else. I'm not sure if there's an easily-accessible API of some kind to ask GH what the default branch name is for a given repo. Barring that, I'm thinking we'll just need to try both main and master and pick the first one that doesn't 404. If you have ideas for how to do this cleanly, please let me know!