UNPKG

@atomist/sdm-pack-aspect

Version:

an Atomist SDM Extension Pack for visualizing drift across an organization

151 lines 8.12 kB
"use strict"; /* * 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 sdm_1 = require("@atomist/sdm"); const sdm_pack_fingerprint_1 = require("@atomist/sdm-pack-fingerprint"); const fingerprints_1 = require("@atomist/sdm-pack-fingerprint/lib/adhoc/fingerprints"); const runner_1 = require("@atomist/sdm-pack-fingerprint/lib/machine/runner"); const _ = require("lodash"); function calculateFingerprintTask(sdm, aspects) { // Also register the provided aspects in the registration metadata sdm.configuration.metadata = Object.assign(Object.assign({}, sdm.configuration.metadata), { "atomist.aspects": JSON.stringify(aspects.map(a => ({ name: a.name, displayName: a.displayName }))) }); return { name: "CalculateFingerprintTask", description: "Trigger calculation of fingerprints on the provided repository", parameters: { providerId: { description: "Id of the SCMProvider" }, repoId: { description: "Id of the Repo" }, owner: { uri: automation_client_1.MappedParameters.GitHubOwner, declarationType: sdm_1.DeclarationType.Mapped }, name: { uri: automation_client_1.MappedParameters.GitHubRepository, declarationType: sdm_1.DeclarationType.Mapped }, branch: { description: "Name of the branch" }, }, listener: (ci) => __awaiter(this, void 0, void 0, function* () { try { const provider = _.get(yield ci.context.graphClient.query({ name: "ScmProviderById", variables: { providerId: ci.parameters.providerId, }, options: automation_client_1.QueryNoCacheOptions, }), "SCMProvider[0]"); const app = _.get(yield ci.context.graphClient.query({ name: "GitHubAppInstallationByOwner", variables: { name: ci.parameters.owner, }, options: automation_client_1.QueryNoCacheOptions, }), "GitHubAppInstallation[0]"); const token = _.get(provider, "credential.secret") || _.get(app, "token.secret"); const id = automation_client_1.GitHubRepoRef.from({ owner: ci.parameters.owner, repo: ci.parameters.name, branch: ci.parameters.branch, rawApiBase: provider.apiUrl, }); const credentials = !!token ? { token } : undefined; yield ci.configuration.sdm.projectLoader.doWithProject(Object.assign(Object.assign({}, ci), { id, credentials }), (p) => __awaiter(this, void 0, void 0, function* () { if (sdm_1.isLazyProjectLoader(ci.configuration.sdm.projectLoader)) { yield p.materialize(); } // git rev-parse HEAD = sha const sha = (yield sdm_1.execPromise("git", ["rev-parse", "HEAD"], { cwd: p.baseDir })).stdout.trim(); // const author = (await execPromise("git", ["show", "-s", "--format=%an"], { cwd: p.baseDir })).stdout.trim(); const email = (yield sdm_1.execPromise("git", ["show", "-s", "--format=%ae"], { cwd: p.baseDir })).stdout.trim(); const ts = (yield sdm_1.execPromise("git", ["show", "-s", "--format=%at"], { cwd: p.baseDir })).stdout.trim(); const message = (yield sdm_1.execPromise("git", ["show", "-s", "--format=%B"], { cwd: p.baseDir })).stdout.trim(); // Ingest initial commit const commit = { repoId: ci.parameters.repoId, branchName: ci.parameters.branch, timestamp: new Date(+ts * 1000).toISOString(), email: { address: email, }, sha, message, }; yield ci.context.graphClient.mutate({ name: "IngestScmCommit", variables: { providerId: provider.id, commit, }, }); // Run the fingerprint code const pi = { context: ci.context, configuration: ci.configuration, project: p, addressChannels: ci.addressChannels, preferences: ci.preferences, credentials: ci.credentials, id, impactedSubProject: p, filesChanged: undefined, commit: { sha, message, }, push: { repo: { defaultBranch: ci.parameters.branch, channels: [], name: ci.parameters.name, owner: ci.parameters.owner, org: { owner: ci.parameters.owner, provider: { apiUrl: provider.apiUrl, providerId: ci.parameters.providerId, providerType: sdm_1.ProviderType.github_com, }, }, }, after: { sha, message, }, commits: [{ sha, message, }], branch: ci.parameters.branch, timestamp: ts, }, }; const fingerprintComputer = runner_1.createFingerprintComputer(aspects); yield sdm_pack_fingerprint_1.fingerprintRunner(aspects, [], fingerprintComputer, fingerprints_1.sendFingerprintsToAtomist)(pi); })); } catch (e) { automation_client_1.logger.error(`Fingerprinting task failed to execute: ${e.message}`); } }), }; } exports.calculateFingerprintTask = calculateFingerprintTask; //# sourceMappingURL=fingerprintTask.js.map