@atomist/sdm-core
Version:
Atomist Software Delivery Machine - Implementation
153 lines (136 loc) • 5.13 kB
text/typescript
/*
* Copyright © 2019 Atomist, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
GitHubRepoRef,
Maker,
MappedParameter,
MappedParameters,
Parameters,
Success,
Value,
} from "@atomist/automation-client";
import {
chooseAndSetGoals,
CommandHandlerRegistration,
CommandListener,
CommandListenerInvocation,
GitHubRepoTargets,
RepoTargetingParameters,
RepoTargets,
slackSuccessMessage,
slackWarningMessage,
SoftwareDeliveryMachine,
toRepoTargetingParametersMaker,
} from "@atomist/sdm";
import {
bold,
codeLine,
italic,
} from "@atomist/slack-messages";
import {
fetchBranchTips,
fetchPushForCommit,
tipOfBranch,
} from "../../util/graph/queryCommits";
()
export class ResetGoalsParameters {
(MappedParameters.GitHubRepositoryProvider)
public providerId: string;
("name")
public name: string;
("version")
public version: string;
}
export function resetGoalsCommand(
sdm: SoftwareDeliveryMachine,
repoTargets: Maker<RepoTargets> = GitHubRepoTargets,
): CommandHandlerRegistration<ResetGoalsParameters & RepoTargetingParameters> {
return {
name: "ResetGoalsOnCommit",
description: "Plan goals on a commit",
paramsMaker: toRepoTargetingParametersMaker(ResetGoalsParameters, repoTargets),
listener: resetGoalsOnCommit(sdm),
intent: [
`reset goals ${sdm.configuration.name.replace("@", "")}`,
`plan goals ${sdm.configuration.name.replace("@", "")}`,
],
};
}
function resetGoalsOnCommit(sdm: SoftwareDeliveryMachine): CommandListener<ResetGoalsParameters & RepoTargetingParameters> {
return async (cli: CommandListenerInvocation<ResetGoalsParameters & RepoTargetingParameters>) => {
const rules = {
projectLoader: sdm.configuration.sdm.projectLoader,
repoRefResolver: sdm.configuration.sdm.repoRefResolver,
goalsListeners: [...sdm.goalsSetListeners],
goalSetter: sdm.pushMapping,
implementationMapping: sdm.goalFulfillmentMapper,
preferencesFactory: sdm.configuration.sdm.preferenceStoreFactory,
};
const slug = `${cli.parameters.targets.repoRef.owner}/${cli.parameters.targets.repoRef.repo}`;
let repoData;
try {
repoData = await fetchBranchTips(cli.context, {
providerId: cli.parameters.providerId,
owner: cli.parameters.targets.repoRef.owner,
repo: cli.parameters.targets.repoRef.repo,
});
} catch (e) {
const text = `Repository ${bold(slug)} not found`;
return cli.context.messageClient.respond(slackWarningMessage("Set Goal State", text, cli.context));
}
const branch = cli.parameters.targets.repoRef.branch || repoData.defaultBranch;
let sha;
try {
sha = cli.parameters.targets.repoRef.sha || tipOfBranch(repoData, branch);
} catch (e) {
return cli.context.messageClient.respond(
slackWarningMessage(
"Set Goal State",
`Branch ${bold(branch)} not found on ${bold(slug)}`,
cli.context));
}
const id = GitHubRepoRef.from({
owner: cli.parameters.targets.repoRef.owner,
repo: cli.parameters.targets.repoRef.repo,
sha,
branch,
});
const push = await fetchPushForCommit(cli.context, id, cli.parameters.providerId);
const goals = await chooseAndSetGoals(rules, {
context: cli.context,
credentials: cli.credentials,
push,
});
const slugBranch = `${id.owner}/${id.repo}/${push.branch}`;
if (goals) {
await cli.addressChannels(slackSuccessMessage(
"Plan Goals",
`Successfully planned goals on ${codeLine(push.after.sha.slice(0, 7))} of ${bold(slugBranch)} to ${italic(goals.name)}`,
{
footer: `${cli.parameters.name}:${cli.parameters.version}`,
}));
} else {
await cli.addressChannels(slackWarningMessage(
"Plan Goals",
`No goals found for ${codeLine(push.after.sha.slice(0, 7))} of ${bold(slugBranch)}`,
cli.context,
{
footer: `${cli.parameters.name}:${cli.parameters.version}`,
}));
}
return Success;
};
}