@backstage/plugin-scaffolder-backend-module-bitbucket 0.1.2-next.1 → 0.2.0-next.3

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,22 +2,42 @@
2
2
 
3
3
  Object.defineProperty(exports, '__esModule', { value: true });
4
4
 
5
+ var bitbucketCloud = require('@backstage/plugin-scaffolder-backend-module-bitbucket-cloud');
6
+ var bitbucketServer = require('@backstage/plugin-scaffolder-backend-module-bitbucket-server');
5
7
  var errors = require('@backstage/errors');
6
8
  var pluginScaffolderNode = require('@backstage/plugin-scaffolder-node');
7
9
  var fetch = require('node-fetch');
8
10
  var yaml = require('yaml');
9
- var integration = require('@backstage/integration');
10
- var fs = require('fs-extra');
11
11
  var backendPluginApi = require('@backstage/backend-plugin-api');
12
12
  var alpha = require('@backstage/plugin-scaffolder-node/alpha');
13
+ var integration = require('@backstage/integration');
13
14
 
14
15
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
15
16
 
17
+ function _interopNamespace(e) {
18
+ if (e && e.__esModule) return e;
19
+ var n = Object.create(null);
20
+ if (e) {
21
+ Object.keys(e).forEach(function (k) {
22
+ if (k !== 'default') {
23
+ var d = Object.getOwnPropertyDescriptor(e, k);
24
+ Object.defineProperty(n, k, d.get ? d : {
25
+ enumerable: true,
26
+ get: function () { return e[k]; }
27
+ });
28
+ }
29
+ });
30
+ }
31
+ n["default"] = e;
32
+ return Object.freeze(n);
33
+ }
34
+
35
+ var bitbucketCloud__namespace = /*#__PURE__*/_interopNamespace(bitbucketCloud);
36
+ var bitbucketServer__namespace = /*#__PURE__*/_interopNamespace(bitbucketServer);
16
37
  var fetch__default = /*#__PURE__*/_interopDefaultLegacy(fetch);
17
38
  var yaml__default = /*#__PURE__*/_interopDefaultLegacy(yaml);
18
- var fs__default = /*#__PURE__*/_interopDefaultLegacy(fs);
19
39
 
