-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathservice.ts
More file actions
73 lines (58 loc) · 2.17 KB
/
service.ts
File metadata and controls
73 lines (58 loc) · 2.17 KB
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
import { ConfigService } from "src/config/service";
import { FetchService } from "src/fetch/service";
import { Service } from "typedi";
import {
BitbucketRepositoryContributor,
BitbucketUser,
GetRepositoryInput,
GetRepositoryResponse,
ListRepositoryContributorsResponse,
} from "./types";
@Service()
export class BitbucketService {
constructor(
private readonly configService: ConfigService,
private readonly fetchService: FetchService,
) {}
public getRepository = async ({
owner,
repo,
}: GetRepositoryInput): Promise<GetRepositoryResponse> => {
const repoInfo = await this.fetchService.get<GetRepositoryResponse>(
`${this.apiURL}/repositories/${owner}/${repo}`,
{ headers: this.bitbucketToken ? { Authorization: `Token ${this.bitbucketToken}` } : {} },
);
return repoInfo;
};
public listRepositoryContributors = async ({
owner,
repo,
}: GetRepositoryInput): Promise<ListRepositoryContributorsResponse> => {
interface RepoCommitsResponse {
values: Array<{ author: { user?: BitbucketUser } }>;
next?: string;
}
const contributorsRecord: Record<string, BitbucketRepositoryContributor> = {};
let url: string | null = `${this.apiURL}/repositories/${owner}/${repo}/commits`;
while (url) {
const commits: RepoCommitsResponse = await this.fetchService.get<RepoCommitsResponse>(url, {
headers: this.bitbucketToken ? { Authorization: `Token ${this.bitbucketToken}` } : {},
});
for (const commit of commits.values) {
if (!commit.author.user) continue;
const author = commit.author.user;
if (!contributorsRecord[author.uuid]) {
contributorsRecord[author.uuid] = { ...author, contributions: 0 };
}
contributorsRecord[author.uuid].contributions += 1;
}
url = commits.next || null;
}
const contributors = Object.values(contributorsRecord);
// @TODO-ZM: validate responses using DTOs, for all fetchService methods
if (!Array.isArray(contributors)) return [];
return contributors;
};
private bitbucketToken = this.configService.env().BITBUCKET_TOKEN;
private apiURL = "https://api.bitbucket.org/2.0";
}