From 8685b377a061138fb4fcf2acfd0f24966c5845d4 Mon Sep 17 00:00:00 2001 From: Louis Bompart Date: Tue, 18 Oct 2022 15:06:54 -0400 Subject: [PATCH] chore(release): fix jenkins artifacting (#988) ## Proposed changes - Fix ESM import (need file-ext) - Recover tag from release (simpler) - Infer file architecture from filename instead of 'redoing' oclif logic ## Testing - [x] Manual Tests: Created a PAT and run it on my machine, checked the artifacts folder. --- .../get-release-artifact-and-commit-sha.mjs | 76 ++++++++++--------- scripts/github-client.js | 33 ++++---- 2 files changed, 54 insertions(+), 55 deletions(-) diff --git a/scripts/get-release-artifact-and-commit-sha.mjs b/scripts/get-release-artifact-and-commit-sha.mjs index 244a360acb..2eca0cdbd8 100644 --- a/scripts/get-release-artifact-and-commit-sha.mjs +++ b/scripts/get-release-artifact-and-commit-sha.mjs @@ -1,48 +1,35 @@ -import {downloadReleaseAssets} from './github-client'; -import {getLastTag} from '@coveo/semantic-monorepo-tools'; 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 getLastTag('@coveo/cli@'); + 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 subDirectoryForTarball = [ - topLevelDirectory, - 'versions', - tag.name.substring(1), - tag.commit.sha.substring(0, 7), - ].join('/'); - const subDirectoryForManifest = [ - topLevelDirectory, - 'channels', - 'stable', - ].join('/'); + const getSubDirectoryForTarball = ({version, commitSHA}) => + join(topLevelDirectory, 'versions', version, commitSHA); + const subDirectoryForManifest = join(topLevelDirectory, 'channels', 'stable'); 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-(?v?\d+\.\d+\.\d+(-\d+)?)-(?\w+)-(?.*-buildmanifest)$/; + const tarballMatcher = + /^coveo-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.name, (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; @@ -50,9 +37,24 @@ async function main() { console.info(assetName, `--> ${topLevelDirectory}`); return topLevelDirectory; } - }); + }; - writeFileSync('latest-commit', tag.commit.sha); + 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}` + ); + } + // https://stackoverflow.com/a/1862542 + const tagCommit = spawnSync('git', ['rev-list', '-n', '1', tag], { + encoding: 'utf-8', + }).stdout.trim(); + 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..049a20e57c 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'; @@ -76,23 +75,19 @@ const createOrUpdateReleaseDescription = async (tag, body) => { } }; -const downloadReleaseAssets = async (tag, determineAssetLocation) => { - const release = await octokit.rest.repos.getReleaseByTag({repo, owner, tag}); - const assets = await octokit.rest.repos.listReleaseAssets({ - owner, - repo, - release_id: release.data.id, - }); +const getLastCliRelease = () => + octokit.rest.repos.getLatestRelease({repo, owner}); - 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 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 = () => { @@ -114,6 +109,8 @@ module.exports = { getBaseBranchName, getLatestTag, createOrUpdateReleaseDescription, - downloadReleaseAssets, + getAssetsMetadataFromRelease, getSnykCodeAlerts, + getLastCliRelease, + getTagFromRelease, };