@backstage/plugin-scaffolder-backend-module-gitlab 0.4.4 → 0.4.5-next.0

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/CHANGELOG.md CHANGED
@@ -1,5 +1,28 @@
1
1
  # @backstage/plugin-scaffolder-backend-module-gitlab
2
2
 
3
+ ## 0.4.5-next.0
4
+
5
+ ### Patch Changes
6
+
7
+ - fad1b90: Allow the `createGitlabProjectVariableAction` to use oauth tokens
8
+ - aab708e: Added test cases for gitlab:issue:edit examples
9
+ - ef742dc: Added test cases for gitlab:projectAccessToken:create example
10
+ - 1ba4c2f: Added test cases for gitlab:pipeline:trigger examples
11
+ - a6603e4: Add custom action for merge request: **auto**
12
+
13
+ The **Auto** action selects the committed action between _create_ and _update_.
14
+
15
+ The **Auto** action fetches files using the **/projects/repository/tree endpoint**.
16
+ After fetching, it checks if the file exists locally and in the repository. If it does, it chooses **update**; otherwise, it chooses **create**.
17
+
18
+ - Updated dependencies
19
+ - @backstage/backend-common@0.23.4-next.0
20
+ - @backstage/integration@1.14.0-next.0
21
+ - @backstage/backend-plugin-api@0.7.1-next.0
22
+ - @backstage/config@1.2.0
23
+ - @backstage/errors@1.2.4
24
+ - @backstage/plugin-scaffolder-node@0.4.9-next.0
25
+
3
26
  ## 0.4.4
4
27
 
5
28
  ### Patch Changes
package/dist/index.cjs.js CHANGED
@@ -1020,7 +1020,7 @@ const examples$6 = [
1020
1020
  title: "Test Issue",
1021
1021
  assignees: [18],
1022
1022
  description: "This is the edited description of the issue",
1023
- updatedAt: "2024-05-10 18:00:00.000",
1023
+ updatedAt: "2024-05-10T18:00:00.000Z",
1024
1024
  dueDate: "2024-09-28"
1025
1025
  }
1026
1026
  }
@@ -1042,7 +1042,7 @@ const examples$6 = [
1042
1042
  assignees: [18, 15],
1043
1043
  description: "This is the description of the issue",
1044
1044
  confidential: false,
1045
- updatedAt: "2024-05-10 18:00:00.000",
1045
+ updatedAt: "2024-05-10T18:00:00.000Z",
1046
1046
  dueDate: "2024-09-28",
1047
1047
  discussionLocked: true,
1048
1048
  epicId: 1,
@@ -1051,6 +1051,178 @@ const examples$6 = [
1051
1051
  }
1052
1052
  ]
1053
1053
  })
