Skip to content

Commit af0f7ed

Browse files
committed
ran prettier and did some refactoring to add a main file
1 parent 3a32e60 commit af0f7ed

35 files changed

+385
-321
lines changed

.prettierignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.data
2+
dist

.travis.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
language: node_js
22
node_js:
3-
- '8'
3+
- "8"
44
cache: yarn
5-
script: yarn test
5+
script: yarn test

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ build-stats <service>:<user>/<repo> <command> [...options]
6565
- `service`: CI Service (`travis` or `bitbucket` or `bamboo`)
6666
- `user/repo`: Project specifier (Example: `https://travis-ci.org/boltpkg/bolt` &rarr; `boltpkg/bolt`)
6767

68-
**Note: In case for Bamboo, user is the url to the Bamboo server and repo is the plankey of the project.
68+
\*\*Note: In case for Bamboo, user is the url to the Bamboo server and repo is the plankey of the project.
6969

7070
### Commands
7171

__fixtures__/testRepo/test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
// for test support
1+
// for test support

cli.ts

Lines changed: 24 additions & 154 deletions
Original file line numberDiff line numberDiff line change
@@ -1,189 +1,59 @@
11
#!/usr/bin/env node
2-
import meow from 'meow';
3-
import { calculate, download, history, success, clean, cache } from './index';
2+
import meow from "meow";
3+
import main from "./index";
4+
import Text from "./lib/text";
45

5-
interface Flags {
6-
auth?: string;
7-
branch?: string;
8-
concurrency: number;
9-
json?: boolean;
10-
last?: number;
11-
period?: number;
12-
result?: string;
13-
since?: number;
14-
threshold?: number;
15-
}
16-
17-
async function main(argv: string[]) {
6+
async function cli(argv: string[]) {
187
const cli = meow({
198
argv,
20-
help: `
21-
Usage
22-
$ build-stats <service>:<user>/<repo> <command> [...opts]
23-
24-
Commands
25-
download Download history for a repository
26-
calculate Calculate average build time and success rates over time
27-
history List individual builds
28-
success Get quick stats of number of success and failed builds
29-
clean Delete the downloaded history of repository
30-
cache Outputs the directory where data will be cached
31-
32-
Options
33-
--auth [authentication] (download) Authentication to access private repo
34-
--concurrency [number] (download) How many parallel downloads should be used when downloading data (Default: 10)
35-
--since [buildNumber] (download) Overrides the normal logic of which builds to download data for.
36-
This should only be required in debugging/fixing errors (Default: last downloaded build)
37-
--branch [name] (calculate/history) Which branch(es) to display (Comma-separated list) (Default: *)
38-
--result [name] (calculate/history) Which branch(es) to display (Comma-separated list) (Default: *)
39-
--period [days] (calculate) How many days in a time period to calculate the means for (Default: 1)
40-
--last [count] (calculate) How many periods to calculate back to (Default: 30)
41-
--threshold [time] (calculate) Time under which builds graph is shown in green color. Default is mean of all the builds in that period
42-
43-
Services
44-
- bitbucket Bitbucket Pipelines
45-
- travis Travis CI
46-
47-
Examples
48-
Download travis builds history to .data folder:
49-
$ build-stats travis:boltpkg/bolt download
50-
51-
Download travis builds history to .data folder for private repository:
52-
$ build-stats travis:boltpkg/bolt download --auth <token>
53-
54-
Download a subset of builds very quickly:
55-
$ build-stats travis:boltpkg/bolt download --concurrency=20 --since=300
56-
57-
Calculate monthly average build time and success rate of a repo over the last year
58-
$ build-stats travis:boltpkg/bolt calculate
59-
60-
Calculate daily average build time and success rate of a repo over the last month
61-
$ build-stats travis:boltpkg/bolt calculate --period 1 --last 30
62-
63-
Calculate daily average build time and success rate of the master branch of a repo over the last 90 days
64-
$ build-stats travis:boltpkg/bolt calculate --branch master --period 1 --last 90
65-
66-
Display build history
67-
$ build-stats travis:boltpkg/bolt history
68-
69-
Display build history for master branch for builds that were either successful or failed
70-
$ build-stats travis:boltpkg/bolt history --branch master --result SUCCESSFUL,FAILED
71-
72-
Display the number of success and failed builds
73-
$ build-stats travis:boltpkg/bolt success
74-
75-
Delete the downloaded history of repository
76-
$ build-stats travis:boltpkg/bolt clean
77-
78-
Output the cache directory of a repository
79-
$ build-stats travis:boltpkg/bolt cache
80-
`,
9+
help: Text.mainText.help,
8110
flags: {
8211
auth: {
83-
type: 'string'
12+
type: "string",
8413
},
8514
branch: {
86-
type: 'string'
15+
type: "string",
8716
},
8817
concurrency: {
89-
type: 'number',
90-
default: 10
18+
type: "number",
19+
default: 10,
9120
},
9221
json: {
93-
type: 'boolean'
22+
type: "boolean",
9423
},
9524
last: {
96-
type: 'number'
25+
type: "number",
9726
},
9827
period: {
99-
type: 'number'
28+
type: "number",
10029
},
10130
since: {
102-
type: 'number'
103-
}
104-
}
31+
type: "number",
32+
},
33+
},
10534
});
10635

