azdo-cli 0.2.0-003-cli-settings.17 → 0.2.0-004-update-work-item.22

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 +386 -10
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,7 +1,7 @@
1
1
  #!/usr/bin/env node
2
2
 
3
3
  // src/index.ts
4
- import { Command as Command4 } from "commander";
4
+ import { Command as Command7 } from "commander";
5
5
 
6
6
  // src/version.ts
7
7
  import { readFileSync } from "fs";
@@ -90,6 +90,48 @@ async function getWorkItem(context, id, pat, extraFields) {
90
90
  extraFields: extraFields && extraFields.length > 0 ? buildExtraFields(data.fields, extraFields) : null
91
91
  };
92
92
  }
93
+ async function updateWorkItem(context, id, pat, fieldName, operations) {
94
+ const url = `https://dev.azure.com/${context.org}/${context.project}/_apis/wit/workitems/${id}?api-version=7.1`;
95
+ const token = Buffer.from(`:${pat}`).toString("base64");
96
+ let response;
97
+ try {
98
+ response = await fetch(url, {
99
+ method: "PATCH",
100
+ headers: {
101
+ Authorization: `Basic ${token}`,
102
+ "Content-Type": "application/json-patch+json"
103
+ },
104
+ body: JSON.stringify(operations)
105
+ });
106
+ } catch {
107
+ throw new Error("NETWORK_ERROR");
108
+ }
109
+ if (response.status === 401) throw new Error("AUTH_FAILED");
110
+ if (response.status === 403) throw new Error("PERMISSION_DENIED");
111
+ if (response.status === 404) throw new Error("NOT_FOUND");
112
+ if (response.status === 400) {
113
+ let serverMessage = "Unknown error";
114
+ try {
115
+ const body = await response.json();
116
+ if (body.message) serverMessage = body.message;
117
+ } catch {
118
+ }
119
+ throw new Error(`UPDATE_REJECTED: ${serverMessage}`);
120
+ }
121
+ if (!response.ok) {
122
+ throw new Error(`HTTP_${response.status}`);
123
+ }
124
+ const data = await response.json();
125
+ const lastOp = operations[operations.length - 1];
126
+ const fieldValue = lastOp.value ?? null;
127
+ return {
128
+ id: data.id,
129
+ rev: data.rev,
130
+ title: data.fields["System.Title"],
131
+ fieldName,
132
+ fieldValue
133
+ };
134
+ }
93
135
 
94
136
  // src/services/auth.ts
95
137
  import { createInterface } from "readline";
