From a564f9390b2932dfbdfc9f8a42e5264287aa6f4d Mon Sep 17 00:00:00 2001 From: Louis Bompart Date: Fri, 14 Oct 2022 19:03:10 -0400 Subject: [PATCH 1/5] add ext --- scripts/get-release-artifact-and-commit-sha.mjs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/get-release-artifact-and-commit-sha.mjs b/scripts/get-release-artifact-and-commit-sha.mjs index 244a360acb..f561e6ced4 100644 --- a/scripts/get-release-artifact-and-commit-sha.mjs +++ b/scripts/get-release-artifact-and-commit-sha.mjs @@ -1,4 +1,4 @@ -import {downloadReleaseAssets} from './github-client'; +import {downloadReleaseAssets} from './github-client.js'; import {getLastTag} from '@coveo/semantic-monorepo-tools'; import { existsSync, From cbb8937e3000172ea51f890060bd48b8ecf9e029 Mon Sep 17 00:00:00 2001 From: Louis Bompart Date: Fri, 14 Oct 2022 19:46:25 -0400 Subject: [PATCH 2/5] get tag from last github release --- .../get-release-artifact-and-commit-sha.mjs | 19 ++++++++++++------- scripts/github-client.js | 6 ++++++ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/scripts/get-release-artifact-and-commit-sha.mjs b/scripts/get-release-artifact-and-commit-sha.mjs index f561e6ced4..713272d2da 100644 --- a/scripts/get-release-artifact-and-commit-sha.mjs +++ b/scripts/get-release-artifact-and-commit-sha.mjs @@ -1,5 +1,4 @@ -import {downloadReleaseAssets} from './github-client.js'; -import {getLastTag} from '@coveo/semantic-monorepo-tools'; +import {downloadReleaseAssets, getLastCliTag} from './github-client.js'; import { existsSync, mkdirSync, @@ -8,17 +7,23 @@ import { copyFileSync, } from 'fs'; import {resolve} from 'path'; +import {spawnSync} from 'child_process'; async function main() { - const tag = await getLastTag('@coveo/cli@'); + const tag = await getLastCliTag(); + const tagCommit = spawnSync('git', ['rev-list', '-n', 1, tag], { + encoding: 'utf-8', + }) + .stdout.toString() + .trim(); // This folder structure needs to be respected in order for the CLI update plugin to // be able to do it's job properly. const topLevelDirectory = './artifacts'; const subDirectoryForTarball = [ topLevelDirectory, 'versions', - tag.name.substring(1), - tag.commit.sha.substring(0, 7), + tag, + tagCommit.substring(0, 7), ].join('/'); const subDirectoryForManifest = [ topLevelDirectory, @@ -39,7 +44,7 @@ async function main() { } ); - await downloadReleaseAssets(tag.name, (assetName) => { + await downloadReleaseAssets(tag, (assetName) => { if (assetName.match(tarballMatcher)) { console.info(assetName, `--> ${subDirectoryForTarball}`); return subDirectoryForTarball; @@ -52,7 +57,7 @@ async function main() { } }); - writeFileSync('latest-commit', tag.commit.sha); + writeFileSync('latest-commit', tagCommit); readdirSync(topLevelDirectory, {withFileTypes: true}).forEach((file) => { const match = binariesMatcher.exec(file.name); diff --git a/scripts/github-client.js b/scripts/github-client.js index 7c9ac37498..7b67a4019d 100644 --- a/scripts/github-client.js +++ b/scripts/github-client.js @@ -76,6 +76,11 @@ const createOrUpdateReleaseDescription = async (tag, body) => { } }; +const getLastCliTag = async () => { + return (await octokit.rest.repos.getLatestRelease({repo, owner})).data + .tag_name; +}; + const downloadReleaseAssets = async (tag, determineAssetLocation) => { const release = await octokit.rest.repos.getReleaseByTag({repo, owner, tag}); const assets = await octokit.rest.repos.listReleaseAssets({ @@ -116,4 +121,5 @@ module.exports = { createOrUpdateReleaseDescription, downloadReleaseAssets, getSnykCodeAlerts, + getLastCliTag, }; From 3b522378e4bc37e4ff51b55432575e5b6f04edc2 Mon Sep 17 00:00:00 2001 From: Louis Bompart Date: Fri, 14 Oct 2022 23:20:58 -0400 Subject: [PATCH 3/5] the SHA grew up by one, and that broke everything --- .../get-release-artifact-and-commit-sha.mjs | 61 ++++++++++--------- scripts/github-client.js | 17 +----- 2 files changed, 36 insertions(+), 42 deletions(-) diff --git a/scripts/get-release-artifact-and-commit-sha.mjs b/scripts/get-release-artifact-and-commit-sha.mjs index 713272d2da..cbe74908d9 100644 --- a/scripts/get-release-artifact-and-commit-sha.mjs +++ b/scripts/get-release-artifact-and-commit-sha.mjs @@ -1,4 +1,4 @@ -import {downloadReleaseAssets, getLastCliTag} from './github-client.js'; +import {getReleaseAssetsMetadata, getLastCliTag} from './github-client.js'; import { existsSync, mkdirSync, @@ -7,47 +7,33 @@ import { copyFileSync, } from 'fs'; import {resolve} from 'path'; -import {spawnSync} from 'child_process'; +import {execSync} from 'node:child_process'; async function main() { const tag = await getLastCliTag(); - const tagCommit = spawnSync('git', ['rev-list', '-n', 1, tag], { - encoding: 'utf-8', - }) - .stdout.toString() - .trim(); // This folder structure needs to be respected in order for the CLI update plugin to // be able to do it's job properly. const topLevelDirectory = './artifacts'; - const subDirectoryForTarball = [ - topLevelDirectory, - 'versions', - tag, - tagCommit.substring(0, 7), - ].join('/'); + const getSubDirectoryForTarball = ({version, commitSHA}) => + [topLevelDirectory, 'versions', version, commitSHA].join('/'); const subDirectoryForManifest = [ topLevelDirectory, 'channels', 'stable', ].join('/'); const binariesMatcher = - /^coveo[_-]{1}(?<_version>v?\d+\.\d+\.\d+(-\d+)?)[_.-]{1}(?<_commitSHA>\w{7})[_-]{0,1}(\d+_)?(?.*\.(exe|deb|pkg))$/; + /^coveo[_-]{1}(?v?\d+\.\d+\.\d+(-\d+)?)[_.-]{1}(?\w+)[_-]?(\d+_)?(?.*\.(exe|deb|pkg))$/; const manifestMatcher = - /^coveo-(?<_version>v?\d+\.\d+\.\d+(-\d+)?)-(?<_commitSHA>\w{7})-(?.*-buildmanifest)$/; - const tarballMatcher = /\.tar\.[gx]z$/; + /^coveo-(?<_version>v?\d+\.\d+\.\d+(-\d+)?)-(?\w+)-(?.*-buildmanifest)$/; + const tarballMatcher = + /^coveo-(?<_version>v?\d+\.\d+\.\d+(-\d+)?)-(?\w+)-(?[\w-]+).tar\.[gx]z$/; - [topLevelDirectory, subDirectoryForManifest, subDirectoryForTarball].forEach( - (dir) => { - if (!existsSync(dir)) { - mkdirSync(dir, {recursive: true}); - } - } - ); - - await downloadReleaseAssets(tag, (assetName) => { + const determineAssetLocation = (assetName) => { if (assetName.match(tarballMatcher)) { - console.info(assetName, `--> ${subDirectoryForTarball}`); - return subDirectoryForTarball; + const match = tarballMatcher.exec(assetName); + const location = getSubDirectoryForTarball(match.groups); + console.info(assetName, `--> ${location}`); + return location; } else if (assetName.match(manifestMatcher)) { console.info(assetName, `--> ${subDirectoryForManifest}`); return subDirectoryForManifest; @@ -55,7 +41,26 @@ async function main() { console.info(assetName, `--> ${topLevelDirectory}`); return topLevelDirectory; } - }); + }; + + [topLevelDirectory, subDirectoryForManifest, subDirectoryForTarball].forEach( + (dir) => { + if (!existsSync(dir)) { + mkdirSync(dir, {recursive: true}); + } + } + ); + + const assets = await getReleaseAssetsMetadata(tag); + for (const asset of assets) { + console.info( + `Downloading asset ${asset.name} from ${asset.browser_download_url}.\nSize: ${asset.size} ...` + ); + const directory = determineAssetLocation(asset.name); + execSync( + `curl -L ${asset.browser_download_url} --output ${directory}/${asset.name}` + ); + } writeFileSync('latest-commit', tagCommit); diff --git a/scripts/github-client.js b/scripts/github-client.js index 7b67a4019d..aa059ac119 100644 --- a/scripts/github-client.js +++ b/scripts/github-client.js @@ -1,6 +1,5 @@ /** @type {import("@actions/github")} */ const github = require('@actions/github'); -const {execSync} = require('child_process'); const octokit = github.getOctokit(process.env.GITHUB_CREDENTIALS); const owner = 'coveo'; const repo = 'cli'; @@ -81,23 +80,13 @@ const getLastCliTag = async () => { .tag_name; }; -const downloadReleaseAssets = async (tag, determineAssetLocation) => { +const getReleaseAssetsMetadata = async (tag) => { const release = await octokit.rest.repos.getReleaseByTag({repo, owner, tag}); - const assets = await octokit.rest.repos.listReleaseAssets({ + return octokit.rest.repos.listReleaseAssets({ owner, repo, release_id: release.data.id, }); - - assets.data.forEach((asset) => { - console.info( - `Downloading asset ${asset.name} from ${asset.browser_download_url}.\nSize: ${asset.size} ...` - ); - const directory = determineAssetLocation(asset.name); - execSync( - `curl -L ${asset.browser_download_url} --output ${directory}/${asset.name}` - ); - }); }; const getSnykCodeAlerts = () => { @@ -119,7 +108,7 @@ module.exports = { getBaseBranchName, getLatestTag, createOrUpdateReleaseDescription, - downloadReleaseAssets, + getReleaseAssetsMetadata, getSnykCodeAlerts, getLastCliTag, }; From 4c50863112b9a3f4117647756e14210fd5462166 Mon Sep 17 00:00:00 2001 From: Louis Bompart Date: Mon, 17 Oct 2022 14:24:06 -0400 Subject: [PATCH 4/5] final fixes+tested --- .../get-release-artifact-and-commit-sha.mjs | 48 ++++++++----------- scripts/github-client.js | 28 ++++++----- 2 files changed, 35 insertions(+), 41 deletions(-) diff --git a/scripts/get-release-artifact-and-commit-sha.mjs b/scripts/get-release-artifact-and-commit-sha.mjs index cbe74908d9..f68aae0fca 100644 --- a/scripts/get-release-artifact-and-commit-sha.mjs +++ b/scripts/get-release-artifact-and-commit-sha.mjs @@ -1,32 +1,28 @@ -import {getReleaseAssetsMetadata, getLastCliTag} from './github-client.js'; import { - existsSync, - mkdirSync, - writeFileSync, - readdirSync, - copyFileSync, -} from 'fs'; -import {resolve} from 'path'; + getAssetsMetadataFromRelease, + getLastCliRelease, + getTagFromRelease, +} from './github-client.js'; +import {mkdirSync, writeFileSync, readdirSync, copyFileSync} from 'fs'; +import {resolve, join} from 'path'; import {execSync} from 'node:child_process'; +import {spawnSync} from 'child_process'; async function main() { - const tag = await getLastCliTag(); + const release = await getLastCliRelease(); + const tag = getTagFromRelease(release); // This folder structure needs to be respected in order for the CLI update plugin to // be able to do it's job properly. const topLevelDirectory = './artifacts'; const getSubDirectoryForTarball = ({version, commitSHA}) => - [topLevelDirectory, 'versions', version, commitSHA].join('/'); - const subDirectoryForManifest = [ - topLevelDirectory, - 'channels', - 'stable', - ].join('/'); + join(topLevelDirectory, 'versions', version, commitSHA); + const subDirectoryForManifest = join(topLevelDirectory, 'channels', 'stable'); const binariesMatcher = /^coveo[_-]{1}(?v?\d+\.\d+\.\d+(-\d+)?)[_.-]{1}(?\w+)[_-]?(\d+_)?(?.*\.(exe|deb|pkg))$/; const manifestMatcher = - /^coveo-(?<_version>v?\d+\.\d+\.\d+(-\d+)?)-(?\w+)-(?.*-buildmanifest)$/; + /^coveo-(?v?\d+\.\d+\.\d+(-\d+)?)-(?\w+)-(?.*-buildmanifest)$/; const tarballMatcher = - /^coveo-(?<_version>v?\d+\.\d+\.\d+(-\d+)?)-(?\w+)-(?[\w-]+).tar\.[gx]z$/; + /^coveo-v?(?\d+\.\d+\.\d+(-\d+)?)-(?\w+)-(?[\w-]+).tar\.[gx]z$/; const determineAssetLocation = (assetName) => { if (assetName.match(tarballMatcher)) { @@ -43,26 +39,22 @@ async function main() { } }; - [topLevelDirectory, subDirectoryForManifest, subDirectoryForTarball].forEach( - (dir) => { - if (!existsSync(dir)) { - mkdirSync(dir, {recursive: true}); - } - } - ); - - const assets = await getReleaseAssetsMetadata(tag); + const assets = await getAssetsMetadataFromRelease(release); for (const asset of assets) { console.info( `Downloading asset ${asset.name} from ${asset.browser_download_url}.\nSize: ${asset.size} ...` ); const directory = determineAssetLocation(asset.name); + mkdirSync(directory, {recursive: true}); execSync( `curl -L ${asset.browser_download_url} --output ${directory}/${asset.name}` ); } - - writeFileSync('latest-commit', tagCommit); + // https://stackoverflow.com/a/1862542 + const tagCommitPs = spawnSync('git', ['rev-list', '-n', '1', tag], { + encoding: 'utf-8', + }); + writeFileSync('latest-commit', tagCommitPs.stdout.trim()); readdirSync(topLevelDirectory, {withFileTypes: true}).forEach((file) => { const match = binariesMatcher.exec(file.name); diff --git a/scripts/github-client.js b/scripts/github-client.js index aa059ac119..049a20e57c 100644 --- a/scripts/github-client.js +++ b/scripts/github-client.js @@ -75,18 +75,19 @@ const createOrUpdateReleaseDescription = async (tag, body) => { } }; -const getLastCliTag = async () => { - return (await octokit.rest.repos.getLatestRelease({repo, owner})).data - .tag_name; -}; +const getLastCliRelease = () => + octokit.rest.repos.getLatestRelease({repo, owner}); -const getReleaseAssetsMetadata = async (tag) => { - const release = await octokit.rest.repos.getReleaseByTag({repo, owner, tag}); - return octokit.rest.repos.listReleaseAssets({ - owner, - repo, - release_id: release.data.id, - }); +const getTagFromRelease = (release) => release.data.tag_name; + +const getAssetsMetadataFromRelease = async (release) => { + return ( + await octokit.rest.repos.listReleaseAssets({ + owner, + repo, + release_id: release.data.id, + }) + ).data; }; const getSnykCodeAlerts = () => { @@ -108,7 +109,8 @@ module.exports = { getBaseBranchName, getLatestTag, createOrUpdateReleaseDescription, - getReleaseAssetsMetadata, + getAssetsMetadataFromRelease, getSnykCodeAlerts, - getLastCliTag, + getLastCliRelease, + getTagFromRelease, }; From 1cafe7bc974b64f8323086ca74840d9f61b04951 Mon Sep 17 00:00:00 2001 From: Louis Bompart Date: Mon, 17 Oct 2022 15:57:01 -0400 Subject: [PATCH 5/5] Update scripts/get-release-artifact-and-commit-sha.mjs --- scripts/get-release-artifact-and-commit-sha.mjs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/get-release-artifact-and-commit-sha.mjs b/scripts/get-release-artifact-and-commit-sha.mjs index f68aae0fca..2eca0cdbd8 100644 --- a/scripts/get-release-artifact-and-commit-sha.mjs +++ b/scripts/get-release-artifact-and-commit-sha.mjs @@ -51,10 +51,10 @@ async function main() { ); } // https://stackoverflow.com/a/1862542 - const tagCommitPs = spawnSync('git', ['rev-list', '-n', '1', tag], { + const tagCommit = spawnSync('git', ['rev-list', '-n', '1', tag], { encoding: 'utf-8', - }); - writeFileSync('latest-commit', tagCommitPs.stdout.trim()); + }).stdout.trim(); + writeFileSync('latest-commit', tagCommit); readdirSync(topLevelDirectory, {withFileTypes: true}).forEach((file) => { const match = binariesMatcher.exec(file.name);