Skip to content

Commit 9711255

Browse files
authored
feat: log all events (#299)
1 parent 379e333 commit 9711255

15 files changed

+182
-184
lines changed

index.js

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
module.exports = wip;
22

33
const handlePullRequestChange = require("./lib/handle-pull-request-change");
4-
const handleMarketplacePurchase = require("./lib/handle-marketplace-purchase");
54
const handleInstallation = require("./lib/handle-installation");
5+
const logEvent = require("./lib/log-event");
66

77
/**
88
* @param {import('probot').Probot} app
@@ -20,19 +20,12 @@ function wip(app) {
2020
handlePullRequestChange.bind(null, app)
2121
);
2222

23-
// listen to relevant marketplace purchase events
24-
app.on(
25-
[
26-
"marketplace_purchase.purchased",
27-
"marketplace_purchase.changed",
28-
"marketplace_purchase.cancelled",
29-
],
30-
handleMarketplacePurchase.bind(null, app)
31-
);
32-
3323
// listen to installation events
3424
app.on(
3525
["installation", "installation_repositories"],
3626
handleInstallation.bind(null, app)
3727
);
28+
29+
// Log all events
30+
app.on("*", logEvent);
3831
}

lib/handle-marketplace-purchase.js

Lines changed: 0 additions & 50 deletions
This file was deleted.

lib/log-event.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module.exports = logEvent;
2+
3+
function logEvent({ id, name, payload, log }) {
4+
const fullEventName = payload.action ? `${name}.${payload.action}` : name;
5+
const owner = payload.installation.account.login.toLowerCase();
6+
log.info(
7+
{
8+
id,
9+
event: name,
10+
action: payload.action,
11+
installation: payload.installation.id,
12+
owner,
13+
isEventStat: true,
14+
},
15+
`${fullEventName} event received for ${owner} (id: ${id})`
16+
);
17+
}
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
const Stream = require("stream");
2+
3+
const { beforeEach, test, expectUncaughtException } = require("tap");
4+
const pino = require("pino");
5+
6+
const { Probot, ProbotOctokit } = require("probot");
7+
8+
const app = require("../../");
9+
10+
let output;
11+
const streamLogsToOutput = new Stream.Writable({ objectMode: true });
12+
streamLogsToOutput._write = (object, encoding, done) => {
13+
output.push(JSON.parse(object));
14+
done();
15+
};
16+
17+
beforeEach(function (done) {
18+
// Clear log output
19+
output = [];
20+
delete process.env.APP_NAME;
21+
22+
this.probot = new Probot({
23+
id: 1,
24+
githubToken: "test",
25+
Octokit: ProbotOctokit.defaults({
26+
throttle: { enabled: false },
27+
retry: { enabled: false },
28+
log: pino(streamLogsToOutput),
29+
}),
30+
log: pino(streamLogsToOutput),
31+
});
32+
this.probot.load(app);
33+
34+
done();
35+
});
36+
37+
test("logs event with action", async function (t) {
38+
await this.probot.receive({
39+
name: "check_run",
40+
id: "event123",
41+
payload: {
42+
action: "completed",
43+
installation: {
44+
id: 1,
45+
account: {
46+
login: "wip",
47+
},
48+
},
49+
},
50+
});
51+
52+
t.equal(output.length, 1);
53+
delete output[0].pid;
54+
delete output[0].hostname;
55+
delete output[0].time;
56+
t.deepEqual(output[0], {
57+
level: 30,
58+
name: "event",
59+
id: "event123",
60+
event: "check_run",
61+
action: "completed",
62+
installation: 1,
63+
owner: "wip",
64+
isEventStat: true,
65+
msg: "check_run.completed event received for wip (id: event123)",
66+
});
67+
});
68+
69+
test("logs event without action", async function (t) {
70+
await this.probot.receive({
71+
name: "ping",
72+
id: "event123",
73+
payload: {
74+
installation: {
75+
id: 1,
76+
account: {
77+
login: "wip",
78+
},
79+
},
80+
},
81+
});
82+
83+
t.equal(output.length, 1);
84+
delete output[0].pid;
85+
delete output[0].hostname;
86+
delete output[0].time;
87+
t.deepEqual(output[0], {
88+
level: 30,
89+
name: "event",
90+
id: "event123",
91+
event: "ping",
92+
installation: 1,
93+
owner: "wip",
94+
isEventStat: true,
95+
msg: "ping event received for wip (id: event123)",
96+
});
97+
});

test/integration/events/new-pull-request-with-do-not-merge-title.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
"login": "wip"
2626
},
2727
"installation": {
28-
"id": 1
28+
"id": 1,
29+
"account": {
30+
"login": "wip"
31+
}
2932
}
3033
}
3134
}

test/integration/events/new-pull-request-with-emoji-no-space-title.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
"login": "wip"
2626
},
2727
"installation": {
28-
"id": 1
28+
"id": 1,
29+
"account": {
30+
"login": "wip"
31+
}
2932
}
3033
}
3134
}

test/integration/events/new-pull-request-with-emoji-title.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
"login": "wip"
2626
},
2727
"installation": {
28-
"id": 1
28+
"id": 1,
29+
"account": {
30+
"login": "wip"
31+
}
2932
}
3033
}
3134
}

test/integration/events/new-pull-request-with-test-title-from-user.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,10 @@
2222
}
2323
},
2424
"installation": {
25-
"id": 1
25+
"id": 1,
26+
"account": {
27+
"login": "wip"
28+
}
2629
}
2730
}
2831
}

test/integration/events/new-pull-request-with-test-title.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
"login": "wip"
2626
},
2727
"installation": {
28-
"id": 1
28+
"id": 1,
29+
"account": {
30+
"login": "wip"
31+
}
2932
}
3033
}
3134
}

test/integration/events/new-pull-request-with-wip-label.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
"login": "wip"
3030
},
3131
"installation": {
32-
"id": 1
32+
"id": 1,
33+
"account": {
34+
"login": "wip"
35+
}
3336
}
3437
}
3538
}

0 commit comments

Comments
 (0)