@@ -201,7 +243,11 @@ function parseAzdoRemote(url) {
201
243
  for (const pattern of patterns) {
202
244
  const match = url.match(pattern);
203
245
  if (match) {
204
- return { org: match[1], project: match[2] };
246
+ const project = match[2];
247
+ if (/^DefaultCollection$/i.test(project)) {
248
+ return { org: match[1], project: "" };
249
+ }
250
+ return { org: match[1], project };
205
251
  }
206
252
  }
207
253
  return null;
@@ -214,7 +260,7 @@ function detectAzdoContext() {
214
260
  throw new Error("Not in a git repository. Provide --org and --project explicitly.");
215
261
  }
216
262
  const context = parseAzdoRemote(remoteUrl);
217
- if (!context) {
263
+ if (!context || !context.org && !context.project) {
218
264
  throw new Error('Git remote "origin" is not an Azure DevOps URL. Provide --org and --project explicitly.');
219
265
  }
220
266
  return context;
@@ -380,12 +426,19 @@ function createGetItemCommand() {
380
426
  if (options.org && options.project) {
381
427
  context = { org: options.org, project: options.project };
382
428
  } else {
383
- try {
384
- context = detectAzdoContext();
385
- } catch {
386
- const config = loadConfig();
387
- if (config.org && config.project) {
388
- context = { org: config.org, project: config.project };
429
+ const config = loadConfig();
430
+ if (config.org && config.project) {
431
+ context = { org: config.org, project: config.project };
432
+ } else {
433
+ let gitContext = null;
434
+ try {
435
+ gitContext = detectAzdoContext();
436
+ } catch {
437
+ }
438
+ const org = config.org || gitContext?.org;
439
+ const project = config.project || gitContext?.project;
440
+ if (org && project) {
441
+ context = { org, project };
389
442
  } else {
390
443
  throw new Error(
391
444
  'Could not determine org/project. Use --org and --project flags, work from an Azure DevOps git repo, or run "azdo config set org/project".'
@@ -597,12 +650,335 @@ function createConfigCommand() {
597
650
  return config;
598
651
  }
599
652
 
653
+ // src/commands/set-state.ts
654
+ import { Command as Command4 } from "commander";
655
+ function resolveContext(options) {
656
+ if (options.org && options.project) {
657
+ return { org: options.org, project: options.project };
658
+ }
659
+ const config = loadConfig();
660
+ if (config.org && config.project) {
661
+ return { org: config.org, project: config.project };
662
+ }
663
+ let gitContext = null;
664
+ try {
665
+ gitContext = detectAzdoContext();
666
+ } catch {
667
+ }
668
+ const org = config.org || gitContext?.org;
669
+ const project = config.project || gitContext?.project;
670
+ if (org && project) {
671
+ return { org, project };
672
+ }
673
+ throw new Error(
674
+ 'Could not determine org/project. Use --org and --project flags, work from an Azure DevOps git repo, or run "azdo config set org/project".'
675
+ );
676
+ }
677
+ function createSetStateCommand() {
678
+ const command = new Command4("set-state");
679
+ command.description("Change the state of a work item").argument("<id>", "work item ID").argument("<state>", 'target state (e.g., "Active", "Closed")').option("--org <org>", "Azure DevOps organization").option("--project <project>", "Azure DevOps project").option("--json", "output result as JSON").action(
680
+ async (idStr, state, options) => {
681
+ const id = parseInt(idStr, 10);
682
+ if (!Number.isInteger(id) || id <= 0) {
683
+ process.stderr.write(
684
+ `Error: Work item ID must be a positive integer. Got: "${idStr}"
685
+ `
686
+ );
687
+ process.exit(1);
688
+ }
689
+ const hasOrg = options.org !== void 0;
690
+ const hasProject = options.project !== void 0;
691
+ if (hasOrg !== hasProject) {
692
+ process.stderr.write(
693
+ "Error: --org and --project must both be provided, or both omitted.\n"
694
+ );
695
+ process.exit(1);
696
+ }
697
+ let context;
698
+ try {
699
+ context = resolveContext(options);
700
+ const credential = await resolvePat();
701
+ const operations = [
702
+ { op: "add", path: "/fields/System.State", value: state }
703
+ ];
704
+ const result = await updateWorkItem(context, id, credential.pat, "System.State", operations);
705
+ if (options.json) {
706
+ process.stdout.write(
707
+ JSON.stringify({
708
+ id: result.id,
709
+ rev: result.rev,
710
+ title: result.title,
711
+ field: result.fieldName,
712
+ value: result.fieldValue
713
+ }) + "\n"
714
+ );
715
+ } else {
716
+ process.stdout.write(`Updated work item ${result.id}: State -> ${state}
717
+ `);
718
+ }
719
+ } catch (err) {
720
+ const error = err instanceof Error ? err : new Error(String(err));
721
+ const msg = error.message;
722
+ if (msg === "AUTH_FAILED") {
723
+ process.stderr.write(
724
+ 'Error: Authentication failed. Check that your PAT is valid and has the "Work Items (Read & Write)" scope.\n'
725
+ );
726
+ } else if (msg === "PERMISSION_DENIED") {
727
+ process.stderr.write(
728
+ `Error: Access denied. Your PAT may lack write permissions for project "${context.project}".
729
+ `
730
+ );
731
+ } else if (msg === "NOT_FOUND") {
732
+ process.stderr.write(
733
+ `Error: Work item ${id} not found in ${context.org}/${context.project}.
734
+ `
735
+ );
736
+ } else if (msg.startsWith("UPDATE_REJECTED:")) {
737
+ const serverMsg = msg.replace("UPDATE_REJECTED: ", "");
738
+ process.stderr.write(`Error: Update rejected: ${serverMsg}
739
+ `);
740
+ } else if (msg === "NETWORK_ERROR") {
741
+ process.stderr.write(
742
+ "Error: Could not connect to Azure DevOps. Check your network connection.\n"
743
+ );
744
+ } else {
745
+ process.stderr.write(`Error: ${msg}
746
+ `);
747
+ }
748
+ process.exit(1);
749
+ }
750
+ }
751
+ );
752
+ return command;
753
+ }
754
+
755
+ // src/commands/assign.ts
756
+ import { Command as Command5 } from "commander";
757
+ function resolveContext2(options) {
758
+ if (options.org && options.project) {
759
+ return { org: options.org, project: options.project };
760
+ }
761
+ const config = loadConfig();
762
+ if (config.org && config.project) {
763
+ return { org: config.org, project: config.project };
764
+ }
765
+ let gitContext = null;
766
+ try {
767
+ gitContext = detectAzdoContext();
768
+ } catch {
769
+ }
770
+ const org = config.org || gitContext?.org;
771
+ const project = config.project || gitContext?.project;
772
+ if (org && project) {
773
+ return { org, project };
774
+ }
775
+ throw new Error(
776
+ 'Could not determine org/project. Use --org and --project flags, work from an Azure DevOps git repo, or run "azdo config set org/project".'
777
+ );
778
+ }
779
+ function createAssignCommand() {
780
+ const command = new Command5("assign");
781
+ command.description("Assign a work item to a user, or unassign it").argument("<id>", "work item ID").argument("[name]", "user display name or email").option("--unassign", "clear the Assigned To field").option("--org <org>", "Azure DevOps organization").option("--project <project>", "Azure DevOps project").option("--json", "output result as JSON").action(
782
+ async (idStr, name, options) => {
783
+ const id = parseInt(idStr, 10);
784
+ if (!Number.isInteger(id) || id <= 0) {
785
+ process.stderr.write(
786
+ `Error: Work item ID must be a positive integer. Got: "${idStr}"
787
+ `
788
+ );
789
+ process.exit(1);
790
+ }
791
+ if (!name && !options.unassign) {
792
+ process.stderr.write(
793
+ "Error: Either provide a user name or use --unassign.\n"
794
+ );
795
+ process.exit(1);
796
+ }
797
+ if (name && options.unassign) {
798
+ process.stderr.write(
799
+ "Error: Cannot provide both a user name and --unassign.\n"
800
+ );
801
+ process.exit(1);
802
+ }
803
+ const hasOrg = options.org !== void 0;
804
+ const hasProject = options.project !== void 0;
805
+ if (hasOrg !== hasProject) {
806
+ process.stderr.write(
807
+ "Error: --org and --project must both be provided, or both omitted.\n"
808
+ );
809
+ process.exit(1);
810
+ }
811
+ let context;
812
+ try {
813
+ context = resolveContext2(options);
814
+ const credential = await resolvePat();
815
+ const value = options.unassign ? "" : name;
816
+ const operations = [
817
+ { op: "add", path: "/fields/System.AssignedTo", value }
818
+ ];
819
+ const result = await updateWorkItem(context, id, credential.pat, "System.AssignedTo", operations);
820
+ if (options.json) {
821
+ process.stdout.write(
822
+ JSON.stringify({
823
+ id: result.id,
824
+ rev: result.rev,
825
+ title: result.title,
826
+ field: result.fieldName,
827
+ value: result.fieldValue
828
+ }) + "\n"
829
+ );
830
+ } else {
831
+ const displayValue = options.unassign ? "(unassigned)" : name;
832
+ process.stdout.write(`Updated work item ${result.id}: Assigned To -> ${displayValue}
833
+ `);
834
+ }
835
+ } catch (err) {
836
+ const error = err instanceof Error ? err : new Error(String(err));
837
+ const msg = error.message;
838
+ if (msg === "AUTH_FAILED") {
839
+ process.stderr.write(
840
+ 'Error: Authentication failed. Check that your PAT is valid and has the "Work Items (Read & Write)" scope.\n'
841
+ );
842
+ } else if (msg === "PERMISSION_DENIED") {
843
+ process.stderr.write(
844
+ `Error: Access denied. Your PAT may lack write permissions for project "${context.project}".
845
+ `
846
+ );
847
+ } else if (msg === "NOT_FOUND") {
848
+ process.stderr.write(
849
+ `Error: Work item ${id} not found in ${context.org}/${context.project}.
850
+ `
851
+ );
852
+ } else if (msg.startsWith("UPDATE_REJECTED:")) {
853
+ const serverMsg = msg.replace("UPDATE_REJECTED: ", "");
854
+ process.stderr.write(`Error: Update rejected: ${serverMsg}
855
+ `);
856
+ } else if (msg === "NETWORK_ERROR") {
857
+ process.stderr.write(
858
+ "Error: Could not connect to Azure DevOps. Check your network connection.\n"
859
+ );
860
+ } else {
861
+ process.stderr.write(`Error: ${msg}
862
+ `);
863
+ }
864
+ process.exit(1);
865
+ }
866
+ }
867
+ );
868
+ return command;
869
+ }
870
+
871
+ // src/commands/set-field.ts
872
+ import { Command as Command6 } from "commander";
873
+ function resolveContext3(options) {
874
+ if (options.org && options.project) {
875
+ return { org: options.org, project: options.project };
876
+ }
877
+ const config = loadConfig();
878
+ if (config.org && config.project) {
879
+ return { org: config.org, project: config.project };
880
+ }
881
+ let gitContext = null;
882
+ try {
883
+ gitContext = detectAzdoContext();
884
+ } catch {
885
+ }
886
+ const org = config.org || gitContext?.org;
887
+ const project = config.project || gitContext?.project;
888
+ if (org && project) {
889
+ return { org, project };
890
+ }
891
+ throw new Error(
892
+ 'Could not determine org/project. Use --org and --project flags, work from an Azure DevOps git repo, or run "azdo config set org/project".'
893
+ );
894
+ }
895
+ function createSetFieldCommand() {
896
+ const command = new Command6("set-field");
897
+ command.description("Set any work item field by its reference name").argument("<id>", "work item ID").argument("<field>", "field reference name (e.g., System.Title)").argument("<value>", "new value for the field").option("--org <org>", "Azure DevOps organization").option("--project <project>", "Azure DevOps project").option("--json", "output result as JSON").action(
898
+ async (idStr, field, value, options) => {
899
+ const id = parseInt(idStr, 10);
900
+ if (!Number.isInteger(id) || id <= 0) {
901
+ process.stderr.write(
902
+ `Error: Work item ID must be a positive integer. Got: "${idStr}"
903
+ `
904
+ );
905
+ process.exit(1);
906
+ }
907
+ const hasOrg = options.org !== void 0;
908
+ const hasProject = options.project !== void 0;
909
+ if (hasOrg !== hasProject) {
910
+ process.stderr.write(
911
+ "Error: --org and --project must both be provided, or both omitted.\n"
912
+ );
913
+ process.exit(1);
914
+ }
915
+ let context;
916
+ try {
917
+ context = resolveContext3(options);
918
+ const credential = await resolvePat();
919
+ const operations = [
920
+ { op: "add", path: `/fields/${field}`, value }
921
+ ];
922
+ const result = await updateWorkItem(context, id, credential.pat, field, operations);
923
+ if (options.json) {
924
+ process.stdout.write(
925
+ JSON.stringify({
926
+ id: result.id,
927
+ rev: result.rev,
928
+ title: result.title,
929
+ field: result.fieldName,
930
+ value: result.fieldValue
931
+ }) + "\n"
932
+ );
933
+ } else {
934
+ process.stdout.write(`Updated work item ${result.id}: ${field} -> ${value}
935
+ `);
936
+ }
937
+ } catch (err) {
938
+ const error = err instanceof Error ? err : new Error(String(err));
939
+ const msg = error.message;
940
+ if (msg === "AUTH_FAILED") {
941
+ process.stderr.write(
942
+ 'Error: Authentication failed. Check that your PAT is valid and has the "Work Items (Read & Write)" scope.\n'
943
+ );
944
+ } else if (msg === "PERMISSION_DENIED") {
945
+ process.stderr.write(
946
+ `Error: Access denied. Your PAT may lack write permissions for project "${context.project}".
947
+ `
948
+ );
949
+ } else if (msg === "NOT_FOUND") {
950
+ process.stderr.write(
951
+ `Error: Work item ${id} not found in ${context.org}/${context.project}.
952
+ `
953
+ );
954
+ } else if (msg.startsWith("UPDATE_REJECTED:")) {
955
+ const serverMsg = msg.replace("UPDATE_REJECTED: ", "");
956
+ process.stderr.write(`Error: Update rejected: ${serverMsg}
957
+ `);
958
+ } else if (msg === "NETWORK_ERROR") {
959
+ process.stderr.write(
960
+ "Error: Could not connect to Azure DevOps. Check your network connection.\n"
961
+ );
962
+ } else {
963
+ process.stderr.write(`Error: ${msg}
964
+ `);
965
+ }
966
+ process.exit(1);
967
+ }
968
+ }
969
+ );
970
+ return command;
971
+ }
972
+
600
973
  // src/index.ts
601
- var program = new Command4();
974
+ var program = new Command7();
602
975
  program.name("azdo").description("Azure DevOps CLI tool").version(version, "-v, --version");
603
976
  program.addCommand(createGetItemCommand());
604
977
  program.addCommand(createClearPatCommand());
605
978
  program.addCommand(createConfigCommand());
979
+ program.addCommand(createSetStateCommand());
980
+ program.addCommand(createAssignCommand());
981
+ program.addCommand(createSetFieldCommand());
606
982
  program.showHelpAfterError();
607
983
  program.parse();
608
984
  if (process.argv.length <= 2) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "azdo-cli",
3
- "version": "0.2.0-003-cli-settings.17",
3
+ "version": "0.2.0-004-update-work-item.22",
4
4
  "description": "Azure DevOps CLI tool",
5
5
  "type": "module",
6
6
  "bin": {