20
- const examples$1 = [
40
+ const examples = [
21
41
  {
22
42
  description: "Initializes a git repository of contents in workspace and publish it to Bitbucket with default configuration.",
23
43
  example: yaml__default["default"].stringify({
@@ -276,7 +296,7 @@ const createBitbucketServerRepository = async (opts) => {
276
296
  const repoContentsUrl = `${r.links.self[0].href}`;
277
297
  return { remoteUrl, repoContentsUrl };
278
298
  };
279
- const getAuthorizationHeader$1 = (config) => {
299
+ const getAuthorizationHeader = (config) => {
280
300
  if (config.username && config.appPassword) {
281
301
  const buffer = Buffer.from(
282
302
  `${config.username}:${config.appPassword}`,
@@ -291,7 +311,7 @@ const getAuthorizationHeader$1 = (config) => {
291
311
  `Authorization has not been provided for Bitbucket. Please add either username + appPassword or token to the Integrations config`
292
312
  );
293
313
  };
294
- const performEnableLFS$1 = async (opts) => {
314
+ const performEnableLFS = async (opts) => {
295
315
  const { authorization, host, project, repo } = opts;
296
316
  const options = {
297
317
  method: "PUT",
@@ -313,7 +333,7 @@ function createPublishBitbucketAction(options) {
313
333
  return pluginScaffolderNode.createTemplateAction({
314
334
  id: "publish:bitbucket",
315
335
  description: "Initializes a git repository of the content in the workspace, and publishes it to Bitbucket.",
316
- examples: examples$1,
336
+ examples,
317
337
  schema: {
318
338
  input: {
319
339
  type: "object",
@@ -424,7 +444,7 @@ function createPublishBitbucketAction(options) {
424
444
  `No matching integration configuration for host ${host}, please check your integrations config`
425
445
  );
426
446
  }
427
- const authorization = getAuthorizationHeader$1(
447
+ const authorization = getAuthorizationHeader(
428
448
  ctx.input.token ? {
429
449
  host: integrationConfig.config.host,
430
450
  apiBaseUrl: integrationConfig.config.apiBaseUrl,
@@ -469,210 +489,8 @@ function createPublishBitbucketAction(options) {
469
489
  gitAuthorInfo
470
490
  });
471
491
  if (enableLFS && host !== "bitbucket.org") {
472
- await performEnableLFS$1({ authorization, host, project, repo });
473
- }
474
- ctx.output("commitHash", commitResult == null ? void 0 : commitResult.commitHash);
475
- ctx.output("remoteUrl", remoteUrl);
476
- ctx.output("repoContentsUrl", repoContentsUrl);
477
- }
478
- });
479
- }
480
-
481
- const getAuthorizationHeader = (config) => {
482
- if (config.username && config.appPassword) {
483
- const buffer = Buffer.from(
484
- `${config.username}:${config.appPassword}`,
485
- "utf8"
486
- );
487
- return `Basic ${buffer.toString("base64")}`;
488
- }
489
- if (config.token) {
490
- return `Bearer ${config.token}`;
491
- }
492
- throw new Error(
493
- `Authorization has not been provided for Bitbucket Cloud. Please add either username + appPassword to the Integrations config or a user login auth token`
494
- );
495
- };
496
-
497
- const createRepository$1 = async (opts) => {
498
- const {
499
- workspace,
500
- project,
501
- repo,
502
- description,
503
- repoVisibility,
504
- mainBranch,
505
- authorization,
506
- apiBaseUrl
507
- } = opts;
508
- const options = {
509
- method: "POST",
510
- body: JSON.stringify({
511
- scm: "git",
512
- description,
513
- is_private: repoVisibility === "private",
514
- project: { key: project }
515
- }),
516
- headers: {
517
- Authorization: authorization,
518
- "Content-Type": "application/json"
519
- }
520
- };
521
- let response;
522
- try {
523
- response = await fetch__default["default"](
524
- `${apiBaseUrl}/repositories/${workspace}/${repo}`,
525
- options
526
- );
527
- } catch (e) {
528
- throw new Error(`Unable to create repository, ${e}`);
529
- }
530
- if (response.status !== 200) {
531
- throw new Error(
532
- `Unable to create repository, ${response.status} ${response.statusText}, ${await response.text()}`
533
- );
534
- }
535
- const r = await response.json();
536
- let remoteUrl = "";
537
- for (const link of r.links.clone) {
538
- if (link.name === "https") {
539
- remoteUrl = link.href;
540
- }
541
- }
542
- const repoContentsUrl = `${r.links.html.href}/src/${mainBranch}`;
543
- return { remoteUrl, repoContentsUrl };
544
- };
545
- function createPublishBitbucketCloudAction(options) {
546
- const { integrations, config } = options;
547
- return pluginScaffolderNode.createTemplateAction({
548
- id: "publish:bitbucketCloud",
549
- description: "Initializes a git repository of the content in the workspace, and publishes it to Bitbucket Cloud.",
550
- schema: {
551
- input: {
552
- type: "object",
553
- required: ["repoUrl"],
554
- properties: {
555
- repoUrl: {
556
- title: "Repository Location",
557
- type: "string"
558
- },
559
- description: {
560
- title: "Repository Description",
561
- type: "string"
562
- },
563
- repoVisibility: {
564
- title: "Repository Visibility",
565
- type: "string",
566
- enum: ["private", "public"]
567
- },
568
- defaultBranch: {
569
- title: "Default Branch",
570
- type: "string",
571
- description: `Sets the default branch on the repository. The default value is 'master'`
572
- },
573
- sourcePath: {
574
- title: "Source Path",
575
- description: "Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the repository.",
576
- type: "string"
577
- },
578
- token: {
579
- title: "Authentication Token",
580
- type: "string",
581
- description: "The token to use for authorization to BitBucket Cloud"
582
- }
583
- }
584
- },
585
- output: {
586
- type: "object",
587
- properties: {
588
- remoteUrl: {
589
- title: "A URL to the repository with the provider",
590
- type: "string"
591
- },
592
- repoContentsUrl: {
593
- title: "A URL to the root of the repository",
594
- type: "string"
595
- },
596
- commitHash: {
597
- title: "The git commit hash of the initial commit",
598
- type: "string"
599
- }
600
- }
601
- }
602
- },
603
- async handler(ctx) {
604
- const {
605
- repoUrl,
606
- description,
607
- defaultBranch = "master",
608
- repoVisibility = "private"
609
- } = ctx.input;
610
- const { workspace, project, repo, host } = pluginScaffolderNode.parseRepoUrl(
611
- repoUrl,
612
- integrations
613
- );
614
- if (!workspace) {
615
- throw new errors.InputError(
616
- `Invalid URL provider was included in the repo URL to create ${ctx.input.repoUrl}, missing workspace`
617
- );
618
- }
619
- if (!project) {
620
- throw new errors.InputError(
621
- `Invalid URL provider was included in the repo URL to create ${ctx.input.repoUrl}, missing project`
622
- );
623
- }
624
- const integrationConfig = integrations.bitbucketCloud.byHost(host);
625
- if (!integrationConfig) {
626
- throw new errors.InputError(
627
- `No matching integration configuration for host ${host}, please check your integrations config`
628
- );
629
- }
630
- const authorization = getAuthorizationHeader(
631
- ctx.input.token ? { token: ctx.input.token } : integrationConfig.config
632
- );
633
- const apiBaseUrl = integrationConfig.config.apiBaseUrl;
634
- const { remoteUrl, repoContentsUrl } = await createRepository$1({
635
- authorization,
636
- workspace: workspace || "",
637
- project,
638
- repo,
639
- repoVisibility,
640
- mainBranch: defaultBranch,
641
- description,
642
- apiBaseUrl
643
- });
644
- const gitAuthorInfo = {
645
- name: config.getOptionalString("scaffolder.defaultAuthor.name"),
646
- email: config.getOptionalString("scaffolder.defaultAuthor.email")
647
- };
648
- let auth;
649
- if (ctx.input.token) {
650
- auth = {
651
- username: "x-token-auth",
652
- password: ctx.input.token
653
- };
654
- } else {
655
- if (!integrationConfig.config.username || !integrationConfig.config.appPassword) {
656
- throw new Error(
657
- "Credentials for Bitbucket Cloud integration required for this action."
658
- );
659
- }
660
- auth = {
661
- username: integrationConfig.config.username,
662
- password: integrationConfig.config.appPassword
663
- };
492
+ await performEnableLFS({ authorization, host, project, repo });
664
493
  }
665
- const commitResult = await pluginScaffolderNode.initRepoAndPush({
666
- dir: pluginScaffolderNode.getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath),
667
- remoteUrl,
668
- auth,
669
- defaultBranch,
670
- logger: ctx.logger,
671
- commitMessage: config.getOptionalString(
672
- "scaffolder.defaultCommitMessage"
673
- ),
674
- gitAuthorInfo
675
- });
676
494
  ctx.output("commitHash", commitResult == null ? void 0 : commitResult.commitHash);
677
495
  ctx.output("remoteUrl", remoteUrl);
678
496
  ctx.output("repoContentsUrl", repoContentsUrl);
@@ -680,892 +498,10 @@ function createPublishBitbucketCloudAction(options) {
680
498
  });
681
499
  }
682
500
 
683
- const createRepository = async (opts) => {
684
- const {
685
- project,
686
- repo,
687
- description,
688
- authorization,
689
- repoVisibility,
690
- defaultBranch,
691
- apiBaseUrl
692
- } = opts;
693
- let response;
694
- const options = {
695
- method: "POST",
696
- body: JSON.stringify({
697
- name: repo,
698
- description,
699
- defaultBranch,
700
- public: repoVisibility === "public"
701
- }),
702
- headers: {
703
- Authorization: authorization,
704
- "Content-Type": "application/json"
705
- }
706
- };
707
- try {
708
- response = await fetch__default["default"](`${apiBaseUrl}/projects/${project}/repos`, options);
709
- } catch (e) {
710
- throw new Error(`Unable to create repository, ${e}`);
711
- }
712
- if (response.status !== 201) {
713
- throw new Error(
714
- `Unable to create repository, ${response.status} ${response.statusText}, ${await response.text()}`
715
- );
716
- }
717
- const r = await response.json();
718
- let remoteUrl = "";
719
- for (const link of r.links.clone) {
720
- if (link.name === "http") {
721
- remoteUrl = link.href;
722
- }
723
- }
724
- const repoContentsUrl = `${r.links.self[0].href}`;
725
- return { remoteUrl, repoContentsUrl };
726
- };
727
- const performEnableLFS = async (opts) => {
728
- const { authorization, host, project, repo } = opts;
729
- const options = {
730
- method: "PUT",
731
- headers: {
732
- Authorization: authorization
733
- }
734
- };
735
- const { ok, status, statusText } = await fetch__default["default"](
736
- `https://${host}/rest/git-lfs/admin/projects/${project}/repos/${repo}/enabled`,
737
- options
738
- );
739
- if (!ok)
740
- throw new Error(
741
- `Failed to enable LFS in the repository, ${status}: ${statusText}`
742
- );
743
- };
744
- function createPublishBitbucketServerAction(options) {
745
- const { integrations, config } = options;
746
- return pluginScaffolderNode.createTemplateAction({
747
- id: "publish:bitbucketServer",
748
- description: "Initializes a git repository of the content in the workspace, and publishes it to Bitbucket Server.",
749
- schema: {
750
- input: {
751
- type: "object",
752
- required: ["repoUrl"],
753
- properties: {
754
- repoUrl: {
755
- title: "Repository Location",
756
- type: "string"
757
- },
758
- description: {
759
- title: "Repository Description",
760
- type: "string"
761
- },
762
- repoVisibility: {
763
- title: "Repository Visibility",
764
- type: "string",
765
- enum: ["private", "public"]
766
- },
767
- defaultBranch: {
768
- title: "Default Branch",
769
- type: "string",
770
- description: `Sets the default branch on the repository. The default value is 'master'`
771
- },
772
- sourcePath: {
773
- title: "Source Path",
774
- description: "Path within the workspace that will be used as the repository root. If omitted, the entire workspace will be published as the repository.",
775
- type: "string"
776
- },
777
- enableLFS: {
778
- title: "Enable LFS?",
779
- description: "Enable LFS for the repository.",
780
- type: "boolean"
781
- },
782
- token: {
783
- title: "Authentication Token",
784
- type: "string",
785
- description: "The token to use for authorization to BitBucket Server"
786
- },
787
- gitCommitMessage: {
788
- title: "Git Commit Message",
789
- type: "string",
790
- description: `Sets the commit message on the repository. The default value is 'initial commit'`
791
- },
792
- gitAuthorName: {
793
- title: "Author Name",
794
- type: "string",
795
- description: `Sets the author name for the commit. The default value is 'Scaffolder'`
796
- },
797
- gitAuthorEmail: {
798
- title: "Author Email",
799
- type: "string",
800
- description: `Sets the author email for the commit.`
801
- }
802
- }
803
- },
804
- output: {
805
- type: "object",
806
- properties: {
807
- remoteUrl: {
808
- title: "A URL to the repository with the provider",
809
- type: "string"
810
- },
811
- repoContentsUrl: {
812
- title: "A URL to the root of the repository",
813
- type: "string"
814
- },
815
- commitHash: {
816
- title: "The git commit hash of the initial commit",
817
- type: "string"
818
- }
819
- }
820
- }
821
- },
822
- async handler(ctx) {
823
- var _a;
824
- const {
825
- repoUrl,
826
- description,
827
- defaultBranch = "master",
828
- repoVisibility = "private",
829
- enableLFS = false,
830
- gitCommitMessage = "initial commit",
831
- gitAuthorName,
832
- gitAuthorEmail
833
- } = ctx.input;
834
- const { project, repo, host } = pluginScaffolderNode.parseRepoUrl(repoUrl, integrations);
835
- if (!project) {
836
- throw new errors.InputError(
837
- `Invalid URL provider was included in the repo URL to create ${ctx.input.repoUrl}, missing project`
838
- );
839
- }
840
- const integrationConfig = integrations.bitbucketServer.byHost(host);
841
- if (!integrationConfig) {
842
- throw new errors.InputError(
843
- `No matching integration configuration for host ${host}, please check your integrations config`
844
- );
845
- }
846
- const token = (_a = ctx.input.token) != null ? _a : integrationConfig.config.token;
847
- const authConfig = {
848
- ...integrationConfig.config,
849
- ...{ token }
850
- };
851
- const reqOpts = integration.getBitbucketServerRequestOptions(authConfig);
852
- const authorization = reqOpts.headers.Authorization;
853
- if (!authorization) {
854
- throw new Error(
855
- `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.`
856
- );
857
- }
858
- const apiBaseUrl = integrationConfig.config.apiBaseUrl;
859
- const { remoteUrl, repoContentsUrl } = await createRepository({
860
- authorization,
861
- project,
862
- repo,
863
- repoVisibility,
864
- defaultBranch,
865
- description,
866
- apiBaseUrl
867
- });
868
- const gitAuthorInfo = {
869
- name: gitAuthorName ? gitAuthorName : config.getOptionalString("scaffolder.defaultAuthor.name"),
870
- email: gitAuthorEmail ? gitAuthorEmail : config.getOptionalString("scaffolder.defaultAuthor.email")
871
- };
872
- const auth = authConfig.token ? {
873
- token
874
- } : {
875
- username: authConfig.username,
876
- password: authConfig.password
877
- };
878
- const commitResult = await pluginScaffolderNode.initRepoAndPush({
879
- dir: pluginScaffolderNode.getRepoSourceDirectory(ctx.workspacePath, ctx.input.sourcePath),
880
- remoteUrl,
881
- auth,
882
- defaultBranch,
883
- logger: ctx.logger,
884
- commitMessage: gitCommitMessage ? gitCommitMessage : config.getOptionalString("scaffolder.defaultCommitMessage"),
885
- gitAuthorInfo
886
- });
887
- if (enableLFS) {
888
- await performEnableLFS({ authorization, host, project, repo });
889
- }
890
- ctx.output("commitHash", commitResult == null ? void 0 : commitResult.commitHash);
891
- ctx.output("remoteUrl", remoteUrl);
892
- ctx.output("repoContentsUrl", repoContentsUrl);
893
- }
894
- });
895
- }
896
-
897
- const createPullRequest = async (opts) => {
898
- const {
899
- project,
900
- repo,
901
- title,
902
- description,
903
- toRef,
904
- fromRef,
905
- authorization,
906
- apiBaseUrl
907
- } = opts;
908
- let response;
909
- const data = {
910
- method: "POST",
911
- body: JSON.stringify({
912
- title,
913
- description,
914
- state: "OPEN",
915
- open: true,
916
- closed: false,
917
- locked: true,
918
- toRef,
919
- fromRef
920
- }),
921
- headers: {
922
- Authorization: authorization,
923
- "Content-Type": "application/json"
924
- }
925
- };
926
- try {
927
- response = await fetch__default["default"](
928
- `${apiBaseUrl}/projects/${encodeURIComponent(
929
- project
930
- )}/repos/${encodeURIComponent(repo)}/pull-requests`,
931
- data
932
- );
933
- } catch (e) {
934
- throw new Error(`Unable to create pull-reqeusts, ${e}`);
935
- }
936
- if (response.status !== 201) {
937
- throw new Error(
938
- `Unable to create pull requests, ${response.status} ${response.statusText}, ${await response.text()}`
939
- );
940
- }
941
- const r = await response.json();
942
- return `${r.links.self[0].href}`;
943
- };
944
- const findBranches = async (opts) => {
945
- const { project, repo, branchName, authorization, apiBaseUrl } = opts;
946
- let response;
947
- const options = {
948
- method: "GET",
949
- headers: {
950
- Authorization: authorization,
951
- "Content-Type": "application/json"
952
- }
953
- };
954
- try {
955
- response = await fetch__default["default"](
956
- `${apiBaseUrl}/projects/${encodeURIComponent(
957
- project
958
- )}/repos/${encodeURIComponent(
959
- repo
960
- )}/branches?boostMatches=true&filterText=${encodeURIComponent(
961
- branchName
962
- )}`,
963
- options
964
- );
965
- } catch (e) {
966
- throw new Error(`Unable to get branches, ${e}`);
967
- }
968
- if (response.status !== 200) {
969
- throw new Error(
970
- `Unable to get branches, ${response.status} ${response.statusText}, ${await response.text()}`
971
- );
972
- }
973
- const r = await response.json();
974
- for (const object of r.values) {
975
- if (object.displayId === branchName) {
976
- return object;
977
- }
978
- }
979
- return void 0;
980
- };
981
- const createBranch = async (opts) => {
982
- const { project, repo, branchName, authorization, apiBaseUrl, startPoint } = opts;
983
- let response;
984
- const options = {
985
- method: "POST",
986
- body: JSON.stringify({
987
- name: branchName,
988
- startPoint
989
- }),
990
- headers: {
991
- Authorization: authorization,
992
- "Content-Type": "application/json"
993
- }
994
- };
995
- try {
996
- response = await fetch__default["default"](
997
- `${apiBaseUrl}/projects/${encodeURIComponent(
998
- project
999
- )}/repos/${encodeURIComponent(repo)}/branches`,
1000
- options
1001
- );
1002
- } catch (e) {
1003
- throw new Error(`Unable to create branch, ${e}`);
1004
- }
1005
- if (response.status !== 200) {
1006
- throw new Error(
1007
- `Unable to create branch, ${response.status} ${response.statusText}, ${await response.text()}`
1008
- );
1009
- }
1010
- return await response.json();
1011
- };
1012
- function createPublishBitbucketServerPullRequestAction(options) {
1013
- const { integrations, config } = options;
1014
- return pluginScaffolderNode.createTemplateAction({
1015
- id: "publish:bitbucketServer:pull-request",
1016
- schema: {
1017
- input: {
1018
- type: "object",
1019
- required: ["repoUrl", "title", "sourceBranch"],
1020
- properties: {
1021
- repoUrl: {
1022
- title: "Repository Location",
1023
- type: "string"
1024
- },
1025
- title: {
1026
- title: "Pull Request title",
1027
- type: "string",
1028
- description: "The title for the pull request"
1029
- },
1030
- description: {
1031
- title: "Pull Request Description",
1032
- type: "string",
1033
- description: "The description of the pull request"
1034
- },
1035
- targetBranch: {
1036
- title: "Target Branch",
1037
- type: "string",
1038
- description: `Branch of repository to apply changes to. The default value is 'master'`
1039
- },
1040
- sourceBranch: {
1041
- title: "Source Branch",
1042
- type: "string",
1043
- description: "Branch of repository to copy changes from"
1044
- },
1045
- token: {
1046
- title: "Authorization Token",
1047
- type: "string",
1048
- description: "The token to use for authorization to BitBucket Server"
1049
- }
1050
- }
1051
- },
1052
- output: {
1053
- type: "object",
1054
- properties: {
1055
- pullRequestUrl: {
1056
- title: "A URL to the pull request with the provider",
1057
- type: "string"
1058
- }
1059
- }
1060
- }
1061
- },
1062
- async handler(ctx) {
1063
- var _a, _b;
1064
- const {
1065
- repoUrl,
1066
- title,
1067
- description,
1068
- targetBranch = "master",
1069
- sourceBranch
1070
- } = ctx.input;
1071
- const { project, repo, host } = pluginScaffolderNode.parseRepoUrl(repoUrl, integrations);
1072
- if (!project) {
1073
- throw new errors.InputError(
1074
- `Invalid URL provider was included in the repo URL to create ${ctx.input.repoUrl}, missing project`
1075
- );
1076
- }
1077
- const integrationConfig = integrations.bitbucketServer.byHost(host);
1078
- if (!integrationConfig) {
1079
- throw new errors.InputError(
1080
- `No matching integration configuration for host ${host}, please check your integrations config`
1081
- );
1082
- }
1083
- const token = (_a = ctx.input.token) != null ? _a : integrationConfig.config.token;
1084
- const authConfig = {
1085
- ...integrationConfig.config,
1086
- ...{ token }
1087
- };
1088
- const reqOpts = integration.getBitbucketServerRequestOptions(authConfig);
1089
- const authorization = reqOpts.headers.Authorization;
1090
- if (!authorization) {
1091
- throw new Error(
1092
- `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.`
1093
- );
1094
- }
1095
- const apiBaseUrl = integrationConfig.config.apiBaseUrl;
1096
- const toRef = await findBranches({
1097
- project,
1098
- repo,
1099
- branchName: targetBranch,
1100
- authorization,
1101
- apiBaseUrl
1102
- });
1103
- let fromRef = await findBranches({
1104
- project,
1105
- repo,
1106
- branchName: sourceBranch,
1107
- authorization,
1108
- apiBaseUrl
1109
- });
1110
- if (!fromRef) {
1111
- ctx.logger.info(
1112
- `source branch not found -> creating branch named: ${sourceBranch} lastCommit: ${toRef.latestCommit}`
1113
- );
1114
- const latestCommit = toRef.latestCommit;
1115
- fromRef = await createBranch({
1116
- project,
1117
- repo,
1118
- branchName: sourceBranch,
1119
- authorization,
1120
- apiBaseUrl,
1121
- startPoint: latestCommit
1122
- });
1123
- const remoteUrl = `https://${host}/scm/${project}/${repo}.git`;
1124
- const auth = authConfig.token ? {
1125
- token
1126
- } : {
1127
- username: authConfig.username,
1128
- password: authConfig.password
1129
- };
1130
- const gitAuthorInfo = {
1131
- name: config.getOptionalString("scaffolder.defaultAuthor.name"),
1132
- email: config.getOptionalString("scaffolder.defaultAuthor.email")
1133
- };
1134
- const tempDir = await ctx.createTemporaryDirectory();
1135
- const sourceDir = pluginScaffolderNode.getRepoSourceDirectory(ctx.workspacePath, void 0);
1136
- await pluginScaffolderNode.cloneRepo({
1137
- url: remoteUrl,
1138
- dir: tempDir,
1139
- auth,
1140
- logger: ctx.logger,
1141
- ref: sourceBranch
1142
- });
1143
- await pluginScaffolderNode.createBranch({
1144
- dir: tempDir,
1145
- auth,
1146
- logger: ctx.logger,
1147
- ref: sourceBranch
1148
- });
1149
- fs__default["default"].cpSync(sourceDir, tempDir, {
1150
- recursive: true,
1151
- filter: (path) => {
1152
- return !(path.indexOf(".git") > -1);
1153
- }
1154
- });
1155
- await pluginScaffolderNode.addFiles({
1156
- dir: tempDir,
1157
- auth,
1158
- logger: ctx.logger,
1159
- filepath: "."
1160
- });
1161
- await pluginScaffolderNode.commitAndPushBranch({
1162
- dir: tempDir,
1163
- auth,
1164
- logger: ctx.logger,
1165
- commitMessage: (_b = description != null ? description : config.getOptionalString("scaffolder.defaultCommitMessage")) != null ? _b : "",
1166
- gitAuthorInfo,
1167
- branch: sourceBranch
1168
- });
1169
- }
1170
- const pullRequestUrl = await createPullRequest({
1171
- project,
1172
- repo,
1173
- title,
1174
- description,
1175
- toRef,
1176
- fromRef,
1177
- authorization,
1178
- apiBaseUrl
1179
- });
1180
- ctx.output("pullRequestUrl", pullRequestUrl);
1181
- }
1182
- });
1183
- }
1184
-
1185
- const examples = [
1186
- {
1187
- description: "Trigger a pipeline for a branch",
1188
- example: yaml__default["default"].stringify({
1189
- steps: [
1190
- {
1191
- action: "bitbucket:pipelines:run",
1192
- id: "run-bitbucket-pipeline",
1193
- name: "Run an example bitbucket pipeline",
1194
- input: {
1195
- workspace: "test-workspace",
1196
- repo_slug: "test-repo-slug",
1197
- body: {
1198
- target: {
1199
- ref_type: "branch",
1200
- type: "pipeline_ref_target",
1201
- ref_name: "master"
1202
- }
1203
- }
1204
- }
1205
- }
1206
- ]
1207
- })
1208
- },
1209
- {
1210
- description: "Trigger a pipeline for a commit on a branch",
1211
- example: yaml__default["default"].stringify({
1212
- steps: [
1213
- {
1214
- action: "bitbucket:pipelines:run",
1215
- id: "run-bitbucket-pipeline",
1216
- name: "Run an example bitbucket pipeline",
1217
- input: {
1218
- workspace: "test-workspace",
1219
- repo_slug: "test-repo-slug",
1220
- body: {
1221
- target: {
1222
- commit: {
1223
- type: "commit",
1224
- hash: "ce5b7431602f7cbba007062eeb55225c6e18e956"
1225
- },
1226
- ref_type: "branch",
1227
- type: "pipeline_ref_target",
1228
- ref_name: "master"
1229
- }
1230
- }
1231
- }
1232
- }
1233
- ]
1234
- })
1235
- },
1236
- {
1237
- description: "Trigger a specific pipeline definition for a commit",
1238
- example: yaml__default["default"].stringify({
1239
- steps: [
1240
- {
1241
- action: "bitbucket:pipelines:run",
1242
- id: "run-bitbucket-pipeline",
1243
- name: "Run an example bitbucket pipeline",
1244
- input: {
1245
- workspace: "test-workspace",
1246
- repo_slug: "test-repo-slug",
1247
- body: {
1248
- target: {
1249
- commit: {
1250
- type: "commit",
1251
- hash: "a3c4e02c9a3755eccdc3764e6ea13facdf30f923"
1252
- },
1253
- selector: {
1254
- type: "custom",
1255
- pattern: "Deploy to production"
1256
- },
1257
- type: "pipeline_commit_target"
1258
- }
1259
- }
1260
- }
1261
- }
1262
- ]
1263
- })
1264
- },
1265
- {
1266
- description: "Trigger a specific pipeline definition for a commit on a branch or tag",
1267
- example: yaml__default["default"].stringify({
1268
- steps: [
1269
- {
1270
- action: "bitbucket:pipelines:run",
1271
- id: "run-bitbucket-pipeline",
1272
- name: "Run an example bitbucket pipeline",
1273
- input: {
1274
- workspace: "test-workspace",
1275
- repo_slug: "test-repo-slug",
1276
- body: {
1277
- target: {
1278
- commit: {
1279
- type: "commit",
1280
- hash: "a3c4e02c9a3755eccdc3764e6ea13facdf30f923"
1281
- },
1282
- selector: {
1283
- type: "custom",
1284
- pattern: "Deploy to production"
1285
- },
1286
- type: "pipeline_ref_target",
1287
- ref_name: "master",
1288
- ref_type: "branch"
1289
- }
1290
- }
1291
- }
1292
- }
1293
- ]
1294
- })
1295
- },
1296
- {
1297
- description: "Trigger a custom pipeline with variables",
1298
- example: yaml__default["default"].stringify({
1299
- steps: [
1300
- {
1301
- action: "bitbucket:pipelines:run",
1302
- id: "run-bitbucket-pipeline",
1303
- name: "Run an example bitbucket pipeline",
1304
- input: {
1305
- workspace: "test-workspace",
1306
- repo_slug: "test-repo-slug",
1307
- body: {
1308
- target: {
1309
- type: "pipeline_ref_target",
1310
- ref_name: "master",
1311
- ref_type: "branch",
1312
- selector: {
1313
- type: "custom",
1314
- pattern: "Deploy to production"
1315
- }
1316
- },
1317
- variables: [
1318
- { key: "var1key", value: "var1value", secured: true },
1319
- {
1320
- key: "var2key",
1321
- value: "var2value"
1322
- }
1323
- ]
1324
- }
1325
- }
1326
- }
1327
- ]
1328
- })
1329
- },
1330
- {
1331
- description: "Trigger a pull request pipeline",
1332
- example: yaml__default["default"].stringify({
1333
- steps: [
1334
- {
1335
- action: "bitbucket:pipelines:run",
1336
- id: "run-bitbucket-pipeline",
1337
- name: "Run an example bitbucket pipeline",
1338
- input: {
1339
- workspace: "test-workspace",
1340
- repo_slug: "test-repo-slug",
1341
- body: {
1342
- target: {
1343
- type: "pipeline_pullrequest_target",
1344
- source: "pull-request-branch",
1345
- destination: "master",
1346
- destination_commit: {
1347
- hash: "9f848b7"
1348
- },
1349
- commit: {
1350
- hash: "1a372fc"
1351
- },
1352
- pull_request: {
1353
- id: "3"
1354
- },
1355
- selector: {
1356
- type: "pull-requests",
1357
- pattern: "**"
1358
- }
1359
- }
1360
- }
1361
- }
1362
- }
1363
- ]
1364
- })
1365
- }
1366
- ];
1367
-
1368
- const workspace = {
1369
- title: "Workspace",
1370
- description: `The workspace name`,
1371
- type: "string"
1372
- };
1373
- const repo_slug = {
1374
- title: "Repository name",
1375
- description: "The repository name",
1376
- type: "string"
1377
- };
1378
- const ref_type = {
1379
- title: "ref_type",
1380
- type: "string"
1381
- };
1382
- const type = {
1383
- title: "type",
1384
- type: "string"
1385
- };
1386
- const ref_name = {
1387
- title: "ref_name",
1388
- type: "string"
1389
- };
1390
- const source = {
1391
- title: "source",
1392
- type: "string"
1393
- };
1394
- const destination = {
1395
- title: "destination",
1396
- type: "string"
1397
- };
1398
- const hash = {
1399
- title: "hash",
1400
- type: "string"
1401
- };
1402
- const pattern = {
1403
- title: "pattern",
1404
- type: "string"
1405
- };
1406
- const id$1 = {
1407
- title: "id",
1408
- type: "string"
1409
- };
1410
- const key = {
1411
- title: "key",
1412
- type: "string"
1413
- };
1414
- const value = {
1415
- title: "value",
1416
- type: "string"
1417
- };
1418
- const secured = {
1419
- title: "secured",
1420
- type: "boolean"
1421
- };
1422
- const token = {
1423
- title: "Authentication Token",
1424
- type: "string",
1425
- description: "The token to use for authorization to BitBucket Cloud"
1426
- };
1427
- const destination_commit = {
1428
- title: "destination_commit",
1429
- type: "object",
1430
- properties: {
1431
- hash
1432
- }
1433
- };
1434
- const commit = {
1435
- title: "commit",
1436
- type: "object",
1437
- properties: {
1438
- type,
1439
- hash
1440
- }
1441
- };
1442
- const selector = {
1443
- title: "selector",
1444
- type: "object",
1445
- properties: {
1446
- type,
1447
- pattern
1448
- }
1449
- };
1450
- const pull_request = {
1451
- title: "pull_request",
1452
- type: "object",
1453
- properties: {
1454
- id: id$1
1455
- }
1456
- };
1457
- const pipelinesRunBody = {
1458
- title: "Request Body",
1459
- description: "Request body properties: see Bitbucket Cloud Rest API documentation for more details",
1460
- type: "object",
1461
- properties: {
1462
- target: {
1463
- title: "target",
1464
- type: "object",
1465
- properties: {
1466
- ref_type,
1467
- type,
1468
- ref_name,
1469
- source,
1470
- destination,
1471
- destination_commit,
1472
- commit,
1473
- selector,
1474
- pull_request
1475
- }
1476
- },
1477
- variables: {
1478
- title: "variables",
1479
- type: "array",
1480
- items: {
1481
- type: "object",
1482
- properties: {
1483
- key,
1484
- value,
1485
- secured
1486
- }
1487
- }
1488
- }
1489
- }
1490
- };
1491
-
1492
- const id = "bitbucket:pipelines:run";
1493
- const createBitbucketPipelinesRunAction = (options) => {
1494
- const { integrations } = options;
1495
- return pluginScaffolderNode.createTemplateAction({
1496
- id,
1497
- description: "Run a bitbucket cloud pipeline",
1498
- examples,
1499
- schema: {
1500
- input: {
1501
- type: "object",
1502
- required: ["workspace", "repo_slug"],
1503
- properties: {
1504
- workspace: workspace,
1505
- repo_slug: repo_slug,
1506
- body: pipelinesRunBody,
1507
- token: token
1508
- }
1509
- },
1510
- output: {
1511
- type: "object",
1512
- properties: {
1513
- buildNumber: {
1514
- title: "Build number",
1515
- type: "number"
1516
- },
1517
- repoUrl: {
1518
- title: "A URL to the pipeline repositry",
1519
- type: "string"
1520
- },
1521
- repoContentsUrl: {
1522
- title: "A URL to the pipeline",
1523
- type: "string"
1524
- }
1525
- }
1526
- }
1527
- },
1528
- supportsDryRun: false,
1529
- async handler(ctx) {
1530
- var _a;
1531
- const { workspace, repo_slug, body, token } = ctx.input;
1532
- const host = "bitbucket.org";
1533
- const integrationConfig = integrations.bitbucketCloud.byHost(host);
1534
- const authorization = getAuthorizationHeader(
1535
- token ? { token } : integrationConfig.config
1536
- );
1537
- let response;
1538
- try {
1539
- response = await fetch__default["default"](
1540
- `https://api.bitbucket.org/2.0/repositories/${workspace}/${repo_slug}/pipelines`,
1541
- {
1542
- method: "POST",
1543
- headers: {
1544
- Authorization: authorization,
1545
- Accept: "application/json",
1546
- "Content-Type": "application/json"
1547
- },
1548
- body: (_a = JSON.stringify(body)) != null ? _a : {}
1549
- }
1550
- );
1551
- } catch (e) {
1552
- throw new Error(`Unable to run pipeline, ${e}`);
1553
- }
1554
- if (response.status !== 201) {
1555
- throw new Error(
1556
- `Unable to run pipeline, ${response.status} ${response.statusText}, ${await response.text()}`
1557
- );
1558
- }
1559
- const responseObject = await response.json();
1560
- ctx.output("buildNumber", responseObject.build_number);
1561
- ctx.output("repoUrl", responseObject.repository.links.html.href);
1562
- ctx.output(
1563
- "pipelinesUrl",
1564
- `${responseObject.repository.links.html.href}/pipelines`
1565
- );
1566
- }
1567
- });
1568
- };
501
+ const createPublishBitbucketCloudAction = bitbucketCloud__namespace.createPublishBitbucketCloudAction;
502
+ const createBitbucketPipelinesRunAction = bitbucketCloud__namespace.createBitbucketPipelinesRunAction;
503
+ const createPublishBitbucketServerAction = bitbucketServer__namespace.createPublishBitbucketServerAction;
504
+ const createPublishBitbucketServerPullRequestAction = bitbucketServer__namespace.createPublishBitbucketServerPullRequestAction;
1569
505
 
1570
506
  const bitbucketModule = backendPluginApi.createBackendModule({
1571
507
  moduleId: "bitbucket",