Bulk create workflows

Create workflows and related statuses. **[Permissions](#permissions) required:** * *Administer Jira* project permission to create all, including global-scoped, workflows * *Administer projects* project permissions to create project-scoped workflows

Script jira Verified

by hugo697 ยท 11/2/2023

The script

Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Jira = {
2
  username: string;
3
  password: string;
4
  domain: string;
5
};
6
/**
7
 * Bulk create workflows
8
 * Create workflows and related statuses.
9

10
**[Permissions](#permissions) required:**
11

12
 *  *Administer Jira* project permission to create all, including global-scoped, workflows
13
 *  *Administer projects* project permissions to create project-scoped workflows
14
 */
15
export async function main(
16
  auth: Jira,
17
  body: {
18
    scope: { project?: { id: string }; type: "PROJECT" | "GLOBAL" };
19
    statuses: {
20
      description?: string;
21
      id?: string;
22
      name: string;
23
      statusCategory: "TODO" | "IN_PROGRESS" | "DONE";
24
      statusReference: string;
25
      [k: string]: unknown;
26
    }[];
27
    workflows: {
28
      description?: string;
29
      name: string;
30
      startPointLayout?: { x?: number; y?: number };
31
      statuses: {
32
        layout?: { x?: number; y?: number };
33
        properties: { [k: string]: string };
34
        statusReference: string;
35
        [k: string]: unknown;
36
      }[];
37
      transitions: {
38
        actions?: {
39
          id?: string;
40
          parameters?: { [k: string]: string };
41
          ruleKey: string;
42
        }[];
43
        conditions?: {
44
          conditionGroups?: {}[];
45
          conditions?: {
46
            id?: string;
47
            parameters?: { [k: string]: string };
48
            ruleKey: string;
49
          }[];
50
          operation: "ANY" | "ALL";
51
        };
52
        customIssueEventId?: string;
53
        description?: string;
54
        from?: { port?: number; statusReference: string }[];
55
        id: string;
56
        name: string;
57
        properties?: { [k: string]: string };
58
        to?: { port?: number; statusReference: string };
59
        transitionScreen?: {
60
          id?: string;
61
          parameters?: { [k: string]: string };
62
          ruleKey: string;
63
        };
64
        triggers?: {
65
          id?: string;
66
          parameters: { [k: string]: string };
67
          ruleKey: string;
68
        }[];
69
        type: "INITIAL" | "GLOBAL" | "DIRECTED";
70
        validators?: {
71
          id?: string;
72
          parameters?: { [k: string]: string };
73
          ruleKey: string;
74
        }[];
75
        [k: string]: unknown;
76
      }[];
77
    }[];
78
  }
79
) {
80
  const url = new URL(
81
    `https://${auth.domain}.atlassian.net/rest/api/2/workflows/create`
82
  );
83

84
  const response = await fetch(url, {
85
    method: "POST",
86
    headers: {
87
      "Content-Type": "application/json",
88
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
89
    },
90
    body: JSON.stringify(body),
91
  });
92
  if (!response.ok) {
93
    const text = await response.text();
94
    throw new Error(`${response.status} ${text}`);
95
  }
96
  return await response.json();
97
}
98