Search code examples
gitgithubgithub-enterprise

Github Status API


I am fairly new to setting up complex features of Github, but I would like to make sure code actually builds before a Pull Request can be merged through my Branches (i.e. Feature -> Develop, Develop -> Release, Release -> Master, etc).

I found a section that will allow me to make sure a status of "success" is there before a PR can be merged, but my question is this:

Why in Github does there have to be a status already defined before I can select it from the list of status contexts required? Isn't that a chicken-before-the-egg situation?

Basically, I go to the "settings" tab of my repository, click "branches", then under "Protected Branches", I select my branch and click "Edit". From there I select the section that says, "Require status checks to pass before merging". From there if there have been no status updates on any SHAs for that branch, it tells me that there have been none for the last week. Why cannot I not enter in what the context of my status update will be before actually having to select one? What if this was the first time someone is pushing to my branch? That code could theoretically make it all the way through?


Solution

  • It is a chicken-and-egg problem, but not one that's difficult to work around. Make yourself a github access token and follow along with curl:

    $ curl -XPOST -H "Authorization: token $GITHUB_API_TOKEN" \
           -d '{"state":"success"}' \
           https://api.github.com/repos/<your_username>/<your_repo>/statuses/<latest_sha>
    

    That'll create a default status (you can override with context, see the docs) and you can go back to the settings to configure that status for your protected branch.