|
| 1 | +"use strict"; |
| 2 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 3 | +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { |
| 4 | + if (k2 === undefined) k2 = k; |
| 5 | + var desc = Object.getOwnPropertyDescriptor(m, k); |
| 6 | + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { |
| 7 | + desc = { enumerable: true, get: function() { return m[k]; } }; |
| 8 | + } |
| 9 | + Object.defineProperty(o, k2, desc); |
| 10 | +}) : (function(o, m, k, k2) { |
| 11 | + if (k2 === undefined) k2 = k; |
| 12 | + o[k2] = m[k]; |
| 13 | +})); |
| 14 | +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { |
| 15 | + Object.defineProperty(o, "default", { enumerable: true, value: v }); |
| 16 | +}) : function(o, v) { |
| 17 | + o["default"] = v; |
| 18 | +}); |
| 19 | +var __importStar = (this && this.__importStar) || function (mod) { |
| 20 | + if (mod && mod.__esModule) return mod; |
| 21 | + var result = {}; |
| 22 | + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); |
| 23 | + __setModuleDefault(result, mod); |
| 24 | + return result; |
| 25 | +}; |
| 26 | +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { |
| 27 | + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } |
| 28 | + return new (P || (P = Promise))(function (resolve, reject) { |
| 29 | + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } |
| 30 | + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } |
| 31 | + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } |
| 32 | + step((generator = generator.apply(thisArg, _arguments || [])).next()); |
| 33 | + }); |
| 34 | +}; |
| 35 | +var __importDefault = (this && this.__importDefault) || function (mod) { |
| 36 | + return (mod && mod.__esModule) ? mod : { "default": mod }; |
| 37 | +}; |
| 38 | +Object.defineProperty(exports, "__esModule", { value: true }); |
| 39 | +const axios_1 = __importDefault(require("axios")); |
| 40 | +const core = __importStar(require("@actions/core")); |
| 41 | +/** |
| 42 | + * Checks if the given date is older than specified days |
| 43 | + * |
| 44 | + * @param {string} dateString - ISO date string to check |
| 45 | + * @param {number} days - Number of days to compare against |
| 46 | + * @returns {boolean} - True if the date is older than specified days |
| 47 | + */ |
| 48 | +function isOlderThanDays(dateString, days) { |
| 49 | + const date = new Date(dateString); |
| 50 | + const daysInMs = days * 24 * 60 * 60 * 1000; |
| 51 | + const cutoffDate = new Date(Date.now() - daysInMs); |
| 52 | + return date < cutoffDate; |
| 53 | +} |
| 54 | +/** |
| 55 | + * Lists the environments from the Gitpod API and identifies those that should be deleted. |
| 56 | + * Environments are selected for deletion if they are stopped, do not have changed files |
| 57 | + * or unpushed commits, and are older than the specified number of days. |
| 58 | + * |
| 59 | + * @param {string} gitpodToken - The access token for Gitpod API. |
| 60 | + * @param {string} organizationId - The organization ID. |
| 61 | + * @param {number} olderThanDays - Delete environments older than these many days |
| 62 | + * @returns {Promise<string[]>} - A promise that resolves to an array of environment IDs to be deleted. |
| 63 | + */ |
| 64 | +function listEnvironments(gitpodToken, organizationId, olderThanDays) { |
| 65 | + return __awaiter(this, void 0, void 0, function* () { |
| 66 | + const toDelete = []; |
| 67 | + let pageToken = undefined; |
| 68 | + try { |
| 69 | + do { |
| 70 | + const response = yield axios_1.default.post("https://app.gitpod.io/api/gitpod.v1.EnvironmentService/ListEnvironments", { |
| 71 | + organization_id: organizationId, |
| 72 | + pagination: { |
| 73 | + page_size: 100, |
| 74 | + page_token: pageToken |
| 75 | + } |
| 76 | + }, { |
| 77 | + headers: { |
| 78 | + "Content-Type": "application/json", |
| 79 | + Authorization: `Bearer ${gitpodToken}`, |
| 80 | + }, |
| 81 | + }); |
| 82 | + core.debug("API Response: " + JSON.stringify(response.data)); |
| 83 | + const environments = response.data.environments; |
| 84 | + environments.forEach((env) => { |
| 85 | + var _a, _b, _c, _d; |
| 86 | + const isStopped = env.status.phase === "ENVIRONMENT_PHASE_STOPPED"; |
| 87 | + const hasNoChangedFiles = !((_b = (_a = env.status.content) === null || _a === void 0 ? void 0 : _a.git) === null || _b === void 0 ? void 0 : _b.totalChangedFiles); |
| 88 | + const hasNoUnpushedCommits = !((_d = (_c = env.status.content) === null || _c === void 0 ? void 0 : _c.git) === null || _d === void 0 ? void 0 : _d.totalUnpushedCommits); |
| 89 | + const isOldEnough = isOlderThanDays(env.metadata.createdAt, olderThanDays); |
| 90 | + if (isStopped && hasNoChangedFiles && hasNoUnpushedCommits && isOldEnough) { |
| 91 | + toDelete.push(env.id); |
| 92 | + core.debug(`Environment ${env.id} created at ${env.metadata.createdAt} is ${olderThanDays} days old and marked for deletion`); |
| 93 | + } |
| 94 | + }); |
| 95 | + pageToken = response.data.pagination.next_page_token; |
| 96 | + } while (pageToken); // Continue until no more pages |
| 97 | + return toDelete; |
| 98 | + } |
| 99 | + catch (error) { |
| 100 | + core.error(`Error in listEnvironments: ${error}`); |
| 101 | + throw error; |
| 102 | + } |
| 103 | + }); |
| 104 | +} |
| 105 | +/** |
| 106 | + * Deletes a specified environment using the Gitpod API. |
| 107 | + * |
| 108 | + * @param {string} environmentId - The ID of the environment to be deleted. |
| 109 | + * @param {string} gitpodToken - The access token for the Gitpod API. |
| 110 | + * @param {string} organizationId - The organization ID. |
| 111 | + */ |
| 112 | +function deleteEnvironment(environmentId, gitpodToken, organizationId) { |
| 113 | + return __awaiter(this, void 0, void 0, function* () { |
| 114 | + try { |
| 115 | + yield axios_1.default.post("https://app.gitpod.io/api/gitpod.v1.EnvironmentService/DeleteEnvironment", { |
| 116 | + environment_id: environmentId, |
| 117 | + organization_id: organizationId |
| 118 | + }, { |
| 119 | + headers: { |
| 120 | + "Content-Type": "application/json", |
| 121 | + Authorization: `Bearer ${gitpodToken}`, |
| 122 | + }, |
| 123 | + }); |
| 124 | + core.debug(`Deleted environment: ${environmentId}`); |
| 125 | + } |
| 126 | + catch (error) { |
| 127 | + core.error(`Error in deleteEnvironment: ${error}`); |
| 128 | + throw error; |
| 129 | + } |
| 130 | + }); |
| 131 | +} |
| 132 | +/** |
| 133 | + * Main function to run the action. It retrieves the Gitpod access token, organization ID, |
| 134 | + * and age threshold, lists environments, deletes the selected environments, and outputs the result. |
| 135 | + */ |
| 136 | +function run() { |
| 137 | + return __awaiter(this, void 0, void 0, function* () { |
| 138 | + try { |
| 139 | + const gitpodToken = core.getInput("GITPOD_TOKEN", { required: true }); |
| 140 | + const organizationId = core.getInput("ORGANIZATION_ID", { required: true }); |
| 141 | + const olderThanDays = parseInt(core.getInput("OLDER_THAN_DAYS", { required: false }) || "10"); |
| 142 | + const printSummary = core.getBooleanInput("PRINT_SUMMARY", { required: false }); |
| 143 | + const deletedEnvironments = []; |
| 144 | + if (!gitpodToken) { |
| 145 | + throw new Error("Gitpod access token is required"); |
| 146 | + } |
| 147 | + if (!organizationId) { |
| 148 | + throw new Error("Organization ID is required"); |
| 149 | + } |
| 150 | + if (isNaN(olderThanDays) || olderThanDays < 0) { |
| 151 | + throw new Error("OLDER_THAN_DAYS must be a positive number"); |
| 152 | + } |
| 153 | + const environmentsToDelete = yield listEnvironments(gitpodToken, organizationId, olderThanDays); |
| 154 | + core.info(`Found ${environmentsToDelete.length} environments older than ${olderThanDays} days to delete`); |
| 155 | + for (const environmentId of environmentsToDelete) { |
| 156 | + yield deleteEnvironment(environmentId, gitpodToken, organizationId); |
| 157 | + printSummary ? deletedEnvironments.push(environmentId) : null; |
| 158 | + } |
| 159 | + if (deletedEnvironments.length > 0 && printSummary) { |
| 160 | + core.summary |
| 161 | + .addHeading(`Environments deleted (older than ${olderThanDays} days)`) |
| 162 | + .addList(deletedEnvironments) |
| 163 | + .write(); |
| 164 | + } |
| 165 | + core.setOutput("success", "true"); |
| 166 | + core.setOutput("deleted_count", deletedEnvironments.length); |
| 167 | + } |
| 168 | + catch (error) { |
| 169 | + core.error(error.message); |
| 170 | + core.setOutput("success", "false"); |
| 171 | + core.setOutput("deleted_count", 0); |
| 172 | + } |
| 173 | + }); |
| 174 | +} |
| 175 | +run(); |
0 commit comments