-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
208 lines (185 loc) · 6.58 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import fs from "fs";
import { Blob } from "buffer";
import * as glob from "glob";
import core from "@actions/core";
import gitea from "gitea-api";
import path from 'path';
/**
* Extends packages service, adding publish generic packages api method support
*
* @class PackagesServiceEx
* @extends {gitea.PackageService}
*/
class PackagesServiceEx extends gitea.PackageService {
/**
* The http request instance.
* the base url of the api, e.g. https://try.gitea.io/api
* @private
* @type {gitea.BaseHttpRequest}
* @memberof PackagesServiceEx
*/
baseHttpRequest;
/**
* Creates an instance of Packages service.
* @param {gitea.BaseHttpRequest} httpRequest
* @param {gitea.BaseHttpRequest} baseHttpRequest
* @memberof PackagesServiceEx
*/
constructor(httpRequest, baseHttpRequest) {
super(httpRequest);
this.baseHttpRequest = baseHttpRequest;
}
/**
* try get package files from generic package registry
*
* @private
* @param {string} owner The owner of the package.
* @param {string} packageName The package name. It can contain only lowercase letters (a-z), uppercase letter (A-Z), numbers (0-9), dots (.), hyphens (-), pluses (+), or underscores (_).
* @param {string} packageVersion The package version, a non-empty string without trailing or leading whitespaces.
* @return {gitea.CancelablePromise<Array<gitea.PackageFile>>}
* @memberof PackagesServiceEx
*/
async trylistGenericPackageFiles(owner, packageName, packageVersion) {
try {
const response = await this.listPackageFiles({
owner: owner,
type: 'generic',
name: packageName,
version: packageVersion
});
return response;
} catch (error) {
core.warning(error);
return [];
}
}
/**
* Publish zip files to generic package registry
*
* @param {string} owner The owner of the package.
* @param {string} packageName The package name. It can contain only lowercase letters (a-z), uppercase letter (A-Z), numbers (0-9), dots (.), hyphens (-), pluses (+), or underscores (_).
* @param {string} packageVersion The package version, a non-empty string without trailing or leading whitespaces.
* @param {Array<string>} zip_files The zip package files.
* @returns {gitea.CancelablePromise<void>}
* @throws {gitea.ApiError}
* @memberof PackagesServiceEx
*/
async publishGenericPackages(owner, packageName, packageVersion, zip_files) {
core.debug(`Uploading these generic packages: ${zip_files.join(', ')}`);
const genericPackages = await this.trylistGenericPackageFiles(owner, packageName, packageVersion);
if (!Array.isArray(genericPackages) || genericPackages.length === 0) {
core.debug(`The version [${packageVersion}] does not have any generic packages, uploading...`);
} else {
core.debug(`The version [${packageVersion}] already has these generic packages: ${genericPackages.map((genericPackage) => genericPackage.name).join(', ')}`);
}
for (const filepath of zip_files) {
const fileName = path.basename(filepath);
// Check if the file exists. If exists, skip.
const isExists = Array.isArray(genericPackages) && genericPackages.length > 0 && genericPackages.some((genericPackage) => {
return genericPackage.name === fileName;
});
if (isExists) {
core.warning(`Generic package [${fileName}] already exists, skip.`);
continue;
} else {
core.debug(`Generic package [${fileName}] does not exist, uploading...`);
}
// Upload the file.
const content = fs.readFileSync(filepath);
const blob = new Blob([content]);
await this.baseHttpRequest.request({
method: 'PUT',
url: '/packages/{owner}/generic/{name}/{version}/{filename}',
path: {
'owner': owner,
'name': packageName,
'version': packageVersion,
'filename': fileName
},
body: blob,
errors: {
400: `The package name and/or version and/or file name are invalid.`,
409: `A file with the same name exist already in the package.`
}
});
core.debug(`Successfully uploaded generic package ${filepath}`);
}
}
}
async function run() {
try {
const api_url = core.getInput("api_url");
const owner = core.getInput("owner");
const package_name = core.getInput("package_name");
const package_version = core.getInput("package_version");
const files = core.getInput("files");
const token = core.getInput("token");
// if api_url is empty or null or undefined.
if (!api_url) {
core.setFailed(`api_url is required.`);
return;
}
// if owner is empty or null or undefined.
if (!owner) {
core.setFailed(`owner is required.`);
return;
}
// if package_name is empty or null or undefined.
if (!package_name) {
core.setFailed(`package_name is required.`);
return;
}
// if package_version is empty or null or undefined.
if (!package_version) {
core.setFailed(`package_version is required.`);
return;
}
// if files is empty or null or undefined.
if (!files) {
core.setFailed(`files is required.`);
return;
}
// if token is empty or null or undefined.
if (!token) {
core.setFailed(`token is required.`);
return;
}
// Get all files using file patterns.
const file_patterns = files.split('\n')
const all_files = paths(file_patterns);
if (all_files.length == 0) {
core.setFailed(`${file_patterns} not include valid file.`);
return;
}
// The publish package method is an api that is not publicly available in api/v1
const baseApiUrl = api_url.indexOf('/v1') > 0 ? api_url.slice(0, api_url.indexOf('/v1')) : api_url;
const internal_gitea_client = new gitea.GiteaApi({
BASE: baseApiUrl,
WITH_CREDENTIALS: true,
TOKEN: token
});
const gitea_client = new gitea.GiteaApi({
BASE: api_url,
WITH_CREDENTIALS: true,
TOKEN: token
});
const packagesService = new PackagesServiceEx(gitea_client.request, internal_gitea_client.request);
await packagesService.publishGenericPackages(owner, package_name, package_version, all_files);
core.info(`🎉 Successfully uploaded generic packages: ${all_files.join(', ')}`);
} catch (error) {
core.setFailed(error);
}
}
/**
*
* @param {Array<String>} patterns
* @returns {Array<String>}
*/
function paths(patterns) {
return patterns.reduce((acc, pattern) => {
return acc.concat(
glob.sync(pattern).filter((path) => fs.statSync(path).isFile())
);
}, []);
};
run();