Skip to content

Commit

Permalink
fix: OPTIC-489: Enable version.json creation whenever the correspondi…
Browse files Browse the repository at this point in the history
…ng FE source changes (#5535)

* fix: OPTIC-489: Enable version.json creation whenever the corresponding FE source changes

* enable versioning against only changed source, run it with sample output.

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8148704358

* enable postbuild

* use origin/develop

* use different arg syntax

* make the version.mjs generator available as a binary and update ci frontend-build.yml to directly use version:affected to ensure other yarn builds do not trigger this.

* fix action.yml

* fix action.yml

* let CI set the base branch

* let CI set the base branch

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8156783878

* only fetch develop in addition to the PR branch

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8156904605

* build version on change

* remove error and console reporting

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8177622887

* use rev-list and show to get latest affected commit info

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8178473301

* get the log info more accurately

* get the log info more accurately

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8178976383

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8179332128

* checking branch output of log info

* updating version script name

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8179543959

* removing unnecessary extra steps just for debug

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8179721910

* update the version.mjs script to be compatible and used within LSE as well

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8196310545

* Removing from docker build step as the committed file version.json is not the one we want, its the release time version.json that we require.

* ci: Build frontend

Workflow run: https://github.com/HumanSignal/label-studio/actions/runs/8196378391

---------

Co-authored-by: robot-ci-heartex <robot-ci-heartex@users.noreply.github.com>
Co-authored-by: bmartel <bmartel@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 14, 2024
1 parent b08b623 commit e0bdde2
Show file tree
Hide file tree
Showing 14 changed files with 175 additions and 6 deletions.
7 changes: 7 additions & 0 deletions web/apps/labelstudio/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@
"codeCoverage": true
}
}
},
"version": {
"executor": "nx:run-commands",
"options": {
"cwd": "apps/labelstudio",
"command": "node ../../tools/version/version.mjs"
}
}
},
"tags": []
Expand Down
2 changes: 1 addition & 1 deletion web/dist/apps/labelstudio/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion web/dist/apps/labelstudio/main.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion web/dist/libs/datamanager/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion web/dist/libs/editor/main.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion web/dist/libs/editor/main.js.map

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions web/libs/datamanager/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@
"codeCoverage": true
}
}
},
"version": {
"executor": "nx:run-commands",
"options": {
"cwd": "libs/datamanager",
"command": "node ../../tools/version/version.mjs"
}
}
}
}
7 changes: 7 additions & 0 deletions web/libs/editor/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@
"testingType": "e2e",
"baseUrl": "http://localhost:3000"
}
},
"version": {
"executor": "nx:run-commands",
"options": {
"cwd": "libs/editor",
"command": "node ../../tools/version/version.mjs"
}
}
}
}
11 changes: 10 additions & 1 deletion web/nx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"tasksRunnerOptions": {
"default": {
"options": {
"cacheableOperations": ["build-storybook"]
"cacheableOperations": ["build-storybook", "version"]
}
}
},
Expand All @@ -25,6 +25,15 @@
"test": {
"inputs": ["default", "^production", "{workspaceRoot}/jest.preset.js"]
},
"version": {
"cache": true,
"inputs": [
"default",
"^production",
"{workspaceRoot}/tools/version.mjs"
],
"outputs": ["{workspaceRoot}/dist/{projectRoot}/version.json"]
},
"build-storybook": {
"cache": true,
"inputs": [
Expand Down
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
"dm:watch": "nx run datamanager:build:development --watch",
"dm:unit": "nx run datamanager:unit",
"build": "NODE_ENV=production nx run-many --target=build --configuration=production",
"version:libs": "nx run-many --target=version",
"watch": "nx run-many --target=build --configuration=production --watch",
"test:unit": "yarn lsf:unit && yarn ls:unit && yarn dm:unit",
"test:e2e": "yarn ls:e2e && yarn lsf:e2e",
Expand Down
9 changes: 9 additions & 0 deletions web/tools/version/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"name": "@humansignal/version",
"version": "0.0.1",
"description": "Version generator for internal libraries",
"main": "version.mjs",
"bin": "version",
"license": "MIT",
"dependencies": {}
}
3 changes: 3 additions & 0 deletions web/tools/version/version
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env node

import './version.mjs';
122 changes: 122 additions & 0 deletions web/tools/version/version.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
import path from 'node:path';
import {spawn} from 'node:child_process';
import fs from 'node:fs/promises';

const git = async (command, options) => {
// create a promise based git wrapper around a spawned process
return new Promise((resolve, reject) => {
const currentPwd = process.cwd();
const child = spawn('git', [command, ...options], {
cwd: currentPwd,
stdio: ['ignore', 'pipe', 'pipe']
});
let stdout = '';
let stderr = '';

child.stdout.on('data', (data) => {
stdout += data;
});

child.stderr.on('data', (data) => {
stderr += data;
});

child.on('close', (code) => {
if (code === 0) {
resolve(stdout);
} else {
reject(stderr);
}
});
});
}

/**
* Get the commits affecting the current project
* @param options
* @returns {Promise<string>}
*/
const gitLog = async (options = []) => {
const log = await git('log', options);
return log.trim();
}

/**
* Get the branch info of the current project
* @param options
* @returns {Promise<string>}
*/
const gitBranch = async (options = []) => {
const branch = await git('branch', options);
return branch.trim();
}


/**
* @typedef {Object} CommitVersion
* @property {string} message - The commit message of the latest commit to affect the current project
* @property {string} commit - The commit hash of the latest commit to affect the current project
* @property {string} date - The date of the latest commit to affect the current project
* @property {string} branch - The current branch
*/

/**
* Get the last commit data to have affected the current project
* @returns {Promise<CommitVersion>}
*/
const getVersionData = async () => {
const latestCommitInfo = await gitLog(['-n 1', '-p', 'src/*']);
const commitInfo = latestCommitInfo.split('\n');
let commit = commitInfo.find((line) => line.startsWith('commit'))?.trim().replace('commit', '').trim() ?? '';
let date = commitInfo.find((line) => line.startsWith('Date:')) ?? '';
// First non-empty line after the Date: line is the commit message
let message = commitInfo.slice(commitInfo.indexOf(date) + 1).find((line) => line.trim().length > 0)?.trim() ?? '';
// Remove the Date: prefix from the date
date = date.replace('Date:', '').trim();

// Get the current branch of the latest commit
const contains = (await gitBranch(['--contains', commit])).split('\n')
let branch = (contains.find((line) => line.startsWith('develop') || line.startsWith('*')) ?? '').replace('*', '').trim();

if (branch === '' || branch.includes('HEAD')) {
branch = 'develop';
}

return {
message,
commit,
date: new Date(date).toISOString(),
branch,
};
}

const versionLib = async () => {
const currentPwd = process.cwd();
// if the currentPwd includes 'node_modules', we are running from within the monorepo package itself
// and we have to account for the difference
let workspaceRoot, currentProjectPath;
if (currentPwd.includes('node_modules')) {
const [_workspaceRoot, nodeModulesPath, _currentProjectPath] = currentPwd.split('web')
workspaceRoot = path.join(_workspaceRoot, 'web', nodeModulesPath)
currentProjectPath = _currentProjectPath
} else {
const [_workspaceRoot, _currentProjectPath] = currentPwd.split('web')
workspaceRoot = _workspaceRoot
currentProjectPath = _currentProjectPath
}
const distPath = path.join(workspaceRoot, 'web', 'dist', currentProjectPath)

try {
await fs.mkdir(distPath, {recursive: true})
} catch { /* ignore */
}

const versionData = await getVersionData();
const versionJson = JSON.stringify(versionData, null, 2);
const versionFile = path.join(distPath, 'version.json');
await fs.writeFile(versionFile, versionJson);
}

versionLib().then(() => {
console.log('Versioning complete');
});
4 changes: 4 additions & 0 deletions web/tools/version/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


0 comments on commit e0bdde2

Please sign in to comment.