Skip to content

Commit

Permalink
Merge pull request #2 from specialblend/diff-before-publish
Browse files Browse the repository at this point in the history
compare local & remote comment body before updating
  • Loading branch information
specialblend authored Jan 2, 2025
2 parents bd49ce3 + 2a42e0b commit a82fae0
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 31 deletions.
51 changes: 23 additions & 28 deletions karen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,7 @@ export async function main() {
} = {},
) {
const issueStore = IssueStore(storage);
const reportStore = ReportStore(storage);
const reporting = ReportingService(storage, settings);

if (options.all) return await reportAll(options);
Expand All @@ -853,19 +854,20 @@ export async function main() {
const issue = await issueStore
.get(key)
.catch(console.expect("Issue not found"));
const report = await reporting.collect(issue, options);
if (["markdown", "jira"].includes(options.format ?? "markdown")) {
const markdown = await reporting.format(report, {
format: options.format ?? "markdown",
});
console.log(markdown);
} else {
console.print(report, options.format);
}

if (options.publish) {
await reporting.publish(report);
console.info(Fmt.green("Report published"));
return await reportIssue(issue, options);

async function reportAll(
options: {
publish?: boolean;
force?: boolean;
format?: string;
model?: string;
} = {},
) {
const issues = issueStore.list();
for await (const issue of issues) await reportIssue(issue, options);
return console.info("done");
}

async function reportIssue(
Expand All @@ -875,32 +877,25 @@ export async function main() {
format?: string;
force?: boolean;
model?: string;
details?: boolean;
} = {},
) {
console.log("Collecting report for", issue.key);
const report = await reporting.collect(issue, options);
if (options.publish) {
await reporting.publish(report);
return console.info(Fmt.green(`Published report for ${issue.key}`));
const published = await reporting.publish(report);
if (published) {
console.info(Fmt.green(`Published ${issue.key}`));
} else {
console.info(Fmt.yellow(`Already published ${issue.key}`));
}
}
if (options.format === "markdown") {
const markdown = await reporting.format(report, { format: "markdown" });
return console.log(markdown);
}
return console.print(report, options.format);
}

async function reportAll(
options: {
publish?: boolean;
force?: boolean;
format?: string;
model?: string;
} = {},
) {
const issues = issueStore.list();
for await (const issue of issues) await reportIssue(issue, options);
return console.info("done");
if (options.details) return console.print(report, options.format);
return console.print(reportStore.summarize(report), options.format);
}
}

Expand Down
29 changes: 27 additions & 2 deletions src/Issue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,23 @@ export function IssueService(storage: Deno.Kv) {
await response.text();
}

async function getComment(issue: Issue, id: string): Promise<Comment> {
const baseUrl = await getBaseUrl();
const headers = await getHeaders();
const url = new URL(
`/rest/api/2/issue/${issue.key}/comment/${id}`,
baseUrl,
);
const request = new Request(url, {
method: "GET",
headers,
});
const response = await fetch(request);
if (!response.ok) throw response;
const comment = await response.json().then(fmtComment);
return comment;
}

async function postComment(issue: Issue, body: string): Promise<Comment> {
const baseUrl = await getBaseUrl();
const headers = await getHeaders();
Expand Down Expand Up @@ -309,7 +326,15 @@ export function IssueService(storage: Deno.Kv) {
const cached = await myCommentsStore
.get(issue.key)
.catch(() => null);
if (cached) return await updateComment(issue, cached, body);
return await postComment(issue, body);
if (cached) {
const remote = await getComment(issue, cached.id);
if (body && remote.body && remote.body !== body) {
await updateComment(issue, cached, body);
return true;
}
return false;
}
await postComment(issue, body);
return true;
}
}
2 changes: 1 addition & 1 deletion src/Reporting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function ReportingService(storage: Deno.Kv, settings: SettingsV1) {

async function publish(report: Report) {
const text = await format(report, { format: "jira" });
await issueService.upsertComment(report.issue, text);
return await issueService.upsertComment(report.issue, text);
}

async function format(report: Report, options: { format: string }) {
Expand Down

0 comments on commit a82fae0

Please sign in to comment.