10736
if (cli.input.length < 2) {
10837
cli.showHelp();
10938
}
11039

111-
let match = cli.input[0].match(/(.*):(.*)\/(.*)/);
40+
const repoSlug = cli.input[0].match(/(.*):(.*)\/(.*)/);
11241

113-
if (!match) {
42+
if (!repoSlug) {
11443
throw new Error(
11544
`Invalid repo "${cli.input[0]}", should be "host:user/repo"`
11645
);
11746
}
11847

119-
let [, host, user, repo]: string[] = match;
120-
let command: string = cli.input[1];
121-
let flags: Flags = cli.flags;
122-
let cwd: string = __dirname;
123-
124-
if (command === 'download') {
125-
await download({
126-
cwd,
127-
host,
128-
user,
129-
repo,
130-
auth: flags.auth,
131-
concurrency: flags.concurrency,
132-
since: flags.since
133-
});
134-
} else if (command === 'calculate') {
135-
await calculate({
136-
cwd,
137-
host,
138-
user,
139-
repo,
140-
branch: flags.branch,
141-
result: flags.result,
142-
period: flags.period,
143-
last: flags.last,
144-
threshold: flags.threshold,
145-
json: flags.json
146-
});
147-
} else if (command === 'history') {
148-
await history({
149-
cwd,
150-
host,
151-
user,
152-
repo,
153-
branch: flags.branch,
154-
result: flags.result,
155-
threshold: flags.threshold,
156-
json: flags.json
157-
});
158-
} else if (command === 'success') {
159-
await success({
160-
cwd,
161-
host,
162-
user,
163-
repo,
164-
branch: flags.branch,
165-
result: flags.result,
166-
period: flags.period,
167-
last: flags.last,
168-
json: flags.json
169-
});
170-
} else if (command === 'clean') {
171-
await clean({
172-
cwd,
173-
host,
174-
user,
175-
repo
176-
});
177-
} else if (command === 'cache') {
178-
await cache({ cwd, host, user, repo });
179-
} else {
180-
throw new Error(
181-
`Unknown command "${command}", should be "download", "calculate", "history", "success", "clean", "cache"`
182-
);
183-
}
48+
await main({
49+
cwd: __dirname,
50+
repoSlug,
51+
command: cli.input[1],
52+
flags: cli.flags,
53+
});
18454
}
18555

186-
main(process.argv.slice(2)).catch(err => {
56+
cli(process.argv.slice(2)).catch((err) => {
18757
console.error(err);
18858
process.exit(1);
18959
});

index.ts

Lines changed: 93 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,93 @@
1-
export { default as cache } from './lib/commands/cache';
2-
export { default as calculate } from './lib/commands/calculate';
3-
export { default as clean } from './lib/commands/clean';
4-
export { default as download } from './lib/commands/download';
5-
export { default as history } from './lib/commands/history';
6-
export { default as success } from './lib/commands/success';
1+
import { default as cache } from "./lib/commands/cache";
2+
import { default as calculate } from "./lib/commands/calculate";
3+
import { default as clean } from "./lib/commands/clean";
4+
import { default as download } from "./lib/commands/download";
5+
import { default as history } from "./lib/commands/history";
6+
import { default as success } from "./lib/commands/success";
7+
import { flagsEnum, MainTypes } from "./types";
8+
import pick from "lodash.pick";
9+
10+
export default async function main({
11+
command,
12+
repoSlug,
13+
flags,
14+
cwd,
15+
}: MainTypes) {
16+
let [, host, user, repo]: string[] = repoSlug;
17+
18+
switch (command) {
19+
case "download":
20+
await download({
21+
cwd,
22+
host,
23+
user,
24+
repo,
25+
...pick(flags, [
26+
flagsEnum.auth,
27+
flagsEnum.concurrency,
28+
flagsEnum.since,
29+
]),
30+
});
31+
break;
32+
case "calculate":
33+
await calculate({
34+
cwd,
35+
host,
36+
user,
37+
repo,
38+
...pick(flags, [
39+
flagsEnum.branch,
40+
flagsEnum.result,
41+
flagsEnum.period,
42+
flagsEnum.last,
43+
flagsEnum.threshold,
44+
flagsEnum.json,
45+
]),
46+
});
47+
break;
48+
case "history":
49+
await history({
50+
cwd,
51+
host,
52+
user,
53+
repo,
54+
...pick(flags, [
55+
flagsEnum.branch,
56+
flagsEnum.result,
57+
flagsEnum.threshold,
58+
flagsEnum.json,
59+
]),
60+
});
61+
break;
62+
case "success":
63+
await success({
64+
cwd,
65+
host,
66+
user,
67+
repo,
68+
...pick(flags, [
69+
flagsEnum.branch,
70+
flagsEnum.result,
71+
flagsEnum.period,
72+
flagsEnum.last,
73+
flagsEnum.json,
74+
]),
75+
});
76+
break;
77+
case "clean":
78+
await clean({
79+
cwd,
80+
host,
81+
user,
82+
repo,
83+
});
84+
break;
85+
case "cache":
86+
await cache({ cwd, host, user, repo });
87+
break;
88+
default:
89+
throw new Error(
90+
`Unknown command "${command}", should be "download", "calculate", "history", "success", "clean", "cache"`
91+
);
92+
}
93+
}

lib/adapters/bamboo.ts

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@ import got from "got";
44
import ora from "ora";
55
import path from "path";
66
import * as fs from "../utils/fs";
7-
import {
8-
getLastDownloadedBuildNumber
9-
} from "../utils/builds";
7+
import { getLastDownloadedBuildNumber } from "../utils/builds";
108

119
/**
1210
* URL for build -> curl --user <userName>:<password> https://<url-to-bamboo>/rest/api/latest/result/<ProjectKey-<BuildKey>-latest.json
@@ -19,7 +17,7 @@ function toStandardBuildConfig(build) {
1917
duration: build.buildDurationInSeconds,
2018
result: build.buildState && build.buildState.toUpperCase(),
2119
refType: "not available",
22-
refName: "master"
20+
refName: "master",
2321
};
2422
}
2523

@@ -31,7 +29,7 @@ async function getTotalBuilds(bambooUrl, planKey, auth) {
3129
// const bambooLatestBuild = `${bambooBuildUrl}.json?max-result=0`;
3230
const bambooLatestBuild = `${bambooBuildUrl}-latest.json`;
3331
let res = await got(bambooLatestBuild, {
34-
auth
32+
auth,
3533
});
3634
let resJson = JSON.parse(res.body);
3735
return resJson.buildNumber;
@@ -60,10 +58,7 @@ export default async function bambooBuilds(
6058
*/
6159

6260
// Get data for all the available buils
63-
let urlGetAllBuilds = `${bambooApiUrl(
64-
bambooUrl,
65-
planKey
66-
)}.json?max-result=0`;
61+
let urlGetAllBuilds = `${bambooApiUrl(bambooUrl, planKey)}.json?max-result=0`;
6762

6863
let res = await got(urlGetAllBuilds, { auth });
6964
let resJson = JSON.parse(res.body);
@@ -73,7 +68,7 @@ export default async function bambooBuilds(
7368
limit(async () => {
7469
let build = toStandardBuildConfig(buildData);
7570
let filePath = path.join(buildsDir, `${build.id}.json`);
76-
71+
7772
downloaded += 1;
7873
spinner.text = chalk`Downloaded data for {green ${downloaded}} builds of {green ${totalBuilds}} builds`;
7974
downloadHook && downloadHook(downloaded, totalBuilds);

0 commit comments

Comments
 (0)