Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/src/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export async function createDatabaseDriver(url: string): Promise<DatabaseDriver>
mkdirSync(dirname(dataDir), { recursive: true });
}
const pglite = new PGlite(dataDir);
await pglite.exec("SET TIME ZONE 'UTC'");
const db = drizzle(pglite, { schema });
return {
db,
Expand All @@ -67,6 +68,7 @@ export async function createDatabaseDriver(url: string): Promise<DatabaseDriver>
max: Number(process.env.DB_POOL_MAX) || 10,
connectionTimeoutMillis: Number(process.env.DB_CONNECTION_TIMEOUT_MS) || 30_000,
idleTimeoutMillis: Number(process.env.DB_IDLE_TIMEOUT_MS) || 30_000,
options: "-c TimeZone=UTC",
});
pool.on("error", (err: Error) => {
console.error("[Database] Unexpected pool error:", err.message);
Expand Down
13 changes: 11 additions & 2 deletions api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ import { createTokensService } from "./services/tokens";
import { createTreasuryService } from "./services/treasury";
import { createConsistencyChecker } from "./services/work-orders/consistency";
import { createDocumenso } from "./services/work-orders/documenso";
import { cachedOwnership, createGitHub } from "./services/work-orders/github";
import { cachedByName, createGitHub } from "./services/work-orders/github";
import {
createGoogle,
GoogleError,
Expand Down Expand Up @@ -361,7 +361,8 @@ export default createPlugin.withPlugins<PluginsClient>()({
},
},
github,
ownership: cachedOwnership(github, 10 * 60_000),
ownership: cachedByName((repo) => github.getRepoOwnership(repo), 10 * 60_000),
orgRepos: cachedByName((org) => github.listOrgRepos(org), 10 * 60_000),
proposal: (daoAccountId, proposalId) => findProposal(db, daoAccountId, proposalId),
proposalBond: getProposalBond,
prices: createPriceFeed(),
Expand Down Expand Up @@ -1184,6 +1185,14 @@ export default createPlugin.withPlugins<PluginsClient>()({
.use(agencyManager)
.handler(async ({ context }) => workOrders.nextCode(context.scope)),

githubRepos: builder.workOrders.githubRepos
.use(agencyManager)
.handler(async ({ context, input }) => workOrders.githubRepos(context.scope, input)),

builderDefaults: builder.workOrders.builderDefaults
.use(agencyManager)
.handler(async ({ context, input }) => workOrders.builderDefaults(context.scope, input)),

offer: builder.workOrders.offer
.use(agencyManager)
.handler(async ({ context, input }) => workOrders.offer(context.scope, input)),
Expand Down
55 changes: 47 additions & 8 deletions api/src/services/work-orders/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ export type GhReviewBy = {
submittedAt: string;
};

export type GhRepo = {
fullName: string;
url: string;
description: string | null;
pushedAt: string | null;
};

export interface GitHubPort {
getPull(repo: string, number: number): Promise<GhPull>;
getClosingIssues(repo: string, number: number): Promise<GhIssueRef[]>;
Expand All @@ -82,6 +89,7 @@ export interface GitHubPort {
getAssignmentEvents(repo: string, number: number): Promise<GhAssignmentEvent[]>;
listPullsByAuthor(repo: string, login: string, since: string): Promise<GhPullSummary[]>;
listReviewsBy(repo: string, login: string, since: string): Promise<GhReviewBy[]>;
listOrgRepos(org: string): Promise<GhRepo[]>;
}

export class GitHubError extends Error {
Expand Down Expand Up @@ -137,6 +145,15 @@ type RestSearchItem = {
user?: { login: string } | null;
};

type RestRepo = {
full_name: string;
html_url: string;
description: string | null;
pushed_at: string | null;
archived?: boolean;
private?: boolean;
};

type RestReview = { user: { login: string } | null; state: string; submitted_at?: string | null };

type ClosingData = {
Expand Down Expand Up @@ -374,18 +391,40 @@ export function createGitHub(token: string | null, fetcher: typeof fetch = fetch
}
return out;
},

async listOrgRepos(org) {
const repos = await pages<RestRepo>(
`/orgs/${encodeURIComponent(org)}/repos?type=public&sort=pushed`,
);
return repos
.filter((r) => !r.archived && !r.private)
.map((r) => ({
fullName: r.full_name,
url: r.html_url,
description: r.description,
pushedAt: r.pushed_at,
}));
},
};
}

export function cachedOwnership(github: GitHubPort, ttlMs: number, now: () => number = Date.now) {
const cache = new Map<string, { until: number; value: Promise<GhRepoOwnership> }>();
return (repo: string): Promise<GhRepoOwnership> => {
const key = repo.toLowerCase();
export function cachedByName<T>(
load: (name: string) => Promise<T>,
ttlMs: number,
now: () => number = Date.now,
) {
const cache = new Map<string, { until: number; value: Promise<T> }>();
return (name: string): Promise<T> => {
const key = name.toLowerCase();
const at = now();
const hit = cache.get(key);
if (hit && hit.until > now()) return hit.value;
const value = github.getRepoOwnership(repo);
cache.set(key, { until: now() + ttlMs, value });
value.catch(() => cache.delete(key));
if (hit && hit.until > at) return hit.value;
for (const [stale, entry] of cache) if (entry.until <= at) cache.delete(stale);
const value = load(name);
cache.set(key, { until: at + ttlMs, value });
value.catch(() => {
if (cache.get(key)?.value === value) cache.delete(key);
});
return value;
};
}
Expand Down
8 changes: 0 additions & 8 deletions api/src/services/work-orders/progress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ export type ProgressInput = {
representativeUrl: string | null;
esignError: string | null;
blockers: string[];
payeeNeedsConfirmation: boolean;
documentWarning: string | null;
driveError: string | null;
renewalStatus: "renewed" | "needs_approval" | "declined" | null;
Expand Down Expand Up @@ -372,13 +371,6 @@ export function progressOf(input: ProgressInput): Progress {
});
}
}
if (input.payeeNeedsConfirmation && !terminated && input.status !== "draft") {
attention.push({
key: "payee",
message: `${builder} hasn't confirmed their payout account yet`,
action: null,
});
}
if (input.documentWarning) {
attention.push({ key: "document", message: input.documentWarning, action: null });
}
Expand Down
Loading
Loading