forked from ironoa/polkadot-k8s-payouts
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from w3f/gitConfigLoader
added gitConfigLoader
- Loading branch information
Showing
14 changed files
with
1,052 additions
and
817 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
description: Polkadot K8s Payouts | ||
name: polkadot-k8s-payouts | ||
version: v1.1.5 | ||
appVersion: v1.1.5 | ||
version: v1.2.0 | ||
appVersion: v1.2.0 | ||
apiVersion: v2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { GitConfigLoader } from "./gitConfigLoaderInterface"; | ||
import { Target } from "../types"; | ||
|
||
export class Disabled implements GitConfigLoader { | ||
async downloadAndLoad(): Promise<Array<Target>> { | ||
return [] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { InputConfig } from "../types" | ||
import { Disabled } from "./disabled" | ||
import { GitConfigLoader } from "./gitConfigLoaderInterface" | ||
import { GitHub1kv } from "./gitHub1kv" | ||
import { GitLabPrivate } from "./gitLabPrivate" | ||
|
||
export class GitConfigLoaderFactory { | ||
constructor(private readonly cfg: InputConfig){} | ||
makeGitConfigLoaders = (): Array<GitConfigLoader> => { | ||
|
||
const gitConfig = this.cfg.targetsFromGit | ||
|
||
if(!gitConfig?.enabled) | ||
return [new Disabled()] | ||
|
||
const result: Array<GitConfigLoader> = [] | ||
for (const target of gitConfig.targets) { | ||
switch (target.platform.toLowerCase()) { | ||
|
||
case "github1kv": | ||
result.push(new GitHub1kv(target.url)) | ||
break; | ||
case "gitlab": | ||
result.push(new GitLabPrivate(target.url,target.private.apiToken,target.network)) | ||
break; | ||
default: | ||
result.push(new Disabled()) | ||
} | ||
} | ||
return result | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { Target } from "../types"; | ||
|
||
export interface GitConfigLoader { | ||
downloadAndLoad(): Promise<Array<Target>>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { GitConfigLoader } from "./gitConfigLoaderInterface"; | ||
import fetch from 'node-fetch'; | ||
import { Target } from "../types"; | ||
import { TargetFromGit1kv } from "./types"; | ||
import { parse } from 'yaml' | ||
|
||
export class GitHub1kv implements GitConfigLoader { | ||
|
||
constructor( | ||
protected readonly url: string | ||
) { } | ||
|
||
async downloadAndLoad(): Promise<Array<Target>> { | ||
const response = await fetch(this.url); | ||
let data = await response.text(); | ||
// based on the shape of https://github.com/w3f/1k-validators-be/blob/master/helmfile.d/config/kusama/otv-backend-prod.yaml.gotmpl | ||
data = data.replace(/{{.*}}/gm, '') | ||
const candidates: Array<TargetFromGit1kv> = JSON.parse(parse(data)["config"])["scorekeeper"]["candidates"] | ||
|
||
return candidates.map(c=>{ | ||
const target: Target = { | ||
alias: c.name, | ||
validatorAddress: c.stash | ||
} | ||
return target | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
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"; | ||
|
||
export class GitLabPrivate implements GitConfigLoader { | ||
|
||
constructor( | ||
protected readonly url: string, | ||
protected readonly apiToken: string, | ||
protected readonly network: string | ||
) { } | ||
|
||
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 probelm: " + data) | ||
|
||
fs.writeFileSync("./tmp.yaml",data) | ||
const cfg = new Config<InputConfigFromGitLabPrivate>().parse("./tmp.yaml"); | ||
fs.rmSync("./tmp.yaml") | ||
|
||
let tmp: Array<GitLabTarget> = []; | ||
switch (this.network.toLowerCase()) { | ||
case "kusama": | ||
tmp = cfg.Kusama | ||
break; | ||
case "polkadot": | ||
tmp = cfg.Polkadot | ||
break; | ||
default: | ||
throw new Error("unexpected configuration") | ||
} | ||
return tmp.map(t=>{ | ||
const target: Target = { | ||
alias: t.name, | ||
validatorAddress: t.address | ||
} | ||
return target | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
//https://raw.githubusercontent.com/w3f/1k-validators-be/master/helmfile.d/config/kusama/otv-backend-prod.yaml.gotmpl | ||
export interface TargetFromGit1kv { | ||
name: string; | ||
stash: string; | ||
} | ||
|
||
/*********************/ | ||
|
||
export interface GitLabTarget { | ||
name: string; | ||
address: string; | ||
} | ||
|
||
export interface InputConfigFromGitLabPrivate { | ||
Kusama: Array<GitLabTarget>; | ||
Polkadot: Array<GitLabTarget>; | ||
} | ||
|
||
/*********************/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.