Skip to content

Commit

Permalink
Merge pull request #168 from wandersoncferreira/refactor/bitbucket-re…
Browse files Browse the repository at this point in the history
…lated

Bitbucket refactor
  • Loading branch information
wandersoncferreira authored Jan 3, 2022
2 parents 0ae1ec0 + 25c5877 commit dd618e9
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 38 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- [#162](https://github.com/wandersoncferreira/code-review/pull/162): fix obsolete defgeneric to cl-defgeneric.
- [#166](https://github.com/wandersoncferreira/code-review/pull/166): github-related code refactor
- [#167](https://github.com/wandersoncferreira/code-review/pull/167): gitlab-related code refactor
- [#168](https://github.com/wandersoncferreira/code-review/pull/168): bitbucket-related code refactor

# v0.0.5

Expand Down
24 changes: 17 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,25 @@ code-review-forge-pr-at-point` if you are in a forge buffer over a PR.

### Code Review

Set `code-review-fill-column` to define line wrap comment sections.
Define line wrap in comment sections.

If you want to use `code-review` in a full buffer you can change the function
used to display the buffer at `code-review-new-buffer-window-strategy` e.g. you
can set `(setq code-review-new-buffer-window-strategy #'switch-to-buffer)` to
not open a new window.
``` emacs-lisp
(setq code-review-fill-column 80)
```

Change how `code-review` splits the buffer when opening a new PR. Defaults to
`#'switch-to-buffer-other-window`.

``` emacs-lisp
(setq code-review-new-buffer-window-strategy #'switch-to-buffer)
```

Change the destination where binary files is downloaded.

``` emacs-lisp
(setq code-review-download-dir "/tmp/code-review/")
```

Set `code-review-download-dir` to change the place Code Review will download
binary files in your pull request when you decide to visit them.

### Forge specific

Expand Down
107 changes: 76 additions & 31 deletions code-review-bitbucket.el
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@
(defclass code-review-bitbucket-repo (code-review-db-pullreq)
((callback :initform nil)))

(defun code-review-bitbucket-errback (&rest m)
"Error callback, displays the error message M."
(let-alist m
(code-review-utils--log
"code-review-bitbucket-errback"
(prin1-to-string m))
(error "Unknown error talking to Bitbucket: %s" m)))

(defun code-review-bitbucket--ghub-post (url payload &optional callback)
"Given URL and PAYLOAD perform a POST.
An optionally provide a CALLBACK."
Expand All @@ -48,29 +56,42 @@ An optionally provide a CALLBACK."
:auth 'code-review
:host code-review-bitbucket-host
:payload payload
:callback callback))
:callback callback
:noerror 'return))

(defun code-review-bitbucket--ghub-get (url &optional callback)
"Given URL and PAYLOAD perform a GET.
An optionally provide a CALLBACK."
(defun code-review-bitbucket--ghub-get (url callback)
"Given URL and CALLBACK perform an async GET."
(ghub-request "GET" url
nil
:forge 'bitbucket
:auth 'code-review
:host code-review-bitbucket-host
:callback callback))

(defun code-review-bitbucket--ghub-sync-get (url)
"Given URL perform a sync GET."
(ghub-request "GET" url
nil
:forge 'bitbucket
:auth 'code-review
:host code-review-bitbucket-host
:noerror 'return))

(cl-defmethod code-review-pullreq-diff ((bitbucket code-review-bitbucket-repo) callback)
"Get PR diff from BITBUCKET, run CALLBACK."
(let ((diff-url (let-alist (code-review-bitbucket--ghub-get
(format "/repositories/%s/%s/pullrequests/%s"
(oref bitbucket owner)
(oref bitbucket repo)
(oref bitbucket number)))
(-> .links.diff.href
(split-string code-review-bitbucket-host)
(-second-item)))))
(code-review-bitbucket--ghub-get diff-url callback)))
(let ((pr-infos
(code-review-bitbucket--ghub-sync-get
(format "/repositories/%s/%s/pullrequests/%s"
(oref bitbucket owner)
(oref bitbucket repo)
(oref bitbucket number)))))
(if (string-equal (a-get pr-infos 'type) "error")
(prin1 pr-infos)
(let ((diff-url (let-alist pr-infos
(-> .links.diff.href
(split-string code-review-bitbucket-host)
(-second-item)))))
(code-review-bitbucket--ghub-get diff-url callback)))))

(cl-defmethod code-review-diff-deferred ((bitbucket code-review-bitbucket-repo))
"Get PR diff from BITBUCKET using deferred lib."
Expand Down Expand Up @@ -125,26 +146,50 @@ An optionally provide a CALLBACK."
(path . ,.inline.path)
(createdAt . ,.created_on)))))))))))

