Skip to content

Commit

Permalink
Merge pull request #110 from gov4git/fix-init
Browse files Browse the repository at this point in the history
Fix initializing id repos
  • Loading branch information
dworthen committed Feb 29, 2024
2 parents 1d804e7 + a0767a4 commit 14747fc
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 29 deletions.
7 changes: 7 additions & 0 deletions .changeset/unlucky-horses-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'gov4git-desktop-app': patch
---

Fix initializing id repos

- Addresses #109
11 changes: 9 additions & 2 deletions src/electron/services/Gov4GitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,14 +113,17 @@ export class Gov4GitService {
command: string[],
community?: Community,
skipConfig = false,
configPath = '',
): Promise<T> => {
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')
Expand Down Expand Up @@ -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,
Expand Down
59 changes: 34 additions & 25 deletions src/electron/services/SettingsService.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -22,33 +23,21 @@ export class SettingsService {

public generateConfig = async (
user: User,
community: Community,
): Promise<ServiceResponse<null>> => {
const config = {
community?: Community,
): Promise<ServiceResponse<string>> => {
let config: Record<string, any> = {
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,
},
Expand All @@ -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,
}
}

Expand Down
17 changes: 15 additions & 2 deletions src/electron/services/UserService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand All @@ -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>('db')
this.govService = this.services.load<Gov4GitService>('gov4git')
this.gitHubService = this.services.load<GitHubService>('github')
this.settingsService = this.services.load<SettingsService>('settings')
this.identityRepoName = identityRepoName
Expand All @@ -49,11 +54,19 @@ export class UserService extends AbstractUserService {
])
}

public getUser = async (): Promise<User | null> => {
public getUser = serialAsync(async (): Promise<User | null> => {
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.
Expand Down

0 comments on commit 14747fc

Please sign in to comment.