gcf-utils 13.3.2-beta.1 → 13.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/src/bin/genkey.js +7 -3
- package/build/src/bin/genkey.js.map +1 -1
- package/build/src/bot-request.d.ts +21 -0
- package/build/src/bot-request.js +99 -0
- package/build/src/bot-request.js.map +1 -0
- package/build/src/custom-events.d.ts +4 -0
- package/build/src/custom-events.js +25 -0
- package/build/src/custom-events.js.map +1 -0
- package/build/src/gcf-utils.d.ts +3 -31
- package/build/src/gcf-utils.js +76 -111
- package/build/src/gcf-utils.js.map +1 -1
- package/build/src/logging/gcf-logger.d.ts +5 -0
- package/build/src/logging/gcf-logger.js +11 -1
- package/build/src/logging/gcf-logger.js.map +1 -1
- package/build/src/logging/trigger-info-builder.d.ts +2 -1
- package/build/src/logging/trigger-info-builder.js +6 -7
- package/build/src/logging/trigger-info-builder.js.map +1 -1
- package/build/src/server/server.js +6 -2
- package/build/src/server/server.js.map +1 -1
- package/package.json +3 -3
- package/CHANGELOG.md +0 -628
- package/build/src/#gcf-utils.js# +0 -779
- package/build/src/gcf-utils.js~ +0 -778
package/build/src/bin/genkey.js
CHANGED
@@ -15,7 +15,11 @@
|
|
15
15
|
// limitations under the License.
|
16
16
|
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
17
17
|
if (k2 === undefined) k2 = k;
|
18
|
-
Object.
|
18
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
19
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
20
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
21
|
+
}
|
22
|
+
Object.defineProperty(o, k2, desc);
|
19
23
|
}) : (function(o, m, k, k2) {
|
20
24
|
if (k2 === undefined) k2 = k;
|
21
25
|
o[k2] = m[k];
|
@@ -79,14 +83,14 @@ const botname = argv.bot;
|
|
79
83
|
const webhookSecret = argv.secret;
|
80
84
|
const id = Number(argv.id);
|
81
85
|
async function run(keyfile, webhookSecret, id, project, botname) {
|
82
|
-
const blob = await genkey_util_1.gather(keyfile, id, webhookSecret);
|
86
|
+
const blob = await (0, genkey_util_1.gather)(keyfile, id, webhookSecret);
|
83
87
|
const opts = project
|
84
88
|
? {
|
85
89
|
projectId: project,
|
86
90
|
}
|
87
91
|
: undefined;
|
88
92
|
const smclient = new secret_manager_1.v1.SecretManagerServiceClient(opts);
|
89
|
-
await genkey_util_1.create(smclient, project, botname, blob);
|
93
|
+
await (0, genkey_util_1.create)(smclient, project, botname, blob);
|
90
94
|
}
|
91
95
|
run(keyfile, webhookSecret, id, project, botname);
|
92
96
|
//# sourceMappingURL=genkey.js.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"genkey.js","sourceRoot":"","sources":["../../../src/bin/genkey.ts"],"names":[],"mappings":";;AACA,4BAA4B;AAC5B,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,kDAAkD;AAClD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC
|
1
|
+
{"version":3,"file":"genkey.js","sourceRoot":"","sources":["../../../src/bin/genkey.ts"],"names":[],"mappings":";;AACA,4BAA4B;AAC5B,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,kDAAkD;AAClD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;;;;;;;;;;;;;;;;;;;;;;;;;AAEjC,6CAA+B;AAE/B,iEAAgD;AAChD,+CAA6C;AAE7C,MAAM,IAAI,GAAG,KAAK,CAAC,OAAO,CACxB,KAAK,EACL,iCAAiC,EACjC,CAAC,KAAW,EAAE,EAAE;IACd,OAAO,KAAK;SACT,MAAM,CAAC,SAAS,EAAE;QACjB,QAAQ,EAAE,8BAA8B;QACxC,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G,OAAO;QAClB,CAAC,CAAC;YACE,SAAS,EAAE,OAAO;SACnB;QACH,CAAC,CAAC,SAAS,CAAC;IACd,MAAM,QAAQ,GAAG,IAAI,mBAAE,CAAC,0BAA0B,CAAC,IAAI,CAAC,CAAC;IACzD,MAAM,IAAA,oBAAM,EAAC,QAAQ,EAAE,OAAO,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;AACjD,CAAC;AAED,GAAG,CAAC,OAAO,EAAE,aAAa,EAAE,EAAE,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC"}
|
@@ -0,0 +1,21 @@
|
|
1
|
+
import * as express from 'express';
|
2
|
+
/**
|
3
|
+
* Type of function execution trigger
|
4
|
+
*/
|
5
|
+
export declare enum TriggerType {
|
6
|
+
GITHUB = "GitHub Webhook",
|
7
|
+
SCHEDULER = "Cloud Scheduler",
|
8
|
+
TASK = "Cloud Task",
|
9
|
+
PUBSUB = "Pub/Sub",
|
10
|
+
UNKNOWN = "Unknown"
|
11
|
+
}
|
12
|
+
export interface BotRequest {
|
13
|
+
eventName: string;
|
14
|
+
githubDeliveryId: string;
|
15
|
+
signature?: string;
|
16
|
+
taskName?: string;
|
17
|
+
taskRetryCount?: number;
|
18
|
+
traceId?: string;
|
19
|
+
triggerType: TriggerType;
|
20
|
+
}
|
21
|
+
export declare function parseBotRequest(request: express.Request): BotRequest;
|
@@ -0,0 +1,99 @@
|
|
1
|
+
"use strict";
|
2
|
+
// Copyright 2022 Google LLC
|
3
|
+
//
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
// you may not use this file except in compliance with the License.
|
6
|
+
// You may obtain a copy of the License at
|
7
|
+
//
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
//
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
// See the License for the specific language governing permissions and
|
14
|
+
// limitations under the License.
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
16
|
+
exports.parseBotRequest = exports.TriggerType = void 0;
|
17
|
+
const custom_events_1 = require("./custom-events");
|
18
|
+
/**
|
19
|
+
* Type of function execution trigger
|
20
|
+
*/
|
21
|
+
var TriggerType;
|
22
|
+
(function (TriggerType) {
|
23
|
+
TriggerType["GITHUB"] = "GitHub Webhook";
|
24
|
+
TriggerType["SCHEDULER"] = "Cloud Scheduler";
|
25
|
+
TriggerType["TASK"] = "Cloud Task";
|
26
|
+
TriggerType["PUBSUB"] = "Pub/Sub";
|
27
|
+
TriggerType["UNKNOWN"] = "Unknown";
|
28
|
+
})(TriggerType = exports.TriggerType || (exports.TriggerType = {}));
|
29
|
+
function parseBotRequest(request) {
|
30
|
+
const eventName = request.get('X-GitHub-Event') || '';
|
31
|
+
const githubDeliveryId = request.get('X-GitHub-Delivery') || '';
|
32
|
+
const signature = parseSignatureHeader(request);
|
33
|
+
const taskName = request.get('X-CloudTasks-TaskName');
|
34
|
+
const taskRetryCount = parseInt(request.get('X-CloudTasks-TaskRetryCount') || '0');
|
35
|
+
const triggerType = parseTriggerType(eventName, taskName);
|
36
|
+
const traceId = parseTraceId(request);
|
37
|
+
return {
|
38
|
+
eventName,
|
39
|
+
githubDeliveryId,
|
40
|
+
signature,
|
41
|
+
taskName,
|
42
|
+
taskRetryCount,
|
43
|
+
traceId,
|
44
|
+
triggerType,
|
45
|
+
};
|
46
|
+
}
|
47
|
+
exports.parseBotRequest = parseBotRequest;
|
48
|
+
/**
|
49
|
+
* Determine the type of trigger that started this execution
|
50
|
+
* @param eventName event name from header
|
51
|
+
* @param taskName task id from header
|
52
|
+
*/
|
53
|
+
function parseTriggerType(eventName, taskName) {
|
54
|
+
if (!taskName && custom_events_1.SCHEDULER_EVENT_NAMES.includes(eventName)) {
|
55
|
+
return TriggerType.SCHEDULER;
|
56
|
+
}
|
57
|
+
else if (!taskName && eventName === 'pubsub.message') {
|
58
|
+
return TriggerType.PUBSUB;
|
59
|
+
}
|
60
|
+
else if (!taskName && eventName) {
|
61
|
+
return TriggerType.GITHUB;
|
62
|
+
}
|
63
|
+
else if (eventName) {
|
64
|
+
return TriggerType.TASK;
|
65
|
+
}
|
66
|
+
return TriggerType.UNKNOWN;
|
67
|
+
}
|
68
|
+
/**
|
69
|
+
* Parse the signature from the request headers.
|
70
|
+
*
|
71
|
+
* If the expected header is not set, returns `unset` because the verification
|
72
|
+
* function throws an exception on empty string when we would rather
|
73
|
+
* treat the error as an invalid signature.
|
74
|
+
* @param request incoming trigger request
|
75
|
+
*/
|
76
|
+
function parseSignatureHeader(request) {
|
77
|
+
const sha1Signature = request.get('X-Hub-Signature');
|
78
|
+
if (sha1Signature) {
|
79
|
+
return sha1Signature;
|
80
|
+
}
|
81
|
+
return 'unset';
|
82
|
+
}
|
83
|
+
/**
|
84
|
+
* Parse the trace id from the trace context header. The format of the header
|
85
|
+
* looks something like `<trace-id>/<span-id>;o=<options-flags>`.
|
86
|
+
* @param request incoming trigger request
|
87
|
+
*/
|
88
|
+
function parseTraceId(request) {
|
89
|
+
const traceContext = request.get('X-Cloud-Trace-Context');
|
90
|
+
if (!traceContext) {
|
91
|
+
return undefined;
|
92
|
+
}
|
93
|
+
const parts = traceContext.split('/');
|
94
|
+
if (parts.length === 0 || !parts[0]) {
|
95
|
+
return undefined;
|
96
|
+
}
|
97
|
+
return parts[0];
|
98
|
+
}
|
99
|
+
//# sourceMappingURL=bot-request.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"bot-request.js","sourceRoot":"","sources":["../../src/bot-request.ts"],"names":[],"mappings":";AAAA,4BAA4B;AAC5B,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,kDAAkD;AAClD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;;;AAGjC,mDAAsD;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,CAAC,MAAM,CAAC;KAC3B;SAAM,IAAI,SAAS,EAAE;QACpB,OAAO,WAAW,CAAC,IAAI,CAAC;KACzB;IACD,OAAO,WAAW,CAAC,OAAO,CAAC;AAC7B,CAAC;AAED;;;;;;;GAOG;AACH,SAAS,oBAAoB,CAAC,OAAwB;IACpD,MAAM,aAAa,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC,CAAC;IACrD,IAAI,aAAa,EAAE;QACjB,OAAO,aAAa,CAAC;KACtB;IACD,OAAO,OAAO,CAAC;AACjB,CAAC;AAED;;;;GAIG;AACH,SAAS,YAAY,CAAC,OAAwB;IAC5C,MAAM,YAAY,GAAG,OAAO,CAAC,GAAG,CAAC,uBAAuB,CAAC,CAAC;IAC1D,IAAI,CAAC,YAAY,EAAE;QACjB,OAAO,SAAS,CAAC;KAClB;IAED,MAAM,KAAK,GAAG,YAAY,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IACtC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE;QACnC,OAAO,SAAS,CAAC;KAClB;IAED,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;AAClB,CAAC"}
|
@@ -0,0 +1,4 @@
|
|
1
|
+
export declare const SCHEDULER_GLOBAL_EVENT_NAME = "schedule.global";
|
2
|
+
export declare const SCHEDULER_INSTALLATION_EVENT_NAME = "schedule.installation";
|
3
|
+
export declare const SCHEDULER_REPOSITORY_EVENT_NAME = "schedule.repository";
|
4
|
+
export declare const SCHEDULER_EVENT_NAMES: string[];
|
@@ -0,0 +1,25 @@
|
|
1
|
+
"use strict";
|
2
|
+
// Copyright 2022 Google LLC
|
3
|
+
//
|
4
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
5
|
+
// you may not use this file except in compliance with the License.
|
6
|
+
// You may obtain a copy of the License at
|
7
|
+
//
|
8
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
9
|
+
//
|
10
|
+
// Unless required by applicable law or agreed to in writing, software
|
11
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
12
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
13
|
+
// See the License for the specific language governing permissions and
|
14
|
+
// limitations under the License.
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
16
|
+
exports.SCHEDULER_EVENT_NAMES = exports.SCHEDULER_REPOSITORY_EVENT_NAME = exports.SCHEDULER_INSTALLATION_EVENT_NAME = exports.SCHEDULER_GLOBAL_EVENT_NAME = void 0;
|
17
|
+
exports.SCHEDULER_GLOBAL_EVENT_NAME = 'schedule.global';
|
18
|
+
exports.SCHEDULER_INSTALLATION_EVENT_NAME = 'schedule.installation';
|
19
|
+
exports.SCHEDULER_REPOSITORY_EVENT_NAME = 'schedule.repository';
|
20
|
+
exports.SCHEDULER_EVENT_NAMES = [
|
21
|
+
exports.SCHEDULER_GLOBAL_EVENT_NAME,
|
22
|
+
exports.SCHEDULER_INSTALLATION_EVENT_NAME,
|
23
|
+
exports.SCHEDULER_REPOSITORY_EVENT_NAME,
|
24
|
+
];
|
25
|
+
//# sourceMappingURL=custom-events.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"custom-events.js","sourceRoot":"","sources":["../../src/custom-events.ts"],"names":[],"mappings":";AAAA,4BAA4B;AAC5B,EAAE;AACF,kEAAkE;AAClE,mEAAmE;AACnE,0CAA0C;AAC1C,EAAE;AACF,kDAAkD;AAClD,EAAE;AACF,sEAAsE;AACtE,oEAAoE;AACpE,2EAA2E;AAC3E,sEAAsE;AACtE,iCAAiC;;;AAEpB,QAAA,2BAA2B,GAAG,iBAAiB,CAAC;AAChD,QAAA,iCAAiC,GAAG,uBAAuB,CAAC;AAC5D,QAAA,+BAA+B,GAAG,qBAAqB,CAAC;AACxD,QAAA,qBAAqB,GAAG;IACnC,mCAA2B;IAC3B,yCAAiC;IACjC,uCAA+B;CAChC,CAAC"}
|
package/build/src/gcf-utils.d.ts
CHANGED
@@ -8,6 +8,7 @@ import { Storage } from '@google-cloud/storage';
|
|
8
8
|
import * as express from 'express';
|
9
9
|
import { Octokit } from '@octokit/rest';
|
10
10
|
import { GCFLogger } from './logging/gcf-logger';
|
11
|
+
export { TriggerType } from './bot-request';
|
11
12
|
export interface RequestWithRawBody extends express.Request {
|
12
13
|
rawBody?: Buffer;
|
13
14
|
}
|
@@ -48,16 +49,6 @@ export interface CronPayload {
|
|
48
49
|
};
|
49
50
|
cron_org: string;
|
50
51
|
}
|
51
|
-
/**
|
52
|
-
* Type of function execution trigger
|
53
|
-
*/
|
54
|
-
export declare enum TriggerType {
|
55
|
-
GITHUB = "GitHub Webhook",
|
56
|
-
SCHEDULER = "Cloud Scheduler",
|
57
|
-
TASK = "Cloud Task",
|
58
|
-
PUBSUB = "Pub/Sub",
|
59
|
-
UNKNOWN = "Unknown"
|
60
|
-
}
|
61
52
|
/**
|
62
53
|
* It creates a comment string used for `addOrUpdateissuecomment`.
|
63
54
|
*/
|
@@ -105,26 +96,6 @@ export declare class GCFBootstrapper {
|
|
105
96
|
getSecretName(): string;
|
106
97
|
getLatestSecretVersionName(): string;
|
107
98
|
getProbotConfig(logging?: boolean): Promise<Options>;
|
108
|
-
/**
|
109
|
-
* Parse the signature from the request headers.
|
110
|
-
*
|
111
|
-
* If the expected header is not set, returns `unset` because the verification
|
112
|
-
* function throws an exception on empty string when we would rather
|
113
|
-
* treat the error as an invalid signature.
|
114
|
-
* @param request incoming trigger request
|
115
|
-
*/
|
116
|
-
private static parseSignatureHeader;
|
117
|
-
/**
|
118
|
-
* Parse the event name, delivery id, signature and task id from the request headers
|
119
|
-
* @param request incoming trigger request
|
120
|
-
*/
|
121
|
-
private static parseRequestHeaders;
|
122
|
-
/**
|
123
|
-
* Determine the type of trigger that started this execution
|
124
|
-
* @param name event name from header
|
125
|
-
* @param taskId task id from header
|
126
|
-
*/
|
127
|
-
private static parseTriggerType;
|
128
99
|
private parseWrapConfig;
|
129
100
|
private getRetryLimit;
|
130
101
|
/**
|
@@ -141,6 +112,7 @@ export declare class GCFBootstrapper {
|
|
141
112
|
* @param wrapOptions {WrapOptions} Bot handler options
|
142
113
|
*/
|
143
114
|
gcf(appFn: ApplicationFunction, wrapOptions?: WrapOptions): HandlerFunction;
|
115
|
+
private buildLoggerBindings;
|
144
116
|
/**
|
145
117
|
* Entrypoint for handling all scheduled tasks.
|
146
118
|
*
|
@@ -236,4 +208,4 @@ export declare class GCFBootstrapper {
|
|
236
208
|
private maybeWriteBodyToTmp;
|
237
209
|
private maybeDownloadOriginalBody;
|
238
210
|
}
|
239
|
-
export
|
211
|
+
export declare function getContextLogger(context: any): GCFLogger;
|
package/build/src/gcf-utils.js
CHANGED
@@ -3,7 +3,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
4
|
};
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
-
exports.GCFBootstrapper = exports.addOrUpdateIssueComment = exports.getCommentMark = exports.
|
6
|
+
exports.getContextLogger = exports.GCFBootstrapper = exports.addOrUpdateIssueComment = exports.getCommentMark = exports.logger = exports.TriggerType = void 0;
|
7
7
|
// Copyright 2019 Google LLC
|
8
8
|
//
|
9
9
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
@@ -34,17 +34,13 @@ const uuid_1 = require("uuid");
|
|
34
34
|
const server_1 = require("./server/server");
|
35
35
|
const run_1 = require("@googleapis/run");
|
36
36
|
const google_auth_library_1 = require("google-auth-library");
|
37
|
+
const bot_request_1 = require("./bot-request");
|
38
|
+
const custom_events_1 = require("./custom-events");
|
39
|
+
var bot_request_2 = require("./bot-request");
|
40
|
+
Object.defineProperty(exports, "TriggerType", { enumerable: true, get: function () { return bot_request_2.TriggerType; } });
|
37
41
|
// eslint-disable-next-line @typescript-eslint/no-var-requires
|
38
42
|
const LoggingOctokitPlugin = require('../src/logging/logging-octokit-plugin.js');
|
39
43
|
const DEFAULT_CRON_TYPE = 'repository';
|
40
|
-
const SCHEDULER_GLOBAL_EVENT_NAME = 'schedule.global';
|
41
|
-
const SCHEDULER_INSTALLATION_EVENT_NAME = 'schedule.installation';
|
42
|
-
const SCHEDULER_REPOSITORY_EVENT_NAME = 'schedule.repository';
|
43
|
-
const SCHEDULER_EVENT_NAMES = [
|
44
|
-
SCHEDULER_GLOBAL_EVENT_NAME,
|
45
|
-
SCHEDULER_INSTALLATION_EVENT_NAME,
|
46
|
-
SCHEDULER_REPOSITORY_EVENT_NAME,
|
47
|
-
];
|
48
44
|
const RUNNING_IN_TEST = process.env.NODE_ENV === 'test';
|
49
45
|
const DEFAULT_TASK_CALLER = 'task-caller@repo-automation-bots.iam.gserviceaccount.com';
|
50
46
|
const DEFAULT_WRAP_CONFIG = {
|
@@ -55,17 +51,6 @@ const DEFAULT_WRAP_CONFIG = {
|
|
55
51
|
maxPubSubRetries: 0,
|
56
52
|
};
|
57
53
|
exports.logger = new gcf_logger_1.GCFLogger();
|
58
|
-
/**
|
59
|
-
* Type of function execution trigger
|
60
|
-
*/
|
61
|
-
var TriggerType;
|
62
|
-
(function (TriggerType) {
|
63
|
-
TriggerType["GITHUB"] = "GitHub Webhook";
|
64
|
-
TriggerType["SCHEDULER"] = "Cloud Scheduler";
|
65
|
-
TriggerType["TASK"] = "Cloud Task";
|
66
|
-
TriggerType["PUBSUB"] = "Pub/Sub";
|
67
|
-
TriggerType["UNKNOWN"] = "Unknown";
|
68
|
-
})(TriggerType = exports.TriggerType || (exports.TriggerType = {}));
|
69
54
|
/**
|
70
55
|
* It creates a comment string used for `addOrUpdateissuecomment`.
|
71
56
|
*/
|
@@ -89,7 +74,7 @@ exports.getCommentMark = getCommentMark;
|
|
89
74
|
*/
|
90
75
|
const addOrUpdateIssueComment = async (octokit, owner, repo, issueNumber, installationId, commentBody, onlyUpdate = false) => {
|
91
76
|
var _a;
|
92
|
-
const commentMark = exports.getCommentMark(installationId);
|
77
|
+
const commentMark = (0, exports.getCommentMark)(installationId);
|
93
78
|
const listCommentsResponse = await octokit.issues.listComments({
|
94
79
|
owner: owner,
|
95
80
|
repo: repo,
|
@@ -160,7 +145,7 @@ class GCFBootstrapper {
|
|
160
145
|
async loadProbot(appFn, logging) {
|
161
146
|
if (!this.probot) {
|
162
147
|
const cfg = await this.getProbotConfig(logging);
|
163
|
-
this.probot = probot_1.createProbot({ overrides: cfg });
|
148
|
+
this.probot = (0, probot_1.createProbot)({ overrides: cfg });
|
164
149
|
}
|
165
150
|
await this.probot.load(appFn);
|
166
151
|
return this.probot;
|
@@ -206,63 +191,11 @@ class GCFBootstrapper {
|
|
206
191
|
});
|
207
192
|
return {
|
208
193
|
...config,
|
194
|
+
log: exports.logger,
|
209
195
|
Octokit: DefaultOctokit,
|
210
196
|
};
|
211
197
|
}
|
212
198
|
}
|
213
|
-
/**
|
214
|
-
* Parse the signature from the request headers.
|
215
|
-
*
|
216
|
-
* If the expected header is not set, returns `unset` because the verification
|
217
|
-
* function throws an exception on empty string when we would rather
|
218
|
-
* treat the error as an invalid signature.
|
219
|
-
* @param request incoming trigger request
|
220
|
-
*/
|
221
|
-
static parseSignatureHeader(request) {
|
222
|
-
const sha1Signature = request.get('x-hub-signature') || request.get('X-Hub-Signature');
|
223
|
-
if (sha1Signature) {
|
224
|
-
return sha1Signature;
|
225
|
-
}
|
226
|
-
return 'unset';
|
227
|
-
}
|
228
|
-
/**
|
229
|
-
* Parse the event name, delivery id, signature and task id from the request headers
|
230
|
-
* @param request incoming trigger request
|
231
|
-
*/
|
232
|
-
static parseRequestHeaders(request) {
|
233
|
-
const name = request.get('x-github-event') || request.get('X-GitHub-Event') || '';
|
234
|
-
const id = request.get('x-github-delivery') ||
|
235
|
-
request.get('X-GitHub-Delivery') ||
|
236
|
-
'';
|
237
|
-
const signature = this.parseSignatureHeader(request);
|
238
|
-
const taskId = request.get('X-CloudTasks-TaskName') ||
|
239
|
-
request.get('x-cloudtasks-taskname') ||
|
240
|
-
'';
|
241
|
-
const taskRetries = parseInt(request.get('X-CloudTasks-TaskRetryCount') ||
|
242
|
-
request.get('x-cloudtasks-taskretrycount') ||
|
243
|
-
'0');
|
244
|
-
return { name, id, signature, taskId, taskRetries };
|
245
|
-
}
|
246
|
-
/**
|
247
|
-
* Determine the type of trigger that started this execution
|
248
|
-
* @param name event name from header
|
249
|
-
* @param taskId task id from header
|
250
|
-
*/
|
251
|
-
static parseTriggerType(name, taskId) {
|
252
|
-
if (!taskId && SCHEDULER_EVENT_NAMES.includes(name)) {
|
253
|
-
return TriggerType.SCHEDULER;
|
254
|
-
}
|
255
|
-
else if (!taskId && name === 'pubsub.message') {
|
256
|
-
return TriggerType.PUBSUB;
|
257
|
-
}
|
258
|
-
else if (!taskId && name) {
|
259
|
-
return TriggerType.GITHUB;
|
260
|
-
}
|
261
|
-
else if (name) {
|
262
|
-
return TriggerType.TASK;
|
263
|
-
}
|
264
|
-
return TriggerType.UNKNOWN;
|
265
|
-
}
|
266
199
|
parseWrapConfig(wrapOptions) {
|
267
200
|
const wrapConfig = {
|
268
201
|
...DEFAULT_WRAP_CONFIG,
|
@@ -294,7 +227,7 @@ class GCFBootstrapper {
|
|
294
227
|
* @param wrapOptions {WrapOptions} Bot handler options
|
295
228
|
*/
|
296
229
|
server(appFn, wrapOptions) {
|
297
|
-
return server_1.getServer(this.gcf(appFn, wrapOptions));
|
230
|
+
return (0, server_1.getServer)(this.gcf(appFn, wrapOptions));
|
298
231
|
}
|
299
232
|
/**
|
300
233
|
* Wrap an ApplicationFunction in so it can be started in a Google
|
@@ -307,13 +240,11 @@ class GCFBootstrapper {
|
|
307
240
|
const wrapConfig = this.parseWrapConfig(wrapOptions);
|
308
241
|
this.probot =
|
309
242
|
this.probot || (await this.loadProbot(appFn, wrapConfig.logging));
|
310
|
-
|
311
|
-
const
|
243
|
+
// parse all common fields from a bot request
|
244
|
+
const botRequest = (0, bot_request_1.parseBotRequest)(request);
|
312
245
|
// validate the signature
|
313
|
-
exports.logger.info(`request.rawbody: {request.rawbody}`);
|
314
246
|
if (!wrapConfig.skipVerification &&
|
315
|
-
!(await this.probot.webhooks.verify(request.rawBody ? request.rawBody.toString() : request.body, signature))) {
|
316
|
-
exports.logger.info('Signature verification failed');
|
247
|
+
!(await this.probot.webhooks.verify(request.rawBody ? request.rawBody.toString() : request.body, botRequest.signature))) {
|
317
248
|
response.status(400).send({
|
318
249
|
statusCode: 400,
|
319
250
|
body: JSON.stringify({ message: 'Invalid signature' }),
|
@@ -324,33 +255,34 @@ class GCFBootstrapper {
|
|
324
255
|
* Note: any logs written before resetting bindings may contain
|
325
256
|
* bindings from previous executions
|
326
257
|
*/
|
258
|
+
const loggerBindings = this.buildLoggerBindings(botRequest, request.body);
|
327
259
|
exports.logger.resetBindings();
|
328
|
-
exports.logger.addBindings(
|
260
|
+
exports.logger.addBindings(loggerBindings);
|
261
|
+
const requestLogger = exports.logger.child(loggerBindings);
|
329
262
|
try {
|
330
|
-
if (triggerType === TriggerType.UNKNOWN) {
|
331
|
-
exports.logger.info('Trigger type is unknown');
|
263
|
+
if (botRequest.triggerType === bot_request_1.TriggerType.UNKNOWN) {
|
332
264
|
response.sendStatus(400);
|
333
265
|
return;
|
334
266
|
}
|
335
|
-
else if (triggerType === TriggerType.SCHEDULER) {
|
267
|
+
else if (botRequest.triggerType === bot_request_1.TriggerType.SCHEDULER) {
|
336
268
|
// Cloud scheduler tasks (cron)
|
337
|
-
await this.handleScheduled(
|
269
|
+
await this.handleScheduled(botRequest.githubDeliveryId, request, wrapConfig);
|
338
270
|
}
|
339
|
-
else if (triggerType === TriggerType.PUBSUB) {
|
271
|
+
else if (botRequest.triggerType === bot_request_1.TriggerType.PUBSUB) {
|
340
272
|
const payload = this.parsePubSubPayload(request);
|
341
273
|
await this.enqueueTask({
|
342
|
-
id,
|
343
|
-
name,
|
274
|
+
id: botRequest.githubDeliveryId,
|
275
|
+
name: botRequest.eventName,
|
344
276
|
body: JSON.stringify(payload),
|
345
277
|
});
|
346
278
|
}
|
347
|
-
else if (triggerType === TriggerType.TASK) {
|
348
|
-
const maxRetries = this.getRetryLimit(wrapConfig,
|
279
|
+
else if (botRequest.triggerType === bot_request_1.TriggerType.TASK) {
|
280
|
+
const maxRetries = this.getRetryLimit(wrapConfig, botRequest.eventName);
|
349
281
|
// Abort task retries if we've hit the max number by
|
350
282
|
// returning "success"
|
351
|
-
if (
|
352
|
-
|
353
|
-
|
283
|
+
if (botRequest.taskRetryCount > maxRetries) {
|
284
|
+
requestLogger.metric('too-many-retries');
|
285
|
+
requestLogger.info(`Too many retries: ${botRequest.taskRetryCount} > ${maxRetries}`);
|
354
286
|
// return 200 so we don't retry the task again
|
355
287
|
response.send({
|
356
288
|
statusCode: 200,
|
@@ -364,28 +296,29 @@ class GCFBootstrapper {
|
|
364
296
|
// The payload does not exist, stop retrying on this task by letting
|
365
297
|
// this request "succeed".
|
366
298
|
if (!payload) {
|
367
|
-
|
299
|
+
requestLogger.metric('payload-expired');
|
368
300
|
response.send({
|
369
301
|
statusCode: 200,
|
370
302
|
body: JSON.stringify({ message: 'Payload expired' }),
|
371
303
|
});
|
372
304
|
return;
|
373
305
|
}
|
306
|
+
setContextLogger(payload, requestLogger);
|
374
307
|
// TODO: find out the best way to get this type, and whether we can
|
375
308
|
// keep using a custom event name.
|
376
309
|
await this.probot.receive({
|
377
310
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
378
|
-
name:
|
311
|
+
name: botRequest.eventName,
|
379
312
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
380
|
-
id:
|
313
|
+
id: botRequest.githubDeliveryId,
|
381
314
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
382
315
|
payload: payload,
|
383
316
|
});
|
384
317
|
}
|
385
|
-
else if (triggerType === TriggerType.GITHUB) {
|
318
|
+
else if (botRequest.triggerType === bot_request_1.TriggerType.GITHUB) {
|
386
319
|
await this.enqueueTask({
|
387
|
-
id,
|
388
|
-
name,
|
320
|
+
id: botRequest.githubDeliveryId,
|
321
|
+
name: botRequest.eventName,
|
389
322
|
body: JSON.stringify(request.body),
|
390
323
|
});
|
391
324
|
}
|
@@ -395,16 +328,30 @@ class GCFBootstrapper {
|
|
395
328
|
});
|
396
329
|
}
|
397
330
|
catch (err) {
|
398
|
-
|
331
|
+
requestLogger.error(err);
|
399
332
|
response.status(500).send({
|
400
333
|
statusCode: 500,
|
401
334
|
body: JSON.stringify({ message: err.message }),
|
402
335
|
});
|
403
336
|
return;
|
404
337
|
}
|
338
|
+
requestLogger.flushSync();
|
405
339
|
exports.logger.flushSync();
|
406
340
|
};
|
407
341
|
}
|
342
|
+
buildLoggerBindings(botRequest,
|
343
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
344
|
+
requestBody) {
|
345
|
+
const extras = {};
|
346
|
+
const triggerInfo = (0, trigger_info_builder_1.buildTriggerInfo)(botRequest, requestBody);
|
347
|
+
if (botRequest.traceId) {
|
348
|
+
extras['logging.googleapis.com/trace'] = `projects/${this.projectId}/traces/${botRequest.traceId}`;
|
349
|
+
}
|
350
|
+
return {
|
351
|
+
...triggerInfo,
|
352
|
+
...extras,
|
353
|
+
};
|
354
|
+
}
|
408
355
|
/**
|
409
356
|
* Entrypoint for handling all scheduled tasks.
|
410
357
|
*
|
@@ -440,7 +387,7 @@ class GCFBootstrapper {
|
|
440
387
|
async handleScheduledGlobal(id, body) {
|
441
388
|
await this.enqueueTask({
|
442
389
|
id,
|
443
|
-
name: SCHEDULER_GLOBAL_EVENT_NAME,
|
390
|
+
name: custom_events_1.SCHEDULER_GLOBAL_EVENT_NAME,
|
444
391
|
body: JSON.stringify(body),
|
445
392
|
});
|
446
393
|
}
|
@@ -501,7 +448,7 @@ class GCFBootstrapper {
|
|
501
448
|
if (body.installation) {
|
502
449
|
await this.enqueueTask({
|
503
450
|
id,
|
504
|
-
name: SCHEDULER_INSTALLATION_EVENT_NAME,
|
451
|
+
name: custom_events_1.SCHEDULER_INSTALLATION_EVENT_NAME,
|
505
452
|
body: JSON.stringify(body),
|
506
453
|
});
|
507
454
|
}
|
@@ -523,7 +470,7 @@ class GCFBootstrapper {
|
|
523
470
|
};
|
524
471
|
await this.enqueueTask({
|
525
472
|
id,
|
526
|
-
name: SCHEDULER_INSTALLATION_EVENT_NAME,
|
473
|
+
name: custom_events_1.SCHEDULER_INSTALLATION_EVENT_NAME,
|
527
474
|
body: JSON.stringify(payload),
|
528
475
|
});
|
529
476
|
}
|
@@ -549,7 +496,7 @@ class GCFBootstrapper {
|
|
549
496
|
var _a, _b;
|
550
497
|
if (body.repo) {
|
551
498
|
// Job was scheduled for a single repository:
|
552
|
-
await this.scheduledToTask(body.repo, id, body, SCHEDULER_REPOSITORY_EVENT_NAME);
|
499
|
+
await this.scheduledToTask(body.repo, id, body, custom_events_1.SCHEDULER_REPOSITORY_EVENT_NAME);
|
553
500
|
}
|
554
501
|
else if (body.installation) {
|
555
502
|
const generator = this.eachInstalledRepository(body.installation.id, wrapConfig);
|
@@ -559,7 +506,7 @@ class GCFBootstrapper {
|
|
559
506
|
if (repo.archived === true || repo.disabled === true) {
|
560
507
|
continue;
|
561
508
|
}
|
562
|
-
promises.push(this.scheduledToTask(repo.full_name, id, body, SCHEDULER_REPOSITORY_EVENT_NAME));
|
509
|
+
promises.push(this.scheduledToTask(repo.full_name, id, body, custom_events_1.SCHEDULER_REPOSITORY_EVENT_NAME));
|
563
510
|
if (promises.length >= batchSize) {
|
564
511
|
await Promise.all(promises);
|
565
512
|
promises.splice(0, promises.length);
|
@@ -601,7 +548,7 @@ class GCFBootstrapper {
|
|
601
548
|
if (repo.archived === true || repo.disabled === true) {
|
602
549
|
continue;
|
603
550
|
}
|
604
|
-
promises.push(this.scheduledToTask(repo.full_name, id, payload, SCHEDULER_REPOSITORY_EVENT_NAME));
|
551
|
+
promises.push(this.scheduledToTask(repo.full_name, id, payload, custom_events_1.SCHEDULER_REPOSITORY_EVENT_NAME));
|
605
552
|
if (promises.length >= batchSize) {
|
606
553
|
await Promise.all(promises);
|
607
554
|
promises.splice(0, promises.length);
|
@@ -716,7 +663,7 @@ class GCFBootstrapper {
|
|
716
663
|
scopes: ['https://www.googleapis.com/auth/cloud-platform'],
|
717
664
|
});
|
718
665
|
const authClient = await auth.getClient();
|
719
|
-
const client = await run_1.run({
|
666
|
+
const client = await (0, run_1.run)({
|
720
667
|
version: 'v1',
|
721
668
|
auth: authClient,
|
722
669
|
});
|
@@ -765,6 +712,7 @@ class GCFBootstrapper {
|
|
765
712
|
await this.cloudTasksClient.createTask({
|
766
713
|
parent: queuePath,
|
767
714
|
task: {
|
715
|
+
dispatchDeadline: { seconds: 60 * 30 },
|
768
716
|
httpRequest: {
|
769
717
|
httpMethod: 'POST',
|
770
718
|
headers: {
|
@@ -813,13 +761,13 @@ class GCFBootstrapper {
|
|
813
761
|
*/
|
814
762
|
async maybeWriteBodyToTmp(body) {
|
815
763
|
if (this.payloadBucket) {
|
816
|
-
const tmp = `${Date.now()}-${uuid_1.v4()}.txt`;
|
764
|
+
const tmp = `${Date.now()}-${(0, uuid_1.v4)()}.txt`;
|
817
765
|
const bucket = this.storage.bucket(this.payloadBucket);
|
818
766
|
const writeable = bucket.file(tmp).createWriteStream({
|
819
767
|
validation: !RUNNING_IN_TEST,
|
820
768
|
});
|
821
769
|
exports.logger.info(`uploading payload to ${tmp}`);
|
822
|
-
into_stream_1.default(body).pipe(writeable);
|
770
|
+
(0, into_stream_1.default)(body).pipe(writeable);
|
823
771
|
await new Promise((resolve, reject) => {
|
824
772
|
writeable.on('error', reject);
|
825
773
|
writeable.on('finish', resolve);
|
@@ -849,7 +797,7 @@ class GCFBootstrapper {
|
|
849
797
|
validation: !RUNNING_IN_TEST,
|
850
798
|
});
|
851
799
|
try {
|
852
|
-
const content = await get_stream_1.default(readable);
|
800
|
+
const content = await (0, get_stream_1.default)(readable);
|
853
801
|
exports.logger.info(`downloaded payload from ${payload.tmpUrl}`);
|
854
802
|
return JSON.parse(content);
|
855
803
|
}
|
@@ -868,4 +816,21 @@ class GCFBootstrapper {
|
|
868
816
|
}
|
869
817
|
}
|
870
818
|
exports.GCFBootstrapper = GCFBootstrapper;
|
819
|
+
const loggerCache = new WeakMap();
|
820
|
+
// Helper to inject the request logger
|
821
|
+
function setContextLogger(payload, logger) {
|
822
|
+
loggerCache.set(payload, logger);
|
823
|
+
}
|
824
|
+
// Helper to extract the request logger from the request payload.
|
825
|
+
// If gcf-utils wrapper did not provide a logger, fall back to the
|
826
|
+
// default logger.
|
827
|
+
function getContextLogger(context) {
|
828
|
+
const requestLogger = loggerCache.get(context === null || context === void 0 ? void 0 : context.payload);
|
829
|
+
if (!requestLogger) {
|
830
|
+
exports.logger.warn('Failed to find a context logger');
|
831
|
+
return exports.logger;
|
832
|
+
}
|
833
|
+
return requestLogger;
|
834
|
+
}
|
835
|
+
exports.getContextLogger = getContextLogger;
|
871
836
|
//# sourceMappingURL=gcf-utils.js.map
|