forked from danielbayerlein/dashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BitbucketPullRequestCount.tsx
78 lines (68 loc) · 1.94 KB
/
BitbucketPullRequestCount.tsx
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
import React, { Component } from "react";
import fetch from "isomorphic-unfetch";
import Widget from "../../Widget";
import Counter from "../../Counter";
import { basicAuthHeader } from "../../../lib/auth";
import {
IBitbucketPullRequestCountProps,
IBitbucketPullRequestCountState,
} from "./bitbucket-model";
export default class BitbucketPullRequestCount extends Component<
IBitbucketPullRequestCountProps,
IBitbucketPullRequestCountState
> {
static defaultProps = {
interval: 1000 * 60 * 5,
title: "Bitbucket PR Count",
users: [],
};
state = {
count: 0,
error: false,
loading: true,
};
timeout: any = 0;
componentDidMount() {
this.fetchInformation().catch((err) => {
console.error(`${err.name} @ ${this.constructor.name}`, err.errors);
this.setState({ error: true, loading: false });
});
}
componentWillUnmount() {
clearTimeout(this.timeout);
}
async fetchInformation() {
const { authKey, url, project, repository, users } = this.props;
const opts = authKey ? { headers: basicAuthHeader(authKey) } : {};
try {
const res = await fetch(
`${url}/rest/api/1.0/projects/${project}/repos/${repository}/pull-requests?limit=100`,
opts
);
const json = await res.json();
let count: number;
if (users.length) {
count = json.values.filter((el) => users.includes(el.user.slug)).length;
} else {
count = json.size;
}
this.setState({ count, error: false, loading: false });
} catch (error) {
this.setState({ error: true, loading: false });
} finally {
this.timeout = setTimeout(
() => this.fetchInformation(),
this.props.interval
);
}
}
render() {
const { count, error, loading } = this.state;
const { title } = this.props;
return (
<Widget title={title} loading={loading} error={error}>
<Counter value={count} />
</Widget>
);
}
}