Skip to content

Commit c9fa05e

Browse files
committed
Add backward compatibility for legacy config
1 parent cd411eb commit c9fa05e

File tree

3 files changed

+115
-18
lines changed

3 files changed

+115
-18
lines changed

src/index.js

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,13 @@ export default async function track({ services, types, extractOnly, schedule })
5656
}
5757

5858
if (process.env.OTA_ENGINE_GITHUB_TOKEN || process.env.OTA_ENGINE_GITLAB_TOKEN) {
59-
if (config.has('@opentermsarchive/engine.reporter.repositories.declarations')) {
60-
try {
61-
const reporter = new Reporter(config.get('@opentermsarchive/engine.reporter'));
62-
63-
await reporter.initialize();
64-
archivist.attach(reporter);
65-
} catch (error) {
66-
logger.error('Cannot instantiate the Reporter module; it will be ignored:', error);
67-
}
68-
} else {
69-
logger.warn('Configuration key "reporter.repositories.declarations" was not found; issues on the declarations repository cannot be created');
59+
try {
60+
const reporter = new Reporter(config.get('@opentermsarchive/engine.reporter'));
61+
62+
await reporter.initialize();
63+
archivist.attach(reporter);
64+
} catch (error) {
65+
logger.error('Cannot instantiate the Reporter module; it will be ignored:', error);
7066
}
7167
} else {
7268
logger.warn('Environment variable with token for GitHub or GitLab was not found; the Reporter module will be ignored');

src/reporter/index.js

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import mime from 'mime';
22

33
import { toISODateWithoutMilliseconds } from '../archivist/utils/date.js';
4+
import logger from '../logger/index.js';
45

56
import { createReporter } from './factory.js';
67

@@ -33,16 +34,53 @@ function getLabelNameFromError(error) {
3334
// In the following class, it is assumed that each issue is managed using its title as a unique identifier
3435
export default class Reporter {
3536
constructor(config) {
36-
const { repositories } = config;
37+
const normalizedConfig = Reporter.normalizeConfig(config);
3738

38-
for (const repositoryType of Object.keys(repositories)) {
39-
if (!repositories[repositoryType].includes('/') || repositories[repositoryType].includes('https://')) {
40-
throw new Error(`Configuration entry "reporter.repositories.${repositoryType}" is expected to be a string in the format <owner>/<repo>, but received: "${repositories[repositoryType]}"`);
41-
}
39+
Reporter.validateConfiguration(normalizedConfig.repositories);
40+
41+
this.reporter = createReporter(normalizedConfig);
42+
this.repositories = normalizedConfig.repositories;
43+
}
44+
45+
/**
46+
* Support for legacy config format where reporter configuration was nested under "githubIssues"
47+
* Example:
48+
*
49+
* ```json
50+
* {
51+
* "githubIssues": {
52+
* "repositories": {
53+
* "declarations": "OpenTermsArchive/sandbox-declarations"
54+
* }
55+
* }
56+
* }
57+
* ```
58+
*
59+
* @deprecated
60+
*/
61+
static normalizeConfig(config) {
62+
if (config.githubIssues) {
63+
logger.warn('The "reporter.githubIssues" key is deprecated; please see configuration documentation for the new format: https://docs.opentermsarchive.org/#configuring');
64+
65+
return {
66+
type: 'github',
67+
repositories: config.githubIssues.repositories,
68+
};
69+
}
70+
71+
return config;
72+
}
73+
74+
static validateConfiguration(repositories) {
75+
if (!repositories?.declarations) {
76+
throw new Error('Required configuration key "reporter.repositories.declarations" was not found; issues on the declarations repository cannot be created');
4277
}
4378

44-
this.reporter = createReporter(config);
45-
this.repositories = repositories;
79+
for (const [ type, repo ] of Object.entries(repositories)) {
80+
if (!repo.includes('/') || repo.includes('https://')) {
81+
throw new Error(`Configuration entry "reporter.repositories.${type}" is expected to be a string in the format <owner>/<repo>, but received: "${repo}"`);
82+
}
83+
}
4684
}
4785

4886
initialize() {

src/reporter/index.test.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { expect } from 'chai';
2+
3+
import Reporter from './index.js';
4+
5+
describe('Reporter', () => {
6+
describe('#normalizeConfig', () => {
7+
context('with current config format', () => {
8+
it('returns the config as is', () => {
9+
const config = { repositories: { declarations: 'owner/repo' } };
10+
const normalizedConfig = Reporter.normalizeConfig(config);
11+
12+
expect(normalizedConfig).to.deep.equal(config);
13+
});
14+
});
15+
16+
context('with old config format where githubIssues is nested under reporter', () => {
17+
it('returns a normalized config', () => {
18+
const config = { githubIssues: { repositories: { declarations: 'owner/repo' } } };
19+
const expectedConfig = {
20+
type: 'github',
21+
repositories: { declarations: 'owner/repo' },
22+
};
23+
const normalizedConfig = Reporter.normalizeConfig(config);
24+
25+
expect(normalizedConfig).to.deep.equal(expectedConfig);
26+
});
27+
});
28+
});
29+
30+
describe('#validateConfiguration', () => {
31+
context('with valid configuration', () => {
32+
it('does not throw an error', () => {
33+
const repositories = { declarations: 'owner/repo' };
34+
35+
expect(() => {
36+
Reporter.validateConfiguration(repositories);
37+
}).not.to.throw();
38+
});
39+
});
40+
41+
context('with invalid configuration', () => {
42+
context('when declarations key is missing', () => {
43+
it('throws an error', () => {
44+
const repositories = {};
45+
46+
expect(() => {
47+
Reporter.validateConfiguration(repositories);
48+
}).to.throw('Required configuration key "reporter.repositories.declarations" was not found; issues on the declarations repository cannot be created');
49+
});
50+
});
51+
52+
context('when repository format is incorrect', () => {
53+
it('throws an error', () => {
54+
const repositories = { declarations: 'invalidFormat' };
55+
56+
expect(() => {
57+
Reporter.validateConfiguration(repositories);
58+
}).to.throw('Configuration entry "reporter.repositories.declarations" is expected to be a string in the format <owner>/<repo>, but received: "invalidFormat"');
59+
});
60+
});
61+
});
62+
});
63+
});

0 commit comments

Comments
 (0)