Skip to content

Commit 6546ff1

Browse files
authored
Merge pull request #511 from abraham/setup
apply overrides on setup and update
2 parents c7a417a + 859ac4d commit 6546ff1

File tree

7 files changed

+107
-40
lines changed

7 files changed

+107
-40
lines changed

.github/workflows/update.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ jobs:
2929
run: npm ci
3030

3131
- name: Update docs SHA
32-
run: npm run update-docs-sha
32+
run: npm run update-docs
3333

3434
- name: Install dependencies
3535
run: npm ci

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Generate the most accurate OpenAPI spec for the current stable release of Mastod
1717
### Update documentation version
1818

1919
```bash
20-
npm run update-docs-sha
20+
npm run update-docs
2121
```
2222

2323
### Generate Schema

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"lint": "eslint .",
1919
"lint:fix": "eslint . --fix",
2020
"postinstall": "ts-node scripts/setup-docs.ts",
21-
"update-docs-sha": "ts-node scripts/update-docs-sha.ts"
21+
"update-docs": "ts-node scripts/update-docs.ts"
2222
},
2323
"keywords": [
2424
"mastodon",

scripts/apply-overrides.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/usr/bin/env ts-node
2+
3+
import fs from 'fs';
4+
import path from 'path';
5+
import { execSync } from 'child_process';
6+
7+
/**
8+
* Apply override commits to the mastodon-documentation repository
9+
*/
10+
function applyOverrides(): void {
11+
const configPath = path.join(__dirname, '..', 'config.json');
12+
const docsDir = path.join(__dirname, '..', 'mastodon-documentation');
13+
14+
if (!fs.existsSync(configPath)) {
15+
console.error('config.json not found');
16+
process.exit(1);
17+
}
18+
19+
if (!fs.existsSync(docsDir)) {
20+
console.error(
21+
'mastodon-documentation directory not found. Run setup-docs first.'
22+
);
23+
process.exit(1);
24+
}
25+
26+
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
27+
const overrideCommits = config.overrideCommits || [];
28+
const overridesRepository = config.overridesRepository;
29+
30+
// Exit early if no overrides to apply
31+
if (overrideCommits.length === 0) {
32+
console.log('No override commits to apply.');
33+
return;
34+
}
35+
36+
console.log(`Applying ${overrideCommits.length} override commit(s)...`);
37+
38+
try {
39+
// Configure git user for cherry-picking
40+
console.log('Configuring git user...');
41+
execSync(`git config user.email "[email protected]"`, {
42+
stdio: 'inherit',
43+
cwd: docsDir,
44+
});
45+
execSync(`git config user.name "bot"`, {
46+
stdio: 'inherit',
47+
cwd: docsDir,
48+
});
49+
50+
// Add overrides remote if repository is specified
51+
if (overridesRepository) {
52+
console.log(`Adding ${overridesRepository} overrides remote...`);
53+
execSync(`git remote add overrides ${overridesRepository} || true`, {
54+
stdio: 'inherit',
55+
cwd: docsDir,
56+
});
57+
execSync(`git fetch overrides`, {
58+
stdio: 'inherit',
59+
cwd: docsDir,
60+
});
61+
}
62+
63+
// Apply each override commit
64+
for (const commit of overrideCommits) {
65+
console.log(`Cherry-picking commit ${commit}...`);
66+
execSync(`git cherry-pick ${commit}`, {
67+
stdio: 'inherit',
68+
cwd: docsDir,
69+
});
70+
}
71+
72+
console.log('Override commits applied successfully.');
73+
} catch (error) {
74+
console.error('Error applying override commits:', (error as Error).message);
75+
process.exit(1);
76+
}
77+
}
78+
79+
if (require.main === module) {
80+
applyOverrides();
81+
}
82+
83+
export { applyOverrides };

scripts/setup-docs.ts

Lines changed: 3 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fs from 'fs';
44
import path from 'path';
55
import { execSync } from 'child_process';
6+
import { applyOverrides } from './apply-overrides';
67

78
/**
89
* Setup Mastodon documentation repository at the configured commit SHA
@@ -18,8 +19,6 @@ function setupMastodonDocs(): void {
1819

1920
const config = JSON.parse(fs.readFileSync(configPath, 'utf8'));
2021
const targetCommit = config.mastodonDocsCommit;
21-
const overrideCommits = config.overrideCommits || [];
22-
const overridesRepository = config.overridesRepository;
2322

2423
if (!targetCommit) {
2524
console.error('mastodonDocsCommit not found in config.json');
@@ -54,35 +53,8 @@ function setupMastodonDocs(): void {
5453
cwd: docsDir,
5554
});
5655

57-
// Apply override commits if any
58-
if (overrideCommits.length > 0) {
59-
console.log(`Adding ${overridesRepository} overrides remote...`);
60-
execSync(`git config user.email "[email protected]"`, {
61-
stdio: 'inherit',
62-
cwd: docsDir,
63-
});
64-
execSync(`git config user.name "bot"`, {
65-
stdio: 'inherit',
66-
cwd: docsDir,
67-
});
68-
execSync(`git remote add overrides ${overridesRepository} || true`, {
69-
stdio: 'inherit',
70-
cwd: docsDir,
71-
});
72-
execSync(`git fetch overrides`, {
73-
stdio: 'inherit',
74-
cwd: docsDir,
75-
});
76-
77-
console.log(`Applying ${overrideCommits.length} override commit(s)...`);
78-
for (const commit of overrideCommits) {
79-
console.log(`Cherry-picking commit ${commit}...`);
80-
execSync(`git cherry-pick ${commit}`, {
81-
stdio: 'inherit',
82-
cwd: docsDir,
83-
});
84-
}
85-
}
56+
// Apply override commits
57+
applyOverrides();
8658

8759
console.log('Mastodon documentation setup complete.');
8860
} catch (error) {
Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import fs from 'fs';
44
import path from 'path';
55
import { execSync } from 'child_process';
6+
import { applyOverrides } from './apply-overrides';
67

78
/**
89
* Update the mastodonDocsCommit in config.json to the latest commit from main branch
@@ -28,9 +29,16 @@ function updateDocsCommit(): boolean {
2829
);
2930

3031
try {
31-
// Pull the latest changes from the remote repository
32-
console.log('Pulling latest changes...');
33-
execSync('git pull origin main', {
32+
// Fetch the latest changes from the remote repository
33+
console.log('Fetching latest changes...');
34+
execSync('git fetch origin', {
35+
cwd: docsDir,
36+
stdio: 'inherit',
37+
});
38+
39+
// Reset local branch to match origin/main, discarding any local changes
40+
console.log('Resetting to origin/main...');
41+
execSync('git reset --hard origin/main', {
3442
cwd: docsDir,
3543
stdio: 'inherit',
3644
});
@@ -61,6 +69,10 @@ function updateDocsCommit(): boolean {
6169
console.log(
6270
`Updated mastodonDocsCommit from ${oldCommit} to ${latestCommit}`
6371
);
72+
73+
// Apply override commits
74+
applyOverrides();
75+
6476
return true;
6577
} catch (error) {
6678
console.error('Error updating docs commit:', (error as Error).message);

src/__tests__/scripts.test.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ describe('Scripts', () => {
99
expect(fs.existsSync(setupDocsPath)).toBe(true);
1010
});
1111

12-
it('should have update-docs-sha.ts script', () => {
13-
const updateShaPath = path.join(scriptsDir, 'update-docs-sha.ts');
12+
it('should have update-docs.ts script', () => {
13+
const updateShaPath = path.join(scriptsDir, 'update-docs.ts');
1414
expect(fs.existsSync(updateShaPath)).toBe(true);
1515
});
1616

@@ -20,7 +20,7 @@ describe('Scripts', () => {
2020
});
2121

2222
it('should export updateDocsCommit function', async () => {
23-
const { updateDocsCommit } = await import('../../scripts/update-docs-sha');
23+
const { updateDocsCommit } = await import('../../scripts/update-docs');
2424
expect(typeof updateDocsCommit).toBe('function');
2525
});
2626
});

0 commit comments

Comments
 (0)