Skip to content

Commit 3d3629a

Browse files
committed
feat: add workflows for en and jp branches
1 parent 9be8564 commit 3d3629a

File tree

9 files changed

+315
-128
lines changed

9 files changed

+315
-128
lines changed

.github/scripts/add-code-samples.ts

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import * as fs from 'fs';
44
import * as path from 'path';
55
import * as os from 'os';
66
import simpleGit from 'simple-git';
7+
import { findMatchingFiles } from './utils';
78

89
// SDK Repository configurations in format: REPO_URL#BRANCH#DIRECTORY
910
// Repository names use language codes as prefixes (e.g., CURL_, DOTNET_, etc.)
@@ -186,49 +187,6 @@ function parseSamplesFromContent(content: string, lang: string): SampleData[] {
186187
return samples;
187188
}
188189

189-
/**
190-
* Find all files in a directory that match the given regex pattern
191-
*/
192-
function findMatchingFiles(directoryPath: string, pattern: string): string[] {
193-
try {
194-
// Check if directory exists
195-
if (!fs.existsSync(directoryPath)) {
196-
console.error(`❌ Error: Directory not found: ${directoryPath}`);
197-
process.exit(1);
198-
}
199-
200-
// Check if it's a directory
201-
const stats = fs.statSync(directoryPath);
202-
if (!stats.isDirectory()) {
203-
console.error(`❌ Error: Path is not a directory: ${directoryPath}`);
204-
process.exit(1);
205-
}
206-
207-
// Read all files in the directory
208-
const files = fs.readdirSync(directoryPath);
209-
210-
// Create regex from pattern
211-
let regex: RegExp;
212-
try {
213-
regex = new RegExp(pattern);
214-
} catch (e) {
215-
console.error(`❌ Error: Invalid regex pattern: ${pattern}`);
216-
console.error(` ${(e as Error).message}`);
217-
process.exit(1);
218-
}
219-
220-
// Filter files that match the pattern
221-
const matchingFiles = files
222-
.filter(file => regex.test(file))
223-
.map(file => path.join(directoryPath, file));
224-
225-
return matchingFiles;
226-
} catch (e) {
227-
console.error(`❌ Error reading directory ${directoryPath}:`, (e as Error).message);
228-
process.exit(1);
229-
}
230-
}
231-
232190
/**
233191
* Load OpenAPI schemas from files
234192
*/

