diff --git a/.changeset/unlucky-horses-sneeze.md b/.changeset/unlucky-horses-sneeze.md new file mode 100644 index 0000000..d1ec647 --- /dev/null +++ b/.changeset/unlucky-horses-sneeze.md @@ -0,0 +1,7 @@ +--- +'gov4git-desktop-app': patch +--- + +Fix initializing id repos + +- Addresses #109 diff --git a/src/electron/services/Gov4GitService.ts b/src/electron/services/Gov4GitService.ts index c1b29fa..7fe43a1 100644 --- a/src/electron/services/Gov4GitService.ts +++ b/src/electron/services/Gov4GitService.ts @@ -113,14 +113,17 @@ export class Gov4GitService { command: string[], community?: Community, skipConfig = false, + configPath = '', ): Promise => { - if (!skipConfig) { + if (!skipConfig && configPath === '') { const configPathResponse = await this.loadConfigPath(community) if (!configPathResponse.ok) { throw new Error(`${configPathResponse.error}`) } else { command.push('--config', configPathResponse.data) } + } else if (configPath !== '') { + command.push('--config', configPath) } command.push('-v') @@ -171,7 +174,11 @@ export class Gov4GitService { if (isPublicEmpty || isPrivateEmpty) { try { - await this.mustRun(['init-id']) + const response = await this.settingsService.generateConfig(user) + if (!response.ok) { + return response + } + await this.mustRun(['init-id'], undefined, false, response.data) return { ok: true, statusCode: 201, diff --git a/src/electron/services/SettingsService.ts b/src/electron/services/SettingsService.ts index 5f22fbf..0a3eb7a 100644 --- a/src/electron/services/SettingsService.ts +++ b/src/electron/services/SettingsService.ts @@ -1,8 +1,9 @@ import { existsSync, mkdirSync, writeFileSync } from 'fs' -import { dirname } from 'path' +import { dirname, resolve } from 'path' import type { ServiceResponse } from '~/shared' +import { CONFIG_PATH } from '../configs.js' import { DB } from '../db/db.js' import { communities, Community, User, users } from '../db/schema.js' import { Services } from './Services.js' @@ -22,33 +23,21 @@ export class SettingsService { public generateConfig = async ( user: User, - community: Community, - ): Promise> => { - const config = { + community?: Community, + ): Promise> => { + let config: Record = { notice: 'Do not modify this file. It will be overwritten by Gov4Git application', - configPath: community.configPath, - community_name: community.name, - project_repo: community.projectUrl, user: { username: user.username, pat: user.pat, }, - gov_public_url: community.url, - gov_public_branch: community.branch, - gov_private_url: community.privateUrl, - gov_private_branch: community.branch, + member_public_url: user.memberPublicUrl, member_public_branch: user.memberPublicBranch, member_private_url: user.memberPrivateUrl, member_private_branch: user.memberPrivateBranch, auth: { - [community.url]: { - access_token: user.pat, - }, - [community.privateUrl]: { - access_token: user.pat, - }, [user.memberPublicUrl]: { access_token: user.pat, }, @@ -58,29 +47,49 @@ export class SettingsService { }, } + let configPath = resolve(CONFIG_PATH, 'user-config.json') + if (community != null) { + configPath = community.configPath + config = { + ...config, + configPath: community.configPath, + community_name: community.name, + project_repo: community.projectUrl, + gov_public_url: community.url, + gov_public_branch: community.branch, + gov_private_url: community.privateUrl, + gov_private_branch: community.branch, + auth: { + ...config['auth'], + [community.url]: { + access_token: user.pat, + }, + [community.privateUrl]: { + access_token: user.pat, + }, + }, + } + } + try { - const dir = dirname(community.configPath) + const dir = dirname(configPath) if (!existsSync(dir)) { mkdirSync(dir, { recursive: true }) } - writeFileSync( - community.configPath, - JSON.stringify(config, undefined, 2), - 'utf-8', - ) + writeFileSync(configPath, JSON.stringify(config, undefined, 2), 'utf-8') } catch (ex) { return { ok: false, statusCode: 500, - error: `Failed to write config file ${community.configPath}. ${ex}`, + error: `Failed to write config file ${configPath}. ${ex}`, } } return { ok: true, statusCode: 200, - data: null, + data: configPath, } } diff --git a/src/electron/services/UserService.ts b/src/electron/services/UserService.ts index 72cee81..d6b3350 100644 --- a/src/electron/services/UserService.ts +++ b/src/electron/services/UserService.ts @@ -6,6 +6,7 @@ import { AbstractUserService, serialAsync } from '~/shared' import { DB } from '../db/db.js' import { communities, motions, User, users } from '../db/schema.js' import { GitHubService } from './GitHubService.js' +import { Gov4GitService } from './Gov4GitService.js' import { Services } from './Services.js' import { SettingsService } from './SettingsService.js' @@ -20,14 +21,18 @@ export class UserService extends AbstractUserService { private declare readonly repoUrlBase: string private declare readonly db: DB private declare readonly settingsService: SettingsService + private declare readonly govService: Gov4GitService + private declare initialized: boolean constructor({ services, identityRepoName = 'gov4git-identity', }: UserServiceOptions) { super() + this.initialized = false this.services = services this.db = this.services.load('db') + this.govService = this.services.load('gov4git') this.gitHubService = this.services.load('github') this.settingsService = this.services.load('settings') this.identityRepoName = identityRepoName @@ -49,11 +54,19 @@ export class UserService extends AbstractUserService { ]) } - public getUser = async (): Promise => { + public getUser = serialAsync(async (): Promise => { const userInfo = (await this.db.select().from(users).limit(1))[0] + if (!this.initialized) { + const errors = await this.initializeIdRepos() + if (errors.length === 0) { + await this.govService.initId() + } + this.initialized = true + } + return userInfo ?? null - } + }) /** * Bypasses interactive user login flow and verification.