Skip to content

Commit

Permalink
Merge pull request #131 from gov4git/community-filter
Browse files Browse the repository at this point in the history
Filter out deleted communities
  • Loading branch information
dworthen authored Mar 19, 2024
2 parents fcd4a1e + 3ff310e commit 80ebe00
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 24 deletions.
7 changes: 7 additions & 0 deletions .changeset/pretty-pigs-brush.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'gov4git-desktop-app': patch
---

Filter out deleted communities

- Addresses #114, #128
53 changes: 52 additions & 1 deletion src/electron/services/CommunityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { DB } from '../db/db.js'
import {
communities,
type Community,
motions,
type Policy,
type User,
} from '../db/schema.js'
Expand Down Expand Up @@ -88,6 +89,48 @@ export class CommunityService extends AbstractCommunityService {
this.policyService = this.services.load<PolicyService>('policy')
}

private communityExists = async (
user: User,
community: Community,
): Promise<boolean> => {
const projectRepoSegments = urlToRepoSegments(community.projectUrl)
const projectRepoExists = await this.gitHubService.doesRepoExist({
repoName: projectRepoSegments.repo,
username: projectRepoSegments.owner,
token: user.pat,
})
if (!projectRepoExists) return false
const communityRepoSegments = urlToRepoSegments(community.url)
return await this.gitHubService.doesRepoExist({
repoName: communityRepoSegments.repo,
username: communityRepoSegments.owner,
token: user.pat,
})
}

public deleteCommunity = async (url: string) => {
await Promise.all([
this.db.delete(communities).where(eq(communities.url, url)),
this.db.delete(motions).where(eq(motions.communityUrl, url)),
])
}

private removeNonExistentCommunities = async (
user: User,
communityList: Community[],
): Promise<Community[]> => {
const newCommunityList: Community[] = []
for (const community of communityList) {
const exists = await this.communityExists(user, community)
if (!exists) {
await this.deleteCommunity(community.url)
} else {
newCommunityList.push(community)
}
}
return newCommunityList
}

public getCommunity = async (): Promise<Community | null> => {
const user = await this.userService.getUser()
if (user == null) return null
Expand All @@ -99,14 +142,22 @@ export class CommunityService extends AbstractCommunityService {
.limit(1)
)[0]
if (community == null) return null
if (!(await this.communityExists(user, community))) {
await this.deleteCommunity(community.url)
return null
}
return await this.syncCommunity(user, community)
}

public getCommunities = async () => {
const user = await this.userService.getUser()
if (user == null) return []
const allCommunities = await this.db.select().from(communities)
const syncCommunities = allCommunities.map((c) => {
const existingCommunities = await this.removeNonExistentCommunities(
user,
allCommunities,
)
const syncCommunities = existingCommunities.map((c) => {
return this.syncCommunity(user, c)
})
return await Promise.all(syncCommunities)
Expand Down
30 changes: 30 additions & 0 deletions src/electron/services/GitHubService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,36 @@ export class GitHubService {
)
}

public doesRepoExist = async ({
repoName,
username,
token,
}: GetRepoInfoArgs): Promise<boolean> => {
try {
await this.getRepoInfo({
repoName: repoName,
username: username,
token: token,
})
return true
} catch (ex: any) {
if (ex instanceof Error) {
try {
const error = JSON.parse(ex.message)
if ('status' in error && error.status === 404) {
return false
} else {
throw ex
}
} catch (error) {
throw ex
}
} else {
throw new Error(`${ex}`)
}
}
}

public getDefaultBranch = async (args: GetRepoInfoArgs) => {
try {
const response = await this.getRepoInfo(args)
Expand Down
28 changes: 5 additions & 23 deletions src/electron/services/ValidationService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,29 +96,11 @@ export class ValidationService extends AbstractValidationService {
): Promise<boolean> => {
const repoSegments = urlToRepoSegments(repoUrl)

try {
await this.gitHubService.getRepoInfo({
repoName: repoSegments.repo,
username: repoSegments.owner,
token: user.pat,
})
return true
} catch (ex: any) {
if (ex instanceof Error) {
try {
const error = JSON.parse(ex.message)
if ('status' in error && error.status === 404) {
return false
} else {
throw ex
}
} catch (error) {
throw ex
}
} else {
throw new Error(`${ex}`)
}
}
return this.gitHubService.doesRepoExist({
repoName: repoSegments.repo,
username: repoSegments.owner,
token: user.pat,
})
}

private validateIdRepos = async (
Expand Down

0 comments on commit 80ebe00

Please sign in to comment.