forked from ironoa/polkadot-k8s-payouts
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathgitLabPrivate.ts
85 lines (79 loc) · 2.43 KB
/
gitLabPrivate.ts
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
import { GitConfigLoader } from "./gitConfigLoaderInterface";
import fetch from 'node-fetch';
import fs from 'fs';
import { Config } from '@w3f/config';
import { Target } from "../types";
import { GitLabTarget, InputConfigFromGitLabPrivate } from "./types";
import { GitConfigVersion } from "../constants";
import { ConfigAccountSettings, MonitoringGroup, Chain } from "@w3f/monitoring-types";
import { ConfigProcessor } from "@w3f/monitoring-config";
export class GitLabPrivate implements GitConfigLoader {
constructor(
protected readonly url: string,
protected readonly apiToken: string,
protected readonly network: string,
protected readonly version: GitConfigVersion
) { }
async downloadAndLoad(): Promise<Array<Target>> {
const response = await fetch(this.url, {
headers: {
'PRIVATE-TOKEN': this.apiToken
}
});
const data = await response.text();
if(!response.ok) throw new Error("git config download problem: " + data)
let configV1;
fs.writeFileSync("./tmp.yaml", data);
switch (this.version) {
case GitConfigVersion.V2: {
const configV2 = ConfigProcessor.processConfigs(["./tmp.yaml"]);
configV1 = this.configV2toV1(configV2);
break;
}
case GitConfigVersion.V1:
default: {
configV1 = new Config<InputConfigFromGitLabPrivate>().parse("./tmp.yaml");
break;
}
}
fs.rmSync("./tmp.yaml");
let tmp: Array<GitLabTarget> = [];
switch (this.network.toLowerCase()) {
case "kusama":
tmp = configV1.Kusama
break;
case "polkadot":
tmp = configV1.Polkadot
break;
default:
throw new Error("unexpected configuration")
}
return tmp.map(t=>{
const target: Target = {
alias: t.name,
validatorAddress: t.address
}
return target
})
}
configV2toV1(groups: MonitoringGroup[]): InputConfigFromGitLabPrivate {
const result: InputConfigFromGitLabPrivate = {
Kusama: [],
Polkadot: [],
};
for (const group of groups) {
if (group.chain !== Chain.Kusama && group.chain !== Chain.Polkadot) {
continue;
}
if (!group.enablePayout) {
continue;
}
const targets = group.accounts.map((account: ConfigAccountSettings) => ({
name: account.name,
address: account.ss58,
}));
result[group.chain].push(...targets);
}
return result;
}
}