Skip to content

Commit 703ef55

Browse files
committed
fix: changelogs tweaks
1 parent 4ae046d commit 703ef55

8 files changed

Lines changed: 52 additions & 34 deletions

File tree

apps/changelog-api/src/typed-handlers/changelog-handlers.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as schema from "@asyncstatus/db";
22
import { TypedHandlersError, typedHandler } from "@asyncstatus/typed-handlers";
3-
import { and, desc, eq } from "drizzle-orm";
3+
import { and, desc, eq, or } from "drizzle-orm";
44
import { nanoid } from "nanoid";
55
import type { TypedHandlersContext } from "../lib/env";
66
import { getCommitRange, getDateRange } from "../lib/filters";
@@ -133,7 +133,10 @@ export const startChangelogGenerationHandler = typedHandler<
133133
const existingJob = await db.query.changelogGenerationJob.findFirst({
134134
where: and(
135135
eq(schema.changelogGenerationJob.inputUrl, input.url.toString()),
136-
eq(schema.changelogGenerationJob.state, "queued"),
136+
or(
137+
eq(schema.changelogGenerationJob.state, "queued"),
138+
eq(schema.changelogGenerationJob.state, "running"),
139+
),
137140
),
138141
});
139142
if (existingJob) {

apps/changelog-api/src/workflows/changelogs/generate-changelog-content.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ import { getChangelogJobTool } from "../tools/get-changelog-job-tool";
99
import { getGithubCommitChangesTool } from "../tools/get-github-commit-changes-tool";
1010
import { getGithubFileTool } from "../tools/get-github-file-tool";
1111
import { listChangelogEventsTool } from "../tools/list-changelog-events-tool";
12-
import { listGithubCommitsTool } from "../tools/list-github-commits-tool";
13-
import { listGithubIssuesTool } from "../tools/list-github-issues-tool";
14-
import { listGithubPullRequestsTool } from "../tools/list-github-pull-requests-tool";
15-
import { listGithubReleasesTool } from "../tools/list-github-releases-tool";
16-
import { listGithubTagsTool } from "../tools/list-github-tags-tool";
1712
import { systemPrompt } from "./system-prompt";
1813

1914
export type GenerateChangelogContentOptions = {
@@ -59,11 +54,12 @@ Return Markdown content only.`,
5954
messages,
6055
toolChoice: "auto",
6156
tools: {
62-
listGithubCommits: listGithubCommitsTool(octokit),
63-
listGithubPullRequests: listGithubPullRequestsTool(octokit),
64-
listGithubIssues: listGithubIssuesTool(octokit),
65-
listGithubTags: listGithubTagsTool(octokit),
66-
listGithubReleases: listGithubReleasesTool(octokit),
57+
// @TODO: those should be available in events either way
58+
// listGithubCommits: listGithubCommitsTool(octokit),
59+
// listGithubPullRequests: listGithubPullRequestsTool(octokit),
60+
// listGithubIssues: listGithubIssuesTool(octokit),
61+
// listGithubTags: listGithubTagsTool(octokit),
62+
// listGithubReleases: listGithubReleasesTool(octokit),
6763
compareGithubCommits: compareGithubCommitsTool(octokit),
6864
getGithubCommitChanges: getGithubCommitChangesTool(octokit),
6965
getGithubFile: getGithubFileTool(octokit),

apps/changelog-api/src/workflows/github/changelog-generation-job.ts

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ export class ChangelogGenerationJobWorkflow extends WorkflowEntrypoint<
3838
throw new Error("Changelog generation job not found");
3939
}
4040

41+
const basicAuth = btoa(`${this.env.GITHUB_CLIENT_ID}:${this.env.GITHUB_CLIENT_SECRET}`);
42+
const AuthenticatedOctokit = Octokit.defaults({
43+
request: { headers: { authorization: `Basic ${basicAuth}` } },
44+
});
45+
4146
const owner = job.repoOwner;
4247
const repo = job.repoName;
4348
const since = job.rangeStart ?? undefined;
@@ -57,12 +62,13 @@ export class ChangelogGenerationJobWorkflow extends WorkflowEntrypoint<
5762
{ retries: { limit: 3, delay: 30, backoff: "exponential" }, timeout: 30000 },
5863
async () => {
5964
const db = createDb(this.env);
60-
const octokit = new Octokit();
65+
const octokit = new AuthenticatedOctokit();
6166
const report = createReportStatusFn({
6267
db,
6368
jobId,
6469
status: "Syncing repositories",
6570
statusOnError: "Failed to sync repositories",
71+
canSkip: true,
6672
});
6773
await report(() => fetchAndSyncRepositories({ octokit, db, owner }));
6874
},
@@ -73,13 +79,14 @@ export class ChangelogGenerationJobWorkflow extends WorkflowEntrypoint<
7379
{ retries: { limit: 3, delay: 30, backoff: "exponential" }, timeout: 30000 },
7480
async () => {
7581
const db = createDb(this.env);
76-
const octokit = new Octokit();
82+
const octokit = new AuthenticatedOctokit();
7783

7884
const report = createReportStatusFn({
7985
db,
8086
jobId,
8187
status: "Syncing contributors",
8288
statusOnError: "Failed to sync contributors",
89+
canSkip: true,
8390
});
8491
await report(() => fetchAndSyncContributors({ octokit, db, owner, repo }));
8592
},
@@ -90,12 +97,13 @@ export class ChangelogGenerationJobWorkflow extends WorkflowEntrypoint<
9097
{ retries: { limit: 3, delay: 30, backoff: "exponential" }, timeout: 30000 },
9198
async () => {
9299
const db = createDb(this.env);
93-
const octokit = new Octokit();
100+
const octokit = new AuthenticatedOctokit();
94101
const report = createReportStatusFn({
95102
db,
96103
jobId,
97104
status: "Syncing commits",
98105
statusOnError: "Failed to sync commits",
106+
canSkip: true,
99107
});
100108
await report(() =>
101109
fetchAndSyncCommits({ octokit, db, owner, repo, since, until, headSha }),
@@ -108,12 +116,13 @@ export class ChangelogGenerationJobWorkflow extends WorkflowEntrypoint<
108116
{ retries: { limit: 3, delay: 30, backoff: "exponential" }, timeout: 30000 },
109117
async () => {
110118
const db = createDb(this.env);
111-
const octokit = new Octokit();
119+
const octokit = new AuthenticatedOctokit();
112120
const report = createReportStatusFn({
113121
db,
114122
jobId,
115123
status: "Syncing tags",
116124
statusOnError: "Failed to sync tags",
125+
canSkip: true,
117126
});
118127
await report(() => fetchAndSyncTags({ octokit, db, owner, repo }));
119128
},
@@ -124,12 +133,13 @@ export class ChangelogGenerationJobWorkflow extends WorkflowEntrypoint<
124133
{ retries: { limit: 3, delay: 30, backoff: "exponential" }, timeout: 30000 },
125134
async () => {
126135
const db = createDb(this.env);
127-
const octokit = new Octokit();
136+
const octokit = new AuthenticatedOctokit();
128137
const report = createReportStatusFn({
129138
db,
130139
jobId,
131140
status: "Syncing releases",
132141
statusOnError: "Failed to sync releases",
142+
canSkip: true,
133143
});
134144
await report(() => fetchAndSyncReleases({ octokit, db, owner, repo }));
135145
},
@@ -140,12 +150,13 @@ export class ChangelogGenerationJobWorkflow extends WorkflowEntrypoint<
140150
{ retries: { limit: 3, delay: 30, backoff: "exponential" }, timeout: 30000 },
141151
async () => {
142152
const db = createDb(this.env);
143-
const octokit = new Octokit();
153+
const octokit = new AuthenticatedOctokit();
144154
const report = createReportStatusFn({
145155
db,
146156
jobId,
147157
status: "Syncing pull requests",
148158
statusOnError: "Failed to sync pull requests",
159+
canSkip: true,
149160
});
150161
await report(() => fetchAndSyncPullRequests({ octokit, db, owner, repo, since }));
151162
},
@@ -156,12 +167,13 @@ export class ChangelogGenerationJobWorkflow extends WorkflowEntrypoint<
156167
{ retries: { limit: 3, delay: 30, backoff: "exponential" }, timeout: 30000 },
157168
async () => {
158169
const db = createDb(this.env);
159-
const octokit = new Octokit();
170+
const octokit = new AuthenticatedOctokit();
160171
const report = createReportStatusFn({
161172
db,
162173
jobId,
163174
status: "Syncing issues",
164175
statusOnError: "Failed to sync issues",
176+
canSkip: true,
165177
});
166178
await report(() => fetchAndSyncIssues({ octokit, db, owner, repo, since }));
167179
},
@@ -177,6 +189,7 @@ export class ChangelogGenerationJobWorkflow extends WorkflowEntrypoint<
177189
jobId,
178190
status: "Linking PRs and issues",
179191
statusOnError: "Failed to link PRs and issues",
192+
canSkip: true,
180193
});
181194
await report(() => linkPrsAndIssues({ db, owner, repo }));
182195
},
@@ -188,12 +201,13 @@ export class ChangelogGenerationJobWorkflow extends WorkflowEntrypoint<
188201
async () => {
189202
const db = createDb(this.env);
190203
const openRouterProvider = createOpenRouter({ apiKey: this.env.OPENROUTER_API_KEY });
191-
const octokit = new Octokit();
204+
const octokit = new AuthenticatedOctokit();
192205
const report = createReportStatusFn({
193206
db,
194207
jobId,
195208
status: "Generating changelog",
196209
statusOnError: "Failed to generate changelog",
210+
canSkip: false,
197211
});
198212
await report(async () => {
199213
const changelogContent = await generateChangelogContent({

apps/changelog-api/src/workflows/github/steps/common.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ type CreateReportStatusFnParams = {
77
jobId: string;
88
status: string;
99
statusOnError: string;
10+
canSkip?: boolean;
1011
};
1112

1213
export function createReportStatusFn({
1314
db,
1415
jobId,
1516
status,
1617
statusOnError,
18+
canSkip = false,
1719
}: CreateReportStatusFnParams) {
1820
return async (fn: () => Promise<void>) => {
1921
await db
@@ -32,6 +34,7 @@ export function createReportStatusFn({
3234
.set({ updatedAt: new Date() })
3335
.where(eq(schema.changelogGenerationJob.id, jobId));
3436
} catch (error) {
37+
console.error(error);
3538
await db
3639
.update(schema.changelogGenerationJob)
3740
.set({
@@ -42,6 +45,9 @@ export function createReportStatusFn({
4245
metadata: { humanReadableStatus: statusOnError },
4346
})
4447
.where(eq(schema.changelogGenerationJob.id, jobId));
48+
if (canSkip) {
49+
return;
50+
}
4551
throw error;
4652
}
4753
};

apps/changelog-api/src/workflows/github/steps/fetch-and-sync-contributors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export async function fetchAndSyncContributors({
1919
}: FetchAndSyncContributorsParams) {
2020
for await (const contributors of octokit.paginate.iterator(
2121
"GET /repos/{owner}/{repo}/contributors",
22-
{ owner, repo, per_page: 100 },
22+
{ owner, repo, per_page: 10 },
2323
)) {
2424
const now = new Date();
2525

apps/changelog-api/src/workflows/github/steps/fetch-and-sync-repositories.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ export async function fetchAndSyncRepositories({
2020
db,
2121
owner,
2222
}: FetchAndSyncRepositoriesParams) {
23-
const user = await octokit.rest.users.getByUsername({ username: owner });
24-
const isOrg = user.data.type === "Organization";
23+
// @TODO: this call has really low rate limits, so we're just assuming it's an org for now
24+
// const user = await octokit.rest.users.getByUsername({ username: owner });
25+
const isOrg = true;
2526

2627
const baseParams = {
2728
per_page: 50,

apps/changelog-api/src/workflows/tools/list-changelog-events-tool.ts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as schema from "@asyncstatus/db";
22
import type { Db } from "@asyncstatus/db/create-db";
33
import { tool } from "ai";
4-
import { and, asc, between, desc, eq, gte, lte } from "drizzle-orm";
4+
import { and, asc, between, desc, eq, gte, inArray, lte } from "drizzle-orm";
55
import { z } from "zod";
66

77
export function listChangelogEventsTool(db: Db) {
@@ -50,7 +50,7 @@ export function listChangelogEventsTool(db: Db) {
5050
}
5151

5252
if (params.sourceTypes && params.sourceTypes.length > 0) {
53-
// simple OR using IN is not defined here; we can filter post-query for simplicity
53+
conditions.push(inArray(schema.changelogGithubEvent.sourceType, params.sourceTypes));
5454
}
5555

5656
const rows = await db
@@ -79,15 +79,7 @@ export function listChangelogEventsTool(db: Db) {
7979
)
8080
.limit(params.limit);
8181

82-
const filtered = params.sourceTypes
83-
? rows.filter((r) =>
84-
(params.sourceTypes ?? []).includes(
85-
r.sourceType as (typeof params.sourceTypes)[number],
86-
),
87-
)
88-
: rows;
89-
90-
return filtered;
82+
return rows;
9183
},
9284
});
9385
}

apps/changelog-app/src/routes/$owner/$repo/$filters/index.tsx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ export const Route = createFileRoute("/$owner/$repo/$filters/")({
5757
(changelogGenerationJobs.length === 0 ||
5858
changelogGenerationJobs.some((job) => job.state === "error"))
5959
) {
60+
if (
61+
changelogGenerationJobs.some((job) => job.state === "running" || job.state === "queued")
62+
) {
63+
return { changelogs, changelogGenerationJobs };
64+
}
65+
6066
const nextChangelogGenerationJob = await typedContractFetch(
6167
startChangelogGenerationContract,
6268
{

0 commit comments

Comments
 (0)