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
14 changes: 13 additions & 1 deletion coderhelm-jira/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const { GATEWAY_URL } = require("./gateway");
const { existingComments } = require("./comments");
const { postReply, parentIdOf } = require("./thread-reply");
const { versionFields } = require("./version");
const { relatedTickets } = require("./related");

/** Jira's comment calls as the app, for a threaded reply (see thread-reply.js). */
const jiraComments = {
Expand All @@ -23,6 +24,14 @@ const jiraComments = {
// see whether Jira's comment events carry a parent id.
let loggedCommentKeys = false;

/** A related ticket's summary, status and description, read as the app. */
async function readRelatedIssue(key) {
const res = await api.asApp().requestJira(route`/rest/api/3/issue/${key}?fields=summary,description,status`);
if (!res.ok) return null;
const full = await res.json();
return full.fields || null;
}

/** The parts of a Jira user CoderHelm keeps: account id, name, email. */
function jiraUser(u) {
return { accountId: u.accountId, displayName: u.displayName, emailAddress: u.emailAddress };
Expand Down Expand Up @@ -188,7 +197,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,comment`);
const res = await api.asApp().requestJira(route`/rest/api/3/issue/${issue.id}?fields=summary,description,labels,assignee,reporter,creator,project,attachment,issuetype,comment,issuelinks`);
if (res.ok) {
const full = await res.json();
fields = full.fields || {};
Expand Down Expand Up @@ -238,6 +247,9 @@ exports.handler = async (event, context) => {
// 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),
// The tickets linked to this one and the ones its text names: their
// acceptance criteria are part of the change (see related.js).
related: await relatedTickets(issue.key, fields, readRelatedIssue),
user: requester(event.atlassianId, fields),
coderhelm: {
repo_owner: repoOwner || undefined,
Expand Down
81 changes: 81 additions & 0 deletions coderhelm-jira/src/related.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* The tickets related to the one CoderHelm works on: its issue links ("relates
* to", "blocks", "is cloned by") and the tickets its description names. Their
* acceptance criteria are part of the change, so the run reads them before it
* plans and checks them before the pull request is called ready.
*
* A pull request was once closed by a person who expected a linked ticket's
* pull request to cover it; that pull request had never read the linked
* ticket, and the button it was about still pointed to the old address.
*/

/** The most related tickets sent with one ticket event. */
const MAX_RELATED = 6;

/** The most characters of one related ticket's description sent. */
const MAX_BODY_CHARS = 8000;

/** Ticket keys a text names (`ABC-123`), in order, without repeats. */
function keysInText(text, own) {
const out = [];
const re = /(^|[^A-Za-z0-9-])([A-Z][A-Z0-9]+-\d+)(?![A-Za-z0-9])/g;
let m;
while ((m = re.exec(text || "")) !== null) {
const key = m[2];
if (key !== own && !out.includes(key)) out.push(key);
}
return out;
}

/**
* The related tickets' keys with how each relates, issue links first.
* @param {string} own The ticket's own key.
* @param {object} fields The ticket's fields (with `issuelinks` and `description`).
* @returns {{key: string, relation: string}[]}
*/
function relatedKeys(own, fields) {
const out = [];
for (const l of (fields && fields.issuelinks) || []) {
const other = l.outwardIssue || l.inwardIssue;
if (!other || !other.key || other.key === own) continue;
const type = l.type || {};
const relation = (l.outwardIssue ? type.outward : type.inward) || type.name || "relates to";
if (!out.some((r) => r.key === other.key)) out.push({ key: other.key, relation });
}
const text = typeof fields?.description === "string" ? fields.description : JSON.stringify(fields?.description || "");
for (const key of keysInText(text, own)) {
if (!out.some((r) => r.key === key)) out.push({ key, relation: "named in the ticket" });
}
return out.slice(0, MAX_RELATED);
}

/**
* Each related ticket with its summary, status and description (the
* gateway reads the description's words), read as the app.
* @param {string} own The ticket's own key.
* @param {object} fields The ticket's fields.
* @param {(key: string) => Promise<object|null>} readIssue The issue's fields, or null.
*/
async function relatedTickets(own, fields, readIssue) {
const out = [];
for (const { key, relation } of relatedKeys(own, fields)) {
let f = null;
try {
f = await readIssue(key);
} catch (e) {
f = null;
}
if (!f) continue;
const body = typeof f.description === "string" ? f.description : JSON.stringify(f.description || "");
out.push({
key,
relation,
summary: f.summary || "",
status: (f.status && f.status.name) || "",
description: body.slice(0, MAX_BODY_CHARS),
});
}
return out;
}

module.exports = { keysInText, relatedKeys, relatedTickets, MAX_RELATED };
37 changes: 37 additions & 0 deletions coderhelm-jira/test/related.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { test } = require("node:test");
const assert = require("node:assert/strict");
const { keysInText, relatedKeys, relatedTickets, MAX_RELATED } = require("../src/related");

test("keys named in a text, without the ticket's own", () => {
assert.deepEqual(keysInText("See ABC-2 and XY-3, not ABC-1 or abc-4. ABC-2 again.", "ABC-1"), ["ABC-2", "XY-3"]);
});

test("issue links come first, with how they relate, then keys in the description", () => {
const fields = {
issuelinks: [
{ type: { name: "Relates", inward: "relates to", outward: "relates to" }, outwardIssue: { key: "ABC-2" } },
{ type: { name: "Blocks", inward: "is blocked by", outward: "blocks" }, inwardIssue: { key: "ABC-3" } },
],
description: { type: "doc", content: [{ type: "text", text: "Also covers ABC-4 and ABC-2." }] },
};
assert.deepEqual(relatedKeys("ABC-1", fields), [
{ key: "ABC-2", relation: "relates to" },
{ key: "ABC-3", relation: "is blocked by" },
{ key: "ABC-4", relation: "named in the ticket" },
]);
});

test("each related ticket is read with its summary and description; an unreadable one is left out", async () => {
const fields = { issuelinks: [{ type: { outward: "relates to" }, outwardIssue: { key: "ABC-2" } }, { type: { outward: "relates to" }, outwardIssue: { key: "ABC-9" } }] };
const read = async (key) => (key === "ABC-2" ? { summary: "Point the drawer button to the new page", status: { name: "Closed" }, description: "Acceptance criteria\n- The drawer Register button links to /new/register" } : null);
const got = await relatedTickets("ABC-1", fields, read);
assert.equal(got.length, 1);
assert.equal(got[0].key, "ABC-2");
assert.equal(got[0].status, "Closed");
assert.match(got[0].description, /links to \/new\/register/);
});

test("at most a handful are sent", () => {
const links = Array.from({ length: 10 }, (_, i) => ({ type: { outward: "relates to" }, outwardIssue: { key: `ABC-${i + 2}` } }));
assert.equal(relatedKeys("ABC-1", { issuelinks: links }).length, MAX_RELATED);
});
5 changes: 5 additions & 0 deletions docs/incidents/bz_ticket_answer_decides.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# bz. `bz_ticket_answer_decides`

**What happened:** A run asked on its ticket for a page's heading and intro copy, with a proposed default. The product owner answered on the ticket to skip both and use the title. That run applied the answer as a round on its own pull request. Two days later a new run on the same ticket opened a new pull request: the comment was already marked handled, so the new run never read it, built the intro anyway and listed it under "Decisions pending product input".

**The guarantee:** Every answer and instruction people wrote on the ticket is a decision for every run on it, whichever run handled it. It is merged into the run's answers before triage and planning, read again at each boundary, and given to every coder round and every feedback and resume round. Wherever a description is written, an item the answers settle is taken out of the pending lists and listed under "Decided on the ticket", and an answer that goes against an acceptance criterion gets one line saying so. The decisions are kept per installation, so one team's never reach another's description.
5 changes: 5 additions & 0 deletions docs/incidents/ca_related_ticket_covered.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ca. `ca_related_ticket_covered`

**What happened:** A ticket's pull request was closed by a person who expected a linked ticket's pull request to cover it. The pull request that was kept never read the linked ticket: when it was called ready, the drawer's Register button still pointed to the old address.

**The guarantee:** The Jira app sends the ticket's issue links and the tickets its text names, with their summaries and descriptions. The gateway keeps them on the run's message, the planner reads their acceptance criteria as part of the change, and the run keeps them. Before the pull request is called ready, each address a pull request closed in favor of this one moved is checked on the head's files, each link criterion is checked on the preview page, and anything else is listed as not covered with why, in the description's "Related tickets" section.
6 changes: 6 additions & 0 deletions services/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ pub mod problems;
/// Where this deployment's dashboard and API are (links, redirects, webhooks).
pub mod public_urls;
pub mod registry_check;
/// Linked tickets and pull requests closed in favor of this one: their criteria are
/// part of the change and checked before it is called ready.
pub mod related_tickets;
/// What CoderHelm says back to a person, written from the thread and the run's records.
pub mod reply_compose;
/// Who asked for a run: one identity per person, for per-person analytics.
Expand Down Expand Up @@ -293,6 +296,9 @@ pub mod stop_rules;
/// A team's run and PR-reviewed counts, each added exactly once.
pub mod team_counts;
pub mod ticket_comments;
/// What a person answered on the ticket is a decision in every run on it; a
/// description never lists an answered item as pending.
pub mod ticket_decisions;
/// 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
6 changes: 6 additions & 0 deletions services/common/src/messages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1147,6 +1147,11 @@ pub struct TicketMessage {
/// planner doesn't ask them again (see `run_questions`).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub answers: Vec<crate::run_questions::Answer>,
/// The tickets linked to this one (Jira issue links, tickets its text
/// names), as the Jira app sends them: their acceptance criteria are
/// part of the change (`related_tickets`).
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub related: Vec<crate::related_tickets::Related>,
/// The id the new run takes, when whoever sent the ticket named it
/// first (answering on the dashboard links the asking run to the run
/// that picks the answers up). A continuation keeps its own id.
Expand Down Expand Up @@ -1253,6 +1258,7 @@ impl Default for TicketMessage {
coder_learning: String::new(),
person_direction: None,
answers: Vec::new(),
related: Vec::new(),
new_run_id: None,
}
}
Expand Down
Loading
Loading