1054
+ },
1055
+ {
1056
+ description: "Edit a gitlab issue to change its state to close",
1057
+ example: yaml__default.default.stringify({
1058
+ steps: [
1059
+ {
1060
+ id: "gitlabIssue",
1061
+ name: "EditIssues",
1062
+ action: "gitlab:issue:edit",
1063
+ input: {
1064
+ ...commonGitlabConfigExample,
1065
+ projectId: 12,
1066
+ stateEvent: "close"
1067
+ }
1068
+ }
1069
+ ]
1070
+ })
1071
+ },
1072
+ {
1073
+ description: "Edit a gitlab issue to change its state to reopened",
1074
+ example: yaml__default.default.stringify({
1075
+ steps: [
1076
+ {
1077
+ id: "gitlabIssue",
1078
+ name: "EditIssues",
1079
+ action: "gitlab:issue:edit",
1080
+ input: {
1081
+ ...commonGitlabConfigExample,
1082
+ projectId: 12,
1083
+ stateEvent: "reopen"
1084
+ }
1085
+ }
1086
+ ]
1087
+ })
1088
+ },
1089
+ {
1090
+ description: "Edit a gitlab issue to assign it to multiple users and set milestone",
1091
+ example: yaml__default.default.stringify({
1092
+ steps: [
1093
+ {
1094
+ id: "gitlabIssue",
1095
+ name: "EditIssues",
1096
+ action: "gitlab:issue:edit",
1097
+ input: {
1098
+ ...commonGitlabConfigExample,
1099
+ projectId: 12,
1100
+ title: "Test issue with milestone",
1101
+ assignees: [18, 20],
1102
+ description: "This issue has milestone set",
1103
+ milestoneId: 5
1104
+ }
1105
+ }
1106
+ ]
1107
+ })
1108
+ },
1109
+ {
1110
+ description: "Edit a gitlab issue to add weight and update labels",
1111
+ example: yaml__default.default.stringify({
1112
+ steps: [
1113
+ {
1114
+ id: "gitlabIssue",
1115
+ name: "EditIssues",
1116
+ action: "gitlab:issue:edit",
1117
+ input: {
1118
+ ...commonGitlabConfigExample,
1119
+ projectId: 12,
1120
+ title: "Issue with weight and labels",
1121
+ description: "This issue has weight and new labels",
1122
+ weight: 3,
1123
+ labels: "bug,urgent"
1124
+ }
1125
+ }
1126
+ ]
1127
+ })
1128
+ },
1129
+ {
1130
+ description: "Edit a gitlab issue to make it confidential",
1131
+ example: yaml__default.default.stringify({
1132
+ steps: [
1133
+ {
1134
+ id: "gitlabIssue",
1135
+ name: "EditIssues",
1136
+ action: "gitlab:issue:edit",
1137
+ input: {
1138
+ ...commonGitlabConfigExample,
1139
+ projectId: 12,
1140
+ title: "Confidential Issue",
1141
+ description: "This issue is confidential",
1142
+ confidential: true
1143
+ }
1144
+ }
1145
+ ]
1146
+ })
1147
+ },
1148
+ {
1149
+ description: "Edit a gitlab issue to lock the discussion",
1150
+ example: yaml__default.default.stringify({
1151
+ steps: [
1152
+ {
1153
+ id: "gitlabIssue",
1154
+ name: "EditIssues",
1155
+ action: "gitlab:issue:edit",
1156
+ input: {
1157
+ ...commonGitlabConfigExample,
1158
+ projectId: 12,
1159
+ title: "Locked Discussion Issue",
1160
+ description: "This discussion on this issue is locked",
1161
+ discussionLocked: true
1162
+ }
1163
+ }
1164
+ ]
1165
+ })
1166
+ },
1167
+ {
1168
+ description: "Edit a gitlab issue to remove labels and update milestone",
1169
+ example: yaml__default.default.stringify({
1170
+ steps: [
1171
+ {
1172
+ id: "gitlabIssue",
1173
+ name: "EditIssues",
1174
+ action: "gitlab:issue:edit",
1175
+ input: {
1176
+ ...commonGitlabConfigExample,
1177
+ projectId: 12,
1178
+ title: "Issue with labels removed and milestone updated",
1179
+ description: "This issue has labels removed and milestone updated",
1180
+ removeLabels: "phase1:label1",
1181
+ milestoneId: 6
1182
+ }
1183
+ }
1184
+ ]
1185
+ })
1186
+ },
1187
+ {
1188
+ description: "Edit a gitlab issue to remove some labels and new ones",
1189
+ example: yaml__default.default.stringify({
1190
+ steps: [
1191
+ {
1192
+ id: "gitlabIssue",
1193
+ name: "EditIssues",
1194
+ action: "gitlab:issue:edit",
1195
+ input: {
1196
+ ...commonGitlabConfigExample,
1197
+ projectId: 12,
1198
+ title: "Issue with labels updated",
1199
+ description: "This issue has labels removed and new ones added",
1200
+ removeLabels: "bug,urgent",
1201
+ labels: "enhancement:documentation"
1202
+ }
1203
+ }
1204
+ ]
1205
+ })
1206
+ },
1207
+ {
1208
+ description: "Edit a gitlab issue to change issue type and add labels",
1209
+ example: yaml__default.default.stringify({
1210
+ steps: [
1211
+ {
1212
+ id: "gitlabIssue",
1213
+ name: "EditIssues",
1214
+ action: "gitlab:issue:edit",
1215
+ input: {
1216
+ ...commonGitlabConfigExample,
1217
+ projectId: 12,
1218
+ title: "Issue with type and labels",
1219
+ description: "This issue has been changes and new labels added",
1220
+ labels: "task,high-priority",
1221
+ issueType: "task"
1222
+ }
1223
+ }
1224
+ ]
1225
+ })
1054
1226
  }
1055
1227
  ];
1056
1228
 
@@ -1350,6 +1522,13 @@ const examples$5 = [
1350
1522
  }
1351
1523
  ];
1352
1524
 
