@atomist/sdm-core
Version:
Atomist Software Delivery Machine - Implementation
106 lines • 4.67 kB
JavaScript
/*
* 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.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const types_1 = require("../../typings/types");
const ghub_1 = require("../../util/github/ghub");
function createPendingGitHubStatusOnGoalSet(sdm) {
return (inv) => __awaiter(this, void 0, void 0, function* () {
const { id, credentials } = inv;
if (inv.goalSet && inv.goalSet.goals && inv.goalSet.goals.length > 0) {
return ghub_1.createStatus(credentials, id, {
context: context(sdm),
description: `${prefix(sdm)} in progress`,
target_url: link(inv, inv.context),
state: "pending",
});
}
else {
return Promise.resolve();
}
});
}
exports.createPendingGitHubStatusOnGoalSet = createPendingGitHubStatusOnGoalSet;
function setGitHubStatusOnGoalCompletion(sdm) {
return (inv) => __awaiter(this, void 0, void 0, function* () {
const { id, completedGoal, allGoals, credentials } = inv;
if (completedGoal.state === "failure") {
automation_client_1.logger.debug("Setting GitHub status to failed on %s", id.sha);
return ghub_1.createStatus(credentials, id, {
context: context(sdm),
description: `${prefix(sdm)}: ${completedGoal.description}`,
target_url: link(inv.completedGoal, inv.context),
state: "failure",
});
}
if (allSuccessful(allGoals)) {
automation_client_1.logger.debug("Setting GitHub status to success on %s", id.sha);
return ghub_1.createStatus(credentials, id, {
context: context(sdm),
description: `${prefix(sdm)}: all succeeded`,
target_url: link(inv.completedGoal, inv.context),
state: "success",
});
}
return;
});
}
exports.setGitHubStatusOnGoalCompletion = setGitHubStatusOnGoalCompletion;
function allSuccessful(goals) {
return !goals.some(g => g.state !== "success");
}
function sdmGoalStateToGitHubStatusState(goalState) {
switch (goalState) {
case types_1.SdmGoalState.planned:
case types_1.SdmGoalState.requested:
case types_1.SdmGoalState.in_process:
case types_1.SdmGoalState.waiting_for_approval:
case types_1.SdmGoalState.waiting_for_pre_approval:
case types_1.SdmGoalState.approved:
case types_1.SdmGoalState.pre_approved:
return "pending";
case types_1.SdmGoalState.success:
return "success";
case types_1.SdmGoalState.failure:
case types_1.SdmGoalState.skipped:
case types_1.SdmGoalState.stopped:
case types_1.SdmGoalState.canceled:
return "failure";
default:
throw new Error("Unknown goal state " + goalState);
}
}
exports.sdmGoalStateToGitHubStatusState = sdmGoalStateToGitHubStatusState;
function prefix(sdm) {
return sdm.name && sdm.name.length > 0 ? `${sdm.name} goals` : "Atomist SDM goals";
}
function context(sdm) {
return `sdm/${sdm.configuration.name.replace("@", "")}`;
}
function link(event, ctx) {
return `https://app.atomist.com/workspace/${ctx.workspaceId}/goalset/${event.goalSetId}`;
}
//# sourceMappingURL=statusSetters.js.map
;