Skip to content

Commit 525f1d3

Browse files
committed
feat: add v2/git and v2/status api
1 parent 4ac60f3 commit 525f1d3

14 files changed

Lines changed: 667 additions & 22 deletions

package-lock.json

Lines changed: 35 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/api/v2/git.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import Debug from 'debug'
2+
import { Response } from 'express'
3+
import { BadRequestError } from 'src/error'
4+
import { lockApi } from 'src/middleware/session'
5+
import { OpenApiRequestExt } from 'src/otomi-models'
6+
7+
const debug = Debug('otomi:api:v2:git')
8+
9+
/**
10+
* PUT /v2/git
11+
* Migrate the values repository to a new git remote.
12+
* Flow:
13+
* 1. Validate connectivity to new remote (400 on failure)
14+
* 2. Write + commit git config locally, push to new remote first (so new remote has correct config),
15+
* then push to current remote (so operator picks up the switch)
16+
* 3. Lock API
17+
*/
18+
export const migrateGit = async (req: OpenApiRequestExt, res: Response): Promise<void> => {
19+
debug('migrateGit')
20+
const { repoUrl, username, password, email, branch } = req.body as {
21+
repoUrl: string
22+
username?: string
23+
password: string
24+
email: string
25+
branch: string
26+
}
27+
28+
// Validate new remote connectivity; returns true if remote already has content
29+
let remoteHasContent: boolean
30+
try {
31+
remoteHasContent = await req.otomi.git.testRemoteConnection(repoUrl, password, username)
32+
} catch (e: any) {
33+
throw new BadRequestError(`Cannot connect to new git remote: ${e.message}`)
34+
}
35+
36+
// Write config + commit locally → push to new remote (if empty) → push to current remote
37+
await req.otomi.migrateGitSettings({ repoUrl, username, password, email, branch, remoteHasContent })
38+
39+
await lockApi()
40+
41+
res.sendStatus(200)
42+
}

src/api/v2/status.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import Debug from 'debug'
2+
import { Response } from 'express'
3+
import { OpenApiRequestExt } from 'src/otomi-models'
4+
5+
const debug = Debug('otomi:api:v2:status')
6+
7+
/**
8+
* GET /v2/status
9+
* Returns the current API status including the locked state.
10+
*/
11+
export const getApiStatus = (req: OpenApiRequestExt, res: Response): void => {
12+
debug('getApiStatus')
13+
res.json(req.otomi.getApiStatus())
14+
}

src/error.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ export class BadRequestError extends OtomiError {
7070
this.code = 400
7171
}
7272
}
73+
export class ApiLockedError extends OtomiError {
74+
public constructor(err?: string) {
75+
super('Api is locked. A git migration has completed. No further changes are accepted.', err)
76+
this.code = 503
77+
}
78+
}
79+
7380
export class HttpError extends OtomiError {
7481
protected static messages: Record<number, string> = {
7582
400: 'Bad Request',

0 commit comments

Comments
 (0)