Skip to content

Commit

Permalink
Extract the version of a branch name so it can be used as the Accent …
Browse files Browse the repository at this point in the history
…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 <[email protected]>

* Format

* Fix typo

* Disable eslint for camelcase

---------

Co-authored-by: Etienne St-Pierre <[email protected]>
Co-authored-by: Simon Prévost <[email protected]>
  • Loading branch information
3 people authored Dec 21, 2023
1 parent cfe1e9a commit 8105d07
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 5 deletions.
20 changes: 19 additions & 1 deletion cli/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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 document’s language
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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. We’re 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).
Expand Down
5 changes: 4 additions & 1 deletion cli/examples/simple/accent.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"source": "localization.json",
"target": "%document_path%.json"
}
]
],
"version": {
"branchVersionPrefix": "release/"
}
}
4 changes: 4 additions & 0 deletions cli/src/commands/stats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
4 changes: 4 additions & 0 deletions cli/src/commands/sync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
32 changes: 31 additions & 1 deletion cli/src/services/config.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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) {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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, '');
}
}
2 changes: 1 addition & 1 deletion cli/src/services/project-fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion cli/src/types/config.ts
Original file line number Diff line number Diff line change
@@ -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[];
}
4 changes: 4 additions & 0 deletions cli/src/types/version-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface VersionConfig {
branchVersionPrefix?: string;
tag?: string;
}

0 comments on commit 8105d07

Please sign in to comment.