Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating Pull requests right from Emacs

I abhore manual, tedious and repetitive tasks as any normal developer should. Recently I realized - creating pull requests on numerous git repos taking up too much of my time. And most of the time you have to follow almost exact steps over and over:

  • log into git provider's web client - we use Stash
  • click, click, click until you find "Create Pull Request" button, then click it
  • select a branch - usually it's a branch that's just been pushed
  • select a target branch - most of the time it's "develop"
  • add reviewers - most of the time exact same people
  • add description - optional

At some point I started wondering if I could do all that without using web client. And it seems to be possible. Stash and Bitbucket have an api, Github has one as well (although it is different - the first one uses ssh and the latter http)

Now, this thing would probably simplify some things, yet I feel it can be even better.

I use Emacs (Spacemacs distro, to be specific). Now I'm wondering if anyone already built anything that integrates with magit, or maybe I could do it myself? I mean how difficult would it be? Script should let you commit and then push the branch and then create a pull-request based of that branch against "develop", using given defaults. Has anyone done anything like that?

Can you guys point me at some elisp plugins that utilize power of magit for doing similar stuff. Maybe I'd be able to write something myself.

like image 808
iLemming Avatar asked Jan 08 '23 10:01

iLemming


2 Answers

I found this original post on creating PRs from emacs on github. http://endlessparentheses.com/easily-create-github-prs-from-magit.html

This didn't work for bitbucket (stash). But it was enough information for me to be able to hack together a solution that works for me.

https://github.com/flamingbear/emacs-config/blob/master/site-lisp/lisp/mhs-magit.el

(defun endless/visit-pull-request-url ()
  "Visit the current branch's PR on Github."
  (interactive)
  (let ((repo (magit-get "remote" (magit-get-remote) "url")))
    (if (string-match "github\\.com" repo)
    (visit-gh-pull-request repo)
  (visit-bb-pull-request repo))))


(defun visit-gh-pull-request (repo)
  "Visit the current branch's PR on Github."
  (interactive)
  (browse-url
   (format "https://github.com/%s/pull/new/%s"
     (replace-regexp-in-string
      "\\`.+github\\.com:\\(.+\\)\\.git\\'" "\\1"
      repo)
     (cdr (magit-get-remote-branch)))))



;; Bitbucket pull requests are kinda funky, it seems to try to just do the
;; right thing, so there's no branches to include.
;; https://bitbucket.org/<username>/<project>/pull-request/new
(defun visit-bb-pull-request (repo)
  (browse-url
   (format "https://bitbucket.org/%s/pull-request/new"
           (replace-regexp-in-string
            "\\`.+bitbucket\\.org:\\(.+\\)\\.git\\'" "\\1"
            repo))))

;; visit PR for github or bitbucket repositories with "v"
(eval-after-load 'magit
  '(define-key magit-mode-map "v"
     #'endless/visit-pull-request-url))
like image 190
Matt Savoie Avatar answered Jan 21 '23 03:01

Matt Savoie


magit forge

magit now comes with forge, which is to work with issues/PR/etc. in any of the git forges. It supports github, gitlab out of the box with partial support for bitbucket, gitea, gitweb, cgit, gogs etc.

To create a new pull request (or an issue) (from the docs):

' p     (forge-create-pullreq)

C-c C-n [on "Pull requests" section] (forge-create-pullreq)

This command creates a new pull-request for the current repository.

' i     (forge-create-issue)

C-c C-n [on "Issues" section] (forge-create-pullreq)

This command creates a new issue for the current repository.


Old answer:

There is a magit extension for dealing with github pull requests. You can go through the project here.

You should also go through this post by the magit project's maintainer.

So I am afraid there currently isn't a package that does what you want. If you want to write it yourself, I recommend you use request.el and then only implement those parts of the Github api that you actually need, to avoid over-engineering it.

like image 34
hjpotter92 Avatar answered Jan 21 '23 03:01

hjpotter92