-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathget-repository-metadata.js
52 lines (44 loc) · 1.73 KB
/
get-repository-metadata.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
const Promise = require('bluebird');
const YAML = require('js-yaml');
const { getGithubRepositoryMetadata } = require('../repo');
const { logger } = require('../util');
/**
* Retrieve metadata from all GitHub repositories listed in tools stream
*
* Note this uses the Bluebird Promise to provide access to the concurrency
* limit on the map function. This is important as it allows the calls to the
* GitHub API to be regulated
*
* @param {string} rawNormalisedData Raw YAML data from Gulp file stream
* @returns {string} Raw YAML data to return to Gulp stream
*/
module.exports = async (rawNormalisedData) => {
logger(__filename, 'getRepositoryMetadata');
const concurrency = parseInt(process.env.GH_API_CONCURRENCY_LIMIT);
if (!concurrency || Number.isNaN(concurrency)) {
throw new Error('Concurrency limit is not set correctly or is not a number');
}
logger(`Concurrency limit for this run is set to ${concurrency}`);
const normalisedData = YAML.load(rawNormalisedData);
const enrichedData = await Promise.all(
Promise.map(
normalisedData,
async (source) => {
if (source.repository && typeof source.repository === 'string' && source.repository.match(/github\.com/)) {
logger(`Retrieving GitHub metadata for: ${source.repository}`);
const metadata = await getGithubRepositoryMetadata(
source.repository,
process.env.GH_API_USERNAME,
process.env.GH_API_TOKEN,
source.repositoryMetadata,
);
logger(`Successfully retrieved GitHub metadata for: ${source.repository}`);
return { ...source, ...metadata };
}
return source;
},
{ concurrency },
),
);
return YAML.dump(enrichedData);
};