@uxf/scripts 11.21.1 → 11.23.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@uxf/scripts",
3
- "version": "11.21.1",
3
+ "version": "11.23.2",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
@@ -26,7 +26,6 @@
26
26
  "author": "",
27
27
  "license": "ISC",
28
28
  "dependencies": {
29
- "@commitlint/parse": "19.0.3",
30
29
  "axios": "1.6.7",
31
30
  "cheerio": "1.0.0-rc.12",
32
31
  "dayjs": "1.11.10",
package/src/GitLab.js CHANGED
@@ -1,7 +1,6 @@
1
1
  const { env } = require("process");
2
2
  const { create } = require("axios");
3
3
  const dayjs = require("dayjs");
4
- const parse = import("@commitlint/parse");
5
4
 
6
5
  const axios = create({
7
6
  baseURL: `${env.CI_SERVER_URL}/api/v4`,
@@ -36,7 +35,6 @@ async function getAll(url, config) {
36
35
 
37
36
  async function loadCommits(from) {
38
37
  const commits = [];
39
- console.log(`- start date: ${from}`);
40
38
 
41
39
  let nextPage = "1";
42
40
  do {
@@ -52,38 +50,7 @@ async function loadCommits(from) {
52
50
  commits.push(...response.data);
53
51
  } while (nextPage);
54
52
 
55
- const preparedCommits = [];
56
- for (const commit of commits) {
57
- if (commit.created_at === from) {
58
- continue;
59
- }
60
-
61
- const parsedTitle = await parse(commit.title, undefined, { issuePrefixes: ["#"] });
62
-
63
- preparedCommits.push({
64
- ...commit,
65
- parsedTitle: {
66
- scope: parsedTitle.scope,
67
- type: parsedTitle.type,
68
- subject: (parsedTitle.subject || commit.title).replace(/(#[0-9]*)/g, "").trim(),
69
- },
70
- issueIds: parsedTitle.references.map((ref) => Number.parseInt(ref.issue)).filter((i) => !!i),
71
- });
72
- }
73
-
74
- return preparedCommits;
75
- }
76
-
77
- async function loadIssues(iids) {
78
- if (iids.length === 0) {
79
- return [];
80
- }
81
-
82
- const { data } = await axios.get(`/projects/${env.CI_PROJECT_ID}/issues`, {
83
- params: { iids },
84
- });
85
-
86
- return data;
53
+ return commits.filter((commit) => commit.created_at !== from);
87
54
  }
88
55
 
89
56
  async function getLastTag() {
@@ -143,7 +110,6 @@ function getAllProjects() {
143
110
 
144
111
  module.exports = {
145
112
  loadCommits,
146
- loadIssues,
147
113
  getLastTag,
148
114
  createRelease,
149
115
  getAllMergeRequests,
@@ -2,16 +2,17 @@ const { argv, env } = require("process");
2
2
 
3
3
  module.exports = async () => {
4
4
  const cli = require("yargs")
5
- .command("$0", "UXF release helper", yargs => {
5
+ .command("$0", "UXF release helper", (yargs) => {
6
6
  yargs.demandCommand(0, 0).usage(`UXF release helper
7
7
  Usage:
8
8
  uxf-release [options]
9
9
 
10
10
  Environment variables:
11
- GITLAB_TOKEN - required
12
- CI_SERVER_URL - required - setting by GitLab CI
13
- CI_PROJECT_ID - required - setting by GitLab CI
14
- SLACK_TOKEN - optional`);
11
+ GITLAB_TOKEN - required
12
+ CI_SERVER_URL - required - setting by GitLab CI
13
+ CI_PROJECT_ID - required - setting by GitLab CI
14
+ SLACK_TOKEN - optional
15
+ GOOGLE_WEBHOOK_URL - optional`);
15
16
  })
16
17
  .option("m", {
17
18
  alias: "message",
@@ -55,11 +56,6 @@ Environment variables:
55
56
  env.CI_PROJECT_ID = projectId;
56
57
  }
57
58
 
58
- if (!slackChannel) {
59
- console.log("Slack channel must be set. Use parameter -s or --slack-channel");
60
- return 1;
61
- }
62
-
63
59
  if (!env.CI_SERVER_URL) {
64
60
  console.log("GitLab url must be set. Use environment variable CI_SERVER_URL.");
65
61
  return 1;
@@ -1,99 +1,94 @@
1
1
  const GitLab = require("../GitLab");
2
2
  const Slack = require("../Slack");
3
3
  const GoogleChat = require("../GoogleChat");
4
+ const parseCommitMessage = require("./utils/parse-commit-message");
4
5
 
5
- function addCommitsToIssues(commits, issues) {
6
- issues.forEach(issue => {
7
- if (issue.commits === undefined) {
8
- issue.commits = [];
9
- }
10
- });
11
-
12
- commits.forEach(commit => {
13
- if (commit.issueIds.length > 0) {
14
- issues.forEach(issue => {
15
- if (commit.issueIds.includes(issue.iid)) {
16
- issue.commits.push(commit);
17
- }
18
- });
19
- }
20
- });
6
+ function generateSlackCommitMessage(commit) {
7
+ const { title, short_id, web_url, author_email } = commit;
8
+
9
+ const parsedCommit = parseCommitMessage(title);
10
+
11
+ return parsedCommit?.type
12
+ ? `• _${parsedCommit.type.toUpperCase()}_ - ${parsedCommit.message} <${web_url}|${short_id}> (${author_email})`
13
+ : `• ${title} <${web_url}|${short_id}> (${author_email})`;
21
14
  }
22
15
 
23
- function getIssueIdsFromCommits(commits) {
24
- const ids = [];
25
- commits.forEach(c => ids.push(...c.issueIds));
26
- return ids.filter((v, i, s) => s.indexOf(v) === i);
16
+ function generateSlackMessage(commits, messageTitle) {
17
+ return {
18
+ text: `${messageTitle}
19
+
20
+ ${commits.map(generateSlackCommitMessage).join("\n")}
21
+ `,
22
+ };
27
23
  }
28
24
 
29
- function generateSlackMessage(issues, commitsWithoutIssues, messageTitle) {
30
- let message = messageTitle;
25
+ function generateGoogleCommitMessage(commit) {
26
+ const { title, short_id, web_url, author_email } = commit;
27
+ const parsedCommit = parseCommitMessage(title);
31
28
 
32
- issues.forEach(issue => {
33
- message += `\n\n*${issue.title}* <${issue.web_url}|#${issue.iid}>\n${issue.commits
34
- .map(generateSlackCommitMessage)
35
- .join("\n")}`;
36
- });
29
+ const issues = parsedCommit?.issues.map((issue) => `<https://youtrack.uxf.dev/issue/${issue}|${issue}>`) ?? [];
37
30
 
38
- if (commitsWithoutIssues.length > 0) {
39
- message += `\n\n*Commity bez issue*\n${commitsWithoutIssues.map(generateSlackCommitMessage).join("\n")}`;
31
+ const suffix = [`<${web_url}|${short_id}>`, author_email, issues.length > 0 ? issues.join(", ") : null]
32
+ .filter((i) => !!i)
33
+ .join(" | ");
34
+
35
+ if (parsedCommit === null) {
36
+ return `* ${title} ${suffix}`;
40
37
  }
41
38
 
42
- return message;
43
- }
39
+ if (parsedCommit.type !== null && parsedCommit.message !== null) {
40
+ return `* _${parsedCommit.type.toUpperCase()}_ ${parsedCommit.message} ${suffix}`;
41
+ }
44
42
 
45
- function generateGoogleMessage(issues, commitsWithoutIssues, messageTitle) {
46
- const texts = [messageTitle];
47
- issues.forEach(issue => {
48
- texts.push(`*${issue.title}* <${issue.web_url}|#${issue.iid}>`);
49
- issue.commits.forEach((commit) => texts.push(generateSlackCommitMessage(commit)));
50
- });
51
- if (commitsWithoutIssues.length > 0) {
52
- texts.push(`*Commity bez issue*`);
53
- commitsWithoutIssues.forEach((commit) => texts.push(generateSlackCommitMessage(commit)));
43
+ if (parsedCommit.message) {
44
+ return `* ${parsedCommit.message} ${suffix}`;
54
45
  }
46
+
47
+ return `* ${title} ${suffix}`;
48
+ }
49
+
50
+ function generateGoogleMessage(commits, messageTitle) {
55
51
  return {
56
- text: texts.join('\n')
52
+ text: `${messageTitle}
53
+
54
+ ${commits.map(generateGoogleCommitMessage).join("\n")}
55
+ `,
57
56
  };
58
57
  }
59
58
 
60
- function generateSlackCommitMessage(commit) {
61
- const { title, parsedTitle, short_id, web_url, author_email } = commit;
59
+ function generateReleaseCommitMessage(commit) {
60
+ const { title, short_id, web_url, author_email } = commit;
62
61
 
63
- return parsedTitle.type
64
- ? `• _${parsedTitle.type.toUpperCase()}_ ${parsedTitle.scope} - ${
65
- parsedTitle.subject
66
- } <${web_url}|${short_id}> (${author_email})`
67
- : `• ${title} <${web_url}|${short_id}> (${author_email})`;
68
- }
62
+ const parsedCommit = parseCommitMessage(title);
63
+
64
+ const issues = parsedCommit?.issues.map((issue) => `[${issue}](https://youtrack.uxf.dev/issue/${issue})`) ?? [];
65
+
66
+ const suffix = [`[${short_id}](${web_url})`, author_email, issues.length > 0 ? issues.join(", ") : null]
67
+ .filter((i) => !!i)
68
+ .join(" | ");
69
+
70
+ if (parsedCommit === null) {
71
+ return `- ${title} ${suffix}`;
72
+ }
73
+
74
+ if (parsedCommit.type !== null && parsedCommit.message !== null) {
75
+ return `- **${parsedCommit.type.toUpperCase()}** ${parsedCommit.message} ${suffix}`;
76
+ }
69
77
 
70
- function generateCommitMessage(commit) {
71
- const { title, parsedTitle, short_id, web_url, author_email } = commit;
78
+ if (parsedCommit.message) {
79
+ return `- ${parsedCommit.message} ${suffix}`;
80
+ }
72
81
 
73
- return parsedTitle.type
74
- ? `- **${parsedTitle.type.toUpperCase()}** ${parsedTitle.subject} [${short_id}](${web_url}) (${author_email})`
75
- : `- ${title} [${short_id}](${web_url}) (${author_email})`;
82
+ return `- ${title} ${suffix}`;
76
83
  }
77
84
 
78
85
  module.exports = async (dryRun, channel, messageTitle) => {
79
86
  const lastTag = await GitLab.getLastTag();
80
87
  const commits = await GitLab.loadCommits(lastTag ? lastTag.commit.committed_date : null);
81
- const issues = await GitLab.loadIssues(getIssueIdsFromCommits(commits));
82
-
83
- addCommitsToIssues(commits, issues);
84
-
85
- const commitsWithoutIssue = commits.filter(c => c.issueIds.length === 0);
86
88
 
87
- await Slack.chatPostMessage(
88
- channel,
89
- { text: generateSlackMessage(issues, commitsWithoutIssue, messageTitle) },
90
- dryRun,
91
- );
89
+ await Slack.chatPostMessage(channel, { text: generateSlackMessage(commits, messageTitle) }, dryRun);
92
90
 
93
- await GoogleChat.chatPostMessage(
94
- generateGoogleMessage(issues, commitsWithoutIssue, messageTitle),
95
- dryRun,
96
- );
91
+ await GoogleChat.chatPostMessage(generateGoogleMessage(commits, messageTitle), dryRun);
97
92
 
98
- await GitLab.createRelease(commits.map(generateCommitMessage).join("\n"), dryRun);
93
+ await GitLab.createRelease(commits.map(generateReleaseCommitMessage).join("\n"), dryRun);
99
94
  };
@@ -0,0 +1,38 @@
1
+ function parseCommitMessage(commitMessage) {
2
+ /* fix(bo): [KLK-123] commit message */
3
+ const result = /^(.*)\((.*)\): \[(.*)\] (.*)/.exec(commitMessage);
4
+
5
+ if (result !== null) {
6
+ return {
7
+ type: result[1],
8
+ issues: result[3].replace(" ", "").split(","),
9
+ message: result[4],
10
+ };
11
+ }
12
+
13
+ /* fix: [KLK-123] commit message */
14
+ const result_1 = /^(.*): \[(.*)\] (.*)/.exec(commitMessage);
15
+
16
+ if (result_1 !== null) {
17
+ return {
18
+ type: result_1[1],
19
+ issues: result_1[2].replace(" ", "").split(","),
20
+ message: result_1[3],
21
+ };
22
+ }
23
+
24
+ /* [KLK-123] commit message */
25
+ const result_2 = /^\[(.*)\] (.*)/.exec(commitMessage);
26
+
27
+ if (result_2 !== null) {
28
+ return {
29
+ type: null,
30
+ issues: result_2[1].replace(" ", "").split(","),
31
+ message: result_2[2],
32
+ };
33
+ }
34
+
35
+ return null;
36
+ }
37
+
38
+ module.exports = parseCommitMessage;
@@ -0,0 +1,24 @@
1
+ const parseCommitMessage = require("./parse-commit-message");
2
+
3
+ test("strings (variadic)", () => {
4
+ expect(parseCommitMessage("fix(bo): [KLK-1,KLK-2] message")).toEqual({
5
+ type: "fix",
6
+ issues: ["KLK-1", "KLK-2"],
7
+ message: "message",
8
+ });
9
+ expect(parseCommitMessage("fix(bo): [KLK-1] message")).toEqual({
10
+ type: "fix",
11
+ issues: ["KLK-1"],
12
+ message: "message",
13
+ });
14
+ expect(parseCommitMessage("fix: [KLK-1] message")).toEqual({
15
+ type: "fix",
16
+ issues: ["KLK-1"],
17
+ message: "message",
18
+ });
19
+ expect(parseCommitMessage("[KLK-1,KLK-2] message")).toEqual({
20
+ type: null,
21
+ issues: ["KLK-1", "KLK-2"],
22
+ message: "message",
23
+ });
24
+ });