.github/scripts/add-mint-config.ts

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
#!/usr/bin/env ts-node
2+
3+
import * as fs from 'fs';
4+
import * as path from 'path';
5+
import { findMatchingFiles } from './utils';
6+
7+
/**
8+
* Add x-mint configuration to OpenAPI schema
9+
*/
10+
function addMintConfig(schema: any): boolean {
11+
// Check if x-mint already exists
12+
if (schema['x-mint']) {
13+
console.warn(' ⚠️ x-mint configuration already exists, overwriting');
14+
}
15+
16+
// Add x-mint configuration
17+
schema['x-mint'] = {
18+
mcp: {
19+
enabled: true
20+
}
21+
};
22+
23+
return true;
24+
}
25+
26+
/**
27+
* Main execution function
28+
*/
29+
export async function main(directoryPath?: string, pattern?: string): Promise<number> {
30+
// Get command line arguments if not provided
31+
let dir = directoryPath;
32+
let pat = pattern;
33+
34+
if (!dir || !pat) {
35+
const args = process.argv.slice(2);
36+
37+
if (args.length < 2) {
38+
console.error('❌ Error: Missing required arguments');
39+
console.error('');
40+
console.error('Usage: cd .github/scripts && npm run add-mint-config -- <directory> <pattern>');
41+
console.error('');
42+
console.error('Arguments:');
43+
console.error(' <directory> - Path to directory containing OpenAPI JSON files');
44+
console.error(' <pattern> - Regex pattern to match filenames');
45+
console.error('');
46+
console.error('Examples:');
47+
console.error(' cd .github/scripts && npm run add-mint-config -- "../../openapi" "openapi.*\\.json"');
48+
return 1;
49+
}
50+
51+
dir = args[0];
52+
pat = args[1];
53+
}
54+
55+
console.log('🚀 Adding x-mint configuration to OpenAPI schemas...\n');
56+
console.log(`Searching in directory: ${dir}`);
57+
console.log(`Pattern: ${pat}\n`);
58+
59+
// Find matching OpenAPI files
60+
const openapiFiles = findMatchingFiles(dir, pat);
61+
62+
if (openapiFiles.length === 0) {
63+
console.log('⚠️ No files found matching the pattern');
64+
return 1;
65+
}
66+
67+
console.log(`Found ${openapiFiles.length} matching file(s):\n`);
68+
openapiFiles.forEach(file => console.log(` - ${file}`));
69+
console.log('');
70+
71+
const startTime = Date.now();
72+
let filesUpdated = 0;
73+
74+
try {
75+
for (const filePath of openapiFiles) {
76+
console.log(`📄 Processing ${path.basename(filePath)}...`);
77+
78+
// Read and parse the schema
79+
const content = fs.readFileSync(filePath, 'utf-8');
80+
const schema = JSON.parse(content);
81+
82+
// Add x-mint configuration
83+
const updated = addMintConfig(schema);
84+
85+
if (updated) {
86+
// Write back to file
87+
const updatedContent = JSON.stringify(schema, null, 2);
88+
fs.writeFileSync(filePath, updatedContent, 'utf-8');
89+
console.log(' ✅ Added x-mint configuration');
90+
filesUpdated++;
91+
}
92+
93+
console.log();
94+
}
95+
96+
// Summary
97+
const duration = ((Date.now() - startTime) / 1000).toFixed(2);
98+
console.log('📊 Summary:');
99+
console.log(` • Files processed: ${openapiFiles.length}`);
100+
console.log(` • Files updated: ${filesUpdated}`);
101+
console.log(` • Duration: ${duration}s`);
102+
console.log('\n✨ Done!');
103+
104+
return 0;
105+
} catch (error) {
106+
console.error('\n❌ Error:', error);
107+
return 1;
108+
}
109+
}
110+
111+
// Run if executed directly
112+
if (require.main === module) {
113+
main()
114+
.then((exitCode) => process.exit(exitCode))
115+
.catch((error) => {
116+
console.error('Fatal error:', error);
117+
process.exit(1);
118+
});
119+
}
120+

.github/scripts/package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
"description": "Scripts for Box OpenAPI specification",
55
"scripts": {
66
"replace-links": "ts-node replace-links.ts",
7-
"add-code-samples": "ts-node add-code-samples.ts"
7+
"add-code-samples": "ts-node add-code-samples.ts",
8+
"add-mint-config": "ts-node add-mint-config.ts"
89
},
910
"devDependencies": {
1011
"simple-git": "3.21.0",

.github/scripts/replace-links.ts

Lines changed: 1 addition & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import * as fs from 'fs';
44
import * as path from 'path';
5+
import { findMatchingFiles } from './utils';
56

67
/**
78
* Replace all occurrences of provided links with localised version in the given content
@@ -10,49 +11,6 @@ function replaceLinks(content: string, oldUrl: string, newUrl: string): string {
1011
return content.replace(new RegExp(oldUrl, 'g'), newUrl);
1112
}
1213

13-
/**
14-
* Find all files in a directory that match the given regex pattern
15-
*/
16-
function findMatchingFiles(directoryPath: string, pattern: string): string[] {
17-
try {
18-
// Check if directory exists
19-
if (!fs.existsSync(directoryPath)) {
20-
console.error(`❌ Error: Directory not found: ${directoryPath}`);
21-
process.exit(1);
22-
}
23-
24-
// Check if it's a directory
25-
const stats = fs.statSync(directoryPath);
26-
if (!stats.isDirectory()) {
27-
console.error(`❌ Error: Path is not a directory: ${directoryPath}`);
28-
process.exit(1);
29-
}
30-
31-
// Read all files in the directory
32-
const files = fs.readdirSync(directoryPath);
33-
34-
// Create regex from pattern
35-
let regex: RegExp;
36-
try {
37-
regex = new RegExp(pattern);
38-
} catch (e) {
39-
console.error(`❌ Error: Invalid regex pattern: ${pattern}`);
40-
console.error(` ${(e as Error).message}`);
41-
process.exit(1);
42-
}
43-
44-
// Filter files that match the pattern
45-
const matchingFiles = files
46-
.filter(file => regex.test(file))
47-
.map(file => path.join(directoryPath, file));
48-
49-
return matchingFiles;
50-
} catch (e) {
51-
console.error(`❌ Error reading directory ${directoryPath}:`, (e as Error).message);
52-
process.exit(1);
53-
}
54-
}
55-
5614
/**
5715
* Process a single OpenAPI file
5816
*/

.github/scripts/tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,3 @@
1313
"include": ["./**/*"],
1414
"exclude": ["node_modules"]
1515
}
16-

