@backstage/plugin-scaffolder-backend-module-bitbucket-server 0.2.1-next.1 → 0.2.1-next.2

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.
package/dist/index.cjs.js CHANGED
@@ -2,1001 +2,13 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
- var errors = require('@backstage/errors');
6
- var integration = require('@backstage/integration');
7
- var pluginScaffolderNode = require('@backstage/plugin-scaffolder-node');
8
- var fetch = require('node-fetch');
9
- var yaml = require('yaml');
10
- var fs = require('fs-extra');
11
- var backendPluginApi = require('@backstage/backend-plugin-api');
12
- var alpha = require('@backstage/plugin-scaffolder-node/alpha');
5
+ var bitbucketServer = require('./actions/bitbucketServer.cjs.js');
6
+ var bitbucketServerPullRequest = require('./actions/bitbucketServerPullRequest.cjs.js');
7
+ var module$1 = require('./module.cjs.js');
13
8
 
14
- function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e : { default: e }; }
15
9
 
16
- var fetch__default = /*#__PURE__*/_interopDefaultCompat(fetch);
17
- var yaml__default = /*#__PURE__*/_interopDefaultCompat(yaml);
18
- var fs__default = /*#__PURE__*/_interopDefaultCompat(fs);
19
10
 
20
- const examples$1 = [
21
- {
22
- description: "Initialize git repository with required properties",
23
- example: yaml__default.default.stringify({
24
- steps: [
25
- {
26
- action: "publish:bitbucketServer",
27
- id: "publish-bitbucket-server-minimal",
28
- name: "Publish To Bitbucket Server",
29
- input: {
30
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo"
31
- }
32
- }
33
- ]
34
- })
35
- },
36
- {
37
- description: "Initialize git repository with all properties",
38
- example: yaml__default.default.stringify({
39
- steps: [
40
- {
41
- action: "publish:bitbucketServer",
42
- id: "publish-bitbucket-server-minimal",
43
- name: "Publish To Bitbucket Server",
44
- input: {
45
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
46
- description: "This is a test repository",
47
- repoVisibility: "private",
48
- defaultBranch: "main",
49
- sourcePath: "packages/backend",
50
- enableLFS: false,
51
- token: "test-token",
52
- gitCommitMessage: "Init check commit",
53
- gitAuthorName: "Test User",
54
- gitAuthorEmail: "test.user@example.com"
55
- }
56
- }
57
- ]
58
- })
59
- },
60
- {
61
- description: "Initialize git repository with public visibility",
62
- example: yaml__default.default.stringify({
63
- steps: [
64
- {
65
- action: "publish:bitbucketServer",
66
- id: "publish-bitbucket-server-minimal",
67
- name: "Publish To Bitbucket Server",
68
- input: {
69
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
70
- description: "This is a test repository",
71
- repoVisibility: "public",
72
- defaultBranch: "main",
73
- sourcePath: "packages/backend",
74
- enableLFS: true,
75
- token: "test-token",
76
- gitCommitMessage: "Init check commit",
77
- gitAuthorName: "Test User",
78
- gitAuthorEmail: "test.user@example.com"
79
- }
80
- }
81
- ]
82
- })
83
- },
84
- {
85
- description: "Initialize git repository with different default branch",
86
- example: yaml__default.default.stringify({
87
- steps: [
88
- {
89
- action: "publish:bitbucketServer",
90
- id: "publish-bitbucket-server-minimal",
91
- name: "Publish To Bitbucket Server",
92
- input: {
93
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
94
- description: "This is a test repository",
95
- repoVisibility: "public",
96
- defaultBranch: "develop",
97
- sourcePath: "packages/backend",
98
- enableLFS: true,
99
- token: "test-token",
100
- gitCommitMessage: "Init check commit",
101
- gitAuthorName: "Test User",
102
- gitAuthorEmail: "test.user@example.com"
103
- }
104
- }
105
- ]
106
- })
107
- },
108
- {
109
- description: "Initialize git repository with different source path",
110
- example: yaml__default.default.stringify({
111
- steps: [
112
- {
113
- action: "publish:bitbucketServer",
114
- id: "publish-bitbucket-server-minimal",
115
- name: "Publish To Bitbucket Server",
116
- input: {
117
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
118
- description: "This is a test repository",
119
- repoVisibility: "public",
120
- defaultBranch: "develop",
121
- sourcePath: "packages/api",
122
- enableLFS: true,
123
- token: "test-token",
124
- gitCommitMessage: "Init check commit",
125
- gitAuthorName: "Test User",
126
- gitAuthorEmail: "test.user@example.com"
127
- }
128
- }
129
- ]
130
- })
131
- },
132
- {
133
- description: "Initialize git repository with default settings and custom author information",
134
- example: yaml__default.default.stringify({
135
- steps: [
136
- {
137
- action: "publish:bitbucketServer",
138
- id: "publish-bitbucket-server-custom-author",
139
- name: "Publish To Bitbucket Server",
140
- input: {
141
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
142
- gitAuthorName: "Custom Author",
143
- gitAuthorEmail: "custom.author@example.com"
144
- }
145
- }
146
- ]
147
- })
148
- },
149
- {
150
- description: "Initialize git repository with LFS enabled and a specific commit message",
151
- example: yaml__default.default.stringify({
152
- steps: [
153
- {
154
- action: "publish:bitbucketServer",
155
- id: "publish-bitbucket-server-lfs-commit",
156
- name: "Publish To Bitbucket Server",
157
- input: {
158
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
159
- enableLFS: true,
160
- gitCommitMessage: "Initial commit with LFS enabled"
161
- }
162
- }
163
- ]
164
- })
165
- },
166
- {
167
- description: "Initialize git repository with a custom source path and token",
168
- example: yaml__default.default.stringify({
169
- steps: [
170
- {
171
- action: "publish:bitbucketServer",
172
- id: "publish-bitbucket-server-custom-source",
173
- name: "Publish To Bitbucket Server",
174
- input: {
175
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
176
- sourcePath: "custom/source/path",
177
- token: "custom-token"
178
- }
179
- }
180
- ]
181
- })
182
- },
183
- {
184
- description: "Initialize git repository with private visibility and custom author details",
185
- example: yaml__default.default.stringify({
186
- steps: [
187
- {
188
- action: "publish:bitbucketServer",
189
- id: "publish-bitbucket-server-private-custom-author",
190
- name: "Publish To Bitbucket Server",
191
- input: {
192
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
193
- repoVisibility: "private",
194
- gitAuthorName: "Private Author",
195
- gitAuthorEmail: "private.author@example.com"
196
- }
197
- }
198
- ]
199
- })
200
- },
201
- {
202
- description: "Initialize git repository with public visibility and specific commit message",
203
- example: yaml__default.default.stringify({
204
- steps: [
205
- {
206
- action: "publish:bitbucketServer",
207
- id: "publish-bitbucket-server-public-commit",
208
- name: "Publish To Bitbucket Server",
209
- input: {
210
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
211
- repoVisibility: "public",
212
- gitCommitMessage: "Public repository initial commit"
213
- }
214
- }
215
- ]
216
- })
217
- },
218
- {
219
- description: "Initialize git repository with all settings customized",
220
- example: yaml__default.default.stringify({
221
- steps: [
222
- {
223
- action: "publish:bitbucketServer",
224
- id: "publish-bitbucket-server-all-custom",
225
- name: "Publish To Bitbucket Server",
226
- input: {
227
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
228
- description: "A fully customized repository",
229
- repoVisibility: "private",
230
- defaultBranch: "development",
231
- sourcePath: "src/backend",
232
- enableLFS: true,
233
- token: "custom-token",
234
- gitCommitMessage: "Fully customized initial commit",
235
- gitAuthorName: "Custom Dev",
236
- gitAuthorEmail: "custom.dev@example.com"
237
- }
238
- }
239
- ]
240
- })
241
- },
242
- {
243
- description: "Initialize git repository with a specific default branch and no LFS",
244
- example: yaml__default.default.stringify({
245
- steps: [
246
- {
247
- action: "publish:bitbucketServer",
248
- id: "publish-bitbucket-server-no-lfs",
249
- name: "Publish To Bitbucket Server",
250
- input: {
251
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
252
- defaultBranch: "main",
253
- enableLFS: false
254
- }
255
- }
256
- ]
257
- })
258
- },
259
- {
260
- description: "Initialize git repository with a custom repository description and public visibility",
261
- example: yaml__default.default.stringify({
262
- steps: [
263
- {
264
- action: "publish:bitbucketServer",
265
- id: "publish-bitbucket-server-custom-description",
266
- name: "Publish To Bitbucket Server",
267
- input: {
268
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
269
- description: "A public repository with a custom description",
270
- repoVisibility: "public"
271
- }
272
- }
273
- ]
274
- })
275
- },
276
- {
277
- description: "Initialize git repository with a custom token for authentication",
278
- example: yaml__default.default.stringify({
279
- steps: [
280
- {
281
- action: "publish:bitbucketServer",
282
- id: "publish-bitbucket-server-custom-token",
283
- name: "Publish To Bitbucket Server",
284
- input: {
285
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
286
- token: "custom-auth-token"
287
- }
288
- }
289
- ]
290
- })
291
- },
292
- {
293
- description: "Initialize git repository with a different repository root path",
294
- example: yaml__default.default.stringify({
295
- steps: [
296
- {
297
- action: "publish:bitbucketServer",
298
- id: "publish-bitbucket-server-different-root",
299
- name: "Publish To Bitbucket Server",
300
- input: {
301
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
302
- sourcePath: "different/root/path"
303
- }
304
- }
305
- ]
306
- })
307
- },
308
- {
309
- description: "Initialize git repository with private visibility and LFS enabled",
310
- example: yaml__default.default.stringify({
311
- steps: [
312
- {
313
- action: "publish:bitbucketServer",
314
- id: "publish-bitbucket-server-private-lfs",
315
- name: "Publish To Bitbucket Server",
316
- input: {
317
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
318
- repoVisibility: "private",
319
- enableLFS: true
320
- }
321
- }
322
- ]
323
- })
324
- }
325
- ];
326
-
327
- const createRepository = async (opts) => {
328
- const {
329
- project,
330
- repo,
331
- description,
332
- authorization,
333
- repoVisibility,
334
- defaultBranch,
335
- apiBaseUrl
336
- } = opts;
337
- let response;
338
- const options = {
339
- method: "POST",
340
- body: JSON.stringify({
341
- name: repo,
342
- description,
343
- defaultBranch,
344
- public: repoVisibility === "public"
345
- }),
346
- headers: {
347
- Authorization: authorization,
348
- "Content-Type": "application/json"
349
- }
350
- };
351
- try {
352
- response = await fetch__default.default(`${apiBaseUrl}/projects/${project}/repos`, options);
353
- } catch (e) {
354
- throw new Error(`Unable to create repository, ${e}`);
355
- }
356
- if (response.status !== 201) {
357
- throw new Error(
358
- `Unable to create repository, ${response.status} ${response.statusText}, ${await response.text()}`
359
- );
360
- }
361
- const r = await response.json();
362
- let remoteUrl = "";
363
- for (const link of r.links.clone) {
364
- if (link.name === "http") {
365
- remoteUrl = link.href;
366
- }
367
- }
368
- const repoContentsUrl = `${r.links.self[0].href}`;
369
- return { remoteUrl, repoContentsUrl };
370
- };
371
- const performEnableLFS = async (opts) => {
372
- const { authorization, host, project, repo } = opts;
373
- const options = {
374
- method: "PUT",
375
- headers: {
376
- Authorization: authorization
377
- }
378
- };
379
- const { ok, status, statusText } = await fetch__default.default(
380
- `https://${host}/rest/git-lfs/admin/projects/${project}/repos/${repo}/enabled`,
381
- options
382
- );
383
- if (!ok)
384
- throw new Error(
385
- `Failed to enable LFS in the repository, ${status}: ${statusText}`
386
- );
387
- };
388
- function createPublishBitbucketServerAction(options) {
389
- const { integrations, config } = options;
390
- return pluginScaffolderNode.createTemplateAction({
391
- id: "publish:bitbucketServer",
392
- description: "Initializes a git repository of the content in the workspace, and publishes it to Bitbucket Server.",
393
- examples: examples$1,
394
- schema: {
395
- input: {
396
- type: "object",
397
- required: ["repoUrl"],
398
- properties: {
399
- repoUrl: {
400
- title: "Repository Location",
401
- type: "string"
402
- },
403
- description: {
404
- title: "Repository Description",
405
- type: "string"
406
- },
407
- repoVisibility: {
408
- title: "Repository Visibility",
409
- type: "string",
410
- enum: ["private", "public"]
411
- },
412
- defaultBranch: {
413
- title: "Default Branch",
414
- type: "string",
415
- description: `Sets the default branch on the repository. The default value is 'master'`
416
- },
417
- sourcePath: {
418
- title: "Source Path",
419
- description: "Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the repository.",
420
- type: "string"
421
- },
422
- enableLFS: {
423
- title: "Enable LFS?",
424
- description: "Enable LFS for the repository.",
425
- type: "boolean"
426
- },
427
- token: {
428
- title: "Authentication Token",
429
- type: "string",
430
- description: "The token to use for authorization to BitBucket Server"
431
- },
432
- gitCommitMessage: {
433
- title: "Git Commit Message",
434
- type: "string",
435
- description: `Sets the commit message on the repository. The default value is 'initial commit'`
436
- },
437
- gitAuthorName: {
438
- title: "Author Name",
439
- type: "string",
440
- description: `Sets the author name for the commit. The default value is 'Scaffolder'`
441
- },
442
- gitAuthorEmail: {
443
- title: "Author Email",
444
- type: "string",
445
- description: `Sets the author email for the commit.`
446
- }
447
- }
448
- },
449
- output: {
450
- type: "object",
451
- properties: {
452
- remoteUrl: {
453
- title: "A URL to the repository with the provider",
454
- type: "string"
455
- },
456
- repoContentsUrl: {
457
- title: "A URL to the root of the repository",
458
- type: "string"
459
- },
460
- commitHash: {
461
- title: "The git commit hash of the initial commit",
462
- type: "string"
463
- }
464
- }
465
- }
466
- },
467
- async handler(ctx) {
468
- const {
469
- repoUrl,
470
- description,
471
- defaultBranch = "master",
472
- repoVisibility = "private",
473
- enableLFS = false,
474
- gitCommitMessage = "initial commit",
475
- gitAuthorName,
476
- gitAuthorEmail
477
- } = ctx.input;
478
- const { project, repo, host } = pluginScaffolderNode.parseRepoUrl(repoUrl, integrations);
479
- if (!project) {
480
- throw new errors.InputError(
481
- `Invalid URL provider was included in the repo URL to create ${ctx.input.repoUrl}, missing project`
482
- );
483
- }
484
- const integrationConfig = integrations.bitbucketServer.byHost(host);
485
- if (!integrationConfig) {
486
- throw new errors.InputError(
487
- `No matching integration configuration for host ${host}, please check your integrations config`
488
- );
489
- }
490
- const token = ctx.input.token ?? integrationConfig.config.token;
491
- const authConfig = {
492
- ...integrationConfig.config,
493
- ...{ token }
494
- };
495
- const reqOpts = integration.getBitbucketServerRequestOptions(authConfig);
496
- const authorization = reqOpts.headers.Authorization;
497
- if (!authorization) {
498
- throw new Error(
499
- `Authorization has not been provided for ${integrationConfig.config.host}. Please add either (a) a user login auth token, or (b) a token or (c) username + password to the integration config.`
500
- );
501
- }
502
- const apiBaseUrl = integrationConfig.config.apiBaseUrl;
503
- const { remoteUrl, repoContentsUrl } = await createRepository({
504
- authorization,
505
- project,
506
- repo,
507
- repoVisibility,
508
- defaultBranch,
509
- description,
510
- apiBaseUrl
511
- });
512
- const gitAuthorInfo = {
513
- name: gitAuthorName ? gitAuthorName : config.getOptionalString("scaffolder.defaultAuthor.name"),
514
- email: gitAuthorEmail ? gitAuthorEmail : config.getOptionalString("scaffolder.defaultAuthor.email")
515
- };
516
- const auth = authConfig.token ? {
517
- token
518
- } : {
519
- username: authConfig.username,
520
- password: authConfig.password
521
- };
522
- const commitResult = await pluginScaffolderNode.initRepoAndPush({
523
- dir: pluginScaffolderNode.getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath),
524
- remoteUrl,
525
- auth,
526
- defaultBranch,
527
- logger: ctx.logger,
528
- commitMessage: gitCommitMessage ? gitCommitMessage : config.getOptionalString("scaffolder.defaultCommitMessage"),
529
- gitAuthorInfo
530
- });
531
- if (enableLFS) {
532
- await performEnableLFS({ authorization, host, project, repo });
533
- }
534
- ctx.output("commitHash", commitResult?.commitHash);
535
- ctx.output("remoteUrl", remoteUrl);
536
- ctx.output("repoContentsUrl", repoContentsUrl);
537
- }
538
- });
539
- }
540
-
541
- const examples = [
542
- {
543
- description: "Creating pull request on bitbucket server with required fields",
544
- example: yaml__default.default.stringify({
545
- steps: [
546
- {
547
- action: "publish:bitbucketServer:pull-request",
548
- id: "publish-bitbucket-server-pull-request-minimal",
549
- name: "Creating pull request on bitbucket server",
550
- input: {
551
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
552
- title: "My pull request",
553
- sourceBranch: "my-feature-branch"
554
- }
555
- }
556
- ]
557
- })
558
- },
559
- {
560
- description: "Creating pull request on bitbucket server with custom descriptions",
561
- example: yaml__default.default.stringify({
562
- steps: [
563
- {
564
- action: "publish:bitbucketServer:pull-request",
565
- id: "publish-bitbucket-server-pull-request-minimal",
566
- name: "Creating pull request on bitbucket server",
567
- input: {
568
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
569
- title: "My pull request",
570
- sourceBranch: "my-feature-branch",
571
- description: "This is a detailed description of my pull request"
572
- }
573
- }
574
- ]
575
- })
576
- },
577
- {
578
- description: "Creating pull request on bitbucket server with different target branch",
579
- example: yaml__default.default.stringify({
580
- steps: [
581
- {
582
- action: "publish:bitbucketServer:pull-request",
583
- id: "publish-bitbucket-server-pull-request-target-branch",
584
- name: "Creating pull request on bitbucket server",
585
- input: {
586
- repoUrl: "hosted.bitbucket.com?project=project&repo=repo",
587
- title: "My pull request",
588
- sourceBranch: "my-feature-branch",
589
- targetBranch: "development"
590
- }
591
- }
592
- ]
593
- })
594
- },
595
- {
596
- description: "Creating pull request on bitbucket server with authorization token",
597
- example: yaml__default.default.stringify({
598
- steps: [
599
- {
600
- action: "publish:bitbucketServer:pull-request",
601
- id: "publish-bitbucket-server-pull-request-minimal",
602
- name: "Creating pull request on bitbucket server",
603
- input: {
604
- repoUrl: "no-credentials.bitbucket.com?project=project&repo=repo",
605
- title: "My pull request",
606
- sourceBranch: "my-feature-branch",
607
- token: "my-auth-token"
608
- }
609
- }
610
- ]
611
- })
612
- },
613
- {
614
- description: "Creating pull request on bitbucket server with all fields",
615
- example: yaml__default.default.stringify({
616
- steps: [
617
- {
618
- action: "publish:bitbucketServer:pull-request",
619
- id: "publish-bitbucket-server-pull-request-minimal",
620
- name: "Creating pull request on bitbucket server",
621
- input: {
622
- repoUrl: "no-credentials.bitbucket.com?project=project&repo=repo",
623
- title: "My pull request",
624
- sourceBranch: "my-feature-branch",
625
- targetBranch: "development",
626
- description: "This is a detailed description of my pull request",
627
- token: "my-auth-token",
628
- gitAuthorName: "test-user",
629
- gitAuthorEmail: "test-user@sample.com"
630
- }
631
- }
632
- ]
633
- })
634
- }
635
- ];
636
-
637
- const createPullRequest = async (opts) => {
638
- const {
639
- project,
640
- repo,
641
- title,
642
- description,
643
- toRef,
644
- fromRef,
645
- authorization,
646
- apiBaseUrl
647
- } = opts;
648
- let response;
649
- const data = {
650
- method: "POST",
651
- body: JSON.stringify({
652
- title,
653
- description,
654
- state: "OPEN",
655
- open: true,
656
- closed: false,
657
- locked: true,
658
- toRef,
659
- fromRef
660
- }),
661
- headers: {
662
- Authorization: authorization,
663
- "Content-Type": "application/json"
664
- }
665
- };
666
- try {
667
- response = await fetch__default.default(
668
- `${apiBaseUrl}/projects/${encodeURIComponent(
669
- project
670
- )}/repos/${encodeURIComponent(repo)}/pull-requests`,
671
- data
672
- );
673
- } catch (e) {
674
- throw new Error(`Unable to create pull-reqeusts, ${e}`);
675
- }
676
- if (response.status !== 201) {
677
- throw new Error(
678
- `Unable to create pull requests, ${response.status} ${response.statusText}, ${await response.text()}`
679
- );
680
- }
681
- const r = await response.json();
682
- return `${r.links.self[0].href}`;
683
- };
684
- const findBranches = async (opts) => {
685
- const { project, repo, branchName, authorization, apiBaseUrl } = opts;
686
- let response;
687
- const options = {
688
- method: "GET",
689
- headers: {
690
- Authorization: authorization,
691
- "Content-Type": "application/json"
692
- }
693
- };
694
- try {
695
- response = await fetch__default.default(
696
- `${apiBaseUrl}/projects/${encodeURIComponent(
697
- project
698
- )}/repos/${encodeURIComponent(
699
- repo
700
- )}/branches?boostMatches=true&filterText=${encodeURIComponent(
701
- branchName
702
- )}`,
703
- options
704
- );
705
- } catch (e) {
706
- throw new Error(`Unable to get branches, ${e}`);
707
- }
708
- if (response.status !== 200) {
709
- throw new Error(
710
- `Unable to get branches, ${response.status} ${response.statusText}, ${await response.text()}`
711
- );
712
- }
713
- const r = await response.json();
714
- for (const object of r.values) {
715
- if (object.displayId === branchName) {
716
- return object;
717
- }
718
- }
719
- return void 0;
720
- };
721
- const createBranch = async (opts) => {
722
- const { project, repo, branchName, authorization, apiBaseUrl, startPoint } = opts;
723
- let response;
724
- const options = {
725
- method: "POST",
726
- body: JSON.stringify({
727
- name: branchName,
728
- startPoint
729
- }),
730
- headers: {
731
- Authorization: authorization,
732
- "Content-Type": "application/json"
733
- }
734
- };
735
- try {
736
- response = await fetch__default.default(
737
- `${apiBaseUrl}/projects/${encodeURIComponent(
738
- project
739
- )}/repos/${encodeURIComponent(repo)}/branches`,
740
- options
741
- );
742
- } catch (e) {
743
- throw new Error(`Unable to create branch, ${e}`);
744
- }
745
- if (response.status !== 200) {
746
- throw new Error(
747
- `Unable to create branch, ${response.status} ${response.statusText}, ${await response.text()}`
748
- );
749
- }
750
- return await response.json();
751
- };
752
- const getDefaultBranch = async (opts) => {
753
- const { project, repo, authorization, apiBaseUrl } = opts;
754
- let response;
755
- const options = {
756
- method: "GET",
757
- headers: {
758
- Authorization: authorization,
759
- "Content-Type": "application/json"
760
- }
761
- };
762
- try {
763
- response = await fetch__default.default(
764
- `${apiBaseUrl}/projects/${project}/repos/${repo}/default-branch`,
765
- options
766
- );
767
- } catch (error) {
768
- throw error;
769
- }
770
- const { displayId } = await response.json();
771
- const defaultBranch = displayId;
772
- if (!defaultBranch) {
773
- throw new Error(`Could not fetch default branch for ${project}/${repo}`);
774
- }
775
- return defaultBranch;
776
- };
777
- const isApiBaseUrlHttps = (apiBaseUrl) => {
778
- const url = new URL(apiBaseUrl);
779
- return url.protocol === "https:";
780
- };
781
- function createPublishBitbucketServerPullRequestAction(options) {
782
- const { integrations, config } = options;
783
- return pluginScaffolderNode.createTemplateAction({
784
- id: "publish:bitbucketServer:pull-request",
785
- examples,
786
- schema: {
787
- input: {
788
- type: "object",
789
- required: ["repoUrl", "title", "sourceBranch"],
790
- properties: {
791
- repoUrl: {
792
- title: "Repository Location",
793
- type: "string"
794
- },
795
- title: {
796
- title: "Pull Request title",
797
- type: "string",
798
- description: "The title for the pull request"
799
- },
800
- description: {
801
- title: "Pull Request Description",
802
- type: "string",
803
- description: "The description of the pull request"
804
- },
805
- targetBranch: {
806
- title: "Target Branch",
807
- type: "string",
808
- description: `Branch of repository to apply changes to. The default value is 'master'`
809
- },
810
- sourceBranch: {
811
- title: "Source Branch",
812
- type: "string",
813
- description: "Branch of repository to copy changes from"
814
- },
815
- token: {
816
- title: "Authorization Token",
817
- type: "string",
818
- description: "The token to use for authorization to BitBucket Server"
819
- },
820
- gitAuthorName: {
821
- title: "Author Name",
822
- type: "string",
823
- description: `Sets the author name for the commit. The default value is 'Scaffolder'`
824
- },
825
- gitAuthorEmail: {
826
- title: "Author Email",
827
- type: "string",
828
- description: `Sets the author email for the commit.`
829
- }
830
- }
831
- },
832
- output: {
833
- type: "object",
834
- properties: {
835
- pullRequestUrl: {
836
- title: "A URL to the pull request with the provider",
837
- type: "string"
838
- }
839
- }
840
- }
841
- },
842
- async handler(ctx) {
843
- const {
844
- repoUrl,
845
- title,
846
- description,
847
- targetBranch,
848
- sourceBranch,
849
- gitAuthorName,
850
- gitAuthorEmail
851
- } = ctx.input;
852
- const { project, repo, host } = pluginScaffolderNode.parseRepoUrl(repoUrl, integrations);
853
- if (!project) {
854
- throw new errors.InputError(
855
- `Invalid URL provider was included in the repo URL to create ${ctx.input.repoUrl}, missing project`
856
- );
857
- }
858
- const integrationConfig = integrations.bitbucketServer.byHost(host);
859
- if (!integrationConfig) {
860
- throw new errors.InputError(
861
- `No matching integration configuration for host ${host}, please check your integrations config`
862
- );
863
- }
864
- const token = ctx.input.token ?? integrationConfig.config.token;
865
- const authConfig = {
866
- ...integrationConfig.config,
867
- ...{ token }
868
- };
869
- const reqOpts = integration.getBitbucketServerRequestOptions(authConfig);
870
- const authorization = reqOpts.headers.Authorization;
871
- if (!authorization) {
872
- throw new Error(
873
- `Authorization has not been provided for ${integrationConfig.config.host}. Please add either (a) a user login auth token, or (b) a token input from the template or (c) username + password to the integration config.`
874
- );
875
- }
876
- const apiBaseUrl = integrationConfig.config.apiBaseUrl;
877
- let finalTargetBranch = targetBranch;
878
- if (!finalTargetBranch) {
879
- finalTargetBranch = await getDefaultBranch({
880
- project,
881
- repo,
882
- authorization,
883
- apiBaseUrl
884
- });
885
- }
886
- const toRef = await findBranches({
887
- project,
888
- repo,
889
- branchName: finalTargetBranch,
890
- authorization,
891
- apiBaseUrl
892
- });
893
- let fromRef = await findBranches({
894
- project,
895
- repo,
896
- branchName: sourceBranch,
897
- authorization,
898
- apiBaseUrl
899
- });
900
- if (!fromRef) {
901
- ctx.logger.info(
902
- `source branch not found -> creating branch named: ${sourceBranch} lastCommit: ${toRef.latestCommit}`
903
- );
904
- const latestCommit = toRef.latestCommit;
905
- fromRef = await createBranch({
906
- project,
907
- repo,
908
- branchName: sourceBranch,
909
- authorization,
910
- apiBaseUrl,
911
- startPoint: latestCommit
912
- });
913
- const isHttps = isApiBaseUrlHttps(apiBaseUrl);
914
- const remoteUrl = `${isHttps ? "https" : "http"}://${host}/scm/${project}/${repo}.git`;
915
- const auth = authConfig.token ? {
916
- token
917
- } : {
918
- username: authConfig.username,
919
- password: authConfig.password
920
- };
921
- const gitAuthorInfo = {
922
- name: gitAuthorName || config.getOptionalString("scaffolder.defaultAuthor.name"),
923
- email: gitAuthorEmail || config.getOptionalString("scaffolder.defaultAuthor.email")
924
- };
925
- const tempDir = await ctx.createTemporaryDirectory();
926
- const sourceDir = pluginScaffolderNode.getRepoSourceDirectory(ctx.workspacePath, void 0);
927
- await pluginScaffolderNode.cloneRepo({
928
- url: remoteUrl,
929
- dir: tempDir,
930
- auth,
931
- logger: ctx.logger,
932
- ref: sourceBranch
933
- });
934
- await pluginScaffolderNode.createBranch({
935
- dir: tempDir,
936
- auth,
937
- logger: ctx.logger,
938
- ref: sourceBranch
939
- });
940
- fs__default.default.cpSync(sourceDir, tempDir, {
941
- recursive: true,
942
- filter: (path) => {
943
- return !(path.indexOf(".git") > -1);
944
- }
945
- });
946
- await pluginScaffolderNode.addFiles({
947
- dir: tempDir,
948
- auth,
949
- logger: ctx.logger,
950
- filepath: "."
951
- });
952
- await pluginScaffolderNode.commitAndPushBranch({
953
- dir: tempDir,
954
- auth,
955
- logger: ctx.logger,
956
- commitMessage: description ?? config.getOptionalString("scaffolder.defaultCommitMessage") ?? "",
957
- gitAuthorInfo,
958
- branch: sourceBranch
959
- });
960
- }
961
- const pullRequestUrl = await createPullRequest({
962
- project,
963
- repo,
964
- title,
965
- description,
966
- toRef,
967
- fromRef,
968
- authorization,
969
- apiBaseUrl
970
- });
971
- ctx.output("pullRequestUrl", pullRequestUrl);
972
- }
973
- });
974
- }
975
-
976
- const bitbucketServerModule = backendPluginApi.createBackendModule({
977
- moduleId: "bitbucketServer",
978
- pluginId: "scaffolder",
979
- register({ registerInit }) {
980
- registerInit({
981
- deps: {
982
- scaffolder: alpha.scaffolderActionsExtensionPoint,
983
- config: backendPluginApi.coreServices.rootConfig
984
- },
985
- async init({ scaffolder, config }) {
986
- const integrations = integration.ScmIntegrations.fromConfig(config);
987
- scaffolder.addActions(
988
- createPublishBitbucketServerAction({ integrations, config }),
989
- createPublishBitbucketServerPullRequestAction({
990
- integrations,
991
- config
992
- })
993
- );
994
- }
995
- });
996
- }
997
- });
998
-
999
- exports.createPublishBitbucketServerAction = createPublishBitbucketServerAction;
1000
- exports.createPublishBitbucketServerPullRequestAction = createPublishBitbucketServerPullRequestAction;
1001
- exports.default = bitbucketServerModule;
11
+ exports.createPublishBitbucketServerAction = bitbucketServer.createPublishBitbucketServerAction;
12
+ exports.createPublishBitbucketServerPullRequestAction = bitbucketServerPullRequest.createPublishBitbucketServerPullRequestAction;
13
+ exports.default = module$1.bitbucketServerModule;
1002
14
  //# sourceMappingURL=index.cjs.js.map