2020-08-26 16:49:43 -04:00
|
|
|
/**
|
|
|
|
* @license
|
|
|
|
* Copyright Google LLC All Rights Reserved.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by an MIT-style license that can be
|
|
|
|
* found in the LICENSE file at https://angular.io/license
|
|
|
|
*/
|
|
|
|
|
2020-10-08 14:49:10 -04:00
|
|
|
import {alias, onUnion, params, types} from 'typed-graphqlify';
|
2020-08-26 16:49:43 -04:00
|
|
|
|
|
|
|
import {bold, debug, info} from '../../utils/console';
|
|
|
|
import {GitClient} from '../../utils/git';
|
|
|
|
import {CaretakerConfig} from '../config';
|
|
|
|
|
2020-10-08 14:49:10 -04:00
|
|
|
/**
|
|
|
|
* Cap the returned issues in the queries to an arbitrary 100. At that point, caretaker has a lot
|
|
|
|
* of work to do and showing more than that isn't really useful.
|
|
|
|
*/
|
|
|
|
const MAX_RETURNED_ISSUES = 20;
|
2020-08-26 16:49:43 -04:00
|
|
|
|
|
|
|
/** Retrieve the number of matching issues for each github query. */
|
2020-09-15 18:38:29 -04:00
|
|
|
export async function printGithubTasks(git: GitClient, config?: CaretakerConfig) {
|
|
|
|
if (!config?.githubQueries?.length) {
|
2020-08-26 16:49:43 -04:00
|
|
|
debug('No github queries defined in the configuration, skipping.');
|
|
|
|
return;
|
|
|
|
}
|
2020-10-08 14:49:10 -04:00
|
|
|
info.group(bold(`Github Tasks`));
|
2020-08-26 16:49:43 -04:00
|
|
|
await getGithubInfo(git, config);
|
|
|
|
info.groupEnd();
|
|
|
|
info();
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Retrieve query match counts and log discovered counts to the console. */
|
|
|
|
async function getGithubInfo(git: GitClient, {githubQueries: queries = []}: CaretakerConfig) {
|
|
|
|
/** The query object for graphql. */
|
2020-10-08 14:49:10 -04:00
|
|
|
const graphQlQuery: {
|
|
|
|
[key: string]: {
|
|
|
|
issueCount: number,
|
|
|
|
nodes: Array<{url: string}>,
|
|
|
|
}
|
|
|
|
} = {};
|
2020-08-26 16:49:43 -04:00
|
|
|
/** The Github search filter for the configured repository. */
|
|
|
|
const repoFilter = `repo:${git.remoteConfig.owner}/${git.remoteConfig.name}`;
|
|
|
|
queries.forEach(({name, query}) => {
|
|
|
|
/** The name of the query, with spaces removed to match GraphQL requirements. */
|
|
|
|
const queryKey = alias(name.replace(/ /g, ''), 'search');
|
|
|
|
graphQlQuery[queryKey] = params(
|
|
|
|
{
|
|
|
|
type: 'ISSUE',
|
2020-10-08 14:49:10 -04:00
|
|
|
first: MAX_RETURNED_ISSUES,
|
2020-08-26 16:49:43 -04:00
|
|
|
query: `"${repoFilter} ${query.replace(/"/g, '\\"')}"`,
|
|
|
|
},
|
2020-10-08 14:49:10 -04:00
|
|
|
{
|
|
|
|
issueCount: types.number,
|
|
|
|
nodes: [{...onUnion({
|
|
|
|
PullRequest: {
|
|
|
|
url: types.string,
|
|
|
|
},
|
|
|
|
Issue: {
|
|
|
|
url: types.string,
|
|
|
|
},
|
|
|
|
})}],
|
|
|
|
},
|
2020-08-26 16:49:43 -04:00
|
|
|
);
|
|
|
|
});
|
|
|
|
/** The results of the generated github query. */
|
|
|
|
const results = await git.github.graphql.query(graphQlQuery);
|
|
|
|
Object.values(results).forEach((result, i) => {
|
|
|
|
info(`${queries[i]?.name.padEnd(25)} ${result.issueCount}`);
|
2020-10-08 14:49:10 -04:00
|
|
|
if (result.issueCount > 0) {
|
|
|
|
const {owner, name: repo} = git.remoteConfig;
|
|
|
|
const url = encodeURI(`https://github.com/${owner}/${repo}/issues?q=${queries[i]?.query}`);
|
|
|
|
info.group(`${url}`);
|
|
|
|
if (result.nodes.length === MAX_RETURNED_ISSUES && result.nodes.length < result.issueCount) {
|
|
|
|
info(`(first ${MAX_RETURNED_ISSUES})`);
|
|
|
|
}
|
|
|
|
for (const node of result.nodes) {
|
|
|
|
info(`- ${node.url}`);
|
|
|
|
}
|
|
|
|
info.groupEnd();
|
|
|
|
}
|
2020-08-26 16:49:43 -04:00
|
|
|
});
|
|
|
|
}
|