@atomist/atomist-sdm
Version:
Atomist SDM to deliver our own projects
89 lines (87 loc) • 4.7 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 sdm_1 = require("@atomist/sdm");
const sdm_core_1 = require("@atomist/sdm-core");
const slack_messages_1 = require("@atomist/slack-messages");
const _ = require("lodash");
const addCommunityFiles_1 = require("../autofix/addCommunityFiles");
const goals_1 = require("./goals");
function addTeamPolicies(sdm) {
// Upper case the title of a new issue
sdm.addNewIssueListener((l) => __awaiter(this, void 0, void 0, function* () {
return upperCaseTitle(l.issue, l.credentials, l.id);
}));
// Upper case the title of a new pull request
sdm.addPullRequestListener((l) => __awaiter(this, void 0, void 0, function* () {
return upperCaseTitle(l.pullRequest, l.credentials, l.id);
}));
// Check case of commit message; they should use upper case too
goals_1.pushImpact.withListener((l) => __awaiter(this, void 0, void 0, function* () {
const violations = l.push.commits.filter(c => !isUpperCase(c.message) || titleEndsWithPeriod(c.message));
const screenName = _.get(l.push, "after.committer.person.chatId.screenName");
if (screenName && violations.length > 0) {
yield warnAboutInvalidCommitMessages(sdm, l, violations, screenName);
}
return automation_client_1.Success;
}));
goals_1.autofix.with(addCommunityFiles_1.AddCommunityFiles);
// autofix.with(UpdateSupportFilesFix);
}
exports.addTeamPolicies = addTeamPolicies;
function warnAboutInvalidCommitMessages(sdm, pushImpactListenerInvocation, commits, screenName) {
return __awaiter(this, void 0, void 0, function* () {
const msg = sdm_1.slackWarningMessage("Commit Message", `Please make sure that your commit messages start with an upper case letter and do not end with a period.
The following ${commits.length > 1 ? "commits" : "commit"} in ${slack_messages_1.bold(`${pushImpactListenerInvocation.push.repo.owner}/${pushImpactListenerInvocation.push.repo.name}/${pushImpactListenerInvocation.push.branch}`)} ${commits.length > 1 ? "don't" : "doesn't"} follow that standard:
${commits.map(c => `${slack_messages_1.codeLine(c.sha.slice(0, 7))} ${sdm_core_1.truncateCommitMessage(c.message, pushImpactListenerInvocation.push.repo)}`).join("\n")}`, pushImpactListenerInvocation.context, {
footer: `${sdm.configuration.name}:${sdm.configuration.version}`,
});
yield pushImpactListenerInvocation.context.messageClient.addressUsers(msg, screenName, {
id: `team_policies/commit_messages/${pushImpactListenerInvocation.push.after.sha}`,
});
});
}
function upperCaseTitle(issueOrPr, credentials, rr) {
return __awaiter(this, void 0, void 0, function* () {
const title = issueOrPr.title;
if (!isUpperCase(title)) {
const newIssue = {
title: _.upperFirst(title),
body: issueOrPr.body,
};
yield sdm_core_1.github.updateIssue(credentials, rr, issueOrPr.number, newIssue);
}
return automation_client_1.Success;
});
}
function isUpperCase(message) {
return message && message.charAt(0) === message.charAt(0).toUpperCase();
}
function titleEndsWithPeriod(message) {
return message && message.split("\n")[0].trim().endsWith(".");
}
exports.titleEndsWithPeriod = titleEndsWithPeriod;
//# sourceMappingURL=teamPolicies.js.map