@uxf/scripts 11.28.3 → 11.34.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/README.md CHANGED
@@ -32,6 +32,7 @@ Options:
32
32
  set environment variable `GITLAB_TOKEN` in GitLab CI configuration and create `.gitlab-ci.yml`
33
33
 
34
34
  ```yaml
35
+ # .gitlab-ci.yml
35
36
  stages:
36
37
  - release
37
38
 
@@ -44,6 +45,24 @@ release:
44
45
  # ... deploy project ...
45
46
  - uxf-release
46
47
  ```
48
+
49
+ ## uxf-push-notifier
50
+
51
+ ```yaml
52
+ stages:
53
+ - notify
54
+
55
+ notify-push:
56
+ image: node:20
57
+ stage: notify
58
+ except: [schedules]
59
+ only:
60
+ - develop # default branch
61
+ script:
62
+ - yarn global add @uxf/scripts # uxf scripts are installed in uxf docker images by default
63
+ - uxf-push-notifier --google-chat-webhook-url=$GOOGLE_CHAT_WEBHOOK
64
+ ```
65
+
47
66
  ## uxf-lunch
48
67
 
49
68
  ```bash
@@ -60,4 +79,4 @@ Options
60
79
 
61
80
  Options:
62
81
  --version Show version number
63
- ```
82
+ ```
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env node
2
+ require("../src/uxf-push-notifier/cli")()
3
+ .then((exitCode) => {
4
+ process.exitCode = exitCode;
5
+ })
6
+ .catch((e) => {
7
+ process.exitCode = 1;
8
+ });
package/package.json CHANGED
@@ -1,11 +1,12 @@
1
1
  {
2
2
  "name": "@uxf/scripts",
3
- "version": "11.28.3",
3
+ "version": "11.34.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "bin": {
7
7
  "uxf-audit": "bin/uxf-audit.js",
8
8
  "uxf-lunch": "bin/uxf-lunch.js",
9
+ "uxf-push-notifier": "bin/uxf-push-notifier.js",
9
10
  "uxf-release": "bin/uxf-release.js",
10
11
  "uxf-sitemap-check": "bin/uxf-sitemap-check.js",
11
12
  "uxf-sitemap-meta-export": "bin/uxf-sitemap-meta-export.js",
@@ -27,9 +28,9 @@
27
28
  "author": "",
28
29
  "license": "ISC",
29
30
  "dependencies": {
30
- "axios": "1.6.7",
31
- "cheerio": "1.0.0-rc.12",
32
- "dayjs": "1.11.10",
31
+ "axios": "1.7.5",
32
+ "cheerio": "1.0.0",
33
+ "dayjs": "1.11.13",
33
34
  "fast-glob": "3.3.2",
34
35
  "madge": "8.0.0",
35
36
  "yargs": "17.7.2"
package/src/GitLab.js CHANGED
@@ -21,7 +21,7 @@ async function getAll(url, config) {
21
21
  const response = await axios.get(url, {
22
22
  ...config,
23
23
  params: {
24
- ...(config.params ?? {}),
24
+ ...config.params,
25
25
  per_page: "100",
26
26
  page: nextPage,
27
27
  },
@@ -0,0 +1,30 @@
1
+ const { argv, env } = require("process");
2
+
3
+ module.exports = async () => {
4
+ const cli = require("yargs")
5
+ .command("$0", "UXF push notifier", (yargs) => {
6
+ yargs.demandCommand(0, 0).usage(`Usage:
7
+ uxf-push-notifier [options]
8
+
9
+ Environment variables:
10
+ GITLAB_TOKEN - required
11
+ `);
12
+ })
13
+ .option("g", { alias: "google-chat-webhook-url", group: "Options" })
14
+ .option("h", { alias: "help", group: "Options" })
15
+ .strict(false)
16
+ .exitProcess(false);
17
+
18
+ try {
19
+ const { help, g: googleChatWebhookUrl } = cli.parse(argv.slice(2));
20
+
21
+ if (Boolean(help)) {
22
+ return 0;
23
+ }
24
+
25
+ await require("./index")(googleChatWebhookUrl);
26
+ } catch (e) {
27
+ console.error(e);
28
+ return 1;
29
+ }
30
+ };
@@ -0,0 +1,61 @@
1
+ const { create } = require("axios");
2
+ const process = require("process");
3
+
4
+ const { GITLAB_TOKEN, CI_SERVER_URL, CI_COMMIT_SHA, CI_COMMIT_BEFORE_SHA, CI_PROJECT_ID, CI_COMMIT_REF_NAME } =
5
+ process.env;
6
+
7
+ const gitlabAxios = create({
8
+ baseURL: `${CI_SERVER_URL}/api/v4`,
9
+ headers: {
10
+ Authorization: `Bearer ${GITLAB_TOKEN}`,
11
+ },
12
+ });
13
+
14
+ const googleChatAxios = create({});
15
+
16
+ function findCurrentlyMergedMergeRequest() {
17
+ return gitlabAxios
18
+ .get(
19
+ `/projects/${CI_PROJECT_ID}/merge_requests?state=merged&target_branch=${CI_COMMIT_REF_NAME}&order_by=updated_at&sort=desc`,
20
+ )
21
+ .then((response) => (response.data?.[0]?.sha === CI_COMMIT_SHA ? response.data[0] : null));
22
+ }
23
+
24
+ function getApprovalUserNames(iid) {
25
+ return gitlabAxios
26
+ .get(`/projects/${CI_PROJECT_ID}/merge_requests/${iid}/approvals`)
27
+ .then((response) => response.data.approved_by.map((item) => item.user.name));
28
+ }
29
+
30
+ function getPushedCommits() {
31
+ return gitlabAxios
32
+ .get(`/projects/${CI_PROJECT_ID}/repository/compare?from=${CI_COMMIT_BEFORE_SHA}&to=${CI_COMMIT_SHA}`)
33
+ .then((response) => response.data.commits);
34
+ }
35
+
36
+ module.exports = async function (googleChatWebhookUrl) {
37
+ const mr = await findCurrentlyMergedMergeRequest();
38
+
39
+ if (mr !== null) {
40
+ const approvalUserNames = await getApprovalUserNames(mr.iid);
41
+
42
+ const isApproved = !!approvalUserNames.find((name) => name !== mr.author.name);
43
+
44
+ const text = `${isApproved ? "✅" : "❗"} Merge request byl zamergován
45
+ <${mr.web_url}|${mr.title}>
46
+ Autor: ${mr.author.name}
47
+ Schválil: ${isApproved ? approvalUserNames.join(", ") : "*bez schválení*"}
48
+ Zamergoval: ${mr.merged_by.name}`;
49
+
50
+ await googleChatAxios.post(googleChatWebhookUrl, { text });
51
+ } else {
52
+ const commits = (await getPushedCommits()).map(
53
+ (commit) => `${commit.author_name} - <${commit.web_url}|${commit.title}>`,
54
+ );
55
+
56
+ await googleChatAxios.post(googleChatWebhookUrl, {
57
+ text: `❗ Bylo pushnuto do developu.
58
+ ${commits.join("\n")}`,
59
+ });
60
+ }
61
+ };