Skip to content

Commit

Permalink
Single repo operations as jobs
Browse files Browse the repository at this point in the history
[changelog:added]
  • Loading branch information
cdupuis committed Aug 21, 2019
1 parent cf6b0a6 commit 1b87048
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 11 deletions.
7 changes: 0 additions & 7 deletions lib/api-helper/command/transform/chattyDryRunAwareEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,6 @@ export function chattyDryRunAwareEditor(editorName: string,

const tentativeEditResult = await toEditor(underlyingEditor)(project, context, params);
const editResult = await confirmEditedness(tentativeEditResult);
logger.debug(
"Code Transform %s on '%s/%s': git status: '%s', edit result: %s",
editorName,
project.id.owner,
project.id.repo,
stringify(await project.gitStatus(), replacer),
stringify(editResult, replacer));

// Figure out if this CodeTransform is running in dryRun mode; if so capture git diff and don't push changes
if (!editResult.edited) {
Expand Down
68 changes: 64 additions & 4 deletions lib/api-helper/machine/handlerRegistrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import {
codeBlock,
italic,
} from "@atomist/slack-messages";
import * as _ from "lodash";
import { GitHubRepoTargets } from "../../api/command/target/GitHubRepoTargets";
import { isTransformModeSuggestion } from "../../api/command/target/TransformModeSuggestion";
import { NoParameterPrompt } from "../../api/context/parameterPrompt";
Expand Down Expand Up @@ -123,6 +124,7 @@ export function codeTransformRegistrationToCommand(sdm: MachineOrMachineOptions,
tagWith(ctr, TransformTag);
const mo = toMachineOptions(sdm);
addDryRunParameters(ctr);
addJobParameters(ctr);
addParametersDefinedInBuilder(ctr);
ctr.paramsMaker = toRepoTargetingParametersMaker(
ctr.paramsMaker || NoParameters,
Expand Down Expand Up @@ -155,19 +157,28 @@ ${codeBlock(vr.message)}`,
false,
ci.context);

const concurrency = {
maxConcurrent: 2, // TODO make maxConcurrent globally configurable
requiresJob: false,
...(ctr.concurrency || {}),
};

try {
const ids = await relevantRepos(ci.context, repoFinder, andFilter(targets.test, ctr.repoFilter));
if (ids.length > 1) {
const requiresJob = _.get(ci.parameters, "job.required", concurrency.requiresJob);

if (ids.length > 1 || !!requiresJob) {

const params: any = {
...ci.parameters,
};
params.targets.repos = undefined;
params.targets.repo = undefined;
delete params.job;

await createJob(
{
name: `CodeTransform/${ci.commandName}`,
name: _.get(ci.parameters, "job.name") || `CodeTransform/${ci.commandName}`,
command: ci.commandName,
parameters: ids.map(id => ({
...params,
Expand All @@ -177,8 +188,13 @@ ${codeBlock(vr.message)}`,
branch: id.branch,
sha: id.sha,
},
job: {
required: false,
}
})),
description: `Running code transform ${italic(ci.commandName)} on ${ids.length} repositories`,
description: _.get(ci.parameters, "job.description")
|| `Running code transform ${italic(ci.commandName)} on ${ids.length} ${ids.length === 1 ? "repository" : "repositories"}`,
concurrentTasks: concurrency.maxConcurrent,
},
ci.context);

Expand Down Expand Up @@ -227,6 +243,7 @@ ${codeBlock(vr.message)}`,
export function codeInspectionRegistrationToCommand<R>(sdm: MachineOrMachineOptions, cir: CodeInspectionRegistration<R, any>): Maker<HandleCommand> {
tagWith(cir, InspectionTag);
const mo = toMachineOptions(sdm);
addJobParameters(cir);
addParametersDefinedInBuilder(cir);
cir.paramsMaker = toRepoTargetingParametersMaker(
cir.paramsMaker || NoParameters,
Expand Down Expand Up @@ -267,15 +284,24 @@ ${codeBlock(vr.message)}`,
(ci.parameters as RepoTargetingParameters).targets.credentials,
true,
ci.context);

const concurrency = {
maxConcurrent: 2, // TODO make maxConcurrent globally configurable
requiresJob: false,
...(cir.concurrency || {}),
};

try {
const ids = await relevantRepos(ci.context, repoFinder, andFilter(targets.test, cir.repoFilter));
if (ids.length > 1) {
const requiresJob = _.get(ci.parameters, "job.required", concurrency.requiresJob);
if (ids.length > 1 || !!requiresJob) {

const params: any = {
...ci.parameters,
};
params.targets.repos = undefined;
params.targets.repo = undefined;
delete params.job;

await createJob(
{
Expand All @@ -289,6 +315,9 @@ ${codeBlock(vr.message)}`,
branch: id.branch,
sha: id.sha,
},
job: {
required: false,
}
})),
description: `Running code inspection ${italic(ci.commandName)} on ${ids.length} repositories`,
},
Expand Down Expand Up @@ -474,6 +503,37 @@ function addDryRunParameters<PARAMS>(c: CommandRegistration<PARAMS>): void {
c.parameters = params;
}

export const JobNameParameter: NamedParameter = {
name: "job.name",
description: "Name of the job to create",
required: false,
type: "string",
displayable: false,
};
export const JobDescriptionParameter: NamedParameter = {
name: "job.description",
description: "Description of the job to create",
required: false,
type: "string",
displayable: false,
};
export const JobRequiredParameter: NamedParameter = {
name: "job.required",
description: "Is job required",
required: false,
type: "string",
displayable: false,
};

/**
* Add the job parameter into the list of parameters
*/
function addJobParameters<PARAMS>(c: CommandRegistration<PARAMS>): void {
const params = toParametersListing(c.parameters || {} as any);
params.parameters.push(JobNameParameter, JobDescriptionParameter, JobRequiredParameter);
c.parameters = params;
}

/**
* Add to the existing ParametersMaker any parameters defined in the builder itself
* @param {CommandHandlerRegistration<PARAMS>} c
Expand Down
10 changes: 10 additions & 0 deletions lib/api/registration/ProjectsOperationRegistration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,14 @@ export interface ProjectsOperationRegistration<PARAMS> extends CommandRegistrati
*/
projectTest?: ProjectPredicate;

/**
* Configure the concurrency behavior of running project operations
* on many repos
*/
concurrency?: {
/** Maximum number of concurrent project operations would be executing */
maxConcurrent?: number;
/** Indicate if a Job scheduling is required; even for only one project operation */
requiresJob?: boolean;
}
}

0 comments on commit 1b87048

Please sign in to comment.