0

Create Version

by
Published Nov 2, 2023

Create a version (release) in a project. Dates are `yyyy-mm-dd`.

Script jira Verified

The script

Submitted by hugo989 Typescript (fetch-only)
Verified 14 days ago
1
//native
2

3
async function get(auth: RT.Jira, path: string) {
4
  const response = await fetch(`https://${auth.domain}.atlassian.net${path}`, {
5
    headers: {
6
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
7
      Accept: "application/json",
8
    },
9
  })
10
  if (!response.ok) {
11
    throw new Error(`${response.status} ${await response.text()}`)
12
  }
13
  return await response.json()
14
}
15

16
export type DynSelect_project_id = string
17

18
// Resolver: the site's projects (first 100).
19
export async function project_id(auth: RT.Jira) {
20
  const { values } = await get(
21
    auth,
22
    "/rest/api/3/project/search?maxResults=100"
23
  )
24
  return values.map((p: { id: string; key: string; name: string }) => ({
25
    value: p.id,
26
    label: `${p.name} (${p.key})`,
27
  }))
28
}
29

30
/**
31
 * Create Version
32
 * Create a version (release) in a project. Dates are `yyyy-mm-dd`.
33
 */
34
export async function main(
35
  auth: RT.Jira,
36
  project_id: DynSelect_project_id,
37
  name: string,
38
  description: string | undefined,
39
  start_date: string | undefined,
40
  release_date: string | undefined,
41
  archived: boolean | undefined
42
) {
43
  const url = new URL(`https://${auth.domain}.atlassian.net/rest/api/3/version`)
44

45
  const body: { [key: string]: any } = { projectId: Number(project_id), name }
46
  if (description != null && description !== "") {
47
    body.description = description
48
  }
49
  if (start_date != null && start_date !== "") {
50
    body.startDate = start_date
51
  }
52
  if (release_date != null && release_date !== "") {
53
    body.releaseDate = release_date
54
  }
55
  if (archived) {
56
    body.archived = true
57
  }
58

59
  const response = await fetch(url, {
60
    method: "POST",
61
    headers: {
62
      Authorization: `Basic ${btoa(`${auth.username}:${auth.password}`)}`,
63
      "Content-Type": "application/json",
64
      Accept: "application/json",
65
    },
66
    body: JSON.stringify(body),
67
  })
68

69
  if (!response.ok) {
70
    throw new Error(`${response.status} ${await response.text()}`)
71
  }
72

73
  return await response.json()
74
}
75

Other submissions
  • Submitted by hugo697 Typescript (fetch-only)
    Created 538 days ago
    1
    type Jira = {
    2
      username: string;
    3
      password: string;
    4
      domain: string;
    5
    };
    6
    /**
    7
     * Create version
    8
     * Creates a project version.
    9
    
    
    10
    This operation can be accessed anonymously.
    11
    
    
    12
    **[Permissions](#permissions) required:** *Administer Jira* [global permission](https://confluence.atlassian.com/x/x4dKLg) or *Administer Projects* [project permission](https://confluence.atlassian.com/x/yodKLg) for the project the version is added to.
    13
     */
    14
    export async function main(
    15
      auth: Jira,
    16
      body: {
    17
        approvers?: {
    18
          accountId?: string;
    19
          declineReason?: string;
    20
          description?: string;
    21
          status?: string;
    22
          [k: string]: unknown;
    23
        }[];
    24
        archived?: boolean;
    25
        description?: string;
    26
        driver?: string;
    27
        expand?: string;
    28
        id?: string;
    29
        issuesStatusForFixVersion?: {
    30
          done?: number;
    31
          inProgress?: number;
    32
          toDo?: number;
    33
          unmapped?: number;
    34
          [k: string]: unknown;
    35
        };
    36
        moveUnfixedIssuesTo?: string;
    37
        name?: string;
    38
        operations?: {
    39
          href?: string;
    40
          iconClass?: string;
    41
          id?: string;
    42
          label?: string;
    43
          styleClass?: string;
    44
          title?: string;
    45
          weight?: number;
    46
        }[];
    47
        overdue?: boolean;
    48
        project?: string;
    49
        projectId?: number;
    50
        releaseDate?: string;
    51
        released?: boolean;
    52
        self?: string;
    53
        startDate?: string;
    54
        userReleaseDate?: string;
    55
        userStartDate?: string;
    56
      }
    57
    ) {
    58
      const url = new URL(
    59
        `https://${auth.domain}.atlassian.net/rest/api/2/version`
    60
      );
    61
    
    
    62
      const response = await fetch(url, {
    63
        method: "POST",
    64
        headers: {
    65
          "Content-Type": "application/json",
    66
          Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
    67
        },
    68
        body: JSON.stringify(body),
    69
      });
    70
      if (!response.ok) {
    71
        const text = await response.text();
    72
        throw new Error(`${response.status} ${text}`);
    73
      }
    74
      return await response.json();
    75
    }
    76