0
Create a tag
One script reply has been approved by the moderators Verified

Creates a new tag in the specified repository.

Created by hugo697 396 days ago Viewed 9005 times
0
Submitted by hugo697 Typescript (fetch-only)
Verified 396 days ago
1
type Bitbucket = {
2
  username: string;
3
  password: string;
4
};
5
/**
6
 * Create a tag
7
 * Creates a new tag in the specified repository.
8
 */
9
export async function main(
10
  auth: Bitbucket,
11
  repo_slug: string,
12
  workspace: string,
13
  body: {
14
    type: string;
15
    links?: {
16
      self?: { href?: string; name?: string };
17
      commits?: { href?: string; name?: string };
18
      html?: { href?: string; name?: string };
19
    };
20
    name?: string;
21
    target?: ({ type: string; [k: string]: unknown } & {
22
      hash?: string;
23
      date?: string;
24
      author?: { type: string; [k: string]: unknown } & {
25
        raw?: string;
26
        user?: { type: string; [k: string]: unknown } & {
27
          links?: {
28
            avatar?: { href?: string; name?: string };
29
            [k: string]: unknown;
30
          };
31
          created_on?: string;
32
          display_name?: string;
33
          username?: string;
34
          uuid?: string;
35
          [k: string]: unknown;
36
        };
37
        [k: string]: unknown;
38
      };
39
      message?: string;
40
      summary?: {
41
        raw?: string;
42
        markup?: "markdown" | "creole" | "plaintext";
43
        html?: string;
44
      };
45
      parents?: unknown[];
46
      [k: string]: unknown;
47
    }) & {
48
      repository?: { type: string; [k: string]: unknown } & {
49
        links?: {
50
          self?: { href?: string; name?: string };
51
          html?: { href?: string; name?: string };
52
          avatar?: { href?: string; name?: string };
53
          pullrequests?: { href?: string; name?: string };
54
          commits?: { href?: string; name?: string };
55
          forks?: { href?: string; name?: string };
56
          watchers?: { href?: string; name?: string };
57
          downloads?: { href?: string; name?: string };
58
          clone?: { href?: string; name?: string }[];
59
          hooks?: { href?: string; name?: string };
60
        };
61
        uuid?: string;
62
        full_name?: string;
63
        is_private?: boolean;
64
        parent?: unknown;
65
        scm?: "git";
66
        owner?: { type: string; [k: string]: unknown } & {
67
          links?: {
68
            avatar?: { href?: string; name?: string };
69
            [k: string]: unknown;
70
          };
71
          created_on?: string;
72
          display_name?: string;
73
          username?: string;
74
          uuid?: string;
75
          [k: string]: unknown;
76
        };
77
        name?: string;
78
        description?: string;
79
        created_on?: string;
80
        updated_on?: string;
81
        size?: number;
82
        language?: string;
83
        has_issues?: boolean;
84
        has_wiki?: boolean;
85
        fork_policy?: "allow_forks" | "no_public_forks" | "no_forks";
86
        project?: { type: string; [k: string]: unknown } & {
87
          links?: {
88
            html?: { href?: string; name?: string };
89
            avatar?: { href?: string; name?: string };
90
          };
91
          uuid?: string;
92
          key?: string;
93
          owner?: ({ type: string; [k: string]: unknown } & {
94
            links?: {
95
              avatar?: { href?: string; name?: string };
96
              [k: string]: unknown;
97
            };
98
            created_on?: string;
99
            display_name?: string;
100
            username?: string;
101
            uuid?: string;
102
            [k: string]: unknown;
103
          }) & {
104
            links?: {
105
              avatar?: { href?: string; name?: string };
106
              [k: string]: unknown;
107
            } & {
108
              self?: { href?: string; name?: string };
109
              html?: { href?: string; name?: string };
110
              members?: { href?: string; name?: string };
111
              projects?: { href?: string; name?: string };
112
              repositories?: { href?: string; name?: string };
113
              [k: string]: unknown;
114
            };
115
            [k: string]: unknown;
116
          };
117
          name?: string;
118
          description?: string;
119
          is_private?: boolean;
120
          created_on?: string;
121
          updated_on?: string;
122
          has_publicly_visible_repos?: boolean;
123
          [k: string]: unknown;
124
        };
125
        mainbranch?: { [k: string]: unknown } & {
126
          merge_strategies?: ("merge_commit" | "squash" | "fast_forward")[];
127
          default_merge_strategy?: string;
128
          [k: string]: unknown;
129
        };
130
        [k: string]: unknown;
131
      };
132
      participants?: ({ type: string; [k: string]: unknown } & {
133
        user?: { type: string; [k: string]: unknown } & {
134
          links?: {
135
            avatar?: { href?: string; name?: string };
136
            [k: string]: unknown;
137
          };
138
          created_on?: string;
139
          display_name?: string;
140
          username?: string;
141
          uuid?: string;
142
          [k: string]: unknown;
143
        };
144
        role?: "PARTICIPANT" | "REVIEWER";
145
        approved?: boolean;
146
        state?: "approved" | "changes_requested" | null;
147
        participated_on?: string;
148
        [k: string]: unknown;
149
      })[];
150
      [k: string]: unknown;
151
    };
152
    [k: string]: unknown;
153
  } & {
154
    message?: string;
155
    date?: string;
156
    tagger?: { type: string; [k: string]: unknown } & {
157
      raw?: string;
158
      user?: { type: string; [k: string]: unknown } & {
159
        links?: {
160
          avatar?: { href?: string; name?: string };
161
          [k: string]: unknown;
162
        };
163
        created_on?: string;
164
        display_name?: string;
165
        username?: string;
166
        uuid?: string;
167
        [k: string]: unknown;
168
      };
169
      [k: string]: unknown;
170
    };
171
    [k: string]: unknown;
172
  }
173
) {
174
  const url = new URL(
175
    `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/refs/tags`
176
  );
177

178
  const response = await fetch(url, {
179
    method: "POST",
180
    headers: {
181
      "Content-Type": "application/json",
182
      Authorization: "Basic " + btoa(`${auth.username}:${auth.password}`),
183
    },
184
    body: JSON.stringify(body),
185
  });
186
  if (!response.ok) {
187
    const text = await response.text();
188
    throw new Error(`${response.status} ${text}`);
189
  }
190
  return await response.json();
191
}
192