forked from Yitaek/bitbucket-pull-reminders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitbucket.js
105 lines (89 loc) · 2.56 KB
/
Bitbucket.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const BitbucketAPI = require('bitbucket')
class Bitbucket {
constructor( options ) {
this.bitbucket = new BitbucketAPI()
this.bitbucket.authenticate(options.auth)
this.teamName = options.teamName
const projectsArr = options.projects.split(',')
const projectStrings = projectsArr.map( (project) => {
return `project.key="${project}"`
})
this.projects = `(${projectStrings.join(' OR ')})`
}
async getAllPRs() {
try {
const slugs = await this.getAllRepos(1)
const pullRequests = await this.getPRs(slugs)
const reviewers = await this.getReviewers(pullRequests)
return reviewers
} catch (err) {
console.log(err)
return null
}
}
async getAllRepos(pageNumber) {
const {data} = await this.bitbucket.repositories.list({
username: this.teamName,
pagelen: 100,
page: pageNumber,
q: this.projects
})
const {values, page, next} = data
let slugs = []
if (next) {
slugs = await this.getAllRepos(page + 1)
}
const repoSlugs = values.map( (repo) => {
return repo.slug
})
return slugs.concat(repoSlugs)
}
async getPRs(slugs) {
const pullRequests = []
for (let i = 0; i < slugs.length; i++) {
const {data} = await this.bitbucket.repositories.listPullRequests({
repo_slug: slugs[i],
username: this.teamName
})
if (data.size > 0) {
const slug = slugs[i]
const ids = data.values.map((pr) => {
return pr.id
})
const idsBySlug = {
slug,
ids
}
pullRequests.push(idsBySlug)
}
}
return pullRequests
}
async getReviewers(pullrequest) {
const reviewers = []
for (let i=0; i < pullrequest.length; i++) {
const ids = pullrequest[i].ids
const slug = pullrequest[i].slug
for (let j=0; j <ids.length; j++) {
const {data} = await this.bitbucket.repositories.getPullRequest({
username: this.teamName,
repo_slug: slug,
pull_request_id: ids[j]
})
const {title, participants, updated_on, links} = data //eslint-disable-line
const waitingReview = participants.filter( (reviewer) => !reviewer.approved)
const reviewerName = waitingReview.map( (review) => review.user.display_name)
const link = links.html.href
reviewers.push({
title,
updated_on,
reviewerName,
link
})
}
}
// loop thru slug and ids and grab revewer
return reviewers
}
}
module.exports = Bitbucket