azdo-cli 0.24.0-feature-059-delete-work-items.905 → 0.24.0-feature-060-pr-comment-anchor.908

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/dist/index.js +235 -48
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -4296,7 +4296,7 @@ async function getPullRequestThread(context, repo, cred, prId, threadId) {
4296
4296
  const data = await readJsonResponse(response);
4297
4297
  return toActiveCommentThread(data);
4298
4298
  }
4299
- async function createPullRequestThread(context, repo, cred, prId, content, status2) {
4299
+ async function createPullRequestThread(context, repo, cred, prId, content, status2, anchor) {
4300
4300
  const url = new URL(
4301
4301
  `https://dev.azure.com/${encodeURIComponent(context.org)}/${encodeURIComponent(context.project)}/_apis/git/repositories/${encodeURIComponent(repo)}/pullRequests/${prId}/threads`
4302
4302
  );
@@ -4307,6 +4307,10 @@ async function createPullRequestThread(context, repo, cred, prId, content, statu
4307
4307
  if (status2 !== void 0) {
4308
4308
  payload.status = status2;
4309
4309
  }
4310
+ if (anchor !== void 0) {
4311
+ payload.threadContext = anchor.threadContext;
4312
+ payload.pullRequestThreadContext = anchor.pullRequestThreadContext;
4313
+ }
4310
4314
  const response = await fetchWithErrors(url.toString(), {
4311
4315
  method: "POST",
4312
4316
  headers: {
@@ -4318,6 +4322,42 @@ async function createPullRequestThread(context, repo, cred, prId, content, statu
4318
4322
  const data = await readJsonResponse(response);
4319
4323
  return toActiveCommentThread(data);
4320
4324
  }
4325
+ function buildIterationsUrl(context, repo, prId, suffix = "") {
4326
+ const url = new URL(
4327
+ `https://dev.azure.com/${encodeURIComponent(context.org)}/${encodeURIComponent(context.project)}/_apis/git/repositories/${encodeURIComponent(repo)}/pullRequests/${prId}/iterations${suffix}`
4328
+ );
4329
+ url.searchParams.set("api-version", "7.1");
4330
+ return url;
4331
+ }
4332
+ async function getLatestPullRequestIterationId(context, repo, cred, prId) {
4333
+ const url = buildIterationsUrl(context, repo, prId);
4334
+ const response = await fetchWithErrors(url.toString(), { headers: authHeaders(cred) });
4335
+ const data = await readJsonResponse(response);
4336
+ const ids = (data.value ?? []).map((iteration) => iteration.id);
4337
+ if (ids.length === 0) {
4338
+ throw new Error(`Pull request #${prId} has no iterations.`);
4339
+ }
4340
+ return Math.max(...ids);
4341
+ }
4342
+ async function getPullRequestIterationChanges(context, repo, cred, prId, iterationId) {
4343
+ const changes = [];
4344
+ let skip = 0;
4345
+ for (; ; ) {
4346
+ const url = buildIterationsUrl(context, repo, prId, `/${iterationId}/changes`);
4347
+ url.searchParams.set("$top", "2000");
4348
+ if (skip > 0) {
4349
+ url.searchParams.set("$skip", String(skip));
4350
+ }
4351
+ const response = await fetchWithErrors(url.toString(), { headers: authHeaders(cred) });
4352
+ const page = await readJsonResponse(response);
4353
+ changes.push(...page.changeEntries ?? []);
4354
+ const next = page.nextSkip ?? 0;
4355
+ if (next <= skip) {
4356
+ return changes;
4357
+ }
4358
+ skip = next;
4359
+ }
4360
+ }
4321
4361
  function buildThreadCommentUrl(context, repo, prId, threadId) {
4322
4362
  const url = new URL(
4323
4363
  `https://dev.azure.com/${encodeURIComponent(context.org)}/${encodeURIComponent(context.project)}/_apis/git/repositories/${encodeURIComponent(repo)}/pullRequests/${prId}/threads/${threadId}/comments`
@@ -4641,6 +4681,35 @@ async function resolvePullRequestTemplate(context, repo, cred, defaultBranch, ta
4641
4681
  return firstMatchingTemplate(context, repo, cred, defaultBranch, defaultTemplateCandidates(), "default");
4642
4682
  }
4643
4683
 
4684
+ // src/services/thread-anchor.ts
4685
+ var LINE_END_OFFSET = 1e3;
4686
+ function normalizeThreadFilePath(raw) {
4687
+ let path5 = raw.trim().replaceAll("\\", "/");
4688
+ while (path5.startsWith("./")) {
4689
+ path5 = path5.slice(2);
4690
+ }
4691
+ path5 = path5.replace(/^\/+/, "");
4692
+ return path5 === "" ? null : `/${path5}`;
4693
+ }
4694
+ function buildThreadAnchorContext(filePath, line, endLine, side) {
4695
+ const start = { line, offset: 1 };
4696
+ const end = { line: endLine, offset: LINE_END_OFFSET };
4697
+ return side === "right" ? { filePath, rightFileStart: start, rightFileEnd: end } : { filePath, leftFileStart: start, leftFileEnd: end };
4698
+ }
4699
+ function buildPullRequestThreadContext(iterationId, changeTrackingId) {
4700
+ const context = {
4701
+ iterationContext: { firstComparingIteration: 1, secondComparingIteration: iterationId }
4702
+ };
4703
+ if (changeTrackingId !== void 0) {
4704
+ context.changeTrackingId = changeTrackingId;
4705
+ }
4706
+ return context;
4707
+ }
4708
+ function findChangeTrackingId(changes, filePath, side) {
4709
+ const match = changes.find((change) => change.item?.path === filePath) ?? (side === "left" ? changes.find((change) => change.originalPath === filePath) : void 0);
4710
+ return match?.changeTrackingId;
4711
+ }
4712
+
4644
4713
  // src/commands/pr.ts
4645
4714
  function parsePositivePrNumber(raw) {
4646
4715
  if (!/^\d+$/.test(raw)) {
@@ -5794,6 +5863,130 @@ var CREATABLE_THREAD_STATUSES = [
5794
5863
  "byDesign",
5795
5864
  "pending"
5796
5865
  ];
5866
+ var THREAD_ANCHOR_SIDES = ["right", "left"];
5867
+ function parseCommentAddStatus(raw) {
5868
+ if (raw === void 0) {
5869
+ return void 0;
5870
+ }
5871
+ const match = CREATABLE_THREAD_STATUSES.find((candidate) => candidate === raw);
5872
+ if (match === void 0) {
5873
+ writeError(`Invalid --status "${raw}"; expected one of ${CREATABLE_THREAD_STATUSES.join(", ")}.`);
5874
+ return null;
5875
+ }
5876
+ return match;
5877
+ }
5878
+ function parseLineOption(flag, raw) {
5879
+ const value = parsePositivePrNumber(raw);
5880
+ if (value === null) {
5881
+ writeError(`Invalid ${flag} "${raw}"; expected a positive integer (1-based line number).`);
5882
+ }
5883
+ return value;
5884
+ }
5885
+ function parseCommentAnchorOptions(options) {
5886
+ if (options.filePath === void 0) {
5887
+ const stray = [
5888
+ ["--line", options.line],
5889
+ ["--end-line", options.endLine],
5890
+ ["--side", options.side],
5891
+ ["--allow-unchanged-file", options.allowUnchangedFile]
5892
+ ].find(([, value]) => value !== void 0);
5893
+ if (stray !== void 0) {
5894
+ writeError(`${stray[0]} requires --file-path.`);
5895
+ return null;
5896
+ }
5897
+ return void 0;
5898
+ }
5899
+ if (options.line === void 0) {
5900
+ writeError("--file-path requires --line.");
5901
+ return null;
5902
+ }
5903
+ const filePath = normalizeThreadFilePath(options.filePath);
5904
+ if (filePath === null) {
5905
+ writeError(`Invalid --file-path "${options.filePath}"; expected a repository file path.`);
5906
+ return null;
5907
+ }
5908
+ const line = parseLineOption("--line", options.line);
5909
+ if (line === null) {
5910
+ return null;
5911
+ }
5912
+ const endLine = options.endLine === void 0 ? line : parseLineOption("--end-line", options.endLine);
5913
+ if (endLine === null) {
5914
+ return null;
5915
+ }
5916
+ if (endLine < line) {
5917
+ writeError(`--end-line (${endLine}) must not be less than --line (${line}).`);
5918
+ return null;
5919
+ }
5920
+ const side = THREAD_ANCHOR_SIDES.find((candidate) => candidate === (options.side ?? "right"));
5921
+ if (side === void 0) {
5922
+ writeError(`Invalid --side "${options.side}"; expected ${THREAD_ANCHOR_SIDES.join(" or ")}.`);
5923
+ return null;
5924
+ }
5925
+ return { filePath, line, endLine, side, allowUnchangedFile: options.allowUnchangedFile === true };
5926
+ }
5927
+ async function resolveCommentAnchor(target, request) {
5928
+ const prId = target.pullRequest.id;
5929
+ const iterationId = await getLatestPullRequestIterationId(target.context, target.repo, target.pat, prId);
5930
+ const changes = await getPullRequestIterationChanges(target.context, target.repo, target.pat, prId, iterationId);
5931
+ const changeTrackingId = findChangeTrackingId(changes, request.filePath, request.side);
5932
+ if (changeTrackingId === void 0 && !request.allowUnchangedFile) {
5933
+ writeError(
5934
+ `${request.filePath} is not among the files changed by pull request #${prId} (iteration ${iterationId}). Check the path, or pass --allow-unchanged-file to post the comment anyway.`,
5935
+ 3
5936
+ );
5937
+ return null;
5938
+ }
5939
+ return {
5940
+ request,
5941
+ iterationId,
5942
+ changeTrackingId,
5943
+ threadContext: buildThreadAnchorContext(request.filePath, request.line, request.endLine, request.side),
5944
+ pullRequestThreadContext: buildPullRequestThreadContext(iterationId, changeTrackingId)
5945
+ };
5946
+ }
5947
+ function describeCommentAnchor(anchor) {
5948
+ const { filePath, line, endLine, side } = anchor.request;
5949
+ const lines = line === endLine ? `line ${line}` : `lines ${line}-${endLine}`;
5950
+ const tracking = anchor.changeTrackingId === void 0 ? "no changeTrackingId" : `changeTrackingId ${anchor.changeTrackingId}`;
5951
+ return ` at ${filePath} ${lines} (${side} side, iteration ${anchor.iterationId}, ${tracking})`;
5952
+ }
5953
+ function warnIfUntracked(anchor) {
5954
+ if (anchor !== void 0 && anchor.changeTrackingId === void 0) {
5955
+ process.stderr.write(
5956
+ `Note: ${anchor.request.filePath} is not among the pull request's changed files, so the thread is posted without a changeTrackingId and will not follow the line across later pushes.
5957
+ `
5958
+ );
5959
+ }
5960
+ }
5961
+ function withAnchorFields(result, anchor) {
5962
+ if (anchor === void 0) {
5963
+ return result;
5964
+ }
5965
+ return {
5966
+ ...result,
5967
+ threadContext: anchor.threadContext,
5968
+ pullRequestThreadContext: anchor.pullRequestThreadContext,
5969
+ changeTracked: anchor.changeTrackingId !== void 0
5970
+ };
5971
+ }
5972
+ function writeCommentAddDryRun(prId, body, status2, anchor, json) {
5973
+ if (json) {
5974
+ const dryResult = withAnchorFields(
5975
+ { pullRequestId: prId, threadId: null, commentId: null, status: status2 ?? null, content: body, dryRun: true },
5976
+ anchor
5977
+ );
5978
+ process.stdout.write(`${JSON.stringify(dryResult, null, 2)}
5979
+ `);
5980
+ return;
5981
+ }
5982
+ const statusSuffix = status2 === void 0 ? "" : ` with status ${status2}`;
5983
+ const anchorSuffix = anchor === void 0 ? "" : describeCommentAnchor(anchor);
5984
+ process.stdout.write(
5985
+ `Dry run: would post a new comment thread${statusSuffix} on pull request #${prId}${anchorSuffix} (${body.length} chars).
5986
+ ${body}
5987
+ `
5988
+ );
5989
+ }
5797
5990
  async function runCommentAdd(text, options) {
5798
5991
  let context;
5799
5992
  try {
@@ -5801,16 +5994,13 @@ async function runCommentAdd(text, options) {
5801
5994
  if (body === null) {
5802
5995
  return;
5803
5996
  }
5804
- let status2;
5805
- if (options.status !== void 0) {
5806
- const match = CREATABLE_THREAD_STATUSES.find((candidate) => candidate === options.status);
5807
- if (match === void 0) {
5808
- writeError(
5809
- `Invalid --status "${options.status}"; expected one of ${CREATABLE_THREAD_STATUSES.join(", ")}.`
5810
- );
5811
- return;
5812
- }
5813
- status2 = match;
5997
+ const status2 = parseCommentAddStatus(options.status);
5998
+ if (status2 === null) {
5999
+ return;
6000
+ }
6001
+ const anchorRequest = parseCommentAnchorOptions(options);
6002
+ if (anchorRequest === null) {
6003
+ return;
5814
6004
  }
5815
6005
  const target = await resolvePullRequestTarget(options, {
5816
6006
  onContextResolved: (resolved) => {
@@ -5820,26 +6010,13 @@ async function runCommentAdd(text, options) {
5820
6010
  if (target === null) {
5821
6011
  return;
5822
6012
  }
6013
+ const anchor = anchorRequest === void 0 ? void 0 : await resolveCommentAnchor(target, anchorRequest);
6014
+ if (anchor === null) {
6015
+ return;
6016
+ }
6017
+ warnIfUntracked(anchor);
5823
6018
  if (options.dryRun === true) {
5824
- const dryResult = {
5825
- pullRequestId: target.pullRequest.id,
5826
- threadId: null,
5827
- commentId: null,
5828
- status: status2 ?? null,
5829
- content: body,
5830
- dryRun: true
5831
- };
5832
- if (options.json) {
5833
- process.stdout.write(`${JSON.stringify(dryResult, null, 2)}
5834
- `);
5835
- return;
5836
- }
5837
- const statusSuffix = status2 === void 0 ? "" : ` with status ${status2}`;
5838
- process.stdout.write(
5839
- `Dry run: would post a new comment thread${statusSuffix} on pull request #${target.pullRequest.id} (${body.length} chars).
5840
- ${body}
5841
- `
5842
- );
6019
+ writeCommentAddDryRun(target.pullRequest.id, body, status2, anchor, options.json);
5843
6020
  return;
5844
6021
  }
5845
6022
  const thread = await createPullRequestThread(
@@ -5848,24 +6025,29 @@ ${body}
5848
6025
  target.pat,
5849
6026
  target.pullRequest.id,
5850
6027
  body,
5851
- status2
6028
+ status2,
6029
+ ...anchor === void 0 ? [] : [{ threadContext: anchor.threadContext, pullRequestThreadContext: anchor.pullRequestThreadContext }]
5852
6030
  );
5853
6031
  const created = thread.comments[0];
5854
- const result = {
5855
- pullRequestId: target.pullRequest.id,
5856
- threadId: thread.id,
5857
- commentId: created?.id ?? null,
5858
- status: thread.status,
5859
- content: created?.content ?? body,
5860
- dryRun: false
5861
- };
6032
+ const result = withAnchorFields(
6033
+ {
6034
+ pullRequestId: target.pullRequest.id,
6035
+ threadId: thread.id,
6036
+ commentId: created?.id ?? null,
6037
+ status: thread.status,
6038
+ content: created?.content ?? body,
6039
+ dryRun: false
6040
+ },
6041
+ anchor
6042
+ );
5862
6043
  if (options.json) {
5863
6044
  process.stdout.write(`${JSON.stringify(result, null, 2)}
5864
6045
  `);
5865
6046
  return;
5866
6047
  }
6048
+ const anchorSuffix = anchor === void 0 ? "" : describeCommentAnchor(anchor);
5867
6049
  process.stdout.write(
5868
- `Comment posted to pull request #${target.pullRequest.id} (thread #${thread.id}).
6050
+ `Comment posted to pull request #${target.pullRequest.id} (thread #${thread.id})${anchorSuffix}.
5869
6051
  `
5870
6052
  );
5871
6053
  } catch (err) {
@@ -5877,18 +6059,21 @@ function buildCommentAddCommand(name, description) {
5877
6059
  withCommonPrOptions(configureUnwrappedHelp(command)).description(description).argument("[text]", "body of the new comment; omit when using --file").option("--file <path>", "read the comment body from a UTF-8 file instead of the inline argument").option(
5878
6060
  "--status <status>",
5879
6061
  `thread status (${CREATABLE_THREAD_STATUSES.join(" | ")}); omit for a plain, non-resolvable comment`
6062
+ ).option("--file-path <path>", "anchor the thread to this repository file (Files tab); requires --line").option("--line <n>", "first line of the anchored range (1-based)").option("--end-line <n>", "last line of the anchored range (default: --line)").option("--side <side>", "right (new version, default) or left (old version, for deleted lines)").option(
6063
+ "--allow-unchanged-file",
6064
+ "post even when --file-path is not among the pull request's changed files (the thread will not follow the line across pushes)"
5880
6065
  ).option("--dry-run", "resolve the target pull request and print what would be posted, without writing anything").option("--pr-number <N>", PR_NUMBER_HELP).option("--json", "output JSON").action(async (text, _options, command2) => {
5881
6066
  await runCommentAdd(text, mergedPrOptions(command2));
5882
6067
  });
5883
6068
  return command;
5884
6069
  }
5885
6070
  function createPrCommentsAddCommand() {
5886
- return buildCommentAddCommand("add", "Post a new comment thread on the pull request overview");
6071
+ return buildCommentAddCommand("add", "Post a new comment thread on the pull request overview, or on a file and line");
5887
6072
  }
5888
6073
  function createPrCommentAddCommand() {
5889
6074
  return buildCommentAddCommand(
5890
6075
  "comment-add",
5891
- 'Post a new comment thread on the pull request overview (alias of "azdo pr comments add")'
6076
+ 'Post a new comment thread on the pull request overview, or on a file and line (alias of "azdo pr comments add")'
5892
6077
  );
5893
6078
  }
5894
6079
  function parseExplicitCommentId(options) {
@@ -6178,13 +6363,15 @@ async function attachWorkItemIds(context, repo, cred, pullRequests) {
6178
6363
  const result = new Array(pullRequests.length);
6179
6364
  let next = 0;
6180
6365
  const worker = async () => {
6181
- while (next < pullRequests.length) {
6182
- const index = next;
6183
- next += 1;
6184
- const pullRequest = pullRequests[index];
6185
- const workItemIds = await getPullRequestWorkItemIds(context, repo, cred, pullRequest.id);
6186
- result[index] = { ...pullRequest, workItemIds };
6366
+ if (next >= pullRequests.length) {
6367
+ return;
6187
6368
  }
6369
+ const index = next;
6370
+ next += 1;
6371
+ const pullRequest = pullRequests[index];
6372
+ const workItemIds = await getPullRequestWorkItemIds(context, repo, cred, pullRequest.id);
6373
+ result[index] = { ...pullRequest, workItemIds };
6374
+ await worker();
6188
6375
  };
6189
6376
  await Promise.all(Array.from({ length: Math.min(WORK_ITEM_LOOKUP_CONCURRENCY, pullRequests.length) }, worker));
6190
6377
  return result;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azdo-cli",
3
- "version": "0.24.0-feature-059-delete-work-items.905",
3
+ "version": "0.24.0-feature-060-pr-comment-anchor.908",
4
4
  "description": "Azure DevOps CLI tool",
5
5
  "type": "module",
6
6
  "bin": {