Extract the version of a branch name so it can be used as the Accent file version (#397)

* Extract the Accent file version from the current branch name

* Format

* Package lock

* PR review

* Return "" if git not installed

* Update cli/src/services/config.ts

Co-authored-by: Simon Prévost <sprevost@mirego.com>

* Format

* Fix typo

* Disable eslint for camelcase

---------

Co-authored-by: Etienne St-Pierre <Etienne.St-Pierre1@brp.com>
Co-authored-by: Simon Prévost <sprevost@mirego.com>
This commit is contained in:
Etienne St-Pierre 2023-12-21 11:24:25 -05:00 committed by GitHub
parent cfe1e9ab9c
commit 8105d078c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 70 additions and 5 deletions

View File

@ -38,6 +38,9 @@ accent-cli reads from a `accent.json` file. The file should contain valid JSON r
{
"apiUrl": "http://your.accent.instance",
"apiKey": "2nziVSaa8yUJxLkwoZA",
"version": {
"branchVersionPrefix": "release/"
}
"files": [
{
"format": "json",
@ -65,6 +68,10 @@ Available ENV variables. (Each variable will override `accent.json` variables if
- `ACCENT_API_URL`: Api Key to your Accent Instance
- `ACCENT_PROJECT`: Your Project uuid
Version object configuration
- `branchVersionPrefix`: The Git branch prefix use to extract the file version
Each operation section `sync` and `addTranslations` can contain the following object:
- `language`: The identifier of the documents language
@ -130,6 +137,18 @@ Here is a list of available hooks. Those are self-explanatory
- `beforeExport`
- `afterExport`
## Version
Version can be extracted from the current Git branch name.
```
"version": {
"branchVersionPrefix": "release/"
}
```
Naming a branch `release/v1.0.0` will cause the `sync` and `stats` CLI commands to be invoked as if `--version=1.0.0` had been specified.
# Commands
<!-- commands -->
* [`accent export`](#accent-export)
@ -323,7 +342,6 @@ In this example the translations will be synchronized daily at midnight eastern
# License
`accent-cli` is © 2019 [Mirego](http://www.mirego.com) and may be freely distributed under the [New BSD license](http://opensource.org/licenses/BSD-3-Clause). See the [`LICENSE.md`](https://github.com/mirego/accent-cli/blob/master/LICENSE.md) file.
# About Mirego
[Mirego](http://mirego.com) is a team of passionate people who believe that work is a place where you can innovate and have fun. Were a team of [talented people](http://life.mirego.com) who imagine and build beautiful Web and mobile applications. We come together to share ideas and [change the world](http://mirego.org).

View File

@ -5,5 +5,8 @@
"source": "localization.json",
"target": "%document_path%.json"
}
]
],
"version": {
"branchVersionPrefix": "release/"
}
}

View File

@ -32,6 +32,10 @@ export default class Stats extends Command {
async run() {
const {flags} = this.parse(Stats);
if (this.projectConfig.config.version?.tag && !flags.version) {
flags.version = this.projectConfig.config.version.tag;
}
if (flags.version) {
const config = this.projectConfig.config;
const fetcher = new ProjectFetcher();

View File

@ -74,6 +74,10 @@ export default class Sync extends Command {
const t0 = process.hrtime.bigint();
const documents = this.projectConfig.files();
if (this.projectConfig.config.version?.tag && !flags.version) {
flags.version = this.config.version;
}
// From all the documentConfigs, do the sync or peek operations and log the results.
const syncFormatter = new SyncFormatter();
syncFormatter.log(this.project!, flags);

View File

@ -1,9 +1,9 @@
// Vendor
import {error} from '@oclif/errors';
import {execSync} from 'child_process';
import * as fs from 'fs-extra';
import * as path from 'path';
import * as chalk from 'chalk';
// Services
import Document from './document';
@ -19,6 +19,7 @@ export default class ConfigFetcher {
this.config.apiKey = process.env.ACCENT_API_KEY || this.config.apiKey;
this.config.apiUrl = process.env.ACCENT_API_URL || this.config.apiUrl;
this.config.project = process.env.ACCENT_PROJECT || this.config.project;
this.warnings = [];
if (!this.config.apiKey) {
@ -37,6 +38,18 @@ export default class ConfigFetcher {
error('You must have at least 1 document set in your config');
}
if (
this.config.version?.branchVersionPrefix &&
this.getCurrentBranchName().startsWith(
this.config.version?.branchVersionPrefix
)
) {
this.config.version.tag = this.extractVersionFromBranch(
this.getCurrentBranchName(),
this.config.version?.branchVersionPrefix
);
}
const sameFolderPathWarning: Set<string> = new Set();
this.config.files.forEach((documentConfig) => {
@ -71,4 +84,21 @@ Only your master language should be listed in your files config.`
private sourceFolderPath(source: string) {
return source.replace(path.basename(source), '');
}
private getCurrentBranchName() {
try {
return execSync('git rev-parse --abbrev-ref HEAD')
.toString('utf8')
.replace(/[\n\r\s]+$/, '');
} catch {
return '';
}
}
private extractVersionFromBranch(
branchName: string,
gitBranchVersionMatch: string
): string {
return branchName.replace(gitBranchVersionMatch, '');
}
}

View File

@ -32,7 +32,7 @@ export default class ProjectFetcher {
}
private async graphql(config: Config, params: object) {
const query = `query ProjectDetails($project_id: ID! $version_id: ID) {
const query = `query ProjectDetails($project_id: ID! $versionId: ID) {
viewer {
user {
fullname

View File

@ -1,9 +1,11 @@
// Types
import {DocumentConfig} from './document-config';
import {VersionConfig} from './version-config';
export interface Config {
apiUrl: string;
apiKey: string;
project: string | null | undefined;
project?: string | null;
version?: VersionConfig;
files: DocumentConfig[];
}

View File

@ -0,0 +1,4 @@
export interface VersionConfig {
branchVersionPrefix?: string;
tag?: string;
}