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
50 changes: 50 additions & 0 deletions coderhelm-jira/src/comments.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* The comments already on a ticket, as CoderHelm reads them when a run starts
* on it: context people gave before the work began.
*
* Jira returns a ticket's comments in its `comment` field (oldest first). The
* gateway keeps each one once, by its id, so sending the same comment with a
* later event changes nothing. It also leaves out the app's own comments (by
* the author's account type and name), so they are passed through as they are.
*/

/** The most comments sent with one ticket event: the newest ones. */
const MAX_COMMENTS = 20;

/**
* The most characters of comment bodies sent with one ticket event. The
* event also carries the ticket's images, and the whole request has to stay
* well inside what the gateway accepts. When the newest comments are longer
* than this in all, the oldest of them are left out.
*/
const MAX_TOTAL_CHARS = 200000;

/**
* @param {object} fields The ticket's fields, as the issue REST API returns them.
* @returns {{id: string, author: {displayName: string, accountType: string}, body: string}[]}
*/
function existingComments(fields) {
const all = (fields && fields.comment && fields.comment.comments) || [];
if (!Array.isArray(all)) return [];
const newest = all.filter((c) => c && c.id && c.body).slice(-MAX_COMMENTS);
const sent = newest.map((c) => ({
id: String(c.id),
author: {
displayName: (c.author && c.author.displayName) || "",
accountType: (c.author && c.author.accountType) || "",
},
// A comment body is a document (JSON); the gateway reads its words.
body: typeof c.body === "string" ? c.body : JSON.stringify(c.body),
}));
// Keep the newest comments that fit; a body is never cut in the middle
// (it is a JSON document, and half of one cannot be read).
let total = 0;
let from = sent.length;
while (from > 0 && total + sent[from - 1].body.length <= MAX_TOTAL_CHARS) {
total += sent[from - 1].body.length;
from -= 1;
}
return sent.slice(from);
}

module.exports = { existingComments, MAX_COMMENTS, MAX_TOTAL_CHARS };
10 changes: 8 additions & 2 deletions coderhelm-jira/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { fetch } = api;
const { storage } = api;
const { route } = api;
const { GATEWAY_URL } = require("./gateway");
const { existingComments } = require("./comments");