1525
+ function getFileAction(fileInfo, remoteFiles, defaultCommitAction) {
1526
+ if (!defaultCommitAction || defaultCommitAction === "auto") {
1527
+ const filePath = path__default.default.join(fileInfo.targetPath ?? "", fileInfo.file.path);
1528
+ return remoteFiles && remoteFiles.some((remoteFile) => remoteFile.path === filePath) ? "update" : "create";
1529
+ }
1530
+ return defaultCommitAction;
1531
+ }
1353
1532
  const createPublishGitlabMergeRequestAction = (options) => {
1354
1533
  const { integrations } = options;
1355
1534
  return pluginScaffolderNode.createTemplateAction({
@@ -1409,8 +1588,8 @@ const createPublishGitlabMergeRequestAction = (options) => {
1409
1588
  commitAction: {
1410
1589
  title: "Commit action",
1411
1590
  type: "string",
1412
- enum: ["create", "update", "delete"],
1413
- description: "The action to be used for git commit. Defaults to create."
1591
+ enum: ["create", "update", "delete", "auto"],
1592
+ description: 'The action to be used for git commit. Defaults to auto. "auto" is custom action provide by backstage, (automatic assign create or update action) /!\\ Use more api calls /!\\ *'
1414
1593
  },
1415
1594
  removeSourceBranch: {
1416
1595
  title: "Delete source branch",
@@ -1489,19 +1668,37 @@ const createPublishGitlabMergeRequestAction = (options) => {
1489
1668
  const fileContents = await pluginScaffolderNode.serializeDirectoryContents(fileRoot, {
1490
1669
  gitignore: true
1491
1670
  });
1492
- const actions = fileContents.map((file) => ({
1493
- action: ctx.input.commitAction ?? "create",
1494
- filePath: targetPath ? path__default.default.posix.join(targetPath, file.path) : file.path,
1495
- encoding: "base64",
1496
- content: file.content.toString("base64"),
1497
- execute_filemode: file.executable
1498
- }));
1499
1671
  let targetBranch = targetBranchName;
1500
1672
  if (!targetBranch) {
1501
1673
  const projects = await api.Projects.show(repoID);
1502
1674
  const { default_branch: defaultBranch } = projects;
1503
1675
  targetBranch = defaultBranch;
1504
1676
  }
1677
+ let remoteFiles = [];
1678
+ if (!ctx.input.commitAction || ctx.input.commitAction === "auto") {
1679
+ try {
1680
+ remoteFiles = await api.Repositories.tree(repoID, {
1681
+ ref: targetBranch,
1682
+ recursive: true,
1683
+ path: targetPath ?? void 0
1684
+ });
1685
+ } catch (e) {
1686
+ ctx.logger.warn(
1687
+ `Could not retrieve the list of files for ${repoID} (branch: ${targetBranch}) : ${e}`
1688
+ );
1689
+ }
1690
+ }
1691
+ const actions = fileContents.map((file) => ({
1692
+ action: getFileAction(
1693
+ { file, targetPath },
1694
+ remoteFiles,
1695
+ ctx.input.commitAction
1696
+ ),
1697
+ filePath: targetPath ? path__default.default.posix.join(targetPath, file.path) : file.path,
1698
+ encoding: "base64",
1699
+ content: file.content.toString("base64"),
1700
+ execute_filemode: file.executable
1701
+ }));
1505
1702
  try {
1506
1703
  await api.Branches.create(repoID, branchName, String(targetBranch));
1507
1704
  } catch (e) {
@@ -1561,6 +1758,66 @@ const examples$4 = [
1561
1758
  }
1562
1759
  ]
1563
1760
  })
1761
+ },
1762
+ {
1763
+ description: "Trigger a GitLab Project Pipeline with No Variables",
1764
+ example: yaml__default.default.stringify({
1765
+ steps: [
1766
+ {
1767
+ id: "triggerPipeline",
1768
+ name: "Trigger Project Pipeline",
1769
+ action: "gitlab:pipeline:trigger",
1770
+ input: {
1771
+ ...commonGitlabConfigExample,
1772
+ projectId: 12,
1773
+ tokenDescription: "This is the text that will appear in the pipeline token",
1774
+ token: "glpt-xxxxxxxxxxxx",
1775
+ branch: "main",
1776
+ variables: {}
1777
+ }
1778
+ }
1779
+ ]
1780
+ })
1781
+ },
1782
+ {
1783
+ description: "Trigger a GitLab Project Pipeline with Single Variables",
1784
+ example: yaml__default.default.stringify({
1785
+ steps: [
1786
+ {
1787
+ id: "triggerPipeline",
1788
+ name: "Trigger Project Pipeline",
1789
+ action: "gitlab:pipeline:trigger",
1790
+ input: {
1791
+ ...commonGitlabConfigExample,
1792
+ projectId: 12,
1793
+ tokenDescription: "This is the text that will appear in the pipeline token",
1794
+ token: "glpt-xxxxxxxxxxxx",
1795
+ branch: "main",
1796
+ variables: { var_one: "one" }
1797
+ }
1798
+ }
1799
+ ]
1800
+ })
1801
+ },
1802
+ {
1803
+ description: "Trigger a GitLab Project Pipeline with Multiple Variables",
1804
+ example: yaml__default.default.stringify({
1805
+ steps: [
1806
+ {
1807
+ id: "triggerPipeline",
1808
+ name: "Trigger Project Pipeline",
1809
+ action: "gitlab:pipeline:trigger",
1810
+ input: {
1811
+ ...commonGitlabConfigExample,
1812
+ projectId: 12,
1813
+ tokenDescription: "This is the text that will appear in the pipeline token",
1814
+ token: "glpt-xxxxxxxxxxxx",
1815
+ branch: "main",
1816
+ variables: { var_one: "one", var_two: "two", var_three: "three" }
1817
+ }
1818
+ }
1819
+ ]
1820
+ })
1564
1821
  }
1565
1822
  ];
1566
1823
 
@@ -1725,6 +1982,160 @@ const examples$3 = [
1725
1982
  }
1726
1983
  ]
1727
1984
  })
