git ssb

0+

cel / ssb-pull-requests



Commit 30f04e57d8029d9e86a5b522acaf608d63e56a22

Add getRevs method

Charles Lehner committed on 4/19/2016, 5:29:54 PM
Parent: a91f3a70f2da4203ad2bd0f5982534760ce19bc2

Files changed

README.mdchanged
index.jschanged
README.mdView
@@ -35,8 +35,30 @@
3535
3636 - `mergeable`: whether the pull request can be automatically merged (`true`),
3737 or not (`false`), or it is unknown (`null`)
3838
39+#### getRevs: async
40+
41+Get the base and head revisions for a pull request. These revs may be used to
42+calculate what changes are in a pull request when the revs of the branches may
43+have changed if the pull request was merged or the upstream diverged.
44+
45+```js
46+pulls.get(prId, cb)
47+```
48+
49+→
50+```js
51+{
52+ head: string,
53+ base: string
54+}
55+```
56+
57+- `base`: commit ID of the branch to request changes pulled into
58+- `head`: commit ID of the branch to pull from
59+
60+
3961 #### list: source
4062
4163 Get a stream of pull requests
4264
index.jsView
@@ -19,8 +19,9 @@
1919 name: 'pull-requests',
2020 version: '1.0.0',
2121 manifest: {
2222 get: 'async',
23+ getRevs: 'async',
2324 list: 'source'
2425 },
2526 schemas: require('./lib/schemas'),
2627 init: function (sbot) {
@@ -39,8 +40,59 @@
3940 cb(null, issue)
4041 })
4142 })
4243
44+ function getBranchLastUpdate(repoId, branch, lte, cb) {
45+ // TODO: detect and skip updates from subsequent PR with same branch name
46+ getMsg(repoId, function (err, msg) {
47+ if (err) return cb(err)
48+ var repoAuthor = msg.author
49+ pull(
50+ sbot.links({
51+ dest: repoId,
52+ source: repoAuthor,
53+ rel: 'repo',
54+ values: true,
55+ reverse: true
56+ }),
57+ pull.filter(function (link) {
58+ return link.value.content.type == 'git-update'
59+ }),
60+ pull.map(function (link) {
61+ return {
62+ timestamp: link.value.timestamp,
63+ rev: (link.value.content.refs || {})['refs/heads/' + branch]
64+ }
65+ }),
66+ pull.filter(function (update) {
67+ return update.rev
68+ && (lte ? update.timestamp <= lte : true)
69+ }),
70+ pull.take(1),
71+ pull.collect(function (err, links) {
72+ cb(err, links && links[0])
73+ })
74+ )
75+ })
76+ }
77+
78+ function getRevs(prId, cb) {
79+ getPullReq(prId, function (err, pr) {
80+ if (err) return cb(err)
81+ // get the last rev of the head branch (before it was deleted)
82+ getBranchLastUpdate(pr.headRepo, pr.headBranch, pr.updated_at,
83+ function (err, headUpdate) {
84+ if (err) return cb(err)
85+ // get the rev of base when head was last updated
86+ getBranchLastUpdate(pr.baseRepo, pr.baseBranch,
87+ headUpdate.timestamp, function (err, baseUpdate) {
88+ if (err) return cb(err)
89+ cb(null, {base: baseUpdate.rev, head: headUpdate.rev})
90+ })
91+ })
92+ })
93+ }
94+
4395 function listPullReqs(opts) {
4496 opts = opts || {}
4597 opts.type = 'pull-request'
4698 return pull(
@@ -59,8 +111,9 @@
59111 }
60112
61113 return {
62114 get: getPullReq,
115+ getRevs: getRevs,
63116 list: listPullReqs
64117 }
65118 }
66119 }

Built with git-ssb-web