@backstage/plugin-catalog-backend-module-gerrit 0.1.11 → 0.1.12-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 +28 -0
- package/alpha/package.json +1 -1
- package/dist/alpha.cjs.js +8 -164
- package/dist/alpha.cjs.js.map +1 -1
- package/dist/cjs/GerritEntityProvider-941f29ac.cjs.js +168 -0
- package/dist/cjs/GerritEntityProvider-941f29ac.cjs.js.map +1 -0
- package/dist/index.cjs.js +8 -162
- package/dist/index.cjs.js.map +1 -1
- package/package.json +8 -8
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,33 @@
|
|
|
1
1
|
# @backstage/plugin-catalog-backend-module-gerrit
|
|
2
2
|
|
|
3
|
+
## 0.1.12-next.1
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies
|
|
8
|
+
- @backstage/backend-tasks@0.5.1-next.1
|
|
9
|
+
- @backstage/integration@1.4.4-next.0
|
|
10
|
+
- @backstage/backend-common@0.18.4-next.1
|
|
11
|
+
- @backstage/backend-plugin-api@0.5.1-next.1
|
|
12
|
+
- @backstage/catalog-model@1.2.1
|
|
13
|
+
- @backstage/config@1.0.7
|
|
14
|
+
- @backstage/errors@1.1.5
|
|
15
|
+
- @backstage/plugin-catalog-node@1.3.5-next.1
|
|
16
|
+
|
|
17
|
+
## 0.1.12-next.0
|
|
18
|
+
|
|
19
|
+
### Patch Changes
|
|
20
|
+
|
|
21
|
+
- Updated dependencies
|
|
22
|
+
- @backstage/backend-common@0.18.4-next.0
|
|
23
|
+
- @backstage/config@1.0.7
|
|
24
|
+
- @backstage/integration@1.4.3
|
|
25
|
+
- @backstage/backend-plugin-api@0.5.1-next.0
|
|
26
|
+
- @backstage/backend-tasks@0.5.1-next.0
|
|
27
|
+
- @backstage/catalog-model@1.2.1
|
|
28
|
+
- @backstage/errors@1.1.5
|
|
29
|
+
- @backstage/plugin-catalog-node@1.3.5-next.0
|
|
30
|
+
|
|
3
31
|
## 0.1.11
|
|
4
32
|
|
|
5
33
|
### Patch Changes
|
package/alpha/package.json
CHANGED
package/dist/alpha.cjs.js
CHANGED
|
@@ -5,169 +5,13 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
5
5
|
var backendCommon = require('@backstage/backend-common');
|
|
6
6
|
var backendPluginApi = require('@backstage/backend-plugin-api');
|
|
7
7
|
var alpha = require('@backstage/plugin-catalog-node/alpha');
|
|
8
|
-
var
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
16
|
-
|
|
17
|
-
function _interopNamespace(e) {
|
|
18
|
-
if (e && e.__esModule) return e;
|
|
19
|
-
var n = Object.create(null);
|
|
20
|
-
if (e) {
|
|
21
|
-
Object.keys(e).forEach(function (k) {
|
|
22
|
-
if (k !== 'default') {
|
|
23
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
24
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
25
|
-
enumerable: true,
|
|
26
|
-
get: function () { return e[k]; }
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
}
|
|
31
|
-
n["default"] = e;
|
|
32
|
-
return Object.freeze(n);
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
|
|
36
|
-
var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
|
|
37
|
-
|
|
38
|
-
function readGerritConfig(id, config) {
|
|
39
|
-
var _a;
|
|
40
|
-
const branch = (_a = config.getOptionalString("branch")) != null ? _a : "master";
|
|
41
|
-
const host = config.getString("host");
|
|
42
|
-
const query = config.getString("query");
|
|
43
|
-
const schedule = config.has("schedule") ? backendTasks.readTaskScheduleDefinitionFromConfig(config.getConfig("schedule")) : void 0;
|
|
44
|
-
return {
|
|
45
|
-
branch,
|
|
46
|
-
host,
|
|
47
|
-
id,
|
|
48
|
-
query,
|
|
49
|
-
schedule
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
function readGerritConfigs(config) {
|
|
53
|
-
const configs = [];
|
|
54
|
-
const providerConfigs = config.getOptionalConfig("catalog.providers.gerrit");
|
|
55
|
-
if (!providerConfigs) {
|
|
56
|
-
return configs;
|
|
57
|
-
}
|
|
58
|
-
for (const id of providerConfigs.keys()) {
|
|
59
|
-
configs.push(readGerritConfig(id, providerConfigs.getConfig(id)));
|
|
60
|
-
}
|
|
61
|
-
return configs;
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
class GerritEntityProvider {
|
|
65
|
-
static fromConfig(configRoot, options) {
|
|
66
|
-
if (!options.schedule && !options.scheduler) {
|
|
67
|
-
throw new Error("Either schedule or scheduler must be provided.");
|
|
68
|
-
}
|
|
69
|
-
const providerConfigs = readGerritConfigs(configRoot);
|
|
70
|
-
const integrations = integration.ScmIntegrations.fromConfig(configRoot).gerrit;
|
|
71
|
-
const providers = [];
|
|
72
|
-
providerConfigs.forEach((providerConfig) => {
|
|
73
|
-
var _a;
|
|
74
|
-
const integration = integrations.byHost(providerConfig.host);
|
|
75
|
-
if (!integration) {
|
|
76
|
-
throw new errors.InputError(
|
|
77
|
-
`No gerrit integration found that matches host ${providerConfig.host}`
|
|
78
|
-
);
|
|
79
|
-
}
|
|
80
|
-
if (!options.schedule && !providerConfig.schedule) {
|
|
81
|
-
throw new Error(
|
|
82
|
-
`No schedule provided neither via code nor config for gerrit-provider:${providerConfig.id}.`
|
|
83
|
-
);
|
|
84
|
-
}
|
|
85
|
-
const taskRunner = (_a = options.schedule) != null ? _a : options.scheduler.createScheduledTaskRunner(providerConfig.schedule);
|
|
86
|
-
providers.push(
|
|
87
|
-
new GerritEntityProvider(
|
|
88
|
-
providerConfig,
|
|
89
|
-
integration,
|
|
90
|
-
options.logger,
|
|
91
|
-
taskRunner
|
|
92
|
-
)
|
|
93
|
-
);
|
|
94
|
-
});
|
|
95
|
-
return providers;
|
|
96
|
-
}
|
|
97
|
-
constructor(config, integration, logger, taskRunner) {
|
|
98
|
-
this.config = config;
|
|
99
|
-
this.integration = integration;
|
|
100
|
-
this.logger = logger.child({
|
|
101
|
-
target: this.getProviderName()
|
|
102
|
-
});
|
|
103
|
-
this.scheduleFn = this.createScheduleFn(taskRunner);
|
|
104
|
-
}
|
|
105
|
-
getProviderName() {
|
|
106
|
-
return `gerrit-provider:${this.config.id}`;
|
|
107
|
-
}
|
|
108
|
-
async connect(connection) {
|
|
109
|
-
this.connection = connection;
|
|
110
|
-
await this.scheduleFn();
|
|
111
|
-
}
|
|
112
|
-
createScheduleFn(taskRunner) {
|
|
113
|
-
return async () => {
|
|
114
|
-
const taskId = `${this.getProviderName()}:refresh`;
|
|
115
|
-
return taskRunner.run({
|
|
116
|
-
id: taskId,
|
|
117
|
-
fn: async () => {
|
|
118
|
-
const logger = this.logger.child({
|
|
119
|
-
class: GerritEntityProvider.prototype.constructor.name,
|
|
120
|
-
taskId,
|
|
121
|
-
taskInstanceId: uuid__namespace.v4()
|
|
122
|
-
});
|
|
123
|
-
try {
|
|
124
|
-
await this.refresh(logger);
|
|
125
|
-
} catch (error) {
|
|
126
|
-
logger.error(`${this.getProviderName()} refresh failed`, error);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
});
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
async refresh(logger) {
|
|
133
|
-
if (!this.connection) {
|
|
134
|
-
throw new Error("Gerrit discovery connection not initialized");
|
|
135
|
-
}
|
|
136
|
-
let response;
|
|
137
|
-
const baseProjectApiUrl = integration.getGerritProjectsApiUrl(this.integration.config);
|
|
138
|
-
const projectQueryUrl = `${baseProjectApiUrl}?${this.config.query}`;
|
|
139
|
-
try {
|
|
140
|
-
response = await fetch__default["default"](projectQueryUrl, {
|
|
141
|
-
method: "GET",
|
|
142
|
-
...integration.getGerritRequestOptions(this.integration.config)
|
|
143
|
-
});
|
|
144
|
-
} catch (e) {
|
|
145
|
-
throw new Error(
|
|
146
|
-
`Failed to list Gerrit projects for query ${this.config.query}, ${e}`
|
|
147
|
-
);
|
|
148
|
-
}
|
|
149
|
-
const gerritProjectsResponse = await integration.parseGerritJsonResponse(
|
|
150
|
-
response
|
|
151
|
-
);
|
|
152
|
-
const projects = Object.keys(gerritProjectsResponse);
|
|
153
|
-
const locations = projects.map((project) => this.createLocationSpec(project));
|
|
154
|
-
await this.connection.applyMutation({
|
|
155
|
-
type: "full",
|
|
156
|
-
entities: locations.map((location) => ({
|
|
157
|
-
locationKey: this.getProviderName(),
|
|
158
|
-
entity: pluginCatalogNode.locationSpecToLocationEntity({ location })
|
|
159
|
-
}))
|
|
160
|
-
});
|
|
161
|
-
logger.info(`Found ${locations.length} locations.`);
|
|
162
|
-
}
|
|
163
|
-
createLocationSpec(project) {
|
|
164
|
-
return {
|
|
165
|
-
type: "url",
|
|
166
|
-
target: `${this.integration.config.gitilesBaseUrl}/${project}/+/refs/heads/${this.config.branch}/catalog-info.yaml`,
|
|
167
|
-
presence: "optional"
|
|
168
|
-
};
|
|
169
|
-
}
|
|
170
|
-
}
|
|
8
|
+
var GerritEntityProvider = require('./cjs/GerritEntityProvider-941f29ac.cjs.js');
|
|
9
|
+
require('@backstage/errors');
|
|
10
|
+
require('@backstage/plugin-catalog-node');
|
|
11
|
+
require('node-fetch');
|
|
12
|
+
require('@backstage/integration');
|
|
13
|
+
require('uuid');
|
|
14
|
+
require('@backstage/backend-tasks');
|
|
171
15
|
|
|
172
16
|
const catalogModuleGerritEntityProvider = backendPluginApi.createBackendModule({
|
|
173
17
|
pluginId: "catalog",
|
|
@@ -182,7 +26,7 @@ const catalogModuleGerritEntityProvider = backendPluginApi.createBackendModule({
|
|
|
182
26
|
},
|
|
183
27
|
async init({ catalog, config, logger, scheduler }) {
|
|
184
28
|
const winstonLogger = backendCommon.loggerToWinstonLogger(logger);
|
|
185
|
-
const providers = GerritEntityProvider.fromConfig(config, {
|
|
29
|
+
const providers = GerritEntityProvider.GerritEntityProvider.fromConfig(config, {
|
|
186
30
|
logger: winstonLogger,
|
|
187
31
|
scheduler
|
|
188
32
|
});
|
package/dist/alpha.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"alpha.cjs.js","sources":["../src/providers/config.ts","../src/providers/GerritEntityProvider.ts","../src/module/catalogModuleGerritEntityProvider.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 { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks';\nimport { Config } from '@backstage/config';\nimport { GerritProviderConfig } from './types';\n\nfunction readGerritConfig(id: string, config: Config): GerritProviderConfig {\n const branch = config.getOptionalString('branch') ?? 'master';\n const host = config.getString('host');\n const query = config.getString('query');\n\n const schedule = config.has('schedule')\n ? readTaskScheduleDefinitionFromConfig(config.getConfig('schedule'))\n : undefined;\n\n return {\n branch,\n host,\n id,\n query,\n schedule,\n };\n}\n\nexport function readGerritConfigs(config: Config): GerritProviderConfig[] {\n const configs: GerritProviderConfig[] = [];\n\n const providerConfigs = config.getOptionalConfig('catalog.providers.gerrit');\n\n if (!providerConfigs) {\n return configs;\n }\n\n for (const id of providerConfigs.keys()) {\n configs.push(readGerritConfig(id, providerConfigs.getConfig(id)));\n }\n\n return configs;\n}\n","/*\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 { PluginTaskScheduler, TaskRunner } from '@backstage/backend-tasks';\nimport { Config } from '@backstage/config';\nimport { InputError } from '@backstage/errors';\nimport {\n EntityProvider,\n EntityProviderConnection,\n LocationSpec,\n locationSpecToLocationEntity,\n} from '@backstage/plugin-catalog-node';\nimport fetch, { Response } from 'node-fetch';\nimport {\n GerritIntegration,\n getGerritProjectsApiUrl,\n getGerritRequestOptions,\n parseGerritJsonResponse,\n ScmIntegrations,\n} from '@backstage/integration';\nimport * as uuid from 'uuid';\nimport { Logger } from 'winston';\n\nimport { readGerritConfigs } from './config';\nimport { GerritProjectQueryResult, GerritProviderConfig } from './types';\n\n/** @public */\nexport class GerritEntityProvider implements EntityProvider {\n private readonly config: GerritProviderConfig;\n private readonly integration: GerritIntegration;\n private readonly logger: Logger;\n private readonly scheduleFn: () => Promise<void>;\n private connection?: EntityProviderConnection;\n\n static fromConfig(\n configRoot: Config,\n options: {\n logger: Logger;\n schedule?: TaskRunner;\n scheduler?: PluginTaskScheduler;\n },\n ): GerritEntityProvider[] {\n if (!options.schedule && !options.scheduler) {\n throw new Error('Either schedule or scheduler must be provided.');\n }\n\n const providerConfigs = readGerritConfigs(configRoot);\n const integrations = ScmIntegrations.fromConfig(configRoot).gerrit;\n const providers: GerritEntityProvider[] = [];\n\n providerConfigs.forEach(providerConfig => {\n const integration = integrations.byHost(providerConfig.host);\n if (!integration) {\n throw new InputError(\n `No gerrit integration found that matches host ${providerConfig.host}`,\n );\n }\n\n if (!options.schedule && !providerConfig.schedule) {\n throw new Error(\n `No schedule provided neither via code nor config for gerrit-provider:${providerConfig.id}.`,\n );\n }\n\n const taskRunner =\n options.schedule ??\n options.scheduler!.createScheduledTaskRunner(providerConfig.schedule!);\n\n providers.push(\n new GerritEntityProvider(\n providerConfig,\n integration,\n options.logger,\n taskRunner,\n ),\n );\n });\n return providers;\n }\n\n private constructor(\n config: GerritProviderConfig,\n integration: GerritIntegration,\n logger: Logger,\n taskRunner: TaskRunner,\n ) {\n this.config = config;\n this.integration = integration;\n this.logger = logger.child({\n target: this.getProviderName(),\n });\n this.scheduleFn = this.createScheduleFn(taskRunner);\n }\n\n getProviderName(): string {\n return `gerrit-provider:${this.config.id}`;\n }\n\n async connect(connection: EntityProviderConnection): Promise<void> {\n this.connection = connection;\n await this.scheduleFn();\n }\n\n private createScheduleFn(taskRunner: TaskRunner): () => Promise<void> {\n return async () => {\n const taskId = `${this.getProviderName()}:refresh`;\n return taskRunner.run({\n id: taskId,\n fn: async () => {\n const logger = this.logger.child({\n class: GerritEntityProvider.prototype.constructor.name,\n taskId,\n taskInstanceId: uuid.v4(),\n });\n\n try {\n await this.refresh(logger);\n } catch (error) {\n logger.error(`${this.getProviderName()} refresh failed`, error);\n }\n },\n });\n };\n }\n\n async refresh(logger: Logger): Promise<void> {\n if (!this.connection) {\n throw new Error('Gerrit discovery connection not initialized');\n }\n\n let response: Response;\n\n const baseProjectApiUrl = getGerritProjectsApiUrl(this.integration.config);\n const projectQueryUrl = `${baseProjectApiUrl}?${this.config.query}`;\n try {\n response = await fetch(projectQueryUrl, {\n method: 'GET',\n ...getGerritRequestOptions(this.integration.config),\n });\n } catch (e) {\n throw new Error(\n `Failed to list Gerrit projects for query ${this.config.query}, ${e}`,\n );\n }\n const gerritProjectsResponse = (await parseGerritJsonResponse(\n response as any,\n )) as GerritProjectQueryResult;\n const projects = Object.keys(gerritProjectsResponse);\n\n const locations = projects.map(project => this.createLocationSpec(project));\n await this.connection.applyMutation({\n type: 'full',\n entities: locations.map(location => ({\n locationKey: this.getProviderName(),\n entity: locationSpecToLocationEntity({ location }),\n })),\n });\n logger.info(`Found ${locations.length} locations.`);\n }\n\n private createLocationSpec(project: string): LocationSpec {\n return {\n type: 'url',\n target: `${this.integration.config.gitilesBaseUrl}/${project}/+/refs/heads/${this.config.branch}/catalog-info.yaml`,\n presence: 'optional',\n };\n }\n}\n","/*\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 { loggerToWinstonLogger } from '@backstage/backend-common';\nimport {\n coreServices,\n createBackendModule,\n} from '@backstage/backend-plugin-api';\nimport { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha';\nimport { GerritEntityProvider } from '../providers/GerritEntityProvider';\n\n/**\n * @alpha\n */\nexport const catalogModuleGerritEntityProvider = createBackendModule({\n pluginId: 'catalog',\n moduleId: 'gerritEntityProvider',\n register(env) {\n env.registerInit({\n deps: {\n catalog: catalogProcessingExtensionPoint,\n config: coreServices.config,\n logger: coreServices.logger,\n scheduler: coreServices.scheduler,\n },\n async init({ catalog, config, logger, scheduler }) {\n const winstonLogger = loggerToWinstonLogger(logger);\n const providers = GerritEntityProvider.fromConfig(config, {\n logger: winstonLogger,\n scheduler,\n });\n\n catalog.addEntityProvider(providers);\n },\n });\n },\n});\n"],"names":["readTaskScheduleDefinitionFromConfig","ScmIntegrations","InputError","uuid","getGerritProjectsApiUrl","fetch","getGerritRequestOptions","parseGerritJsonResponse","locationSpecToLocationEntity","createBackendModule","catalogProcessingExtensionPoint","coreServices","loggerToWinstonLogger"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA,SAAS,gBAAA,CAAiB,IAAY,MAAsC,EAAA;AApB5E,EAAA,IAAA,EAAA,CAAA;AAqBE,EAAA,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"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 { loggerToWinstonLogger } from '@backstage/backend-common';\nimport {\n coreServices,\n createBackendModule,\n} from '@backstage/backend-plugin-api';\nimport { catalogProcessingExtensionPoint } from '@backstage/plugin-catalog-node/alpha';\nimport { GerritEntityProvider } from '../providers/GerritEntityProvider';\n\n/**\n * @alpha\n */\nexport const catalogModuleGerritEntityProvider = createBackendModule({\n pluginId: 'catalog',\n moduleId: 'gerritEntityProvider',\n register(env) {\n env.registerInit({\n deps: {\n catalog: catalogProcessingExtensionPoint,\n config: coreServices.config,\n logger: coreServices.logger,\n scheduler: coreServices.scheduler,\n },\n async init({ catalog, config, logger, scheduler }) {\n const winstonLogger = loggerToWinstonLogger(logger);\n const providers = GerritEntityProvider.fromConfig(config, {\n logger: winstonLogger,\n scheduler,\n });\n\n catalog.addEntityProvider(providers);\n },\n });\n },\n});\n"],"names":["createBackendModule","catalogProcessingExtensionPoint","coreServices","loggerToWinstonLogger","GerritEntityProvider"],"mappings":";;;;;;;;;;;;;;;AA2BO,MAAM,oCAAoCA,oCAAoB,CAAA;AAAA,EACnE,QAAU,EAAA,SAAA;AAAA,EACV,QAAU,EAAA,sBAAA;AAAA,EACV,SAAS,GAAK,EAAA;AACZ,IAAA,GAAA,CAAI,YAAa,CAAA;AAAA,MACf,IAAM,EAAA;AAAA,QACJ,OAAS,EAAAC,qCAAA;AAAA,QACT,QAAQC,6BAAa,CAAA,MAAA;AAAA,QACrB,QAAQA,6BAAa,CAAA,MAAA;AAAA,QACrB,WAAWA,6BAAa,CAAA,SAAA;AAAA,OAC1B;AAAA,MACA,MAAM,IAAK,CAAA,EAAE,SAAS,MAAQ,EAAA,MAAA,EAAQ,WAAa,EAAA;AACjD,QAAM,MAAA,aAAA,GAAgBC,oCAAsB,MAAM,CAAA,CAAA;AAClD,QAAM,MAAA,SAAA,GAAYC,yCAAqB,CAAA,UAAA,CAAW,MAAQ,EAAA;AAAA,UACxD,MAAQ,EAAA,aAAA;AAAA,UACR,SAAA;AAAA,SACD,CAAA,CAAA;AAED,QAAA,OAAA,CAAQ,kBAAkB,SAAS,CAAA,CAAA;AAAA,OACrC;AAAA,KACD,CAAA,CAAA;AAAA,GACH;AACF,CAAC;;;;"}
|
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var errors = require('@backstage/errors');
|
|
4
|
+
var pluginCatalogNode = require('@backstage/plugin-catalog-node');
|
|
5
|
+
var fetch = require('node-fetch');
|
|
6
|
+
var integration = require('@backstage/integration');
|
|
7
|
+
var uuid = require('uuid');
|
|
8
|
+
var backendTasks = require('@backstage/backend-tasks');
|
|
9
|
+
|
|
10
|
+
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
11
|
+
|
|
12
|
+
function _interopNamespace(e) {
|
|
13
|
+
if (e && e.__esModule) return e;
|
|
14
|
+
var n = Object.create(null);
|
|
15
|
+
if (e) {
|
|
16
|
+
Object.keys(e).forEach(function (k) {
|
|
17
|
+
if (k !== 'default') {
|
|
18
|
+
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
19
|
+
Object.defineProperty(n, k, d.get ? d : {
|
|
20
|
+
enumerable: true,
|
|
21
|
+
get: function () { return e[k]; }
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
n["default"] = e;
|
|
27
|
+
return Object.freeze(n);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
|
|
31
|
+
var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
|
|
32
|
+
|
|
33
|
+
function readGerritConfig(id, config) {
|
|
34
|
+
var _a;
|
|
35
|
+
const branch = (_a = config.getOptionalString("branch")) != null ? _a : "master";
|
|
36
|
+
const host = config.getString("host");
|
|
37
|
+
const query = config.getString("query");
|
|
38
|
+
const schedule = config.has("schedule") ? backendTasks.readTaskScheduleDefinitionFromConfig(config.getConfig("schedule")) : void 0;
|
|
39
|
+
return {
|
|
40
|
+
branch,
|
|
41
|
+
host,
|
|
42
|
+
id,
|
|
43
|
+
query,
|
|
44
|
+
schedule
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
function readGerritConfigs(config) {
|
|
48
|
+
const configs = [];
|
|
49
|
+
const providerConfigs = config.getOptionalConfig("catalog.providers.gerrit");
|
|
50
|
+
if (!providerConfigs) {
|
|
51
|
+
return configs;
|
|
52
|
+
}
|
|
53
|
+
for (const id of providerConfigs.keys()) {
|
|
54
|
+
configs.push(readGerritConfig(id, providerConfigs.getConfig(id)));
|
|
55
|
+
}
|
|
56
|
+
return configs;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
class GerritEntityProvider {
|
|
60
|
+
static fromConfig(configRoot, options) {
|
|
61
|
+
if (!options.schedule && !options.scheduler) {
|
|
62
|
+
throw new Error("Either schedule or scheduler must be provided.");
|
|
63
|
+
}
|
|
64
|
+
const providerConfigs = readGerritConfigs(configRoot);
|
|
65
|
+
const integrations = integration.ScmIntegrations.fromConfig(configRoot).gerrit;
|
|
66
|
+
const providers = [];
|
|
67
|
+
providerConfigs.forEach((providerConfig) => {
|
|
68
|
+
var _a;
|
|
69
|
+
const integration = integrations.byHost(providerConfig.host);
|
|
70
|
+
if (!integration) {
|
|
71
|
+
throw new errors.InputError(
|
|
72
|
+
`No gerrit integration found that matches host ${providerConfig.host}`
|
|
73
|
+
);
|
|
74
|
+
}
|
|
75
|
+
if (!options.schedule && !providerConfig.schedule) {
|
|
76
|
+
throw new Error(
|
|
77
|
+
`No schedule provided neither via code nor config for gerrit-provider:${providerConfig.id}.`
|
|
78
|
+
);
|
|
79
|
+
}
|
|
80
|
+
const taskRunner = (_a = options.schedule) != null ? _a : options.scheduler.createScheduledTaskRunner(providerConfig.schedule);
|
|
81
|
+
providers.push(
|
|
82
|
+
new GerritEntityProvider(
|
|
83
|
+
providerConfig,
|
|
84
|
+
integration,
|
|
85
|
+
options.logger,
|
|
86
|
+
taskRunner
|
|
87
|
+
)
|
|
88
|
+
);
|
|
89
|
+
});
|
|
90
|
+
return providers;
|
|
91
|
+
}
|
|
92
|
+
constructor(config, integration, logger, taskRunner) {
|
|
93
|
+
this.config = config;
|
|
94
|
+
this.integration = integration;
|
|
95
|
+
this.logger = logger.child({
|
|
96
|
+
target: this.getProviderName()
|
|
97
|
+
});
|
|
98
|
+
this.scheduleFn = this.createScheduleFn(taskRunner);
|
|
99
|
+
}
|
|
100
|
+
getProviderName() {
|
|
101
|
+
return `gerrit-provider:${this.config.id}`;
|
|
102
|
+
}
|
|
103
|
+
async connect(connection) {
|
|
104
|
+
this.connection = connection;
|
|
105
|
+
await this.scheduleFn();
|
|
106
|
+
}
|
|
107
|
+
createScheduleFn(taskRunner) {
|
|
108
|
+
return async () => {
|
|
109
|
+
const taskId = `${this.getProviderName()}:refresh`;
|
|
110
|
+
return taskRunner.run({
|
|
111
|
+
id: taskId,
|
|
112
|
+
fn: async () => {
|
|
113
|
+
const logger = this.logger.child({
|
|
114
|
+
class: GerritEntityProvider.prototype.constructor.name,
|
|
115
|
+
taskId,
|
|
116
|
+
taskInstanceId: uuid__namespace.v4()
|
|
117
|
+
});
|
|
118
|
+
try {
|
|
119
|
+
await this.refresh(logger);
|
|
120
|
+
} catch (error) {
|
|
121
|
+
logger.error(`${this.getProviderName()} refresh failed`, error);
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
async refresh(logger) {
|
|
128
|
+
if (!this.connection) {
|
|
129
|
+
throw new Error("Gerrit discovery connection not initialized");
|
|
130
|
+
}
|
|
131
|
+
let response;
|
|
132
|
+
const baseProjectApiUrl = integration.getGerritProjectsApiUrl(this.integration.config);
|
|
133
|
+
const projectQueryUrl = `${baseProjectApiUrl}?${this.config.query}`;
|
|
134
|
+
try {
|
|
135
|
+
response = await fetch__default["default"](projectQueryUrl, {
|
|
136
|
+
method: "GET",
|
|
137
|
+
...integration.getGerritRequestOptions(this.integration.config)
|
|
138
|
+
});
|
|
139
|
+
} catch (e) {
|
|
140
|
+
throw new Error(
|
|
141
|
+
`Failed to list Gerrit projects for query ${this.config.query}, ${e}`
|
|
142
|
+
);
|
|
143
|
+
}
|
|
144
|
+
const gerritProjectsResponse = await integration.parseGerritJsonResponse(
|
|
145
|
+
response
|
|
146
|
+
);
|
|
147
|
+
const projects = Object.keys(gerritProjectsResponse);
|
|
148
|
+
const locations = projects.map((project) => this.createLocationSpec(project));
|
|
149
|
+
await this.connection.applyMutation({
|
|
150
|
+
type: "full",
|
|
151
|
+
entities: locations.map((location) => ({
|
|
152
|
+
locationKey: this.getProviderName(),
|
|
153
|
+
entity: pluginCatalogNode.locationSpecToLocationEntity({ location })
|
|
154
|
+
}))
|
|
155
|
+
});
|
|
156
|
+
logger.info(`Found ${locations.length} locations.`);
|
|
157
|
+
}
|
|
158
|
+
createLocationSpec(project) {
|
|
159
|
+
return {
|
|
160
|
+
type: "url",
|
|
161
|
+
target: `${this.integration.config.gitilesBaseUrl}/${project}/+/refs/heads/${this.config.branch}/catalog-info.yaml`,
|
|
162
|
+
presence: "optional"
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
exports.GerritEntityProvider = GerritEntityProvider;
|
|
168
|
+
//# sourceMappingURL=GerritEntityProvider-941f29ac.cjs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"GerritEntityProvider-941f29ac.cjs.js","sources":["../../src/providers/config.ts","../../src/providers/GerritEntityProvider.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 { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks';\nimport { Config } from '@backstage/config';\nimport { GerritProviderConfig } from './types';\n\nfunction readGerritConfig(id: string, config: Config): GerritProviderConfig {\n const branch = config.getOptionalString('branch') ?? 'master';\n const host = config.getString('host');\n const query = config.getString('query');\n\n const schedule = config.has('schedule')\n ? readTaskScheduleDefinitionFromConfig(config.getConfig('schedule'))\n : undefined;\n\n return {\n branch,\n host,\n id,\n query,\n schedule,\n };\n}\n\nexport function readGerritConfigs(config: Config): GerritProviderConfig[] {\n const configs: GerritProviderConfig[] = [];\n\n const providerConfigs = config.getOptionalConfig('catalog.providers.gerrit');\n\n if (!providerConfigs) {\n return configs;\n }\n\n for (const id of providerConfigs.keys()) {\n configs.push(readGerritConfig(id, providerConfigs.getConfig(id)));\n }\n\n return configs;\n}\n","/*\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 { PluginTaskScheduler, TaskRunner } from '@backstage/backend-tasks';\nimport { Config } from '@backstage/config';\nimport { InputError } from '@backstage/errors';\nimport {\n EntityProvider,\n EntityProviderConnection,\n LocationSpec,\n locationSpecToLocationEntity,\n} from '@backstage/plugin-catalog-node';\nimport fetch, { Response } from 'node-fetch';\nimport {\n GerritIntegration,\n getGerritProjectsApiUrl,\n getGerritRequestOptions,\n parseGerritJsonResponse,\n ScmIntegrations,\n} from '@backstage/integration';\nimport * as uuid from 'uuid';\nimport { Logger } from 'winston';\n\nimport { readGerritConfigs } from './config';\nimport { GerritProjectQueryResult, GerritProviderConfig } from './types';\n\n/** @public */\nexport class GerritEntityProvider implements EntityProvider {\n private readonly config: GerritProviderConfig;\n private readonly integration: GerritIntegration;\n private readonly logger: Logger;\n private readonly scheduleFn: () => Promise<void>;\n private connection?: EntityProviderConnection;\n\n static fromConfig(\n configRoot: Config,\n options: {\n logger: Logger;\n schedule?: TaskRunner;\n scheduler?: PluginTaskScheduler;\n },\n ): GerritEntityProvider[] {\n if (!options.schedule && !options.scheduler) {\n throw new Error('Either schedule or scheduler must be provided.');\n }\n\n const providerConfigs = readGerritConfigs(configRoot);\n const integrations = ScmIntegrations.fromConfig(configRoot).gerrit;\n const providers: GerritEntityProvider[] = [];\n\n providerConfigs.forEach(providerConfig => {\n const integration = integrations.byHost(providerConfig.host);\n if (!integration) {\n throw new InputError(\n `No gerrit integration found that matches host ${providerConfig.host}`,\n );\n }\n\n if (!options.schedule && !providerConfig.schedule) {\n throw new Error(\n `No schedule provided neither via code nor config for gerrit-provider:${providerConfig.id}.`,\n );\n }\n\n const taskRunner =\n options.schedule ??\n options.scheduler!.createScheduledTaskRunner(providerConfig.schedule!);\n\n providers.push(\n new GerritEntityProvider(\n providerConfig,\n integration,\n options.logger,\n taskRunner,\n ),\n );\n });\n return providers;\n }\n\n private constructor(\n config: GerritProviderConfig,\n integration: GerritIntegration,\n logger: Logger,\n taskRunner: TaskRunner,\n ) {\n this.config = config;\n this.integration = integration;\n this.logger = logger.child({\n target: this.getProviderName(),\n });\n this.scheduleFn = this.createScheduleFn(taskRunner);\n }\n\n getProviderName(): string {\n return `gerrit-provider:${this.config.id}`;\n }\n\n async connect(connection: EntityProviderConnection): Promise<void> {\n this.connection = connection;\n await this.scheduleFn();\n }\n\n private createScheduleFn(taskRunner: TaskRunner): () => Promise<void> {\n return async () => {\n const taskId = `${this.getProviderName()}:refresh`;\n return taskRunner.run({\n id: taskId,\n fn: async () => {\n const logger = this.logger.child({\n class: GerritEntityProvider.prototype.constructor.name,\n taskId,\n taskInstanceId: uuid.v4(),\n });\n\n try {\n await this.refresh(logger);\n } catch (error) {\n logger.error(`${this.getProviderName()} refresh failed`, error);\n }\n },\n });\n };\n }\n\n async refresh(logger: Logger): Promise<void> {\n if (!this.connection) {\n throw new Error('Gerrit discovery connection not initialized');\n }\n\n let response: Response;\n\n const baseProjectApiUrl = getGerritProjectsApiUrl(this.integration.config);\n const projectQueryUrl = `${baseProjectApiUrl}?${this.config.query}`;\n try {\n response = await fetch(projectQueryUrl, {\n method: 'GET',\n ...getGerritRequestOptions(this.integration.config),\n });\n } catch (e) {\n throw new Error(\n `Failed to list Gerrit projects for query ${this.config.query}, ${e}`,\n );\n }\n const gerritProjectsResponse = (await parseGerritJsonResponse(\n response as any,\n )) as GerritProjectQueryResult;\n const projects = Object.keys(gerritProjectsResponse);\n\n const locations = projects.map(project => this.createLocationSpec(project));\n await this.connection.applyMutation({\n type: 'full',\n entities: locations.map(location => ({\n locationKey: this.getProviderName(),\n entity: locationSpecToLocationEntity({ location }),\n })),\n });\n logger.info(`Found ${locations.length} locations.`);\n }\n\n private createLocationSpec(project: string): LocationSpec {\n return {\n type: 'url',\n target: `${this.integration.config.gitilesBaseUrl}/${project}/+/refs/heads/${this.config.branch}/catalog-info.yaml`,\n presence: 'optional',\n };\n }\n}\n"],"names":["readTaskScheduleDefinitionFromConfig","ScmIntegrations","InputError","uuid","getGerritProjectsApiUrl","fetch","getGerritRequestOptions","parseGerritJsonResponse","locationSpecToLocationEntity"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;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A,QAClC,MAAQ,EAAAC,8CAAA,CAA6B,EAAE,QAAA,EAAU,CAAA;AAAA,OACjD,CAAA,CAAA;AAAA,KACH,CAAA,CAAA;AACD,IAAO,MAAA,CAAA,IAAA,CAAK,CAAS,MAAA,EAAA,SAAA,CAAU,MAAmB,CAAA,WAAA,CAAA,CAAA,CAAA;AAAA,GACpD;AAAA,EAEQ,mBAAmB,OAA+B,EAAA;AACxD,IAAO,OAAA;AAAA,MACL,IAAM,EAAA,KAAA;AAAA,MACN,MAAA,EAAQ,GAAG,IAAK,CAAA,WAAA,CAAY,OAAO,cAAkB,CAAA,CAAA,EAAA,OAAA,CAAA,cAAA,EAAwB,KAAK,MAAO,CAAA,MAAA,CAAA,kBAAA,CAAA;AAAA,MACzF,QAAU,EAAA,UAAA;AAAA,KACZ,CAAA;AAAA,GACF;AACF;;;;"}
|
package/dist/index.cjs.js
CHANGED
|
@@ -2,169 +2,15 @@
|
|
|
2
2
|
|
|
3
3
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
4
|
|
|
5
|
-
var
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
var GerritEntityProvider = require('./cjs/GerritEntityProvider-941f29ac.cjs.js');
|
|
6
|
+
require('@backstage/errors');
|
|
7
|
+
require('@backstage/plugin-catalog-node');
|
|
8
|
+
require('node-fetch');
|
|
9
|
+
require('@backstage/integration');
|
|
10
|
+
require('uuid');
|
|
11
|
+
require('@backstage/backend-tasks');
|
|
11
12
|
|
|
12
|
-
function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
|
|
13
13
|
|
|
14
|
-
function _interopNamespace(e) {
|
|
15
|
-
if (e && e.__esModule) return e;
|
|
16
|
-
var n = Object.create(null);
|
|
17
|
-
if (e) {
|
|
18
|
-
Object.keys(e).forEach(function (k) {
|
|
19
|
-
if (k !== 'default') {
|
|
20
|
-
var d = Object.getOwnPropertyDescriptor(e, k);
|
|
21
|
-
Object.defineProperty(n, k, d.get ? d : {
|
|
22
|
-
enumerable: true,
|
|
23
|
-
get: function () { return e[k]; }
|
|
24
|
-
});
|
|
25
|
-
}
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
n["default"] = e;
|
|
29
|
-
return Object.freeze(n);
|
|
30
|
-
}
|
|
31
14
|
|
|
32
|
-
|
|
33
|
-
var uuid__namespace = /*#__PURE__*/_interopNamespace(uuid);
|
|
34
|
-
|
|
35
|
-
function readGerritConfig(id, config) {
|
|
36
|
-
var _a;
|
|
37
|
-
const branch = (_a = config.getOptionalString("branch")) != null ? _a : "master";
|
|
38
|
-
const host = config.getString("host");
|
|
39
|
-
const query = config.getString("query");
|
|
40
|
-
const schedule = config.has("schedule") ? backendTasks.readTaskScheduleDefinitionFromConfig(config.getConfig("schedule")) : void 0;
|
|
41
|
-
return {
|
|
42
|
-
branch,
|
|
43
|
-
host,
|
|
44
|
-
id,
|
|
45
|
-
query,
|
|
46
|
-
schedule
|
|
47
|
-
};
|
|
48
|
-
}
|
|
49
|
-
function readGerritConfigs(config) {
|
|
50
|
-
const configs = [];
|
|
51
|
-
const providerConfigs = config.getOptionalConfig("catalog.providers.gerrit");
|
|
52
|
-
if (!providerConfigs) {
|
|
53
|
-
return configs;
|
|
54
|
-
}
|
|
55
|
-
for (const id of providerConfigs.keys()) {
|
|
56
|
-
configs.push(readGerritConfig(id, providerConfigs.getConfig(id)));
|
|
57
|
-
}
|
|
58
|
-
return configs;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
class GerritEntityProvider {
|
|
62
|
-
static fromConfig(configRoot, options) {
|
|
63
|
-
if (!options.schedule && !options.scheduler) {
|
|
64
|
-
throw new Error("Either schedule or scheduler must be provided.");
|
|
65
|
-
}
|
|
66
|
-
const providerConfigs = readGerritConfigs(configRoot);
|
|
67
|
-
const integrations = integration.ScmIntegrations.fromConfig(configRoot).gerrit;
|
|
68
|
-
const providers = [];
|
|
69
|
-
providerConfigs.forEach((providerConfig) => {
|
|
70
|
-
var _a;
|
|
71
|
-
const integration = integrations.byHost(providerConfig.host);
|
|
72
|
-
if (!integration) {
|
|
73
|
-
throw new errors.InputError(
|
|
74
|
-
`No gerrit integration found that matches host ${providerConfig.host}`
|
|
75
|
-
);
|
|
76
|
-
}
|
|
77
|
-
if (!options.schedule && !providerConfig.schedule) {
|
|
78
|
-
throw new Error(
|
|
79
|
-
`No schedule provided neither via code nor config for gerrit-provider:${providerConfig.id}.`
|
|
80
|
-
);
|
|
81
|
-
}
|
|
82
|
-
const taskRunner = (_a = options.schedule) != null ? _a : options.scheduler.createScheduledTaskRunner(providerConfig.schedule);
|
|
83
|
-
providers.push(
|
|
84
|
-
new GerritEntityProvider(
|
|
85
|
-
providerConfig,
|
|
86
|
-
integration,
|
|
87
|
-
options.logger,
|
|
88
|
-
taskRunner
|
|
89
|
-
)
|
|
90
|
-
);
|
|
91
|
-
});
|
|
92
|
-
return providers;
|
|
93
|
-
}
|
|
94
|
-
constructor(config, integration, logger, taskRunner) {
|
|
95
|
-
this.config = config;
|
|
96
|
-
this.integration = integration;
|
|
97
|
-
this.logger = logger.child({
|
|
98
|
-
target: this.getProviderName()
|
|
99
|
-
});
|
|
100
|
-
this.scheduleFn = this.createScheduleFn(taskRunner);
|
|
101
|
-
}
|
|
102
|
-
getProviderName() {
|
|
103
|
-
return `gerrit-provider:${this.config.id}`;
|
|
104
|
-
}
|
|
105
|
-
async connect(connection) {
|
|
106
|
-
this.connection = connection;
|
|
107
|
-
await this.scheduleFn();
|
|
108
|
-
}
|
|
109
|
-
createScheduleFn(taskRunner) {
|
|
110
|
-
return async () => {
|
|
111
|
-
const taskId = `${this.getProviderName()}:refresh`;
|
|
112
|
-
return taskRunner.run({
|
|
113
|
-
id: taskId,
|
|
114
|
-
fn: async () => {
|
|
115
|
-
const logger = this.logger.child({
|
|
116
|
-
class: GerritEntityProvider.prototype.constructor.name,
|
|
117
|
-
taskId,
|
|
118
|
-
taskInstanceId: uuid__namespace.v4()
|
|
119
|
-
});
|
|
120
|
-
try {
|
|
121
|
-
await this.refresh(logger);
|
|
122
|
-
} catch (error) {
|
|
123
|
-
logger.error(`${this.getProviderName()} refresh failed`, error);
|
|
124
|
-
}
|
|
125
|
-
}
|
|
126
|
-
});
|
|
127
|
-
};
|
|
128
|
-
}
|
|
129
|
-
async refresh(logger) {
|
|
130
|
-
if (!this.connection) {
|
|
131
|
-
throw new Error("Gerrit discovery connection not initialized");
|
|
132
|
-
}
|
|
133
|
-
let response;
|
|
134
|
-
const baseProjectApiUrl = integration.getGerritProjectsApiUrl(this.integration.config);
|
|
135
|
-
const projectQueryUrl = `${baseProjectApiUrl}?${this.config.query}`;
|
|
136
|
-
try {
|
|
137
|
-
response = await fetch__default["default"](projectQueryUrl, {
|
|
138
|
-
method: "GET",
|
|
139
|
-
...integration.getGerritRequestOptions(this.integration.config)
|
|
140
|
-
});
|
|
141
|
-
} catch (e) {
|
|
142
|
-
throw new Error(
|
|
143
|
-
`Failed to list Gerrit projects for query ${this.config.query}, ${e}`
|
|
144
|
-
);
|
|
145
|
-
}
|
|
146
|
-
const gerritProjectsResponse = await integration.parseGerritJsonResponse(
|
|
147
|
-
response
|
|
148
|
-
);
|
|
149
|
-
const projects = Object.keys(gerritProjectsResponse);
|
|
150
|
-
const locations = projects.map((project) => this.createLocationSpec(project));
|
|
151
|
-
await this.connection.applyMutation({
|
|
152
|
-
type: "full",
|
|
153
|
-
entities: locations.map((location) => ({
|
|
154
|
-
locationKey: this.getProviderName(),
|
|
155
|
-
entity: pluginCatalogNode.locationSpecToLocationEntity({ location })
|
|
156
|
-
}))
|
|
157
|
-
});
|
|
158
|
-
logger.info(`Found ${locations.length} locations.`);
|
|
159
|
-
}
|
|
160
|
-
createLocationSpec(project) {
|
|
161
|
-
return {
|
|
162
|
-
type: "url",
|
|
163
|
-
target: `${this.integration.config.gitilesBaseUrl}/${project}/+/refs/heads/${this.config.branch}/catalog-info.yaml`,
|
|
164
|
-
presence: "optional"
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
exports.GerritEntityProvider = GerritEntityProvider;
|
|
15
|
+
exports.GerritEntityProvider = GerritEntityProvider.GerritEntityProvider;
|
|
170
16
|
//# sourceMappingURL=index.cjs.js.map
|
package/dist/index.cjs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs.js","sources":["../src/providers/config.ts","../src/providers/GerritEntityProvider.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 { readTaskScheduleDefinitionFromConfig } from '@backstage/backend-tasks';\nimport { Config } from '@backstage/config';\nimport { GerritProviderConfig } from './types';\n\nfunction readGerritConfig(id: string, config: Config): GerritProviderConfig {\n const branch = config.getOptionalString('branch') ?? 'master';\n const host = config.getString('host');\n const query = config.getString('query');\n\n const schedule = config.has('schedule')\n ? readTaskScheduleDefinitionFromConfig(config.getConfig('schedule'))\n : undefined;\n\n return {\n branch,\n host,\n id,\n query,\n schedule,\n };\n}\n\nexport function readGerritConfigs(config: Config): GerritProviderConfig[] {\n const configs: GerritProviderConfig[] = [];\n\n const providerConfigs = config.getOptionalConfig('catalog.providers.gerrit');\n\n if (!providerConfigs) {\n return configs;\n }\n\n for (const id of providerConfigs.keys()) {\n configs.push(readGerritConfig(id, providerConfigs.getConfig(id)));\n }\n\n return configs;\n}\n","/*\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 { PluginTaskScheduler, TaskRunner } from '@backstage/backend-tasks';\nimport { Config } from '@backstage/config';\nimport { InputError } from '@backstage/errors';\nimport {\n EntityProvider,\n EntityProviderConnection,\n LocationSpec,\n locationSpecToLocationEntity,\n} from '@backstage/plugin-catalog-node';\nimport fetch, { Response } from 'node-fetch';\nimport {\n GerritIntegration,\n getGerritProjectsApiUrl,\n getGerritRequestOptions,\n parseGerritJsonResponse,\n ScmIntegrations,\n} from '@backstage/integration';\nimport * as uuid from 'uuid';\nimport { Logger } from 'winston';\n\nimport { readGerritConfigs } from './config';\nimport { GerritProjectQueryResult, GerritProviderConfig } from './types';\n\n/** @public */\nexport class GerritEntityProvider implements EntityProvider {\n private readonly config: GerritProviderConfig;\n private readonly integration: GerritIntegration;\n private readonly logger: Logger;\n private readonly scheduleFn: () => Promise<void>;\n private connection?: EntityProviderConnection;\n\n static fromConfig(\n configRoot: Config,\n options: {\n logger: Logger;\n schedule?: TaskRunner;\n scheduler?: PluginTaskScheduler;\n },\n ): GerritEntityProvider[] {\n if (!options.schedule && !options.scheduler) {\n throw new Error('Either schedule or scheduler must be provided.');\n }\n\n const providerConfigs = readGerritConfigs(configRoot);\n const integrations = ScmIntegrations.fromConfig(configRoot).gerrit;\n const providers: GerritEntityProvider[] = [];\n\n providerConfigs.forEach(providerConfig => {\n const integration = integrations.byHost(providerConfig.host);\n if (!integration) {\n throw new InputError(\n `No gerrit integration found that matches host ${providerConfig.host}`,\n );\n }\n\n if (!options.schedule && !providerConfig.schedule) {\n throw new Error(\n `No schedule provided neither via code nor config for gerrit-provider:${providerConfig.id}.`,\n );\n }\n\n const taskRunner =\n options.schedule ??\n options.scheduler!.createScheduledTaskRunner(providerConfig.schedule!);\n\n providers.push(\n new GerritEntityProvider(\n providerConfig,\n integration,\n options.logger,\n taskRunner,\n ),\n );\n });\n return providers;\n }\n\n private constructor(\n config: GerritProviderConfig,\n integration: GerritIntegration,\n logger: Logger,\n taskRunner: TaskRunner,\n ) {\n this.config = config;\n this.integration = integration;\n this.logger = logger.child({\n target: this.getProviderName(),\n });\n this.scheduleFn = this.createScheduleFn(taskRunner);\n }\n\n getProviderName(): string {\n return `gerrit-provider:${this.config.id}`;\n }\n\n async connect(connection: EntityProviderConnection): Promise<void> {\n this.connection = connection;\n await this.scheduleFn();\n }\n\n private createScheduleFn(taskRunner: TaskRunner): () => Promise<void> {\n return async () => {\n const taskId = `${this.getProviderName()}:refresh`;\n return taskRunner.run({\n id: taskId,\n fn: async () => {\n const logger = this.logger.child({\n class: GerritEntityProvider.prototype.constructor.name,\n taskId,\n taskInstanceId: uuid.v4(),\n });\n\n try {\n await this.refresh(logger);\n } catch (error) {\n logger.error(`${this.getProviderName()} refresh failed`, error);\n }\n },\n });\n };\n }\n\n async refresh(logger: Logger): Promise<void> {\n if (!this.connection) {\n throw new Error('Gerrit discovery connection not initialized');\n }\n\n let response: Response;\n\n const baseProjectApiUrl = getGerritProjectsApiUrl(this.integration.config);\n const projectQueryUrl = `${baseProjectApiUrl}?${this.config.query}`;\n try {\n response = await fetch(projectQueryUrl, {\n method: 'GET',\n ...getGerritRequestOptions(this.integration.config),\n });\n } catch (e) {\n throw new Error(\n `Failed to list Gerrit projects for query ${this.config.query}, ${e}`,\n );\n }\n const gerritProjectsResponse = (await parseGerritJsonResponse(\n response as any,\n )) as GerritProjectQueryResult;\n const projects = Object.keys(gerritProjectsResponse);\n\n const locations = projects.map(project => this.createLocationSpec(project));\n await this.connection.applyMutation({\n type: 'full',\n entities: locations.map(location => ({\n locationKey: this.getProviderName(),\n entity: locationSpecToLocationEntity({ location }),\n })),\n });\n logger.info(`Found ${locations.length} locations.`);\n }\n\n private createLocationSpec(project: string): LocationSpec {\n return {\n type: 'url',\n target: `${this.integration.config.gitilesBaseUrl}/${project}/+/refs/heads/${this.config.branch}/catalog-info.yaml`,\n presence: 'optional',\n };\n }\n}\n"],"names":["readTaskScheduleDefinitionFromConfig","ScmIntegrations","InputError","uuid","getGerritProjectsApiUrl","fetch","getGerritRequestOptions","parseGerritJsonResponse","locationSpecToLocationEntity"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAoBA,SAAS,gBAAA,CAAiB,IAAY,MAAsC,EAAA;AApB5E,EAAA,IAAA,EAAA,CAAA;AAqBE,EAAA,MAAM,MAAS,GAAA,CAAA,EAAA,GAAA,MAAA,CAAO,iBAAkB,CAAA,QAAQ,MAAjC,IAAsC,GAAA,EAAA,GAAA,QAAA,CAAA;AACrD,EAAM,MAAA,IAAA,GAAO,MAAO,CAAA,SAAA,CAAU,MAAM,CAAA,CAAA;AACpC,EAAM,MAAA,KAAA,GAAQ,MAAO,CAAA,SAAA,CAAU,OAAO,CAAA,CAAA;AAEtC,EAAM,MAAA,QAAA,GAAW,MAAO,CAAA,GAAA,CAAI,UAAU,CAAA,GAClCA,kDAAqC,MAAO,CAAA,SAAA,CAAU,UAAU,CAAC,CACjE,GAAA,KAAA,CAAA,CAAA;AAEJ,EAAO,OAAA;AAAA,IACL,MAAA;AAAA,IACA,IAAA;AAAA,IACA,EAAA;AAAA,IACA,KAAA;AAAA,IACA,QAAA;AAAA,GACF,CAAA;AACF,CAAA;AAEO,SAAS,kBAAkB,MAAwC,EAAA;AACxE,EAAA,MAAM,UAAkC,EAAC,CAAA;AAEzC,EAAM,MAAA,eAAA,GAAkB,MAAO,CAAA,iBAAA,CAAkB,0BAA0B,CAAA,CAAA;AAE3E,EAAA,IAAI,CAAC,eAAiB,EAAA;AACpB,IAAO,OAAA,OAAA,CAAA;AAAA,GACT;AAEA,EAAW,KAAA,MAAA,EAAA,IAAM,eAAgB,CAAA,IAAA,EAAQ,EAAA;AACvC,IAAA,OAAA,CAAQ,KA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AAA,GAAA,CAAI,aAAW,IAAK,CAAA,kBAAA,CAAmB,OAAO,CAAC,CAAA,CAAA;AAC1E,IAAM,MAAA,IAAA,CAAK,WAAW,aAAc,CAAA;AAAA,MAClC,IAAM,EAAA,MAAA;AAAA,MACN,QAAA,EAAU,SAAU,CAAA,GAAA,CAAI,CAAa,QAAA,MAAA;AAAA,QACnC,WAAA,EAAa,KAAK,eAAgB,EAAA;AAAA,QAClC,MAAQ,EAAAC,8CAAA,CAA6B,EAAE,QAAA,EAAU,CAAA;AAAA,OACjD,CAAA,CAAA;AAAA,KACH,CAAA,CAAA;AACD,IAAO,MAAA,CAAA,IAAA,CAAK,CAAS,MAAA,EAAA,SAAA,CAAU,MAAmB,CAAA,WAAA,CAAA,CAAA,CAAA;AAAA,GACpD;AAAA,EAEQ,mBAAmB,OAA+B,EAAA;AACxD,IAAO,OAAA;AAAA,MACL,IAAM,EAAA,KAAA;AAAA,MACN,MAAA,EAAQ,GAAG,IAAK,CAAA,WAAA,CAAY,OAAO,cAAkB,CAAA,CAAA,EAAA,OAAA,CAAA,cAAA,EAAwB,KAAK,MAAO,CAAA,MAAA,CAAA,kBAAA,CAAA;AAAA,MACzF,QAAU,EAAA,UAAA;AAAA,KACZ,CAAA;AAAA,GACF;AACF;;;;"}
|
|
1
|
+
{"version":3,"file":"index.cjs.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-catalog-backend-module-gerrit",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12-next.1",
|
|
4
4
|
"main": "./dist/index.cjs.js",
|
|
5
5
|
"types": "./dist/index.d.ts",
|
|
6
6
|
"license": "Apache-2.0",
|
|
@@ -39,22 +39,22 @@
|
|
|
39
39
|
"clean": "backstage-cli package clean"
|
|
40
40
|
},
|
|
41
41
|
"dependencies": {
|
|
42
|
-
"@backstage/backend-common": "^0.18.
|
|
43
|
-
"@backstage/backend-plugin-api": "^0.5.
|
|
44
|
-
"@backstage/backend-tasks": "^0.5.
|
|
42
|
+
"@backstage/backend-common": "^0.18.4-next.1",
|
|
43
|
+
"@backstage/backend-plugin-api": "^0.5.1-next.1",
|
|
44
|
+
"@backstage/backend-tasks": "^0.5.1-next.1",
|
|
45
45
|
"@backstage/catalog-model": "^1.2.1",
|
|
46
46
|
"@backstage/config": "^1.0.7",
|
|
47
47
|
"@backstage/errors": "^1.1.5",
|
|
48
|
-
"@backstage/integration": "^1.4.
|
|
49
|
-
"@backstage/plugin-catalog-node": "^1.3.
|
|
48
|
+
"@backstage/integration": "^1.4.4-next.0",
|
|
49
|
+
"@backstage/plugin-catalog-node": "^1.3.5-next.1",
|
|
50
50
|
"fs-extra": "10.1.0",
|
|
51
51
|
"node-fetch": "^2.6.7",
|
|
52
52
|
"uuid": "^8.0.0",
|
|
53
53
|
"winston": "^3.2.1"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
|
-
"@backstage/backend-test-utils": "^0.1.
|
|
57
|
-
"@backstage/cli": "^0.22.
|
|
56
|
+
"@backstage/backend-test-utils": "^0.1.36-next.1",
|
|
57
|
+
"@backstage/cli": "^0.22.6-next.1",
|
|
58
58
|
"@types/fs-extra": "^9.0.1",
|
|
59
59
|
"luxon": "^3.0.0",
|
|
60
60
|
"msw": "^1.0.0"
|