diff --git a/dist/authentication.js b/dist/authentication.js new file mode 100644 index 0000000..7bf09cc --- /dev/null +++ b/dist/authentication.js @@ -0,0 +1,63 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +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 }); +exports.getAuthenticationToken = void 0; +const core = __importStar(require("@actions/core")); +const httpm = __importStar(require("@actions/http-client")); +const qs = __importStar(require("query-string")); +function getAuthenticationToken(loginEndpoint, terrakubeTenantId, terrakubeApplicationId, terrakubeApplicationSecret, terrakubeScope) { + return __awaiter(this, void 0, void 0, function* () { + const httpClient = new httpm.HttpClient(); + core.info('Generating Azure Ad Body'); + core.info(`grant_type=client_credentials&client_id=${terrakubeApplicationId}&client_secret=${terrakubeApplicationSecret.substring(0, 3)}XXXXXXXX&scope=${encodeURIComponent(terrakubeScope)}`); + const postData = qs.stringify({ + 'grant_type': 'client_credentials', + 'client_id': terrakubeApplicationId, + 'scope': terrakubeScope, + 'client_secret': terrakubeApplicationSecret + }); + const res = yield httpClient.post(`${loginEndpoint}/${terrakubeTenantId}/oauth2/v2.0/token`, postData, { 'content-type': 'application/x-www-form-urlencoded' }); + core.debug('Azure Ad Response received'); + const body = yield res.readBody(); + const azureAdToken = JSON.parse(body); + if (typeof azureAdToken.error === "undefined") { + core.debug(`Returning new Token ${azureAdToken.access_token.substring(0, 25)}XXXXXX`); + return azureAdToken.access_token; + } + else { + core.debug(azureAdToken.error); + } + }); +} +exports.getAuthenticationToken = getAuthenticationToken; diff --git a/dist/index.js b/dist/index.js new file mode 100644 index 0000000..258733b --- /dev/null +++ b/dist/index.js @@ -0,0 +1,244 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +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()); + }); +}; +var __asyncValues = (this && this.__asyncValues) || function (o) { + if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined."); + var m = o[Symbol.asyncIterator], i; + return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i); + function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; } + function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); } +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const core = __importStar(require("@actions/core")); +const glob = __importStar(require("@actions/glob")); +const exec = __importStar(require("@actions/exec")); +const httpm = __importStar(require("@actions/http-client")); +const github = __importStar(require("@actions/github")); +const ansi_to_html_1 = __importDefault(require("ansi-to-html")); +const userInput_1 = require("./userInput"); +const terrakube_1 = require("./terrakube"); +const promises_1 = require("fs/promises"); +function run() { + var e_1, _a; + return __awaiter(this, void 0, void 0, function* () { + try { + const githubActionInput = yield (0, userInput_1.getActionInput)(); + const terrakubeClient = new terrakube_1.TerrakubeClient(githubActionInput); + const patterns = ['**/terrakube.json']; + const globber = yield glob.create(patterns.join('\n')); + const currentDirectory = yield getCurrentDirectory(); + console.debug(`Processing: ${currentDirectory}`); + try { + for (var _b = __asyncValues(globber.globGenerator()), _c; _c = yield _b.next(), !_c.done;) { + const file = _c.value; + const terrakubeData = JSON.parse(yield (0, promises_1.readFile)(`${file}`, "utf8")); + core.startGroup(`Execute Workspace ${terrakubeData.workspace}`); + console.debug(`Processing: ${file}`); + //terrakubeData.respository = await getRepository() + //terrakubeData.folder = file.replace(currentDirectory,'') + core.debug(`Loaded JSON: ${JSON.stringify(terrakubeData)}`); + core.info(`Organization: ${terrakubeData.organization}`); + core.info(`Workspace: ${terrakubeData.workspace}`); + core.info(`Folder: ${terrakubeData.folder}`); + core.info(`Action branch: ${process.env.GITHUB_REF}`); + core.info(`Branch: ${githubActionInput.branch}`); + terrakubeData.branch = githubActionInput.branch; + //Object.keys(terrakubeData.variables).forEach(key => { + // console.log('Key : ' + key + ', Value : ' + terrakubeData.variables[key]) + //}) + core.info(`Running Workspace ${terrakubeData.workspace} with Template ${githubActionInput.terrakubeTemplate}`); + core.info(`Checking if workspace ${terrakubeData.workspace}`); + const organizationId = yield terrakubeClient.getOrganizationId(terrakubeData.organization); + if (organizationId !== "") { + //let updateJob = false + let workspaceId = yield terrakubeClient.getWorkspaceId(organizationId, terrakubeData.workspace); + if (workspaceId === "") { + core.info(`Creating new workspace ${terrakubeData.workspace}`); + workspaceId = yield terrakubeClient.createWorkspace(organizationId, terrakubeData.workspace, terrakubeData.terraform, terrakubeData.folder, terrakubeData.workspaceSrc, terrakubeData.branch); + //updateJob = true + } + core.info(`Searching template ${githubActionInput.terrakubeTemplate}`); + const templateId = yield terrakubeClient.getTemplateId(organizationId, githubActionInput.terrakubeTemplate); + if (templateId !== "") { + core.info(`Using template id ${templateId}`); + /* + for await (const key of Object.keys(terrakubeData.variables)) { + const updateVar = await setupVariable( + terrakubeClient, + organizationId, + workspaceId, + key, + terrakubeData.variables[key] + ) + + if (updateVar && !updateJob) { + updateJob = true; + } + }*/ + //core.info(`Update Job: ${updateJob}`) + //if (updateJob) { + core.info(`Creating new job: `); + const jobId = yield terrakubeClient.createJobId(organizationId, workspaceId, templateId); + core.debug(`JobId: ${jobId}`); + yield checkTerrakubeLogs(terrakubeClient, githubActionInput.githubToken, organizationId, jobId); + //core.setOutput(`Organization: ${terrakubeData.organization} Workspace: ${terrakubeData.workspace} Job`, jobId); + //} + } + else { + core.error(`Template not found: ${githubActionInput.terrakubeTemplate} in Organization: ${terrakubeData.organization}`); + } + } + else { + core.error(`Organization not found: ${terrakubeData.organization} in Endpoint: ${githubActionInput.terrakubeEndpoint}`); + } + core.endGroup(); + } + } + catch (e_1_1) { e_1 = { error: e_1_1 }; } + finally { + try { + if (_c && !_c.done && (_a = _b.return)) yield _a.call(_b); + } + finally { if (e_1) throw e_1.error; } + } + } + catch (error) { + if (error instanceof Error) + core.setFailed(error.message); + } + }); +} +function sleep(ms) { + return __awaiter(this, void 0, void 0, function* () { + return new Promise(resolve => setTimeout(resolve, ms)); + }); +} +function checkTerrakubeLogs(terrakubeClient, githubToken, organizationId, jobId) { + return __awaiter(this, void 0, void 0, function* () { + let jobRunning = true; + let jobResponse = yield terrakubeClient.getJobData(organizationId, jobId); + let jobResponseJson = JSON.parse(jobResponse); + while (jobResponseJson.data.attributes.status !== "completed" && jobResponseJson.data.attributes.status !== "failed") { + yield sleep(5000); + jobResponse = yield terrakubeClient.getJobData(organizationId, jobId); + jobResponseJson = JSON.parse(jobResponse); + core.debug("Waiting for job information..."); + } + core.info(`${jobResponse}`); + core.info(`${JSON.stringify(jobResponseJson.included)}`); + const httpClient = new httpm.HttpClient(); + const jobSteps = jobResponseJson.included; + core.info(`${Object.keys(jobSteps).length}`); + let finalComment = ""; + const convert = new ansi_to_html_1.default(); + for (let index = 0; index < Object.keys(jobSteps).length; index++) { + core.startGroup(`Running ${jobSteps[index].attributes.name}`); + const response = yield httpClient.get(`${jobSteps[index].attributes.output}`); + const body = yield response.readBody(); + core.info(body); + core.endGroup(); + const convert = new ansi_to_html_1.default(); + const commentBody = `Running ${jobSteps[index].attributes.name} \n${convert.toHtml(body)}\n `; + finalComment = finalComment.concat(commentBody); + } + core.info("Setup client"); + const octokit = github.getOctokit(githubToken); + core.info("Getting payload"); + const pull_request = github.context.payload; + core.info("Send message"); + yield octokit.rest.issues.createComment(Object.assign(Object.assign({}, github.context.repo), { issue_number: pull_request.number, body: `${finalComment}` })); + if (jobResponseJson.data.attributes.status === "completed") { + return true; + } + else { + return false; + } + }); +} +function setupVariable(terrakubeClient, organizationId, workspaceId, key, value) { + return __awaiter(this, void 0, void 0, function* () { + const variableId = yield terrakubeClient.getVariableId(organizationId, workspaceId, key); + if (variableId === "") { + yield terrakubeClient.createVariable(organizationId, workspaceId, key, value); + return true; + } + else { + const variableData = yield terrakubeClient.getVariableById(organizationId, workspaceId, variableId); + if (variableData.data.attributes.value === value) { + return false; + } + else { + yield terrakubeClient.updateVariableById(organizationId, workspaceId, variableId, value); + return true; + } + } + }); +} +function getRepository() { + return __awaiter(this, void 0, void 0, function* () { + let repository = ''; + let errorGitCommand = ''; + let options = {}; + options.listeners = { + stdout: (data) => { + repository += data.toString(); + }, + stderr: (data) => { + errorGitCommand += data.toString(); + } + }; + yield exec.exec('git', ['config', '--get', 'remote.origin.url'], options); + return repository; + }); +} +function getCurrentDirectory() { + return __awaiter(this, void 0, void 0, function* () { + let repository = ''; + let errorGitCommand = ''; + let options = {}; + options.listeners = { + stdout: (data) => { + repository += data.toString(); + }, + stderr: (data) => { + errorGitCommand += data.toString(); + } + }; + yield exec.exec('pwd', [], options); + return repository; + }); +} +run(); diff --git a/dist/terrakube.js b/dist/terrakube.js new file mode 100644 index 0000000..550df23 --- /dev/null +++ b/dist/terrakube.js @@ -0,0 +1,297 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +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 }); +exports.TerrakubeClient = void 0; +const core = __importStar(require("@actions/core")); +const httpm = __importStar(require("@actions/http-client")); +class TerrakubeClient { + constructor(gitHubActionInput) { + this.httpClient = new httpm.HttpClient(); + this.gitHubActionInput = gitHubActionInput; + this.authenticationToken = 'empty'; + core.info(`Creating Terrakube CLient....`); + core.info(`Endpoint: ${gitHubActionInput.terrakubeEndpoint}`); + core.info(`Repository: ${gitHubActionInput.terrakubeRepository}`); + core.info(`Template: ${gitHubActionInput.terrakubeTemplate}`); + } + getOrganizationId(organizationName) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + core.debug(`GET ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization?filter[organization]=name==${organizationName}`); + const response = yield this.httpClient.get(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization?filter[organization]=name==${organizationName}`, { + 'Authorization': `Bearer ${this.authenticationToken}` + }); + const body = yield response.readBody(); + const terrakubeResponse = JSON.parse(body); + core.debug(`Response size: ${terrakubeResponse.data.length}`); + if (terrakubeResponse.data.length === 0) { + return ""; + } + else { + core.debug(`Organization Id: ${terrakubeResponse.data[0].id}`); + return terrakubeResponse.data[0].id; + } + }); + } + getWorkspaceId(organizationId, workspaceName) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + core.debug(`GET ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace?filter[workspace]=name==${workspaceName}"`); + const response = yield this.httpClient.get(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace?filter[workspace]=name==${workspaceName}`, { + 'Authorization': `Bearer ${this.authenticationToken}` + }); + const body = yield response.readBody(); + const terrakubeResponse = JSON.parse(body); + core.debug(`Response size: ${terrakubeResponse.data.length}`); + if (terrakubeResponse.data.length === 0) { + return ""; + } + else { + core.info(`Workspace Id: ${terrakubeResponse.data[0].id}`); + return terrakubeResponse.data[0].id; + } + }); + } + createWorkspace(organizationId, name, terraformVersion, folder, repository, branch) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + core.debug(`POST ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace`); + const response = yield this.httpClient.post(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace`, `{ + "data": { + "type": "workspace", + "attributes": { + "name": "${name}", + "source": "${repository}", + "branch": "${branch}", + "folder": "${folder}", + "terraformVersion": "${terraformVersion}" + } + } + }`, { + 'Authorization': `Bearer ${this.authenticationToken}`, + 'Content-Type': 'application/vnd.api+json' + }); + const body = yield response.readBody(); + const terrakubeResponse = JSON.parse(body); + core.debug(`Response: ${JSON.stringify(terrakubeResponse)}`); + core.info(`Variable Id: ${terrakubeResponse.data.id}`); + return terrakubeResponse.data.id; + }); + } + getTemplateId(organizationId, templateName) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + core.debug(`GET ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/template?filter[template]=name==${templateName}`); + const response = yield this.httpClient.get(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/template?filter[template]=name==${templateName}`, { + 'Authorization': `Bearer ${this.authenticationToken}` + }); + const body = yield response.readBody(); + const terrakubeResponse = JSON.parse(body); + core.debug(`Response size: ${terrakubeResponse.data.length}`); + if (terrakubeResponse.data.length === 0) { + return ""; + } + else { + core.info(`Template Id: ${terrakubeResponse.data[0].id}`); + return terrakubeResponse.data[0].id; + } + }); + } + getVariableId(organizationId, workspaceId, variableName) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + core.debug(`GET ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace/${workspaceId}/variable?filter[variable]=key==${variableName}`); + const response = yield this.httpClient.get(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace/${workspaceId}/variable?filter[variable]=key==${variableName}`, { + 'Authorization': `Bearer ${this.authenticationToken}` + }); + const body = yield response.readBody(); + const terrakubeResponse = JSON.parse(body); + core.debug(`Response size: ${terrakubeResponse.data.length === 0}`); + if (terrakubeResponse.data.length === 0) { + return ""; + } + else { + core.info(`Variable Id: ${terrakubeResponse.data[0].id}`); + return terrakubeResponse.data[0].id; + } + }); + } + getVariableById(organizationId, workspaceId, variableId) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + core.debug(`GET ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace/${workspaceId}/variable/${variableId}`); + const response = yield this.httpClient.get(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace/${workspaceId}/variable/${variableId}`, { + 'Authorization': `Bearer ${this.authenticationToken}` + }); + const body = yield response.readBody(); + const terrakubeResponse = JSON.parse(body); + core.debug(`Response: ${terrakubeResponse}`); + return terrakubeResponse; + }); + } + updateVariableById(organizationId, workspaceId, variableId, variableValue) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + core.debug(`PATCH ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace/${workspaceId}/variable/${variableId}`); + const response = yield this.httpClient.patch(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace/${workspaceId}/variable/${variableId}`, `{ + "data": { + "type": "variable", + "id": "${variableId}", + "attributes": { + "value": "${variableValue}" + } + } + }`, { + 'Authorization': `Bearer ${this.authenticationToken}`, + 'Content-Type': 'application/vnd.api+json' + }); + const body = yield response.readBody(); + core.debug(`Updated Variable: ${variableId}`); + return true; + }); + } + createVariable(organizationId, workspaceId, variableName, varaibleValue) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + core.debug(`POST ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace/${workspaceId}/variable`); + const response = yield this.httpClient.post(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace/${workspaceId}/variable`, `{ + "data": { + "type": "variable", + "attributes": { + "key": "${variableName}", + "value": "${varaibleValue}", + "sensitive": false, + "hcl": false, + "category": "TERRAFORM", + "description": "" + } + } + }`, { + 'Authorization': `Bearer ${this.authenticationToken}`, + 'Content-Type': 'application/vnd.api+json' + }); + const body = yield response.readBody(); + const terrakubeResponse = JSON.parse(body); + core.debug(`Response: ${JSON.stringify(terrakubeResponse)}`); + core.info(`Variable Id: ${terrakubeResponse.data.id}`); + return terrakubeResponse.data.id; + }); + } + getVariableValue(organizationId, workspaceId, variableName) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + core.debug(`GET ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace/${workspaceId}/variable?filter[variable]=key==${variableName}`); + const response = yield this.httpClient.get(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/workspace/${workspaceId}/variable?filter[variable]=key==${variableName}`, { + 'Authorization': `Bearer ${this.authenticationToken}` + }); + const body = yield response.readBody(); + const terrakubeResponse = JSON.parse(body); + core.debug(`Response size: ${terrakubeResponse.data.length}`); + if (terrakubeResponse.data.length === 0) { + return ""; + } + else { + core.info(`Variable Id: ${terrakubeResponse.data[0].id}`); + return terrakubeResponse.data[0].attributes.value; + } + }); + } + createJobId(organizationId, workspaceId, templateId) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + const requestBody = { + "data": { + "type": "job", + "attributes": { + "templateReference": templateId + }, + "relationships": { + "workspace": { + "data": { + "type": "workspace", + "id": workspaceId + } + } + } + } + }; + core.debug(`POST ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/job`); + core.debug(`${JSON.stringify(requestBody)}`); + const response = yield this.httpClient.post(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/job`, JSON.stringify(requestBody), { + 'Authorization': `Bearer ${this.authenticationToken}`, + 'Content-Type': 'application/vnd.api+json' + }); + const body = yield response.readBody(); + const terrakubeResponse = JSON.parse(body); + core.debug(JSON.stringify(terrakubeResponse)); + return terrakubeResponse.data.id; + }); + } + getJobData(organizationId, jobId) { + return __awaiter(this, void 0, void 0, function* () { + if (this.authenticationToken === 'empty') { + this.authenticationToken = this.gitHubActionInput.token; + } + core.debug(`GET ${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/job/${jobId}?include=step`); + const response = yield this.httpClient.get(`${this.gitHubActionInput.terrakubeEndpoint}/api/v1/organization/${organizationId}/job/${jobId}?include=step`, { + 'Authorization': `Bearer ${this.authenticationToken}` + }); + const body = yield response.readBody(); + const terrakubeResponse = JSON.parse(body); + core.debug(JSON.stringify(terrakubeResponse)); + return body; + }); + } +} +exports.TerrakubeClient = TerrakubeClient; diff --git a/dist/userInput.js b/dist/userInput.js new file mode 100644 index 0000000..5f65909 --- /dev/null +++ b/dist/userInput.js @@ -0,0 +1,61 @@ +"use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); + __setModuleDefault(result, mod); + return result; +}; +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 }); +exports.getActionInput = void 0; +const core = __importStar(require("@actions/core")); +function getActionInput() { + return __awaiter(this, void 0, void 0, function* () { + const terrakubeToken = core.getInput('terrakube_token', { required: true }); + core.debug(`Terrakube Token: ${terrakubeToken.substring(0, 10)}****`); + const terrakubeEndpoint = core.getInput('terrakube_endpoint', { required: true }); + core.debug(`Terrakube Endpoint: ${terrakubeEndpoint}`); + const terrakubeRepository = core.getInput('terrakube_repository', { required: true }); + core.debug(`Terrakube Repository: ${terrakubeRepository}`); + const terrakubeTemplate = core.getInput('terrakube_template', { required: true }); + core.debug(`Terrakube Template: ${terrakubeTemplate}`); + const terrakubeBranch = core.getInput('terrakube_branch', { required: true }); + core.debug(`Terrakube Branch: ${terrakubeBranch}`); + const githubToken = core.getInput('token', { required: true }); + const terrakubeActionInput = { + token: terrakubeToken, + terrakubeEndpoint: terrakubeEndpoint, + terrakubeRepository: terrakubeRepository, + terrakubeTemplate: terrakubeTemplate, + githubToken: githubToken, + branch: terrakubeBranch + }; + return terrakubeActionInput; + }); +} +exports.getActionInput = getActionInput; diff --git a/dist/wait.js b/dist/wait.js new file mode 100644 index 0000000..7079c93 --- /dev/null +++ b/dist/wait.js @@ -0,0 +1,23 @@ +"use strict"; +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 }); +exports.wait = void 0; +function wait(milliseconds) { + return __awaiter(this, void 0, void 0, function* () { + return new Promise(resolve => { + if (isNaN(milliseconds)) { + throw new Error('milliseconds not a number'); + } + setTimeout(() => resolve('done!'), milliseconds); + }); + }); +} +exports.wait = wait;