@hestia-earth/pipeline-utils 0.10.2 → 0.11.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
@@ -2,6 +2,26 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
4
4
 
5
+ ## [0.11.0](https://gitlab.com/hestia-earth/hestia-pipeline-utils/compare/v0.10.4...v0.11.0) (2023-06-16)
6
+
7
+
8
+ ### ⚠ BREAKING CHANGES
9
+
10
+ * **gitlab:** `readFile` in gitlab has been renamed `readFileRaw`
11
+
12
+ ### Features
13
+
14
+ * **gitlab:** add function to retrun file content ([cec9f1e](https://gitlab.com/hestia-earth/hestia-pipeline-utils/commit/cec9f1e818994925837ade5e3cfc4498e724212f))
15
+
16
+ ### [0.10.4](https://gitlab.com/hestia-earth/hestia-pipeline-utils/compare/v0.10.3...v0.10.4) (2023-06-15)
17
+
18
+ ### [0.10.3](https://gitlab.com/hestia-earth/hestia-pipeline-utils/compare/v0.10.2...v0.10.3) (2023-06-15)
19
+
20
+
21
+ ### Features
22
+
23
+ * **gitlab:** add `listFiles` and `readFile` functions ([76b4daa](https://gitlab.com/hestia-earth/hestia-pipeline-utils/commit/76b4daa2eeb23d15b5a09637756fc0c7c3200aed))
24
+
5
25
  ### [0.10.2](https://gitlab.com/hestia-earth/hestia-pipeline-utils/compare/v0.10.1...v0.10.2) (2023-06-13)
6
26
 
7
27
 
@@ -0,0 +1,14 @@
1
+ /// <reference types="node" />
2
+ import { Readable } from 'stream';
3
+ export interface IGitlabTreeFile {
4
+ id?: string;
5
+ name: string;
6
+ path: string;
7
+ }
8
+ export interface IGitlabFile {
9
+ size: number;
10
+ content: string;
11
+ }
12
+ export declare const listFiles: (path: string, page?: number, limit?: number) => Promise<IGitlabTreeFile[]>;
13
+ export declare const readFile: ({ path }: Partial<IGitlabTreeFile>) => Promise<IGitlabFile>;
14
+ export declare const readFileRaw: ({ id }: Partial<IGitlabTreeFile>) => Promise<Readable>;
package/dist/gitlab.js ADDED
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.readFileRaw = exports.readFile = exports.listFiles = void 0;
4
+ const axios_1 = require("axios");
5
+ const token = process.env.GITLAB_TOKEN;
6
+ const projectId = process.env.GITLAB_PROJECT_ID;
7
+ const branch = process.env.GITLAB_BRANCH;
8
+ const gitlabUrl = `https://gitlab.com/api/v4/projects/${projectId}`;
9
+ const headers = { 'PRIVATE-TOKEN': token };
10
+ const listFiles = async (path, page = 1, limit = 100) => {
11
+ const { data } = await axios_1.default.get(`${gitlabUrl}/repository/tree`, {
12
+ headers,
13
+ params: { page, per_page: limit, ref: branch, path }
14
+ });
15
+ return [
16
+ ...data,
17
+ ...(data.length === limit ? await (0, exports.listFiles)(path, page + 1) : [])
18
+ ];
19
+ };
20
+ exports.listFiles = listFiles;
21
+ const readFile = async ({ path }) => {
22
+ const { data } = await axios_1.default.get(`${gitlabUrl}/repository/files/${encodeURIComponent(path)}`, {
23
+ headers,
24
+ params: { ref: branch }
25
+ });
26
+ return data;
27
+ };
28
+ exports.readFile = readFile;
29
+ const readFileRaw = async ({ id }) => {
30
+ const { data } = await axios_1.default.get(`${gitlabUrl}/repository/blobs/${id}/raw`, {
31
+ headers,
32
+ responseType: 'stream'
33
+ });
34
+ return data;
35
+ };
36
+ exports.readFileRaw = readFileRaw;
37
+ //# sourceMappingURL=gitlab.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"gitlab.js","sourceRoot":"","sources":["../src/gitlab.ts"],"names":[],"mappings":";;;AACA,iCAA0B;AAa1B,MAAM,KAAK,GAAG,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC;AACvC,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,CAAC,iBAAiB,CAAC;AAChD,MAAM,MAAM,GAAG,OAAO,CAAC,GAAG,CAAC,aAAa,CAA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CAAC;IACH,OAAO,IAAI,CAAC;AACd,CAAC,CAAC;AANW,QAAA,WAAW,eAMtB"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hestia-earth/pipeline-utils",
3
- "version": "0.10.2",
3
+ "version": "0.11.0",
4
4
  "description": "Utilities for running pipelines",
5
5
  "main": "dist/index.js",
6
6
  "typings": "dist/index.d.ts",
@@ -41,7 +41,8 @@
41
41
  "@hestia-earth/api": ">=0.19.0",
42
42
  "@hestia-earth/json-schema": ">=20.0.0",
43
43
  "@hestia-earth/schema": ">=20.0.0",
44
- "@hestia-earth/utils": ">=0.11.2"
44
+ "@hestia-earth/utils": ">=0.11.2",
45
+ "axios": "^1.0.0"
45
46
  },
46
47
  "devDependencies": {
47
48
  "@commitlint/cli": "^17.4.2",
@@ -63,6 +64,7 @@
63
64
  "@types/sinon": "^9.0.11",
64
65
  "@typescript-eslint/eslint-plugin": "^5.27.1",
65
66
  "@typescript-eslint/parser": "^5.27.1",
67
+ "axios": "^1.4.0",
66
68
  "chai": "^4.3.7",
67
69
  "eslint": "^7.32.0",
68
70
  "eslint-plugin-jsdoc": "^30.7.13",
package/src/gitlab.ts ADDED
@@ -0,0 +1,47 @@
1
+ import { Readable } from 'stream';
2
+ import axios from 'axios';
3
+
4
+ export interface IGitlabTreeFile {
5
+ id?: string;
6
+ name: string;
7
+ path: string;
8
+ }
9
+
10
+ export interface IGitlabFile {
11
+ size: number;
12
+ content: string;
13
+ }
14
+
15
+ const token = process.env.GITLAB_TOKEN;
16
+ const projectId = process.env.GITLAB_PROJECT_ID;
17
+ const branch = process.env.GITLAB_BRANCH;
18
+
19
+ const gitlabUrl = `https://gitlab.com/api/v4/projects/${projectId}`;
20
+ const headers = { 'PRIVATE-TOKEN': token };
21
+
22
+ export const listFiles = async (path: string, page = 1, limit = 100): Promise<IGitlabTreeFile[]> => {
23
+ const { data } = await axios.get<IGitlabTreeFile[]>(`${gitlabUrl}/repository/tree`, {
24
+ headers,
25
+ params: { page, per_page: limit, ref: branch, path }
26
+ });
27
+ return [
28
+ ...data,
29
+ ...(data.length === limit ? await listFiles(path, page + 1) : [])
30
+ ];
31
+ };
32
+
33
+ export const readFile = async ({ path }: Partial<IGitlabTreeFile>) => {
34
+ const { data } = await axios.get<IGitlabFile>(`${gitlabUrl}/repository/files/${encodeURIComponent(path)}`, {
35
+ headers,
36
+ params: { ref: branch }
37
+ });
38
+ return data;
39
+ };
40
+
41
+ export const readFileRaw = async ({ id }: Partial<IGitlabTreeFile>) => {
42
+ const { data } = await axios.get<Readable>(`${gitlabUrl}/repository/blobs/${id}/raw`, {
43
+ headers,
44
+ responseType: 'stream'
45
+ });
46
+ return data;
47
+ };