-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathverify.js
95 lines (83 loc) · 3.27 KB
/
verify.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
import { isString, isPlainObject, isNil, isArray } from "lodash-es";
import got from "got";
import _debug from "debug";
const debug = _debug("semantic-release:gitlab");
import AggregateError from "aggregate-error";
import resolveConfig from "./resolve-config.js";
import getProjectContext from "./get-project-context.js";
import getError from "./get-error.js";
const isNonEmptyString = (value) => isString(value) && value.trim();
const isStringOrStringArray = (value) =>
isNonEmptyString(value) || (isArray(value) && value.every((item) => isNonEmptyString(item)));
const isArrayOf = (validator) => (array) => isArray(array) && array.every((value) => validator(value));
const canBeDisabled = (validator) => (value) => value === false || validator(value);
const VALIDATORS = {
assets: isArrayOf(
(asset) =>
isStringOrStringArray(asset) ||
(isPlainObject(asset) && (isNonEmptyString(asset.url) || isStringOrStringArray(asset.path)))
),
failTitle: canBeDisabled(isNonEmptyString),
failComment: canBeDisabled(isNonEmptyString),
labels: canBeDisabled(isNonEmptyString),
assignee: isNonEmptyString,
};
export default async (pluginConfig, context) => {
const {
options: { repositoryUrl },
logger,
} = context;
const { gitlabToken, gitlabUrl, gitlabApiUrl, proxy, ...options } = resolveConfig(pluginConfig, context);
const { projectPath, projectApiUrl } = getProjectContext(context, gitlabUrl, gitlabApiUrl, repositoryUrl);
debug("apiUrl: %o", gitlabApiUrl);
debug("projectPath: %o", projectPath);
const isValid = (option, value) => {
const validator = VALIDATORS[option];
return isNil(value) || isNil(validator) || VALIDATORS[option](value);
};
const errors = Object.entries({ ...options })
.filter(([option, value]) => !isValid(option, value))
.map(([option, value]) => getError(`EINVALID${option.toUpperCase()}`, { [option]: value }));
if (!projectPath) {
errors.push(getError("EINVALIDGITLABURL"));
}
if (!gitlabToken) {
errors.push(getError("ENOGLTOKEN", { repositoryUrl }));
}
if (gitlabToken && projectPath) {
let projectAccess;
let groupAccess;
logger.log("Verify GitLab authentication (%s)", gitlabApiUrl);
try {
({
permissions: { project_access: projectAccess, group_access: groupAccess },
} = await got
.get(projectApiUrl, {
headers: { "PRIVATE-TOKEN": gitlabToken },
...proxy,
})
.json());
if (
context.options.dryRun &&
!((projectAccess && projectAccess.access_level >= 10) || (groupAccess && groupAccess.access_level >= 10))
) {
errors.push(getError("EGLNOPULLPERMISSION", { projectPath }));
} else if (
!((projectAccess && projectAccess.access_level >= 30) || (groupAccess && groupAccess.access_level >= 30))
) {
errors.push(getError("EGLNOPUSHPERMISSION", { projectPath }));
}
} catch (error) {
if (error.response && error.response.statusCode === 401) {
errors.push(getError("EINVALIDGLTOKEN", { projectPath }));
} else if (error.response && error.response.statusCode === 404) {
errors.push(getError("EMISSINGREPO", { projectPath }));
} else {
throw error;
}
}
}
if (errors.length > 0) {
throw new AggregateError(errors);
}
};