/** The parts of a Jira user CoderHelm keeps: account id, name, email. */
function jiraUser(u) {
Expand Down Expand Up @@ -134,7 +135,9 @@ exports.handler = async (event, context) => {
comment: {
id: comment.id,
body: commentBody,
author: { ...jiraUser(author), displayName: authorName },
// accountType tells a person ("atlassian") from an app ("app"): the
// gateway never reads an app's comment as a person's.
author: { ...jiraUser(author), displayName: authorName, accountType: author.accountType },
},
image_attachments: imageAttachments,
coderhelm: {
Expand All @@ -159,7 +162,7 @@ exports.handler = async (event, context) => {
let fields = {};
if (issue.id) {
try {
const res = await api.asApp().requestJira(route`/rest/api/3/issue/${issue.id}?fields=summary,description,labels,assignee,reporter,creator,project,attachment,issuetype`);
const res = await api.asApp().requestJira(route`/rest/api/3/issue/${issue.id}?fields=summary,description,labels,assignee,reporter,creator,project,attachment,issuetype,comment`);
if (res.ok) {
const full = await res.json();
fields = full.fields || {};
Expand Down Expand Up @@ -206,6 +209,9 @@ exports.handler = async (event, context) => {
},
},
image_attachments: imageAttachments,
// What people already wrote on the ticket: the run that starts reads it
// before it plans (the gateway keeps each comment once, by its id).
comments: existingComments(fields),
user: requester(event.atlassianId, fields),
coderhelm: {
repo_owner: repoOwner || undefined,
Expand Down
55 changes: 55 additions & 0 deletions coderhelm-jira/test/comments.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const { test } = require("node:test");
const assert = require("node:assert/strict");
const { existingComments, MAX_COMMENTS, MAX_TOTAL_CHARS } = require("../src/comments");

const doc = (text) => ({ type: "doc", version: 1, content: [{ type: "paragraph", content: [{ type: "text", text }] }] });

test("sends a ticket's comments with their id, author and body", () => {
const fields = {
comment: {
comments: [
{ id: "10001", author: { displayName: "Pat Doe", accountType: "atlassian" }, body: doc("Production only.") },
{ id: 10002, author: { displayName: "Coderhelm", accountType: "app" }, body: "Questions only a person can answer" },
],
},
};
const got = existingComments(fields);
assert.equal(got.length, 2);
assert.deepEqual(got[0].author, { displayName: "Pat Doe", accountType: "atlassian" });
assert.equal(got[0].id, "10001");
assert.equal(JSON.parse(got[0].body).content[0].content[0].text, "Production only.");
assert.equal(got[1].id, "10002");
assert.equal(got[1].author.accountType, "app");
assert.equal(got[1].body, "Questions only a person can answer");
});

test("a ticket without comments, or without the field, sends none", () => {
assert.deepEqual(existingComments({}), []);
assert.deepEqual(existingComments(undefined), []);
assert.deepEqual(existingComments({ comment: {} }), []);
assert.deepEqual(existingComments({ comment: { comments: "x" } }), []);
assert.deepEqual(existingComments({ comment: { comments: [null, { id: "1" }, { body: "no id" }] } }), []);
});

test("only the newest comments are sent", () => {
const comments = Array.from({ length: 50 }, (_, i) => ({ id: String(i), author: {}, body: "x" }));
const got = existingComments({ comment: { comments } });
assert.equal(got.length, MAX_COMMENTS);
assert.equal(got[got.length - 1].id, "49");
assert.deepEqual(got[0].author, { displayName: "", accountType: "" });
});

test("very long comments never make the event too large: the oldest are left out whole", () => {
const big = "x".repeat(MAX_TOTAL_CHARS / 2 + 10);
const comments = [
{ id: "1", author: {}, body: big },
{ id: "2", author: {}, body: big },
{ id: "3", author: {}, body: "short" },
];
const got = existingComments({ comment: { comments } });
assert.deepEqual(got.map((c) => c.id), ["2", "3"]);
assert.equal(got[0].body.length, big.length);
// One comment longer than the whole allowance is left out, not cut.
const huge = [{ id: "9", author: {}, body: "y".repeat(MAX_TOTAL_CHARS + 1) }];
assert.deepEqual(existingComments({ comment: { comments: huge } }), []);
});
1 change: 1 addition & 0 deletions docs/incidents.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pipeline still calls it there.
| an. `an_six_database_findings` | Security, Infrastructure showed six database findings, every one a one-command runbook with no pull request although the databases were in the team's code. The same CVE stood on an operating system update, an Aurora PostgreSQL patch and a MySQL upgrade. A database with a Terraform-generated identifier and an Aurora cluster defined through a module were "not found in code". A patch and an upgrade pending on one cluster were unrelated rows, an update on a second instance was dropped, and the account was read one page deep. | An advisory is attached only where it applies (service, engine, version range, kind of action, date); an action AWS publishes with no CVE list gets none. An action is named for what it means on its ARN. One database's pending actions are one ordered set of rows. The defining code is found from stack records, resource type and attributes, module calls and variable files, graded, and a guess never becomes a pull request. An update with a code equivalent is a pull request on the line that sets the value; one that cannot be expressed in code stays a complete runbook and says why. A change that would replace a database is refused. |
| ao. `ao_stopped_log_finding` | Two log findings of one production service stayed on the Logs page as Critical and New, each with "Create fix PR" and "Not started automatically: not sure the repo is ...; check it and start the fix", a day and a half after their error had stopped: 735 matching lines in one six-hour window, then none while the service wrote its usual volume. The team had fixed the cause the day before. Only time closed a finding (three days without a line), so a problem that was over read as a live emergency that needed a person. | Every analysis counts each open finding's own lines on its own log group and keeps the count with the lines the group wrote. Two analyses in a row with no matching line at the service's usual volume make the finding stopped: shown and counted as stopped, never New or a live critical, with no automatic fix and no stale "choose a repo" reason. When the service itself went quiet nobody can tell, and it stays as it was. A burst that stopped after a change in its repo resolves after one day instead of three, with that evidence in the reason; an error that keeps coming back keeps its longer period. Seen again, it is live again and its fix may start. Evidence for the repo that couldn't be weighed is weighed again next sweep and never put to a person. |
| ap. `ap_closed_pr_keeps_findings` | An admin closed CoderHelm's dependency fix pull request for a repository to start again and cancelled its fix run. On the Security page every checkbox for that repository was off, the header's too, with nothing to say why: 274 findings still read "in a pull request" a day later. The pull request webhook recorded the close on the run and never told the findings, cancelling a run wrote only the run, and the one comparison with GitHub was the last step of the scheduled sweep, behind the time every team's sources and triage left, reading a failed request as "still open". | A finding is `pr_open` only while its pull request is open and `fixing` only while its run works (`common::security_release`). A pull request closed unmerged gives its findings back with who closed it and when, a cancelled or closed run releases what it claimed, a reopened pull request takes its findings back, and a merge fixes them: on the webhook or the person's action, and again on every sweep, first for each team and on a clock of its own, bounded and in turn, so a missed webhook and records stale from before heal within a sweep. A person asking for a fix always gets one; automatic fixes wait a week after a person said no. Retry on the run is a new request from the findings as they are now and opens a new pull request. GitHub not answering is never read as open, closed or merged. |
| aq. `aq_comment_during_run` | A run asked three questions on its ticket, proposed defaults and kept building. A person answered on the ticket twenty minutes later; the gateway logged that a run was in progress and dropped the comment. Nothing acknowledged it, the run never saw it, and its notes kept saying it went on without the answers. Comments written before any run existed were dropped too, and nothing read them when the run started. | A person who writes on a ticket CoderHelm is working on gets an acknowledgement, and what they wrote is acted on. The comment is kept once per comment id and acknowledged once (a reaction on GitHub, one short reply in Jira; never for CoderHelm's own comments or other bots). The run reads its open comments together at its next step boundary: answers and instructions go into the plan while it is being made; once the plan is final they become one round on the pull request when the step ends, where the code changes in the place the plan put the choice, and the ticket is told; a comment that says to stop ends the run at once. A comment nobody read yet stays open for what comes next (the next step, the round on the finished run's pull request, the ticket started again, or the next run), and comments written before a run started are read at its first boundary. |
| ar. `ar_description_stamped_to_the_head` | A dependency run's pull request was described from a head, and the head then moved on without it. A feedback round changed an override in the manifest and the description was rebuilt on that commit, where the lockfile still resolved the old version ("not moved on the head"); the lockfile was regenerated in the commits after it and the run completed. The description still said not moved, still counted the advisory as not fixed, still listed the package under open findings and "the lockfile is out of sync" under Open items, and CoderHelm's own review of the final head left that as a note. The checks step on an open pull request never rebuilt the computed sections, a regenerated lockfile was pushed with nothing after it, a review wrote its findings into the copy of the description it had read when it started (undoing a rebuild made meanwhile), and nits on CoderHelm's own pull request ended the loop. | The description names the head it was built for, and whenever that is not the pull request's head it is rebuilt: after every message in which CoderHelm moved the branch (seen in the one place every request leaves the GitHub client), in the checks and settle step, before a review of its own pull request, in the pull request watch, and at the end of the run, which never completes while the two differ. A review writes on the description as it is now and only for the head it reviewed. An item that says a lockfile is out of sync is checked on the head, never carried. A finding that the description is behind the code is settled by the rebuild, and every finding on CoderHelm's own pull request that is not pure style is its own to act on. |
| ba. `ba_verdict_on_old_commit` | A run finished as Done with its pull request ready while its own reviewer stood at "Changes requested" on a commit older than the head, and the run page said a person was needed and that merging was the team's call. The last fix was pushed, and the wake-up that found CI green with no event to read completed the run through an ending that skips the review step, so the new head was never reviewed. | Every completion and the sweep look at the finished run's pull request (`pr_watch`): no reviewer verdict on the head asks for the review of that commit, once per head; changes requested on the head get their fix round, and when the run's rounds are over the findings are deferred on the pull request with the reason. The run page names the one true reason the pull request is where it is (`common::merge_standing`) and names a person only for the team's own rule, with where it is set. |
| bb. `bb_flaky_not_broken` | An end-to-end workflow failed on a timeout in a spec the change never touched, as it had on unrelated pull requests that week. The run spent fix rounds editing the spec's timeout and a retry helper. Where the GitHub App may not write Actions, the re-run tried first was refused and the same fix round followed. | Before a CI fix round the failure is judged (`common::flaky_checks`): a failure naming a changed file is the change's; a check with a record of failing and then passing on one commit of other pull requests, naming no changed file, is re-run once and no code changes for it, and the pull request says which check, the evidence and what was done. A refused re-run is said with the permission by name and still changes no code. The record is per repository and learns only from a commit that failed and then passed unchanged. |
Expand Down
2 changes: 2 additions & 0 deletions services/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ pub mod stop_judge;
pub mod stop_rules;
/// A team's run and PR-reviewed counts, each added exactly once.
pub mod team_counts;
/// What people write on a ticket while a run works on it: kept, acknowledged, acted on.
pub mod ticket_comments;
/// A ticket's comments as CoderHelm read them, and whether its work is done.
pub mod ticket_state;
/// A team's monthly token limits: new tokens, cache read, cache write.
Expand Down
Loading
Loading