/*
* Copyright (C) 2018 StApps
* This program is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see .
*/
import {Api} from '@openstapps/gitlab-api';
import {Group, Project} from '@openstapps/gitlab-api/lib/types';
import {Logger} from '@openstapps/logger';
import {asyncPool} from 'async-pool-native/dist/async-pool';
import {readFile, unlink, writeFile} from 'fs';
import * as glob from 'glob';
import {promisify} from 'util';
/**
* Instantiated logger
*/
export const logger = new Logger();
/**
* Get projects for a list of groups
*
* @param api GitLab API to make requests with
* @param groups List of groups
*/
export async function getProjects(api: Api, groups: number[]): Promise {
logger.info('Fetching all projects for specified groups (' + groups.length + ')...');
const projectResults = await asyncPool(3, groups, (groupId) => {
return api.getProjectsForGroup(groupId);
});
const projects = flatten2dArray(projectResults);
logger.log('Fetched ' + projects.length + ' project(s).');
return projects;
}
/**
* Get subgroups for a list of groups
*
* @param api GitLab API to make requests with
* @param groups List of groups
*/
export async function getSubGroups(api: Api, groups: number[]): Promise {
return flatten2dArray(await asyncPool(2, groups, async (groupId) => {
return await api.getSubGroupsForGroup(groupId);
}));
}
/**
* Flatten 2d array
*
* @param arr Flattened array
*/
export function flatten2dArray(arr: T[][]): T[] {
return ([] as T[]).concat(...arr);
}
/**
* Promisified version of readFile
*/
export const readFilePromisified = promisify(readFile);
/**
* Promisified version of glob
*/
export const globPromisified = promisify(glob);
/**
* Promisified version of writeFile
*/
export const writeFilePromisified = promisify(writeFile);
/**
* Promisified version of unlink
*/
export const unlinkPromisified = promisify(unlink);