Skip to content

Instantly share code, notes, and snippets.

@piecyk
Last active April 22, 2018 19:31
Show Gist options
  • Select an option

  • Save piecyk/35030e4a1f2324eaea01 to your computer and use it in GitHub Desktop.

Select an option

Save piecyk/35030e4a1f2324eaea01 to your computer and use it in GitHub Desktop.
Create github commit comments in magit-diff emacs!
;; api https://developer.github.com/v3/repos/comments/#create-a-commit-comment
;; aslo using https://github.com/sigma/gh.el and https://github.com/sigma/magit-gh-pulls
(defun magit-gh-current-revision (current)
(cond ((derived-mode-p 'magit-revision-mode)
(car magit-refresh-args))
((derived-mode-p 'magit-diff-mode)
(--when-let (car magit-refresh-args)
(and (string-match "\\.\\.\\([^.].*\\)?[ \t]*\\'" it)
(match-string 1 it))))))
(defun magit-gh-current-hunk (current)
(pcase (magit-diff-scope)
((or `hunk `region) current)
((or `file `files) (car (magit-section-children current)))
(`list (car (magit-section-children
(car (magit-section-children current)))))))
(defun magit-gh-diff-position (section)
(let* ((parent-section (magit-section-parent section))
(cpos (marker-position (magit-section-content parent-section)))
(cstart (save-excursion (goto-char cpos) (line-number-at-pos)))
(stop (line-number-at-pos)))
(- stop cstart)))
(defun magit-gh-comments-comment-commit-at-point (file &optional other-window)
(interactive (list (or (magit-file-at-point)
(user-error "No file at point"))
current-prefix-arg))
(let* ((current (magit-current-section))
(repo-info (magit-gh-pulls-guess-repo))
(hunk (magit-gh-current-hunk current)))
(when (and repo-info hunk)
(let* ((owner (car repo-info))
(repo (cdr repo-info))
(rev (magit-gh-current-revision current))
(position (magit-gh-diff-position current))
(api (magit-gh-comments-get-api))
(body (read-from-minibuffer "Create line comment: "))
(comment (make-instance 'gh-comments-comment :body body :path file :position position)))
(message (format "=> %s" position))
(let ((response (oref (gh-comments-new api owner repo rev comment) :data)))
(message (format "response: ok"))) ;TODO: add error handling
))))
(defun magit-gh-comments-comment-commit ()
(interactive)
(let ((repo-info (magit-gh-pulls-guess-repo)))
(when repo
(let* ((current (magit-current-section))
(owner (car repo-info))
(repo (cdr repo-info))
(rev (magit-gh-current-revision current))
(api (magit-gh-comments-get-api))
(body (read-from-minibuffer "Create: "))
(comment (make-instance 'gh-comments-comment :body body))
(response (oref (gh-comments-new api owner repo rev comment) :data)))
(message (format "response: ok"))) ;; TODO: add error handling
)))
(defun magit-gh-comments-comment-list ()
(interactive)
(let ((repo (magit-gh-pulls-guess-repo)))
(when repo
(let* ((current (magit-current-section))
(user (car repo))
(proj (cdr repo))
(rev (magit-gh-current-revision current))
(api (magit-gh-comments-get-api))
(commit-comments (oref (gh-comments-list-commit api user proj rev) :data))
(buffer (get-buffer-create (format "*magit-gh-comments-commit-%s-%s-#%s" user proj rev))))
(pop-to-buffer buffer)
(insert (format "Comments for commit: %s\n\n" rev))
(dolist (comment commit-comments)
(magit-gh-pulls-insert-pull-comment comment user proj rev)
(insert "\n\n")
(if (equal comment (car (last commit-comments)))
(insert "\n---\n\n")))
(beginning-of-buffer)
))))
@jkdufair

Copy link
Copy Markdown

Wow. Thanks for posting this, magit-friend! Was looking for a way to comment on GitLab commits. This is going to be a GREAT place to start.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment