Skip to content

Commit 605586b

Browse files
committed
Add linter 🔪
1 parent a13a171 commit 605586b

File tree

6 files changed

+208
-172
lines changed

6 files changed

+208
-172
lines changed

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
"main": "index.ts",
66
"scripts": {
77
"dist": "tsc",
8+
"lint": "prettier --single-quote --trailing-comma=es5 --debug-check es5 src/**/*.ts",
9+
"lint:fix": "prettier --single-quote --trailing-comma=es5 es5 --write src/**/*.ts",
810
"start": "node ./node_modules/.bin/ts-node src/index.ts",
9-
"test": "node ./node_modules/.bin/jest"
11+
"test": "npm run lint && node ./node_modules/.bin/jest"
1012
},
1113
"author": "Csongor Zalatnai",
1214
"license": "ISC",
@@ -20,6 +22,7 @@
2022
"@types/jest": "^24.0.11",
2123
"@types/node": "^11.13.4",
2224
"jest": "^24.7.1",
25+
"prettier": "1.17.0",
2326
"ts-jest": "^24.0.2"
2427
},
2528
"repository": "[email protected]:zalatnaicsongor/github-metrics-graphite-exporter.git"

src/index.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
test('test', () => {
2-
expect(true).toBe(true);
3-
});
2+
expect(true).toBe(true);
3+
});

src/index.ts

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,71 @@
11
import * as sqlite from 'sqlite';
2-
import * as moment from "moment";
3-
import {durationInSecondsExcludingWeekends} from "./weekend";
2+
import * as moment from 'moment';
3+
import { durationInSecondsExcludingWeekends } from './weekend';
44

