|
| 1 | +"use strict"; |
| 2 | +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 3 | + if (k2 === undefined) k2 = k; |
| 4 | + Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); |
| 5 | +}) : (function(o, m, k, k2) { |
| 6 | + if (k2 === undefined) k2 = k; |
| 7 | + o[k2] = m[k]; |
| 8 | +})); |
| 9 | +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 10 | + Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 11 | +}) : function(o, v) { |
| 12 | + o["default"] = v; |
| 13 | +}); |
| 14 | +var __importStar = (this && this.__importStar) || function (mod) { |
| 15 | + if (mod && mod.__esModule) return mod; |
| 16 | + var result = {}; |
| 17 | + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); |
| 18 | + __setModuleDefault(result, mod); |
| 19 | + return result; |
| 20 | +}; |
| 21 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 22 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 23 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 24 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 25 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 26 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 27 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 28 | + }); |
| 29 | +}; |
| 30 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 31 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 32 | +}; |
| 33 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 34 | +const axios_1 = __importDefault(require("axios")); |
| 35 | +const mablApiClient_1 = require("./mablApiClient"); |
| 36 | +const table_1 = require("./table"); |
| 37 | +const core = __importStar(require("@actions/core")); |
| 38 | +const github = __importStar(require("@actions/github")); |
| 39 | +const DEFAULT_MABL_APP_URL = 'https://app.mabl.com'; |
| 40 | +const EXECUTION_POLL_INTERVAL_MILLIS = 10000; |
| 41 | +const EXECUTION_COMPLETED_STATUSES = [ |
| 42 | + 'succeeded', |
| 43 | + 'failed', |
| 44 | + 'cancelled', |
| 45 | + 'completed', |
| 46 | + 'terminated', |
| 47 | +]; |
| 48 | +const GITHUB_BASE_URL = 'https://api.github.com'; |
| 49 | +function run() { |
| 50 | + var _a, _b, _c; |
| 51 | + return __awaiter(this, void 0, void 0, function* () { |
| 52 | + try { |
| 53 | + core.startGroup('Gathering inputs'); |
| 54 | + const applicationId = core.getInput('application-id', { |
| 55 | + required: false, |
| 56 | + }); |
| 57 | + const environmentId = core.getInput('environment-id', { |
| 58 | + required: false, |
| 59 | + }); |
| 60 | + const apiKey = process.env.MABL_API_KEY || ''; |
| 61 | + if (!apiKey) { |
| 62 | + core.setFailed('MABL_API_KEY required'); |
| 63 | + } |
| 64 | + const browserTypes = core.getInput('browser-types', { |
| 65 | + required: false, |
| 66 | + }); |
| 67 | + const uri = core.getInput('uri', { required: false }); |
| 68 | + const rebaselineImages = parseBoolean(core.getInput('rebaseline-images', { |
| 69 | + required: false, |
| 70 | + })); |
| 71 | + const setStaticBaseline = parseBoolean(core.getInput('set-static-baseline', { |
| 72 | + required: false, |
| 73 | + })); |
| 74 | + const continueOnPlanFailure = parseBoolean(core.getInput('continue-on-failure', { required: false })); |
| 75 | + const pullRequest = yield getRelatedPullRequest(); |
| 76 | + const eventTimeString = core.getInput('event-time', { required: false }); |
| 77 | + const eventTime = eventTimeString ? parseInt(eventTimeString) : Date.now(); |
| 78 | + let properties = { |
| 79 | + triggering_event_name: process.env.GITHUB_EVENT_NAME, |
| 80 | + repository_commit_username: process.env.GITHUB_ACTOR, |
| 81 | + repository_action: process.env.GITHUB_ACTION, |
| 82 | + repository_branch_name: process.env.GITHUB_REF, |
| 83 | + repository_name: process.env.GITHUB_REPOSITORY, |
| 84 | + repository_url: `git@github.com:${process.env.GITHUB_REPOSITORY}.git`, |
| 85 | + }; |
| 86 | + if (pullRequest) { |
| 87 | + properties = Object.assign(properties, { |
| 88 | + repository_pull_request_url: pullRequest.url, |
| 89 | + repository_pull_request_number: pullRequest.number, |
| 90 | + repository_pull_request_title: pullRequest.title, |
| 91 | + repository_pull_request_created_at: pullRequest.created_at, |
| 92 | + }); |
| 93 | + if (pullRequest.merged_at) { |
| 94 | + properties.repository_pull_request_merged_at = pullRequest.merged_at; |
| 95 | + } |
| 96 | + } |
| 97 | + const baseApiUrl = (_a = process.env.APP_URL) !== null && _a !== void 0 ? _a : DEFAULT_MABL_APP_URL; |
| 98 | + const revision = process.env.GITHUB_EVENT_NAME === 'pull_request' |
| 99 | + ? (_c = (_b = github.context.payload.pull_request) === null || _b === void 0 ? void 0 : _b.head) === null || _c === void 0 ? void 0 : _c.sha |
| 100 | + : process.env.GITHUB_SHA; |
| 101 | + core.info(`Using git revision [${revision}]`); |
| 102 | + core.endGroup(); |
| 103 | + core.startGroup('Creating deployment event'); |
| 104 | + const apiClient = new mablApiClient_1.MablApiClient(apiKey); |
| 105 | + const deployment = yield apiClient.postDeploymentEvent(applicationId, environmentId, browserTypes, uri, rebaselineImages, setStaticBaseline, revision, eventTime, properties); |
| 106 | + core.setOutput('mabl-deployment-id', deployment.id); |
| 107 | + let outputLink = baseApiUrl; |
| 108 | + if (applicationId) { |
| 109 | + const application = yield apiClient.getApplication(applicationId); |
| 110 | + outputLink = `${baseApiUrl}/workspaces/${application.organization_id}/events/${deployment.id}`; |
| 111 | + core.info(`Deployment triggered. View output at: ${outputLink}`); |
| 112 | + } |
| 113 | + core.startGroup('Await completion of tests'); |
| 114 | + let executionComplete = false; |
| 115 | + while (!executionComplete) { |
| 116 | + yield new Promise((resolve) => setTimeout(resolve, EXECUTION_POLL_INTERVAL_MILLIS)); |
| 117 | + const executionResult = yield apiClient.getExecutionResults(deployment.id); |
| 118 | + if (executionResult === null || executionResult === void 0 ? void 0 : executionResult.executions) { |
| 119 | + const pendingExecutions = getExecutionsStillPending(executionResult); |
| 120 | + if (pendingExecutions.length === 0) { |
| 121 | + executionComplete = true; |
| 122 | + } |
| 123 | + else { |
| 124 | + core.info(`${pendingExecutions.length} mabl plan(s) are still running`); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + core.info('mabl deployment runs have completed'); |
| 129 | + core.endGroup(); |
| 130 | + core.startGroup('Fetch execution results'); |
| 131 | + const finalExecutionResult = yield apiClient.getExecutionResults(deployment.id); |
| 132 | + finalExecutionResult.executions.forEach((execution) => { |
| 133 | + core.info(table_1.prettyFormatExecution(execution)); |
| 134 | + }); |
| 135 | + core.setOutput('plans_run', '' + finalExecutionResult.plan_execution_metrics.total); |
| 136 | + core.setOutput('plans_passed', '' + finalExecutionResult.plan_execution_metrics.passed); |
| 137 | + core.setOutput('plans_failed', '' + finalExecutionResult.plan_execution_metrics.failed); |
| 138 | + core.setOutput('tests_run', '' + finalExecutionResult.journey_execution_metrics.total); |
| 139 | + core.setOutput('tests_passed', '' + finalExecutionResult.journey_execution_metrics.passed); |
| 140 | + core.setOutput('tests_failed', '' + finalExecutionResult.journey_execution_metrics.failed); |
| 141 | + if (finalExecutionResult.journey_execution_metrics.failed === 0) { |
| 142 | + core.debug('Deployment plans passed'); |
| 143 | + } |
| 144 | + else if (continueOnPlanFailure) { |
| 145 | + core.warning(`There were ${finalExecutionResult.journey_execution_metrics.failed} test failures but the continueOnPlanFailure flag is set so the task has been marked as passing`); |
| 146 | + } |
| 147 | + else { |
| 148 | + core.setFailed(`${finalExecutionResult.journey_execution_metrics.failed} mabl test(s) failed`); |
| 149 | + } |
| 150 | + core.endGroup(); |
| 151 | + } |
| 152 | + catch (err) { |
| 153 | + core.setFailed(`mabl deployment task failed for the following reason: ${err}`); |
| 154 | + } |
| 155 | + }); |
| 156 | +} |
| 157 | +function parseBoolean(toParse) { |
| 158 | + return (toParse === null || toParse === void 0 ? void 0 : toParse.toLowerCase()) === 'true'; |
| 159 | +} |
| 160 | +function getExecutionsStillPending(executionResult) { |
| 161 | + return executionResult.executions.filter((execution) => { |
| 162 | + return !(EXECUTION_COMPLETED_STATUSES.includes(execution.status) && |
| 163 | + execution.stop_time); |
| 164 | + }); |
| 165 | +} |
| 166 | +function getRelatedPullRequest() { |
| 167 | + var _a; |
| 168 | + return __awaiter(this, void 0, void 0, function* () { |
| 169 | + const targetUrl = `${GITHUB_BASE_URL}/repos/${process.env.GITHUB_REPOSITORY}/commits/${process.env.GITHUB_SHA}/pulls`; |
| 170 | + const githubToken = process.env.GITHUB_TOKEN; |
| 171 | + if (!githubToken) { |
| 172 | + return; |
| 173 | + } |
| 174 | + const config = { |
| 175 | + headers: { |
| 176 | + Authorization: `token ${githubToken}`, |
| 177 | + Accept: 'application/vnd.github.groot-preview+json', |
| 178 | + 'Content-Type': 'application/json', |
| 179 | + 'User-Agent': 'mabl-action', |
| 180 | + }, |
| 181 | + }; |
| 182 | + const client = axios_1.default.create(config); |
| 183 | + try { |
| 184 | + const response = yield client.get(targetUrl, config); |
| 185 | + return (_a = response === null || response === void 0 ? void 0 : response.data) === null || _a === void 0 ? void 0 : _a[0]; |
| 186 | + } |
| 187 | + catch (error) { |
| 188 | + if (error.status !== 404) { |
| 189 | + core.warning(error.message); |
| 190 | + } |
| 191 | + } |
| 192 | + return; |
| 193 | + }); |
| 194 | +} |
| 195 | +run(); |
0 commit comments