1985
+ },
1986
+ {
1987
+ description: "Create a GitLab project access token with an access level",
1988
+ example: yaml__default.default.stringify({
1989
+ steps: [
1990
+ {
1991
+ id: "createAccessToken",
1992
+ action: "gitlab:projectAccessToken:create",
1993
+ name: "Create GitLab Project Access Token",
1994
+ input: {
1995
+ repoUrl: "gitlab.com?repo=repo&owner=owner",
1996
+ projectId: "456",
1997
+ accessLevel: 30
1998
+ }
1999
+ }
2000
+ ]
2001
+ })
2002
+ },
2003
+ {
2004
+ description: "Create a GitLab project access token with multiple options",
2005
+ example: yaml__default.default.stringify({
2006
+ steps: [
2007
+ {
2008
+ id: "createAccessToken",
2009
+ action: "gitlab:projectAccessToken:create",
2010
+ name: "Create GitLab Project Access Token",
2011
+ input: {
2012
+ repoUrl: "gitlab.com?repo=repo&owner=owner",
2013
+ projectId: "456",
2014
+ accessLevel: 40,
2015
+ name: "full-access-token",
2016
+ expiresAt: "2024-12-31"
2017
+ }
2018
+ }
2019
+ ]
2020
+ })
2021
+ },
2022
+ {
2023
+ description: "Create a GitLab project access token with a token for authorization",
2024
+ example: yaml__default.default.stringify({
2025
+ steps: [
2026
+ {
2027
+ id: "createAccessToken",
2028
+ action: "gitlab:projectAccessToken:create",
2029
+ name: "Create GitLab Project Access Token",
2030
+ input: {
2031
+ repoUrl: "gitlab.com?repo=repo&owner=owner",
2032
+ projectId: "101112",
2033
+ token: "personal-access-token"
2034
+ }
2035
+ }
2036
+ ]
2037
+ })
2038
+ },
2039
+ {
2040
+ description: "Create a GitLab project access token with read-only scopes",
2041
+ example: yaml__default.default.stringify({
2042
+ steps: [
2043
+ {
2044
+ id: "createAccessToken",
2045
+ action: "gitlab:projectAccessToken:create",
2046
+ name: "Create GitLab Project Access Token",
2047
+ input: {
2048
+ repoUrl: "gitlab.com?repo=repo&owner=owner",
2049
+ projectId: "101112",
2050
+ scopes: ["read_repository", "read_api"]
2051
+ }
2052
+ }
2053
+ ]
2054
+ })
2055
+ },
2056
+ {
2057
+ description: "Create a GitLab project access token with guest access level",
2058
+ example: yaml__default.default.stringify({
2059
+ steps: [
2060
+ {
2061
+ id: "createAccessToken",
2062
+ action: "gitlab:projectAccessToken:create",
2063
+ name: "Create GitLab Project Access Token",
2064
+ input: {
2065
+ repoUrl: "gitlab.com?repo=repo&owner=owner",
2066
+ projectId: "101112",
2067
+ accessLevel: 10
2068
+ }
2069
+ }
2070
+ ]
2071
+ })
2072
+ },
2073
+ {
2074
+ description: "Create a GitLab project access token with maintainer access level",
2075
+ example: yaml__default.default.stringify({
2076
+ steps: [
2077
+ {
2078
+ id: "createAccessToken",
2079
+ action: "gitlab:projectAccessToken:create",
2080
+ name: "Create GitLab Project Access Token",
2081
+ input: {
2082
+ repoUrl: "gitlab.com?repo=repo&owner=owner",
2083
+ projectId: "101112",
2084
+ accessLevel: 40
2085
+ }
2086
+ }
2087
+ ]
2088
+ })
2089
+ },
2090
+ {
2091
+ description: "Create a GitLab project access token with owner access level",
2092
+ example: yaml__default.default.stringify({
2093
+ steps: [
2094
+ {
2095
+ id: "createAccessToken",
2096
+ action: "gitlab:projectAccessToken:create",
2097
+ name: "Create GitLab Project Access Token",
2098
+ input: {
2099
+ repoUrl: "gitlab.com?repo=repo&owner=owner",
2100
+ projectId: "101112",
2101
+ accessLevel: 50
2102
+ }
2103
+ }
2104
+ ]
2105
+ })
2106
+ },
2107
+ {
2108
+ description: "Create a GitLab project access token with a specified name and no expiration date",
2109
+ example: yaml__default.default.stringify({
2110
+ steps: [
2111
+ {
2112
+ id: "createAccessToken",
2113
+ action: "gitlab:projectAccessToken:create",
2114
+ name: "Create GitLab Project Access Token",
2115
+ input: {
2116
+ repoUrl: "gitlab.com?repo=repo&owner=owner",
2117
+ projectId: "101112",
2118
+ name: "no-expiry-token"
2119
+ }
2120
+ }
2121
+ ]
2122
+ })
2123
+ },
2124
+ {
2125
+ description: "Create a GitLab project access token for a specific gitlab instance",
2126
+ example: yaml__default.default.stringify({
2127
+ steps: [
2128
+ {
2129
+ id: "createAccessToken",
2130
+ action: "gitlab:projectAccessToken:create",
2131
+ name: "Create GitLab Project Access Token",
2132
+ input: {
2133
+ repoUrl: "gitlab.example.com?repo=repo&owner=owner",
2134
+ projectId: "101112"
2135
+ }
2136
+ }
2137
+ ]
2138
+ })
1728
2139
  }
