-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgitea-admin.service.ts
More file actions
92 lines (84 loc) · 2.25 KB
/
gitea-admin.service.ts
File metadata and controls
92 lines (84 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import { createComposeCommandArgs } from '../lib/compose.js';
import type { BootstrapEnv, ProjectContext } from '../types/project.types.js';
import { printInfo } from '../ui/logger.js';
import { runCommand } from '../utils/process.js';
const GITEA_CONFIG = '/data/gitea/conf/app.ini';
/**
* Waits for Gitea and ensures the configured root user exists with the right password.
*/
export async function ensureGiteaAdmin(
context: ProjectContext,
env: BootstrapEnv
): Promise<'created' | 'updated'> {
const baseArgs = createComposeCommandArgs(context, [
'exec',
'-T',
'--user',
`${env.GITEA_UID}:${env.GITEA_GID}`,
'gitea',
'gitea',
'admin',
'user'
]);
const listing = await runCommand('docker', [...baseArgs, 'list', '--config', GITEA_CONFIG], {
captureOutput: true,
cwd: context.projectRoot,
scope: 'bootstrap'
});
if (parseGiteaUsernames(listing.stdout).includes(env.GITEA_ROOT_USERNAME)) {
printInfo(`Gitea root user '${env.GITEA_ROOT_USERNAME}' already exists. Updating password.`, 'bootstrap');
await runCommand(
'docker',
[
...baseArgs,
'change-password',
'--config',
GITEA_CONFIG,
'--username',
env.GITEA_ROOT_USERNAME,
'--password',
env.GITEA_ROOT_PASSWORD
],
{
cwd: context.projectRoot,
scope: 'bootstrap'
}
);
return 'updated';
}
printInfo(`Gitea root user '${env.GITEA_ROOT_USERNAME}' not found. Creating it.`, 'bootstrap');
await runCommand(
'docker',
[
...baseArgs,
'create',
'--config',
GITEA_CONFIG,
'--username',
env.GITEA_ROOT_USERNAME,
'--password',
env.GITEA_ROOT_PASSWORD,
'--email',
env.GITEA_ROOT_EMAIL,
'--admin',
'--must-change-password=false'
],
{
cwd: context.projectRoot,
scope: 'bootstrap'
}
);
return 'created';
}
/**
* Parses the `gitea admin user list` table into exact usernames.
*/
export function parseGiteaUsernames(stdout: string): string[] {
return stdout
.split(/\r?\n/gu)
.map((line) => line.trim())
.filter((line) => line.length > 0)
.slice(1)
.map((line) => line.split(/\s+/gu)[1] ?? '')
.filter((username) => username.length > 0);
}