55
class PullRequestData {
66
differenceInSeconds: number;
77

8-
constructor(private createdAt: moment.Moment, private closedAt: moment.Moment, private updatedAt: moment.Moment, private mergedAt: moment.Moment) {
9-
this.differenceInSeconds = durationInSecondsExcludingWeekends(createdAt, mergedAt, true);
8+
constructor(
9+
private createdAt: moment.Moment,
10+
private closedAt: moment.Moment,
11+
private updatedAt: moment.Moment,
12+
private mergedAt: moment.Moment
13+
) {
14+
this.differenceInSeconds = durationInSecondsExcludingWeekends(
15+
createdAt,
16+
mergedAt,
17+
true
18+
);
1019
}
1120

12-
static from(createdAt: string, closedAt: string, updatedAt: string, mergedAt: string): PullRequestData {
13-
return new PullRequestData(moment(createdAt), moment(closedAt), moment(updatedAt), moment(mergedAt));
21+
static from(
22+
createdAt: string,
23+
closedAt: string,
24+
updatedAt: string,
25+
mergedAt: string
26+
): PullRequestData {
27+
return new PullRequestData(
28+
moment(createdAt),
29+
moment(closedAt),
30+
moment(updatedAt),
31+
moment(mergedAt)
32+
);
1433
}
1534

1635
toGraphite(): string {
17-
return `prs.time_to_merge ${this.differenceInSeconds} ${this.createdAt.unix()}\n`;
36+
return `prs.time_to_merge ${
37+
this.differenceInSeconds
38+
} ${this.createdAt.unix()}\n`;
1839
}
1940
}
2041

2142
async function queryDb(dbFileName: string): Promise<PullRequestData[]> {
2243
const db = await sqlite.open(dbFileName);
23-
const results = await db.all("SELECT * FROM pull_requests");
24-
return results.map((result) => {
44+
const results = await db.all('SELECT * FROM pull_requests');
45+
return results.map(result => {
2546
const data = JSON.parse(result.data);
26-
return PullRequestData.from(data.createdAt, data.closedAt, data.updatedAt, data.mergedAt);
27-
})
47+
return PullRequestData.from(
48+
data.createdAt,
49+
data.closedAt,
50+
data.updatedAt,
51+
data.mergedAt
52+
);
53+
});
2854
}
2955

3056
if (!process.env.PULL_REQUESTS_DATABASE_PATH) {
31-
console.error("Env var PULL_REQUESTS_DATABASE_PATH must be defined and" +
32-
" point to the db file");
57+
console.error(
58+
'Env var PULL_REQUESTS_DATABASE_PATH must be defined and' +
59+
' point to the db file'
60+
);
3361
process.exit(1);
3462
}
3563

3664
(async () => {
3765
const results = await queryDb(process.env.PULL_REQUESTS_DATABASE_PATH);
38-
results.forEach((result) => {
39-
process.stdout.write(result.toGraphite())
40-
})
66+
results.forEach(result => {
67+
process.stdout.write(result.toGraphite());
68+
});
4169
})().catch(e => {
4270
console.error(e);
4371
});

src/weekend.test.ts

Lines changed: 98 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,100 @@
1-
import * as moment from "moment";
2-
import { durationInSecondsExcludingWeekends } from "./weekend";
3-
4-
describe("Weekend exclusion", () => {
5-
it("handles dates on the same weekday", () => {
6-
const beginning = moment("2019-04-03T08:34:50Z");
7-
const end = moment("2019-04-03T13:34:50Z");
8-
9-
const duration = durationInSecondsExcludingWeekends(beginning, end);
10-
11-
expect(duration).toEqual(18000);
12-
});
13-
14-
it("handles dates without weekends", () => {
15-
const beginning = moment("2019-04-03T08:34:50Z");
16-
const end = moment("2019-04-04T13:34:50Z");
17-
18-
const duration = durationInSecondsExcludingWeekends(beginning, end);
19-
20-
expect(duration).toEqual(104400);
21-
});
22-
23-
it("handles whole weekends", () => {
24-
const beginning = moment.utc("2019-04-06T00:00:00Z");
25-
const end = moment.utc("2019-04-08T00:00:00Z");
26-
27-
const duration = durationInSecondsExcludingWeekends(beginning, end);
28-
29-
expect(duration).toEqual(0);
30-
});
31-
32-
it("handles partial weekends", () => {
33-
const beginning = moment.utc("2019-04-06T08:00:00Z");
34-
const end = moment.utc("2019-04-07T08:00:00Z");
35-
36-
const duration = durationInSecondsExcludingWeekends(beginning, end);
37-
38-
expect(duration).toEqual(0);
39-
});
40-
41-
it("does not exclude weekends from the calculation if the provided range does not contain any weekend days", () => {
42-
const beginning = moment("2019-04-03T08:34:50Z");
43-
const end = moment("2019-04-05T13:34:51Z");
44-
45-
const duration = durationInSecondsExcludingWeekends(beginning, end);
46-
47-
expect(duration).toEqual(190801);
48-
});
49-
50-
it("excludes weekends if the interval contains one or more whole weekends", () => {
51-
const beginning = moment("2019-04-03T08:34:50Z");
52-
const end = moment("2019-04-18T13:34:51Z");
53-
54-
const duration = durationInSecondsExcludingWeekends(beginning, end);
55-
56-
// Without exclusion: 1314001 seconds
57-
// Excluded: two weekends, 96 hours, 172800 seconds
58-
expect(duration).toEqual(1314001 - 2 * 48 * 60 * 60);
59-
});
60-
61-
it("excludes weekends if the interval contains one or more whole weekends and a partial weekend", () => {
62-
const beginning = moment("2019-04-03T08:34:50Z");
63-
const end = moment("2019-04-21T13:34:51Z");
64-
65-
const duration = durationInSecondsExcludingWeekends(beginning, end);
66-
67-
expect(duration).toEqual(
68-
1573201 - 2 * 48 * 60 * 60 - 24 * 60 * 60 - (13 * 60 * 60 + 34 * 60 + 51)
69-
);
70-
});
71-
72-
it("accounts for business hours (09:00 - 17:00) only", () => {
73-
const usesCases = [
74-
{
75-
// Same day
76-
start: moment.utc("2019-04-01T01:00:00Z"),
77-
end: moment.utc("2019-04-01T23:00:00Z"),
78-
duration: 8 * 60 * 60
79-
},
80-
{
81-
// Spans over multiple days
82-
start: moment.utc("2019-04-01T01:00:00Z"),
83-
end: moment.utc("2019-04-02T23:00:00Z"),
84-
duration: 16 * 60 * 60
85-
},
86-
{
87-
// Spans over multiple weeks
88-
start: moment.utc("2019-04-01T01:00:00Z"),
89-
end: moment.utc("2019-04-30T23:00:00Z"),
90-
duration: 22 * 8 * 60 * 60
91-
}
92-
];
93-
94-
usesCases.forEach(usesCase => {
95-
expect(
96-
durationInSecondsExcludingWeekends(usesCase.start, usesCase.end, true)
97-
).toEqual(usesCase.duration);
98-
});
1+
import * as moment from 'moment';
2+
import { durationInSecondsExcludingWeekends } from './weekend';
3+
4+
describe('Weekend exclusion', () => {
5+
it('handles dates on the same weekday', () => {
6+
const beginning = moment('2019-04-03T08:34:50Z');
7+
const end = moment('2019-04-03T13:34:50Z');
8+
9+
const duration = durationInSecondsExcludingWeekends(beginning, end);
10+
11+
expect(duration).toEqual(18000);
12+
});
13+
14+
it('handles dates without weekends', () => {
15+
const beginning = moment('2019-04-03T08:34:50Z');
16+
const end = moment('2019-04-04T13:34:50Z');
17+
18+
const duration = durationInSecondsExcludingWeekends(beginning, end);
19+
20+
expect(duration).toEqual(104400);
21+
});
22+
23+
it('handles whole weekends', () => {
24+
const beginning = moment.utc('2019-04-06T00:00:00Z');
25+
const end = moment.utc('2019-04-08T00:00:00Z');
26+
27+
const duration = durationInSecondsExcludingWeekends(beginning, end);
28+
29+
expect(duration).toEqual(0);
30+
});
31+
32+
it('handles partial weekends', () => {
33+
const beginning = moment.utc('2019-04-06T08:00:00Z');
34+
const end = moment.utc('2019-04-07T08:00:00Z');
35+
36+
const duration = durationInSecondsExcludingWeekends(beginning, end);
37+
38+
expect(duration).toEqual(0);
39+
});
40+
41+
it('does not exclude weekends from the calculation if the provided range does not contain any weekend days', () => {
42+
const beginning = moment('2019-04-03T08:34:50Z');
43+
const end = moment('2019-04-05T13:34:51Z');
44+
45+
const duration = durationInSecondsExcludingWeekends(beginning, end);
46+
47+
expect(duration).toEqual(190801);
48+
});
49+
50+
it('excludes weekends if the interval contains one or more whole weekends', () => {
51+
const beginning = moment('2019-04-03T08:34:50Z');
52+
const end = moment('2019-04-18T13:34:51Z');
53+
54+
const duration = durationInSecondsExcludingWeekends(beginning, end);
55+
56+
// Without exclusion: 1314001 seconds
57+
// Excluded: two weekends, 96 hours, 172800 seconds
58+
expect(duration).toEqual(1314001 - 2 * 48 * 60 * 60);
59+
});
60+
61+
it('excludes weekends if the interval contains one or more whole weekends and a partial weekend', () => {
62+
const beginning = moment('2019-04-03T08:34:50Z');
63+
const end = moment('2019-04-21T13:34:51Z');
64+
65+
const duration = durationInSecondsExcludingWeekends(beginning, end);
66+
67+
expect(duration).toEqual(
68+
1573201 - 2 * 48 * 60 * 60 - 24 * 60 * 60 - (13 * 60 * 60 + 34 * 60 + 51)
69+
);
70+
});
71+
72+
it('accounts for business hours (09:00 - 17:00) only', () => {
73+
const usesCases = [
74+
{
75+
// Same day
76+
start: moment.utc('2019-04-01T01:00:00Z'),
77+
end: moment.utc('2019-04-01T23:00:00Z'),
78+
duration: 8 * 60 * 60,
79+
},
80+
{
81+
// Spans over multiple days
82+
start: moment.utc('2019-04-01T01:00:00Z'),
83+
end: moment.utc('2019-04-02T23:00:00Z'),
84+
duration: 16 * 60 * 60,
85+
},
86+
{
87+
// Spans over multiple weeks
88+
start: moment.utc('2019-04-01T01:00:00Z'),
89+
end: moment.utc('2019-04-30T23:00:00Z'),
90+
duration: 22 * 8 * 60 * 60,
91+
},
92+
];
93+
94+
usesCases.forEach(usesCase => {
95+
expect(
96+
durationInSecondsExcludingWeekends(usesCase.start, usesCase.end, true)
97+
).toEqual(usesCase.duration);
9998
});
99+
});
100100
});

0 commit comments

Comments
 (0)