From f5c18138dd33d3ed113ad19ff4cf4fa2ca75cd81 Mon Sep 17 00:00:00 2001 From: Shveta Sachdeva Date: Tue, 22 Oct 2024 10:31:48 -0700 Subject: [PATCH] Download latest vsix file according to os and date Signed-off-by: Shveta Sachdeva --- .env.example | 2 +- e2e/pages/vscode.pages.ts | 7 ++-- e2e/tests/vscode.test.ts | 2 +- e2e/utilities/download.utils.ts | 63 +++++++++++++++++++++++++++++ e2e/utilities/utils.ts | 71 ++++----------------------------- 5 files changed, 76 insertions(+), 69 deletions(-) create mode 100644 e2e/utilities/download.utils.ts diff --git a/.env.example b/.env.example index a18cf2a..7b19334 100644 --- a/.env.example +++ b/.env.example @@ -8,7 +8,7 @@ VSCODE_EXECUTABLE_PATH='/usr/share/code/code' VSIX_FILE_PATH="/home/sshveta/Work/kai-ci/" -VSIX_FILE='konveyor-linux-0.0.1.vsix ' +VSIX_FILE_NAME='konveyor-linux-0.0.1.vsix' VSIX_DOWNLOAD_URL= 'https://github.com/konveyor/editor-extensions/releases/download/v0.0.1-dev%2B20241017/konveyor-linux-0.0.1.vsix' PLUGIN_URL= 'https://github.com/konveyor/editor-extensions/releases/download/' PLUGIN_VERSION= 'v0.0.1-dev%2B' diff --git a/e2e/pages/vscode.pages.ts b/e2e/pages/vscode.pages.ts index 5aa7d67..8ce0bc7 100644 --- a/e2e/pages/vscode.pages.ts +++ b/e2e/pages/vscode.pages.ts @@ -1,7 +1,7 @@ import { _electron as electron, ElectronApplication, Page } from 'playwright'; import { execSync } from 'child_process'; -import * as fs from 'fs'; -import { downloadLatestKAIPlugin, getOSInfo } from '../utilities/utils'; +import { downloadLatestKAIPlugin } from '../utilities/download.utils'; +import { getKAIPluginPath } from '../utilities/utils'; class LaunchVSCodePage { private vscodeApp?: ElectronApplication; @@ -16,8 +16,7 @@ class LaunchVSCodePage { executablePath: string ): Promise { try { - const vsixFilePath = - process.env.VSIX_FILE_PATH + `konveyor-${getOSInfo()}-0.0.1.vsix`; + const vsixFilePath = getKAIPluginPath(); console.log(`Installing extension from VSIX file: ${vsixFilePath}`); await LaunchVSCodePage.installExtensionFromVSIX(vsixFilePath); diff --git a/e2e/tests/vscode.test.ts b/e2e/tests/vscode.test.ts index 777d753..adc159c 100644 --- a/e2e/tests/vscode.test.ts +++ b/e2e/tests/vscode.test.ts @@ -22,7 +22,7 @@ test.describe('VSCode Tests', () => { test('should open Extensions tab and verify installed extension', async () => { const window = vscodeApp.getWindow(); - const kaiTab = await window.getByRole('tab', { name: 'KAI', exact: true }); + const kaiTab = await window.getByRole('tab', { name: 'Konveyor' }); await kaiTab.click(); // Assert if KAI explorer is opened. const title = await window.getByRole('heading', { diff --git a/e2e/utilities/download.utils.ts b/e2e/utilities/download.utils.ts new file mode 100644 index 0000000..141faaa --- /dev/null +++ b/e2e/utilities/download.utils.ts @@ -0,0 +1,63 @@ +import axios from 'axios'; +import * as fs from 'fs'; +import { getKAIPluginPath, getOSInfo, getKAIPluginName } from './utils'; + +/** + * Downloads a file from the given URL and saves it to the specified destination. + * @param fileUrl The URL of the file to download. + * @param outputLocationPath The local file path where the file will be saved. + * @returns Promise that resolves when the download is complete. + */ +export async function downloadFile(): Promise { + const outputLocationPath = getKAIPluginPath(); + const fileUrl = buildDownloadUrl(); + + const writer = fs.createWriteStream(outputLocationPath); + + const response = await axios({ + url: fileUrl, + method: 'GET', + responseType: 'stream', + }); + + response.data.pipe(writer); + + return new Promise((resolve, reject) => { + writer.on('finish', resolve); + writer.on('error', reject); + }); +} + +export async function downloadLatestKAIPlugin() { + try { + await downloadFile(); + console.log('File downloaded successfully!'); + } catch (err) { + console.error('Error downloading the file:', err); + } +} + +/** + * Builds the download URL for the KAI plugin with today's date + * and depending on operating system. + * @returns The generated URL string with the current date and os. + */ +export function buildDownloadUrl(): string { + const pluginUrl = + process.env.PLUGIN_URL || + 'https://github.com/konveyor/editor-extensions/releases/download/'; + const version = process.env.PLUGIN_VERSION || 'v0.0.1-dev%2B'; + const fileName = getKAIPluginName(); + + const today = new Date(); + + // Format date as YYYYMMDD + const year = today.getFullYear(); + const month = String(today.getMonth() + 1).padStart(2, '0'); // Month is zero-based, so add 1 + const day = String(today.getDate()).padStart(2, '0'); + const formattedDate = `${year}${month}${day}`; + + // Build the full URL + const url = `${pluginUrl}${version}${formattedDate}/${fileName}`; + return url; +} diff --git a/e2e/utilities/utils.ts b/e2e/utilities/utils.ts index 63adcc9..c71c303 100644 --- a/e2e/utilities/utils.ts +++ b/e2e/utilities/utils.ts @@ -1,6 +1,4 @@ import * as os from 'os'; -import axios from 'axios'; -import * as fs from 'fs'; // Function to get OS information export function getOSInfo(): string { @@ -18,67 +16,14 @@ export function getOSInfo(): string { } } -/** - * Downloads a file from the given URL and saves it to the specified destination. - * @param fileUrl The URL of the file to download. - * @param outputLocationPath The local file path where the file will be saved. - * @returns Promise that resolves when the download is complete. - */ -export async function downloadFile(): Promise { - const outputLocationPath = - process.env.VSIX_FILE_PATH + `konveyor-${getOSInfo()}-0.0.1.vsix`; - const fileUrl = buildDownloadUrl(); - - const writer = fs.createWriteStream(outputLocationPath); - - const response = await axios({ - url: fileUrl, - method: 'GET', - responseType: 'stream', - }); - - response.data.pipe(writer); - - return new Promise((resolve, reject) => { - writer.on('finish', resolve); - writer.on('error', reject); - }); +export function getKAIPluginPath(): string { + const vsixFilePath = process.env.VSIX_FILE_PATH; + const pluginFilePath = vsixFilePath + getKAIPluginName(); + return pluginFilePath; } -export async function downloadLatestKAIPlugin() { - try { - await downloadFile(); - console.log('File downloaded successfully!'); - } catch (err) { - console.error('Error downloading the file:', err); - } -} - -/** - * Builds the download URL for the KAI plugin with today's date - * and depending on operating system. - * @returns The generated URL string with the current date and os. - */ -export function buildDownloadUrl(): string { - const pluginUrl = - process.env.PLUGIN_URL || - 'https://github.com/konveyor/editor-extensions/releases/download/'; - const version = process.env.PLUGIN_VERSION || 'v0.0.1-dev%2B'; - - const platform = getOSInfo(); - const fileName = `konveyor-${platform}-0.0.1.vsix`; - - // Get today's date - const today = new Date(); - - // Format date as YYYYMMDD - const year = today.getFullYear(); - const month = String(today.getMonth() + 1).padStart(2, '0'); // Month is zero-based, so add 1 - const day = String(today.getDate()).padStart(2, '0'); - // Resulting format YYYYMMDD - const formattedDate = `${year}${month}${day}`; - - // Build the full URL - const url = `${pluginUrl}${version}${formattedDate}/${fileName}`; - return url; +export function getKAIPluginName(): string { + const vsixFileName = + process.env.VSIX_FILE_NAME || 'konveyor-linux-0.0.1.vsix'; + return vsixFileName.replace(/(konveyor-)(\w+)(-.*)/, `$1${getOSInfo()}$3`); }