@atomist/sdm-pack-fingerprints
Version:
an Atomist SDM Extension Pack for fingerprinting code
148 lines • 5.82 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) {
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) : new P(function (resolve) { resolve(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 clj_editors_1 = require("@atomist/clj-editors");
const sdm_1 = require("@atomist/sdm");
const array_1 = require("@atomist/sdm-core/lib/util/misc/array");
const messages_1 = require("../support/messages");
/**
* create callback to be used when fingerprint and target are out of sync
*/
function fingerprintOutOfSyncCallback(ctx, diff, aspect) {
return (text, fpTarget, fingerprint) => __awaiter(this, void 0, void 0, function* () {
return {
name: fingerprint.name,
decision: "Against",
abstain: false,
diff,
fingerprint,
fpTarget,
text,
summary: messages_1.getDiffSummary(diff, fpTarget, aspect),
};
});
}
/**
* create callback to be used when fingerprint and target is in sync
*
* @param ctx
* @param diff
* @param goal
*/
function fingerprintInSyncCallback(ctx, diff) {
return (fingerprint) => __awaiter(this, void 0, void 0, function* () {
return {
abstain: false,
name: fingerprint.name,
decision: "For",
};
});
}
/**
* just trying to capture how we update Goals
*
* @param ctx
* @param diff
* @param goal
* @param params
*/
function editGoal(ctx, diff, goal, params) {
return __awaiter(this, void 0, void 0, function* () {
automation_client_1.logger.info(`edit goal ${goal.name} to be in state ${params.state} for ${diff.owner}, ${diff.repo}, ${diff.sha}, ${diff.providerId}`);
try {
const id = new automation_client_1.GitHubRepoRef(diff.owner, diff.repo, diff.sha);
const complianceGoal = yield sdm_1.findSdmGoalOnCommit(ctx, id, diff.providerId, goal);
automation_client_1.logger.info(`found compliance goal in phase ${complianceGoal.phase}`);
if (!(complianceGoal.phase === sdm_1.SdmGoalState.failure)) {
return sdm_1.updateGoal(ctx, complianceGoal, params);
}
else {
return automation_client_1.SuccessPromise;
}
}
catch (error) {
automation_client_1.logger.error(`Error: ${error}`);
return automation_client_1.FailurePromise;
}
});
}
/**
* for target fingerprints, wait until we've seen all of Votes so we can expose both apply and
* apply all choices.
*
* only take this action if one of the diff handlers voted Against
* skip this if we don't know what channel to use
* make the message id unique by the current fingerprint sha, the target sha, the git coordinate and the channel
*
* @param config
*/
function votes(config) {
return (ctx, vs, coord, channel) => __awaiter(this, void 0, void 0, function* () {
const result = clj_editors_1.voteResults(vs);
let goalState;
if (result.failed) {
yield config.messageMaker({
ctx,
msgId: messages_1.updateableMessage([].concat(result.failedVotes.map(vote => vote.fingerprint.sha), result.failedVotes.map(vote => vote.fpTarget.sha)), coord),
channel,
voteResults: result,
coord,
aspects: array_1.toArray(config.aspects || []),
});
goalState = {
state: sdm_1.SdmGoalState.failure,
description: `compliance check for ${clj_editors_1.commaSeparatedList(result.failedVotes.map(vote => vote.fingerprint.name))} has failed`,
};
}
else {
goalState = {
state: sdm_1.SdmGoalState.success,
description: `compliance check for ${votes.length} fingerprints has passed`,
};
}
if (config.complianceGoal) {
return editGoal(ctx, coord, config.complianceGoal, goalState);
}
return automation_client_1.SuccessPromise;
});
}
exports.votes = votes;
/**
* check whether the fingerprint in this diff is the same as the target value
*
* @param ctx
* @param diff
* @param config
* @param registrations
* @param targetsQuery
*/
function checkFingerprintTarget(ctx, diff, aspect, targetsQuery) {
return __awaiter(this, void 0, void 0, function* () {
return clj_editors_1.checkFingerprintTargets(targetsQuery, fingerprintOutOfSyncCallback(ctx, diff, aspect), fingerprintInSyncCallback(ctx, diff), diff);
});
}
exports.checkFingerprintTarget = checkFingerprintTarget;
//# sourceMappingURL=callbacks.js.map