@backstage/plugin-events-backend-module-github 0.3.0 → 0.4.0-next.1
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/CHANGELOG.md +27 -0
- package/README.md +5 -16
- package/dist/http/createGithubSignatureValidator.cjs.js +34 -12
- package/dist/http/createGithubSignatureValidator.cjs.js.map +1 -1
- package/dist/index.cjs.js +0 -2
- package/dist/index.cjs.js.map +1 -1
- package/dist/index.d.ts +2 -16
- package/dist/service/eventsModuleGithubWebhook.cjs.js +8 -3
- package/dist/service/eventsModuleGithubWebhook.cjs.js.map +1 -1
- package/dist/util/createAppIdResolver.cjs.js +38 -0
- package/dist/util/createAppIdResolver.cjs.js.map +1 -0
- package/dist/util/octokitProviderService.cjs.js +67 -0
- package/dist/util/octokitProviderService.cjs.js.map +1 -0
- package/package.json +13 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
# @backstage/plugin-events-backend-module-github
|
|
2
2
|
|
|
3
|
+
## 0.4.0-next.1
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- ae249fc: **BREAKING**: Removed the `createGithubSignatureValidator` export.
|
|
8
|
+
|
|
9
|
+
Added support webhook validation based on `integrations.github.[].apps.[].webhookSecret`.
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- c7ef81c: Correct README installation instructions.
|
|
14
|
+
- Updated dependencies
|
|
15
|
+
- @backstage/backend-plugin-api@1.3.1-next.1
|
|
16
|
+
- @backstage/integration@1.16.4-next.1
|
|
17
|
+
- @backstage/config@1.3.2
|
|
18
|
+
- @backstage/types@1.2.1
|
|
19
|
+
- @backstage/plugin-events-node@0.4.11-next.1
|
|
20
|
+
|
|
21
|
+
## 0.3.1-next.0
|
|
22
|
+
|
|
23
|
+
### Patch Changes
|
|
24
|
+
|
|
25
|
+
- Updated dependencies
|
|
26
|
+
- @backstage/backend-plugin-api@1.3.1-next.0
|
|
27
|
+
- @backstage/plugin-events-node@0.4.11-next.0
|
|
28
|
+
- @backstage/config@1.3.2
|
|
29
|
+
|
|
3
30
|
## 0.3.0
|
|
4
31
|
|
|
5
32
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -27,16 +27,14 @@ Please find all possible webhook event types at the
|
|
|
27
27
|
yarn --cwd packages/backend add @backstage/plugin-events-backend-module-github
|
|
28
28
|
```
|
|
29
29
|
|
|
30
|
-
### Event Router
|
|
31
|
-
|
|
32
30
|
```ts
|
|
33
31
|
// packages/backend/src/index.ts
|
|
34
|
-
import
|
|
35
|
-
// ...
|
|
36
|
-
backend.add(eventsModuleGithubEventRouter);
|
|
32
|
+
backend.add(import('@backstage/plugin-events-backend-module-github'));
|
|
37
33
|
```
|
|
38
34
|
|
|
39
|
-
|
|
35
|
+
### Legacy Backend System
|
|
36
|
+
|
|
37
|
+
#### Event Router
|
|
40
38
|
|
|
41
39
|
```ts
|
|
42
40
|
// packages/backend/src/plugins/events.ts
|
|
@@ -44,16 +42,7 @@ const eventRouter = new GithubEventRouter({ events: env.events });
|
|
|
44
42
|
await eventRouter.subscribe();
|
|
45
43
|
```
|
|
46
44
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
```ts
|
|
50
|
-
// packages/backend/src/index.ts
|
|
51
|
-
import { eventsModuleGithubWebhook } from '@backstage/plugin-events-backend-module-github/alpha';
|
|
52
|
-
// ...
|
|
53
|
-
backend.add(eventsModuleGithubWebhook);
|
|
54
|
-
```
|
|
55
|
-
|
|
56
|
-
#### Legacy Backend System
|
|
45
|
+
#### Signature Validator
|
|
57
46
|
|
|
58
47
|
Add the signature validator for the topic `github`:
|
|
59
48
|
|
|
@@ -1,26 +1,48 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var integration = require('@backstage/integration');
|
|
3
4
|
var webhooksMethods = require('@octokit/webhooks-methods');
|
|
5
|
+
var createAppIdResolver = require('../util/createAppIdResolver.cjs.js');
|
|
4
6
|
|
|
5
|
-
function createGithubSignatureValidator(config) {
|
|
6
|
-
const
|
|
7
|
+
function createGithubSignatureValidator(config, octokitProvider) {
|
|
8
|
+
const integrations = integration.ScmIntegrations.fromConfig(config);
|
|
9
|
+
const githubAppSecrets = /* @__PURE__ */ new Map();
|
|
10
|
+
for (const integration of integrations.github.list()) {
|
|
11
|
+
for (const { appId, webhookSecret } of integration.config.apps ?? []) {
|
|
12
|
+
if (appId && webhookSecret) {
|
|
13
|
+
githubAppSecrets.set(appId, webhookSecret);
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
const genericSecret = config.getOptionalString(
|
|
7
18
|
"events.modules.github.webhookSecret"
|
|
8
19
|
);
|
|
9
|
-
if (!
|
|
20
|
+
if (!genericSecret && githubAppSecrets.size === 0) {
|
|
10
21
|
return void 0;
|
|
11
22
|
}
|
|
23
|
+
const appIdResolver = createAppIdResolver.createAppIdResolver(octokitProvider);
|
|
12
24
|
return async (request, context) => {
|
|
13
25
|
const signature = request.headers["x-hub-signature-256"];
|
|
14
|
-
if (
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
26
|
+
if (signature) {
|
|
27
|
+
const body = request.raw.body.toString(request.raw.encoding);
|
|
28
|
+
if (githubAppSecrets.size) {
|
|
29
|
+
const appId = await appIdResolver(request);
|
|
30
|
+
if (appId && githubAppSecrets.has(appId)) {
|
|
31
|
+
if (await webhooksMethods.verify(githubAppSecrets.get(appId), body, signature)) {
|
|
32
|
+
return;
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (genericSecret) {
|
|
37
|
+
if (await webhooksMethods.verify(genericSecret, body, signature)) {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
}
|
|
23
41
|
}
|
|
42
|
+
context.reject({
|
|
43
|
+
status: 403,
|
|
44
|
+
payload: { message: "invalid signature" }
|
|
45
|
+
});
|
|
24
46
|
};
|
|
25
47
|
}
|
|
26
48
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"createGithubSignatureValidator.cjs.js","sources":["../../src/http/createGithubSignatureValidator.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport {\n RequestDetails,\n RequestValidationContext,\n RequestValidator,\n} from '@backstage/plugin-events-node';\nimport { verify } from '@octokit/webhooks-methods';\n\n/**\n * Validates that the request received is the expected GitHub request\n * using the signature received with the `x-hub-signature-256` header\n * which is based on a secret token configured at GitHub and here.\n *\n * See https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks\n * for more details.\n *\n * @param config - root config\n * @public\n */\nexport function createGithubSignatureValidator(\n config: Config,\n): RequestValidator | undefined {\n const secret = config.getOptionalString(\n 'events.modules.github.webhookSecret',\n );\n if (!
|
|
1
|
+
{"version":3,"file":"createGithubSignatureValidator.cjs.js","sources":["../../src/http/createGithubSignatureValidator.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { Config } from '@backstage/config';\nimport { ScmIntegrations } from '@backstage/integration';\nimport {\n RequestDetails,\n RequestValidationContext,\n RequestValidator,\n} from '@backstage/plugin-events-node';\nimport { verify } from '@octokit/webhooks-methods';\nimport { createAppIdResolver } from '../util/createAppIdResolver';\nimport { OctokitProviderService } from '../util/octokitProviderService';\n\n/**\n * Validates that the request received is the expected GitHub request\n * using the signature received with the `x-hub-signature-256` header\n * which is based on a secret token configured at GitHub and here.\n *\n * See https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks\n * for more details.\n *\n * @param config - root config\n * @public\n */\nexport function createGithubSignatureValidator(\n config: Config,\n octokitProvider: OctokitProviderService,\n): RequestValidator | undefined {\n const integrations = ScmIntegrations.fromConfig(config);\n\n // GitHub App installation ID to secret\n const githubAppSecrets = new Map<number, string>();\n for (const integration of integrations.github.list()) {\n for (const { appId, webhookSecret } of integration.config.apps ?? []) {\n if (appId && webhookSecret) {\n githubAppSecrets.set(appId, webhookSecret);\n }\n }\n }\n\n // A single optional secret for all GitHub events\n const genericSecret = config.getOptionalString(\n 'events.modules.github.webhookSecret',\n );\n\n if (!genericSecret && githubAppSecrets.size === 0) {\n return undefined;\n }\n\n const appIdResolver = createAppIdResolver(octokitProvider);\n\n return async (\n request: RequestDetails,\n context: RequestValidationContext,\n ): Promise<void> => {\n const signature = request.headers['x-hub-signature-256'] as\n | string\n | undefined;\n\n if (signature) {\n const body = request.raw.body.toString(request.raw.encoding);\n\n if (githubAppSecrets.size) {\n const appId = await appIdResolver(request);\n if (appId && githubAppSecrets.has(appId)) {\n if (await verify(githubAppSecrets.get(appId)!, body, signature)) {\n return;\n }\n }\n }\n\n if (genericSecret) {\n if (await verify(genericSecret, body, signature)) {\n return;\n }\n }\n }\n\n context.reject({\n status: 403,\n payload: { message: 'invalid signature' },\n });\n };\n}\n"],"names":["ScmIntegrations","createAppIdResolver","verify"],"mappings":";;;;;;AAsCgB,SAAA,8BAAA,CACd,QACA,eAC8B,EAAA;AAC9B,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AAI,MAAMA,sBAAA,CAAO,aAAe,EAAA,IAAA,EAAM,SAAS,CAAG,EAAA;AAChD,UAAA;AAAA;AACF;AACF;AAGF,IAAA,OAAA,CAAQ,MAAO,CAAA;AAAA,MACb,MAAQ,EAAA,GAAA;AAAA,MACR,OAAA,EAAS,EAAE,OAAA,EAAS,mBAAoB;AAAA,KACzC,CAAA;AAAA,GACH;AACF;;;;"}
|
package/dist/index.cjs.js
CHANGED
|
@@ -3,7 +3,6 @@
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
5
|
var backendPluginApi = require('@backstage/backend-plugin-api');
|
|
6
|
-
var createGithubSignatureValidator = require('./http/createGithubSignatureValidator.cjs.js');
|
|
7
6
|
var GithubEventRouter = require('./router/GithubEventRouter.cjs.js');
|
|
8
7
|
|
|
9
8
|
var index = backendPluginApi.createBackendFeatureLoader({
|
|
@@ -15,7 +14,6 @@ var index = backendPluginApi.createBackendFeatureLoader({
|
|
|
15
14
|
}
|
|
16
15
|
});
|
|
17
16
|
|
|
18
|
-
exports.createGithubSignatureValidator = createGithubSignatureValidator.createGithubSignatureValidator;
|
|
19
17
|
exports.GithubEventRouter = GithubEventRouter.GithubEventRouter;
|
|
20
18
|
exports.default = index;
|
|
21
19
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/index.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * The module `github` for the Backstage backend plugin \"events-backend\"\n * adding an event router and signature validator for GitHub.\n *\n * @packageDocumentation\n */\nimport { createBackendFeatureLoader } from '@backstage/backend-plugin-api';\n\nexport default createBackendFeatureLoader({\n loader() {\n return [\n import('./service/eventsModuleGithubEventRouter'),\n import('./service/eventsModuleGithubWebhook'),\n ];\n },\n});\n\
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":["../src/index.ts"],"sourcesContent":["/*\n * Copyright 2020 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\n/**\n * The module `github` for the Backstage backend plugin \"events-backend\"\n * adding an event router and signature validator for GitHub.\n *\n * @packageDocumentation\n */\n\nimport { createBackendFeatureLoader } from '@backstage/backend-plugin-api';\n\nexport default createBackendFeatureLoader({\n loader() {\n return [\n import('./service/eventsModuleGithubEventRouter'),\n import('./service/eventsModuleGithubWebhook'),\n ];\n },\n});\n\n// TODO(freben): This is not exported at the moment since it depends on the octokit provider.\n// Until we have made that a core thing in integrations, we can't export it\n// export { createGithubSignatureValidator } from './http/createGithubSignatureValidator';\n\nexport { GithubEventRouter } from './router/GithubEventRouter';\n"],"names":["createBackendFeatureLoader"],"mappings":";;;;;;;AAyBA,YAAeA,2CAA2B,CAAA;AAAA,EACxC,MAAS,GAAA;AACP,IAAO,OAAA;AAAA,MACL,OAAO,gDAAyC,CAAA;AAAA,MAChD,OAAO,4CAAqC;AAAA,KAC9C;AAAA;AAEJ,CAAC,CAAA;;;;;"}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,19 +1,5 @@
|
|
|
1
1
|
import * as _backstage_backend_plugin_api from '@backstage/backend-plugin-api';
|
|
2
|
-
import {
|
|
3
|
-
import { RequestValidator, SubTopicEventRouter, EventsService, EventParams } from '@backstage/plugin-events-node';
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Validates that the request received is the expected GitHub request
|
|
7
|
-
* using the signature received with the `x-hub-signature-256` header
|
|
8
|
-
* which is based on a secret token configured at GitHub and here.
|
|
9
|
-
*
|
|
10
|
-
* See https://docs.github.com/en/developers/webhooks-and-events/webhooks/securing-your-webhooks
|
|
11
|
-
* for more details.
|
|
12
|
-
*
|
|
13
|
-
* @param config - root config
|
|
14
|
-
* @public
|
|
15
|
-
*/
|
|
16
|
-
declare function createGithubSignatureValidator(config: Config): RequestValidator | undefined;
|
|
2
|
+
import { SubTopicEventRouter, EventsService, EventParams } from '@backstage/plugin-events-node';
|
|
17
3
|
|
|
18
4
|
/**
|
|
19
5
|
* Subscribes to the generic `github` topic
|
|
@@ -32,4 +18,4 @@ declare class GithubEventRouter extends SubTopicEventRouter {
|
|
|
32
18
|
|
|
33
19
|
declare const _default: _backstage_backend_plugin_api.BackendFeature;
|
|
34
20
|
|
|
35
|
-
export { GithubEventRouter,
|
|
21
|
+
export { GithubEventRouter, _default as default };
|
|
@@ -5,6 +5,7 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var backendPluginApi = require('@backstage/backend-plugin-api');
|
|
6
6
|
var alpha = require('@backstage/plugin-events-node/alpha');
|
|
7
7
|
var createGithubSignatureValidator = require('../http/createGithubSignatureValidator.cjs.js');
|
|
8
|
+
var octokitProviderService = require('../util/octokitProviderService.cjs.js');
|
|
8
9
|
|
|
9
10
|
var eventsModuleGithubWebhook = backendPluginApi.createBackendModule({
|
|
10
11
|
pluginId: "events",
|
|
@@ -13,10 +14,14 @@ var eventsModuleGithubWebhook = backendPluginApi.createBackendModule({
|
|
|
13
14
|
env.registerInit({
|
|
14
15
|
deps: {
|
|
15
16
|
config: backendPluginApi.coreServices.rootConfig,
|
|
16
|
-
events: alpha.eventsExtensionPoint
|
|
17
|
+
events: alpha.eventsExtensionPoint,
|
|
18
|
+
octokitProvider: octokitProviderService.octokitProviderServiceRef
|
|
17
19
|
},
|
|
18
|
-
async init({ config, events }) {
|
|
19
|
-
const validator = createGithubSignatureValidator.createGithubSignatureValidator(
|
|
20
|
+
async init({ config, events, octokitProvider }) {
|
|
21
|
+
const validator = createGithubSignatureValidator.createGithubSignatureValidator(
|
|
22
|
+
config,
|
|
23
|
+
octokitProvider
|
|
24
|
+
);
|
|
20
25
|
if (validator) {
|
|
21
26
|
events.addHttpPostIngress({
|
|
22
27
|
topic: "github",
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eventsModuleGithubWebhook.cjs.js","sources":["../../src/service/eventsModuleGithubWebhook.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createBackendModule,\n} from '@backstage/backend-plugin-api';\nimport { eventsExtensionPoint } from '@backstage/plugin-events-node/alpha';\nimport { createGithubSignatureValidator } from '../http/createGithubSignatureValidator';\n\n/**\n * Module for the events-backend plugin,\n * registering an HTTP POST ingress with request validator\n * which verifies the webhook signature based on a secret.\n *\n * @public\n */\nexport default createBackendModule({\n pluginId: 'events',\n moduleId: 'github-webhook',\n register(env) {\n env.registerInit({\n deps: {\n config: coreServices.rootConfig,\n events: eventsExtensionPoint,\n },\n async init({ config, events }) {\n const validator = createGithubSignatureValidator(config);\n if (validator) {\n events.addHttpPostIngress({\n topic: 'github',\n validator,\n });\n }\n },\n });\n },\n});\n"],"names":["createBackendModule","coreServices","eventsExtensionPoint","createGithubSignatureValidator"],"mappings":"
|
|
1
|
+
{"version":3,"file":"eventsModuleGithubWebhook.cjs.js","sources":["../../src/service/eventsModuleGithubWebhook.ts"],"sourcesContent":["/*\n * Copyright 2022 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createBackendModule,\n} from '@backstage/backend-plugin-api';\nimport { eventsExtensionPoint } from '@backstage/plugin-events-node/alpha';\nimport { createGithubSignatureValidator } from '../http/createGithubSignatureValidator';\nimport { octokitProviderServiceRef } from '../util/octokitProviderService';\n\n/**\n * Module for the events-backend plugin,\n * registering an HTTP POST ingress with request validator\n * which verifies the webhook signature based on a secret.\n *\n * @public\n */\nexport default createBackendModule({\n pluginId: 'events',\n moduleId: 'github-webhook',\n register(env) {\n env.registerInit({\n deps: {\n config: coreServices.rootConfig,\n events: eventsExtensionPoint,\n octokitProvider: octokitProviderServiceRef,\n },\n async init({ config, events, octokitProvider }) {\n const validator = createGithubSignatureValidator(\n config,\n octokitProvider,\n );\n if (validator) {\n events.addHttpPostIngress({\n topic: 'github',\n validator,\n });\n }\n },\n });\n },\n});\n"],"names":["createBackendModule","coreServices","eventsExtensionPoint","octokitProviderServiceRef","createGithubSignatureValidator"],"mappings":";;;;;;;;;AA+BA,gCAAeA,oCAAoB,CAAA;AAAA,EACjC,QAAU,EAAA,QAAA;AAAA,EACV,QAAU,EAAA,gBAAA;AAAA,EACV,SAAS,GAAK,EAAA;AACZ,IAAA,GAAA,CAAI,YAAa,CAAA;AAAA,MACf,IAAM,EAAA;AAAA,QACJ,QAAQC,6BAAa,CAAA,UAAA;AAAA,QACrB,MAAQ,EAAAC,0BAAA;AAAA,QACR,eAAiB,EAAAC;AAAA,OACnB;AAAA,MACA,MAAM,IAAK,CAAA,EAAE,MAAQ,EAAA,MAAA,EAAQ,iBAAmB,EAAA;AAC9C,QAAA,MAAM,SAAY,GAAAC,6DAAA;AAAA,UAChB,MAAA;AAAA,UACA;AAAA,SACF;AACA,QAAA,IAAI,SAAW,EAAA;AACb,UAAA,MAAA,CAAO,kBAAmB,CAAA;AAAA,YACxB,KAAO,EAAA,QAAA;AAAA,YACP;AAAA,WACD,CAAA;AAAA;AACH;AACF,KACD,CAAA;AAAA;AAEL,CAAC,CAAA;;;;"}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var lodash = require('lodash');
|
|
4
|
+
|
|
5
|
+
function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
|
|
6
|
+
|
|
7
|
+
var lodash__default = /*#__PURE__*/_interopDefaultCompat(lodash);
|
|
8
|
+
|
|
9
|
+
function createAppIdResolver(octokitProvider) {
|
|
10
|
+
const installationIdToAppId = /* @__PURE__ */ new Map();
|
|
11
|
+
return async (request) => {
|
|
12
|
+
const installationId = lodash__default.default.get(
|
|
13
|
+
request.body,
|
|
14
|
+
"installation.id"
|
|
15
|
+
);
|
|
16
|
+
if (!installationId || typeof installationId !== "number") {
|
|
17
|
+
return void 0;
|
|
18
|
+
}
|
|
19
|
+
let appIdPromsie = installationIdToAppId.get(installationId);
|
|
20
|
+
if (appIdPromsie) {
|
|
21
|
+
return await appIdPromsie;
|
|
22
|
+
}
|
|
23
|
+
const repositoryUrl = lodash__default.default.get(
|
|
24
|
+
request.body,
|
|
25
|
+
"repository.html_url"
|
|
26
|
+
);
|
|
27
|
+
if (!repositoryUrl || typeof repositoryUrl !== "string") {
|
|
28
|
+
return void 0;
|
|
29
|
+
}
|
|
30
|
+
const octokit = await octokitProvider.getOctokit(repositoryUrl);
|
|
31
|
+
appIdPromsie = octokit.rest.apps.getInstallation({ installation_id: installationId }).then((response) => Number(response.data.app_id)).catch(() => void 0);
|
|
32
|
+
installationIdToAppId.set(installationId, appIdPromsie);
|
|
33
|
+
return await appIdPromsie;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
exports.createAppIdResolver = createAppIdResolver;
|
|
38
|
+
//# sourceMappingURL=createAppIdResolver.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"createAppIdResolver.cjs.js","sources":["../../src/util/createAppIdResolver.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { RequestDetails } from '@backstage/plugin-events-node';\nimport { OctokitProviderService } from './octokitProviderService';\nimport lodash from 'lodash';\n\nexport type AppIdResolver = (\n request: RequestDetails,\n) => Promise<number | undefined>;\n\n/**\n * Helps with resolving what app ID (if any) that sent a webhook event.\n */\nexport function createAppIdResolver(\n octokitProvider: OctokitProviderService,\n): AppIdResolver {\n const installationIdToAppId = new Map<number, Promise<number | undefined>>();\n\n return async (request: RequestDetails) => {\n const installationId = lodash.get(\n request.body,\n 'installation.id',\n ) as unknown;\n\n if (!installationId || typeof installationId !== 'number') {\n return undefined;\n }\n\n let appIdPromsie = installationIdToAppId.get(installationId);\n if (appIdPromsie) {\n return await appIdPromsie;\n }\n\n const repositoryUrl = lodash.get(\n request.body,\n 'repository.html_url',\n ) as unknown;\n\n if (!repositoryUrl || typeof repositoryUrl !== 'string') {\n return undefined;\n }\n\n const octokit = await octokitProvider.getOctokit(repositoryUrl);\n appIdPromsie = octokit.rest.apps\n .getInstallation({ installation_id: installationId })\n .then(response => Number(response.data.app_id))\n .catch(() => undefined);\n\n installationIdToAppId.set(installationId, appIdPromsie);\n return await appIdPromsie;\n };\n}\n"],"names":["lodash"],"mappings":";;;;;;;;AA2BO,SAAS,oBACd,eACe,EAAA;AACf,EAAM,MAAA,qBAAA,uBAA4B,GAAyC,EAAA;AAE3E,EAAA,OAAO,OAAO,OAA4B,KAAA;AACxC,IAAA,MAAM,iBAAiBA,uBAAO,CAAA,GAAA;AAAA,MAC5B,OAAQ,CAAA,IAAA;AAAA,MACR;AAAA,KACF;AAEA,IAAA,IAAI,CAAC,cAAA,IAAkB,OAAO,cAAA,KAAmB,QAAU,EAAA;AACzD,MAAO,OAAA,KAAA,CAAA;AAAA;AAGT,IAAI,IAAA,YAAA,GAAe,qBAAsB,CAAA,GAAA,CAAI,cAAc,CAAA;AAC3D,IAAA,IAAI,YAAc,EAAA;AAChB,MAAA,OAAO,MAAM,YAAA;AAAA;AAGf,IAAA,MAAM,gBAAgBA,uBAAO,CAAA,GAAA;AAAA,MAC3B,OAAQ,CAAA,IAAA;AAAA,MACR;AAAA,KACF;AAEA,IAAA,IAAI,CAAC,aAAA,IAAiB,OAAO,aAAA,KAAkB,QAAU,EAAA;AACvD,MAAO,OAAA,KAAA,CAAA;AAAA;AAGT,IAAA,MAAM,OAAU,GAAA,MAAM,eAAgB,CAAA,UAAA,CAAW,aAAa,CAAA;AAC9D,IAAA,YAAA,GAAe,QAAQ,IAAK,CAAA,IAAA,CACzB,gBAAgB,EAAE,eAAA,EAAiB,gBAAgB,CAAA,CACnD,KAAK,CAAY,QAAA,KAAA,MAAA,CAAO,SAAS,IAAK,CAAA,MAAM,CAAC,CAC7C,CAAA,KAAA,CAAM,MAAM,KAAS,CAAA,CAAA;AAExB,IAAsB,qBAAA,CAAA,GAAA,CAAI,gBAAgB,YAAY,CAAA;AACtD,IAAA,OAAO,MAAM,YAAA;AAAA,GACf;AACF;;;;"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var backendPluginApi = require('@backstage/backend-plugin-api');
|
|
4
|
+
var integration = require('@backstage/integration');
|
|
5
|
+
var types = require('@backstage/types');
|
|
6
|
+
var octokit = require('octokit');
|
|
7
|
+
|
|
8
|
+
class OctokitProviderImpl {
|
|
9
|
+
#integrations;
|
|
10
|
+
#githubCredentials;
|
|
11
|
+
#octokitCache;
|
|
12
|
+
#octokitCacheTtl;
|
|
13
|
+
constructor(config) {
|
|
14
|
+
this.#integrations = integration.ScmIntegrations.fromConfig(config);
|
|
15
|
+
this.#githubCredentials = integration.DefaultGithubCredentialsProvider.fromIntegrations(
|
|
16
|
+
this.#integrations
|
|
17
|
+
);
|
|
18
|
+
this.#octokitCache = /* @__PURE__ */ new Map();
|
|
19
|
+
this.#octokitCacheTtl = { hours: 1 };
|
|
20
|
+
}
|
|
21
|
+
async getOctokit(url) {
|
|
22
|
+
const integration = this.#integrations.github.byUrl(url);
|
|
23
|
+
if (!integration) {
|
|
24
|
+
throw new Error(`No integration found for url: ${url}`);
|
|
25
|
+
}
|
|
26
|
+
const key = integration.config.host;
|
|
27
|
+
if (this.#octokitCache.has(key)) {
|
|
28
|
+
return this.#octokitCache.get(key);
|
|
29
|
+
}
|
|
30
|
+
const { createCallbackAuth } = await import('@octokit/auth-callback');
|
|
31
|
+
const octokit$1 = new octokit.Octokit({
|
|
32
|
+
baseUrl: integration.config.apiBaseUrl,
|
|
33
|
+
authStrategy: createCallbackAuth,
|
|
34
|
+
auth: {
|
|
35
|
+
callback: async () => {
|
|
36
|
+
try {
|
|
37
|
+
const credentials = await this.#githubCredentials.getCredentials({
|
|
38
|
+
url
|
|
39
|
+
});
|
|
40
|
+
return credentials.token;
|
|
41
|
+
} catch {
|
|
42
|
+
return void 0;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
this.#octokitCache.set(key, octokit$1);
|
|
48
|
+
setTimeout(() => {
|
|
49
|
+
this.#octokitCache.delete(key);
|
|
50
|
+
}, types.durationToMilliseconds(this.#octokitCacheTtl));
|
|
51
|
+
return octokit$1;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
const octokitProviderServiceRef = backendPluginApi.createServiceRef({
|
|
55
|
+
id: "octokitProvider",
|
|
56
|
+
scope: "root",
|
|
57
|
+
defaultFactory: async (service) => backendPluginApi.createServiceFactory({
|
|
58
|
+
service,
|
|
59
|
+
deps: { config: backendPluginApi.coreServices.rootConfig },
|
|
60
|
+
async factory({ config }) {
|
|
61
|
+
return new OctokitProviderImpl(config);
|
|
62
|
+
}
|
|
63
|
+
})
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
exports.octokitProviderServiceRef = octokitProviderServiceRef;
|
|
67
|
+
//# sourceMappingURL=octokitProviderService.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"octokitProviderService.cjs.js","sources":["../../src/util/octokitProviderService.ts"],"sourcesContent":["/*\n * Copyright 2025 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport {\n coreServices,\n createServiceFactory,\n createServiceRef,\n RootConfigService,\n} from '@backstage/backend-plugin-api';\nimport {\n DefaultGithubCredentialsProvider,\n GithubCredentialsProvider,\n ScmIntegrationRegistry,\n ScmIntegrations,\n} from '@backstage/integration';\nimport { durationToMilliseconds, HumanDuration } from '@backstage/types';\nimport { Octokit } from 'octokit';\n\nexport interface OctokitProviderService {\n getOctokit: (url: string) => Promise<Octokit>;\n}\n\nclass OctokitProviderImpl implements OctokitProviderService {\n readonly #integrations: ScmIntegrationRegistry;\n readonly #githubCredentials: GithubCredentialsProvider;\n readonly #octokitCache: Map<string, Octokit>;\n readonly #octokitCacheTtl: HumanDuration;\n\n constructor(config: RootConfigService) {\n this.#integrations = ScmIntegrations.fromConfig(config);\n this.#githubCredentials = DefaultGithubCredentialsProvider.fromIntegrations(\n this.#integrations,\n );\n this.#octokitCache = new Map();\n this.#octokitCacheTtl = { hours: 1 };\n }\n\n async getOctokit(url: string): Promise<Octokit> {\n // TODO(freben): Be smart and cache these more granularly, e.g. by\n // organization or even repo.\n const integration = this.#integrations.github.byUrl(url);\n if (!integration) {\n throw new Error(`No integration found for url: ${url}`);\n }\n const key = integration.config.host;\n\n if (this.#octokitCache.has(key)) {\n return this.#octokitCache.get(key)!;\n }\n\n const { createCallbackAuth } = await import('@octokit/auth-callback');\n\n const octokit = new Octokit({\n baseUrl: integration.config.apiBaseUrl,\n authStrategy: createCallbackAuth,\n auth: {\n callback: async () => {\n try {\n const credentials = await this.#githubCredentials.getCredentials({\n url,\n });\n return credentials.token;\n } catch {\n return undefined;\n }\n },\n },\n });\n\n this.#octokitCache.set(key, octokit);\n setTimeout(() => {\n this.#octokitCache.delete(key);\n }, durationToMilliseconds(this.#octokitCacheTtl));\n\n return octokit;\n }\n}\n\nexport const octokitProviderServiceRef =\n createServiceRef<OctokitProviderService>({\n id: 'octokitProvider',\n scope: 'root',\n defaultFactory: async service =>\n createServiceFactory({\n service,\n deps: { config: coreServices.rootConfig },\n async factory({ config }) {\n return new OctokitProviderImpl(config);\n },\n }),\n });\n"],"names":["ScmIntegrations","DefaultGithubCredentialsProvider","octokit","Octokit","durationToMilliseconds","createServiceRef","createServiceFactory","coreServices"],"mappings":";;;;;;;AAmCA,MAAM,mBAAsD,CAAA;AAAA,EACjD,aAAA;AAAA,EACA,kBAAA;AAAA,EACA,aAAA;AAAA,EACA,gBAAA;AAAA,EAET,YAAY,MAA2B,EAAA;AACrC,IAAK,I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AA,CAAA,aAAA,CAAc,GAAI,CAAA,GAAA,EAAKD,SAAO,CAAA;AACnC,IAAA,UAAA,CAAW,MAAM;AACf,MAAK,IAAA,CAAA,aAAA,CAAc,OAAO,GAAG,CAAA;AAAA,KAC5B,EAAAE,4BAAA,CAAuB,IAAK,CAAA,gBAAgB,CAAC,CAAA;AAEhD,IAAO,OAAAF,SAAA;AAAA;AAEX;AAEO,MAAM,4BACXG,iCAAyC,CAAA;AAAA,EACvC,EAAI,EAAA,iBAAA;AAAA,EACJ,KAAO,EAAA,MAAA;AAAA,EACP,cAAA,EAAgB,OAAM,OAAA,KACpBC,qCAAqB,CAAA;AAAA,IACnB,OAAA;AAAA,IACA,IAAM,EAAA,EAAE,MAAQ,EAAAC,6BAAA,CAAa,UAAW,EAAA;AAAA,IACxC,MAAM,OAAA,CAAQ,EAAE,MAAA,EAAU,EAAA;AACxB,MAAO,OAAA,IAAI,oBAAoB,MAAM,CAAA;AAAA;AACvC,GACD;AACL,CAAC;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-events-backend-module-github",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0-next.1",
|
|
4
4
|
"backstage": {
|
|
5
5
|
"role": "backend-plugin-module",
|
|
6
6
|
"pluginId": "events",
|
|
@@ -51,15 +51,20 @@
|
|
|
51
51
|
"test": "backstage-cli package test"
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
|
-
"@backstage/backend-plugin-api": "
|
|
55
|
-
"@backstage/config": "
|
|
56
|
-
"@backstage/
|
|
57
|
-
"@
|
|
54
|
+
"@backstage/backend-plugin-api": "1.3.1-next.1",
|
|
55
|
+
"@backstage/config": "1.3.2",
|
|
56
|
+
"@backstage/integration": "1.16.4-next.1",
|
|
57
|
+
"@backstage/plugin-events-node": "0.4.11-next.1",
|
|
58
|
+
"@backstage/types": "1.2.1",
|
|
59
|
+
"@octokit/auth-callback": "^5.0.0",
|
|
60
|
+
"@octokit/webhooks-methods": "^3.0.0",
|
|
61
|
+
"lodash": "^4.17.21",
|
|
62
|
+
"octokit": "^3.0.0"
|
|
58
63
|
},
|
|
59
64
|
"devDependencies": {
|
|
60
|
-
"@backstage/backend-test-utils": "
|
|
61
|
-
"@backstage/cli": "
|
|
62
|
-
"@backstage/plugin-events-backend-test-utils": "
|
|
65
|
+
"@backstage/backend-test-utils": "1.5.0-next.1",
|
|
66
|
+
"@backstage/cli": "0.32.1-next.1",
|
|
67
|
+
"@backstage/plugin-events-backend-test-utils": "0.1.44-next.1"
|
|
63
68
|
},
|
|
64
69
|
"configSchema": "config.d.ts"
|
|
65
70
|
}
|