Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 10 commits into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
EtienneStPierre marked this conversation as resolved.
Show resolved Hide resolved
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;
}
Loading