1729
2140
  ];
1730
2141
 
@@ -2105,6 +2516,7 @@ const createGitlabProjectVariableAction = (options) => {
2105
2516
  },
2106
2517
  async handler(ctx) {
2107
2518
  const {
2519
+ repoUrl,
2108
2520
  projectId,
2109
2521
  key,
2110
2522
  value,
@@ -2112,21 +2524,17 @@ const createGitlabProjectVariableAction = (options) => {
2112
2524
  variableProtected = false,
2113
2525
  masked = false,
2114
2526
  raw = false,
2115
- environmentScope = "*"
2116
- } = ctx.input;
2117
- const { token, integrationConfig } = getToken(ctx.input, integrations);
2118
- const api = new node.Gitlab({
2119
- host: integrationConfig.config.baseUrl,
2527
+ environmentScope = "*",
2120
2528
  token
2121
- });
2122
- await api.ProjectVariables.create(projectId, {
2123
- key,
2124
- value,
2125
- variable_type: variableType,
2529
+ } = ctx.input;
2530
+ const { host } = parseRepoUrl(repoUrl, integrations);
2531
+ const api = getClient({ host, integrations, token });
2532
+ await api.ProjectVariables.create(projectId, key, value, {
2533
+ variableType,
2126
2534
  protected: variableProtected,
2127
2535
  masked,
2128
2536
  raw,
2129
- environment_scope: environmentScope
2537
+ environmentScope
2130
2538
  });
2131
2539
  }
2132
2540
  });