Skip to content

Commit

Permalink
branch support
Browse files Browse the repository at this point in the history
  • Loading branch information
andrii-bodnar committed Jul 21, 2023
1 parent 8b67da6 commit abbc6e5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 14 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
steps:
- uses: actions/checkout@v3
- uses: ./
with:
branch: main
env:
CROWDIN_PROJECT_ID: ${{ secrets.CROWDIN_PROJECT_ID }}
CROWDIN_PERSONAL_TOKEN: ${{ secrets.CROWDIN_PERSONAL_TOKEN }}
28 changes: 23 additions & 5 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

40 changes: 32 additions & 8 deletions src/translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,44 @@ export class Translator {
this.logger.log('info', 'Uploading sources...');

const readmeFile = await this.getFile();

const storageFile = await this.crowdin.uploadStorageApi.addStorage(
this.config.file,
fs.readFileSync(this.config.file)
);

let branchId = undefined;

if (this.config.branch) {
let branch = await this.getBranch();

if (!branch) {
branch = (
await this.crowdin.sourceFilesApi.createBranch(this.credentials.projectId, {
name: this.config.branch
})
).data;

this.logger.log('info', `Branch ${branch.name} successfully created!`);
}

branchId = branch.id;
}

if (readmeFile) {
await this.crowdin.sourceFilesApi.updateOrRestoreFile(this.credentials.projectId, readmeFile.id, {
storageId: storageFile.data.id
});

this.logger.log('info', 'Source file successfully updated!');
} else {
await this.crowdin.sourceFilesApi.createFile(this.credentials.projectId, {
storageId: storageFile.data.id,
name: this.config.file,
branchId: (await this.getBranch())?.id
branchId: branchId
});
}

this.logger.log('info', 'Sources uploaded!');
this.logger.log('info', 'Source file successfully created!');
}
}

private async downloadTranslations(): Promise<string> {
Expand All @@ -67,11 +86,8 @@ export class Translator {
}

const result = await this.crowdin.translationsApi.buildProject(this.credentials.projectId, request);

let status = result.data.status;

this.logger.log('info', `Status: ${status}`);

while (status === 'inProgress') {
const progress = await this.crowdin.translationsApi.checkBuildStatus(
this.credentials.projectId,
Expand Down Expand Up @@ -102,7 +118,7 @@ export class Translator {
private async getFile(): Promise<SourceFilesModel.File | undefined> {
const files = await this.crowdin.sourceFilesApi.listProjectFiles(this.credentials.projectId);

const sourceFile = files.data.find(file => file.data.path === `/${this.config.file}`);
const sourceFile = files.data.find(file => file.data.path === this.getFilePath());

return sourceFile?.data;
}
Expand All @@ -114,4 +130,12 @@ export class Translator {

return crowdinBranch?.data;
}

private getFilePath(): string {
if (this.config.branch) {
return `/${this.config.branch}/${this.config.file}`;
} else {
return `/${this.config.file}`;
}
}
}

0 comments on commit abbc6e5

Please sign in to comment.