.github/scripts/utils.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import * as fs from 'fs';
2+
import * as path from 'path';
3+
4+
/**
5+
* Find all files in a directory that match the given regex pattern
6+
*/
7+
export function findMatchingFiles(directoryPath: string, pattern: string): string[] {
8+
try {
9+
// Check if directory exists
10+
if (!fs.existsSync(directoryPath)) {
11+
console.error(`❌ Error: Directory not found: ${directoryPath}`);
12+
process.exit(1);
13+
}
14+
15+
// Check if it's a directory
16+
const stats = fs.statSync(directoryPath);
17+
if (!stats.isDirectory()) {
18+
console.error(`❌ Error: Path is not a directory: ${directoryPath}`);
19+
process.exit(1);
20+
}
21+
22+
// Read all files in the directory
23+
const files = fs.readdirSync(directoryPath);
24+
25+
// Create regex from pattern
26+
let regex: RegExp;
27+
try {
28+
regex = new RegExp(pattern);
29+
} catch (e) {
30+
console.error(`❌ Error: Invalid regex pattern: ${pattern}`);
31+
console.error(` ${(e as Error).message}`);
32+
process.exit(1);
33+
}
34+
35+
// Filter files that match the pattern
36+
const matchingFiles = files
37+
.filter(file => regex.test(file))
38+
.map(file => path.join(directoryPath, file));
39+
40+
return matchingFiles;
41+
} catch (e) {
42+
console.error(`❌ Error reading directory ${directoryPath}:`, (e as Error).message);
43+
process.exit(1);
44+
}
45+
}
46+

.github/workflows/en-mint.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# The name of this GH action
2+
name: Process EN Branch
3+
4+
# Defines when this action should be run
5+
on:
6+
# Run on any Push
7+
push:
8+
branches:
9+
- en
10+
# Allow manual triggering
11+
workflow_dispatch:
12+
13+
jobs:
14+
# A task that processes OpenAPI files from the en branch
15+
process-en-branch:
16+
# We run this on the latest ubuntu
17+
runs-on: ubuntu-latest
18+
timeout-minutes: 15
19+
20+
strategy:
21+
matrix:
22+
node-version: [24.x]
23+
24+
steps:
25+
- name: Check out the en branch
26+
uses: actions/checkout@v4
27+
with:
28+
ref: en
29+
token: ${{ secrets.GITHUB_TOKEN }}
30+
fetch-depth: 0
31+
32+
- name: Check out scripts from main branch
33+
uses: actions/checkout@v4
34+
with:
35+
ref: main
36+
sparse-checkout: |
37+
.github/scripts
38+
sparse-checkout-cone-mode: false
39+
path: .main-scripts
40+
token: ${{ secrets.GITHUB_TOKEN }}
41+
42+
- name: Set up Node.js
43+
uses: actions/setup-node@v4
44+
with:
45+
node-version: ${{ matrix.node-version }}
46+
47+
- name: Install script dependencies
48+
working-directory: .main-scripts/.github/scripts
49+
run: npm install
50+
51+
- name: Run add-code-samples script
52+
working-directory: .main-scripts/.github/scripts
53+
run: npm run add-code-samples -- "../../.." "openapi.*\\.json"
54+
55+
- name: Run add-mint-config script
56+
working-directory: .main-scripts/.github/scripts
57+
run: npm run add-mint-config -- "../../.." "openapi.*\\.json"
58+
59+
- name: Cleanup working directory
60+
run: rm -rf .main-scripts
61+
62+
- name: Push processed files to en-mint branch
63+
64+
env:
65+
REPO: self
66+
BRANCH: en-mint
67+
FOLDER: .
68+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
69+
MESSAGE: "Add SDK code samples to OpenAPI files"

0 commit comments

Comments
 (0)