(defun code-review-bitbucket--commits (raw-commits)
`((totalCount . ,(length tt))
(nodes .,(-map
(lambda (it)
(let-alist it
`((commit . ((message . ,(format "%s\n" .summary.raw))
(abbreviatedOid . ,(substring .hash 0 7)))))))
raw-commits))))

(cl-defmethod code-review-pullreq-infos ((bitbucket code-review-bitbucket-repo) callback)
"Get PR infos from BITBUCKET, run CALLBACK."
(let* ((res (code-review-bitbucket--ghub-get
(format "/repositories/%s/%s/pullrequests/%s"
(oref bitbucket owner)
(oref bitbucket repo)
(oref bitbucket number))))
(comments (->> (code-review-bitbucket--ghub-get
(format "/repositories/%s/%s/pullrequests/%s/comments?q=deleted=false&pagelen=100"
(oref bitbucket owner)
(oref bitbucket repo)
(oref bitbucket number)))
(-remove
(lambda (it)
(a-get it 'deleted)))))
(top-level-comments (code-review-bitbucket--top-level-comments comments))
(diff-comments (code-review-bitbucket--diff-comments comments)))
(funcall callback (-> res
(a-assoc-in (list 'comments 'nodes) top-level-comments)
(a-assoc-in (list 'reviews 'nodes) diff-comments)))))
(let ((res (code-review-bitbucket--ghub-sync-get
(format "/repositories/%s/%s/pullrequests/%s"
(oref bitbucket owner)
(oref bitbucket repo)
(oref bitbucket number)))))
(if (string-equal (a-get res 'type) "error")
(prin1 res)
(let ((raw-commits (code-review-bitbucket--ghub-sync-get
(format "/repositories/%s/%s/pullrequests/%s/commits?pagelen=100"
(oref bitbucket owner)
(oref bitbucket repo)
(oref bitbucket number)))))
(setq tt raw-commits)
(if (string-equal (a-get raw-commits 'type) "error")
(prin1 raw-commits)
(let ((commits (code-review-bitbucket--commits raw-commits))
(raw-comments (code-review-bitbucket--ghub-sync-get
(format "/repositories/%s/%s/pullrequests/%s/comments?q=deleted=false&pagelen=100"
(oref bitbucket owner)
(oref bitbucket repo)
(oref bitbucket number)))))
(if (string-equal (a-get raw-comments 'type) "error")
(prin1 raw-comments)
(let* ((comments (-remove
(lambda (it)
(a-get it 'deleted))
raw-comments))
(top-level-comments (code-review-bitbucket--top-level-comments comments))
(diff-comments (code-review-bitbucket--diff-comments comments)))
(funcall callback (-> res
(a-assoc-in (list 'comments 'nodes) top-level-comments)
(a-assoc-in (list 'reviews 'nodes) diff-comments)
(a-assoc 'commits commits)))))))))))

(cl-defmethod code-review-infos-deferred ((bitbucket code-review-bitbucket-repo))
"GET PR infos from BITBUCKET using deferred lib."
Expand Down
1 change: 1 addition & 0 deletions code-review-section.el
Original file line number Diff line number Diff line change
Expand Up @@ -1621,6 +1621,7 @@ If you want to display a minibuffer MSG in the end."
(baseRefName . ,.destination.branch.name)
(headRefName . ,.source.branch.name)
(comments (nodes . ,.comments.nodes))
(commits . ,.commits)
(reviews (nodes . ,.reviews.nodes))))))

;; 1. save raw diff data
Expand Down

0 comments on commit dd618e9

Please sign in to comment.