Converting a GitHub Issue to a Pull Request

The Problem

When I started using GitHub Issues on my projects, I wanted group commits for a project together. Originally, I would make a new branch for my work and use refs #issuenum in my commit messages to link the commits to an issue. But, sometimes I would forget to link a commit and then it would be lost in limbo. Teammates could use the branch comparison view to review changes, but it wasn't an ideal place to have a conversation about a feature or fix.

Instead, GitHub already has a construct for this: the pull request. So, for a while when I started to work on something, I actually closed the original issue and started a brand new pull request.

There had to be a better way.

The Solution

I found that the API allows converting an issue to a pull request. There's no corresponding UI control on the GitHub website, so I needed a way to activate this functionality.

Now, my typical workflow for features is this:

  • Client asks for something.
  • I file an issue on GitHub so I don't forget it.
  • time passes
  • The issue is next to be done, so I start a new branch and start working.
  • After a couple of commits, I push my branch and I'm ready for a pull request.
  • I run this little program, which asks me some questions, and it promotes my issue to a pull request and links it to my branch.


        #!/usr/bin/env ruby

        require 'io/console'
        ORG = 'your-organization-name'
        REPO = 'your-repository-name'
        USER = 'your-github-login-name'
        puts  "Converting a GitHub issue to a pull request for #{ORG}/#{REPO}"
        print "What issue number: "
        issue_num = gets.chomp
        print "What branch to start from (master or staging, maybe?): "
        start_branch = gets.chomp
        print "What branch to pull from (must be pushed already): "
        end_branch = gets.chomp
        print "Enter GitHub password: "
        userpass = STDIN.noecho(&:gets).chomp
        command = %Q(
          curl --user "#{USER}:#{userpass}"
               --request POST
               --data '{"issue": "#{issue_num}",
                  "head": "#{ORG}:#{end_branch}",
                  "base": "#{start_branch}"}'
        puts `#{command}`

Demo (given at KC Ruby)


After I gave this talk at KC Ruby, there were a few questions:
What about the hub gem?
It's great! I couldn't get it working easily and this was my major pain point, but I hear that it does really neat things.
Are pull requests that much better?
In a nutshell, I like them more because:
  • they group commits automatically, since they're based off a branch.
  • commenting on specific lines is much easier, since as additional changes are pushes the comments will reflect that.
  • having all the history grouped together is really helpful in the future when you need to look back wondering why something was done the way it was done!

Established 2005 · Databasically © 2016