-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Checkpoint: functions for loading the config and rule functions from …
….fensak repository Signed-off-by: Yoriyasu Yano <430092+yorinasub17@users.noreply.github.com>
- Loading branch information
1 parent
8ea012e
commit bbee196
Showing
11 changed files
with
287 additions
and
9 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
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,178 @@ | ||
import { base64, Octokit, path } from "../deps.ts"; | ||
|
||
import { compileRuleFn, RuleFnSourceLang } from "../udr/mod.ts"; | ||
|
||
import type { ComputedFensakConfig, OrgConfig, RuleLookup } from "./types.ts"; | ||
import { getRuleLang, parseConfigFile } from "./parser.ts"; | ||
|
||
const fensakCfgRepoName = ".fensak"; | ||
|
||
interface IGitFileInfo { | ||
filename: string; | ||
gitSHA: string; | ||
} | ||
|
||
/** | ||
* Loads a Fensak configuration from GitHub. This looks up the configuration from the repository `.fensak` in the | ||
* organization. | ||
* | ||
* @param clt An authenticated Octokit instance. | ||
* @param owner The GitHub owner to load the config for. | ||
*/ | ||
export async function loadConfigFromGitHub( | ||
clt: Octokit, | ||
owner: string, | ||
): Promise<ComputedFensakConfig> { | ||
const { data: repo } = await clt.repos.get({ | ||
owner: owner, | ||
repo: fensakCfgRepoName, | ||
}); | ||
const defaultBranch = repo.default_branch; | ||
const { data: ref } = await clt.git.getRef({ | ||
owner: owner, | ||
repo: fensakCfgRepoName, | ||
ref: `heads/${defaultBranch}`, | ||
}); | ||
const headSHA = ref.object.sha; | ||
|
||
// TODO | ||
// Load from cache if the SHA is the same. | ||
|
||
const fileSHALookup = await getFileSHALookup(clt, owner, headSHA); | ||
const cfgFinfo = getConfigFinfo(fileSHALookup); | ||
if (!cfgFinfo) { | ||
throw new Error( | ||
`could not find fensak config file in the '${owner}/.fensak' repo`, | ||
); | ||
} | ||
|
||
const orgCfgContents = await loadFileContents(clt, owner, cfgFinfo); | ||
const orgCfg = parseConfigFile(cfgFinfo.filename, orgCfgContents); | ||
const ruleLookup = await loadRuleFiles(clt, owner, orgCfg, fileSHALookup); | ||
|
||
return { | ||
orgConfig: orgCfg, | ||
ruleLookup: ruleLookup, | ||
gitSHA: headSHA, | ||
}; | ||
} | ||
|
||
/** | ||
* Create a lookup table that maps file paths in a repository tree to the file sha. | ||
*/ | ||
async function getFileSHALookup( | ||
clt: Octokit, | ||
owner: string, | ||
sha: string, | ||
): Promise<Record<string, string>> { | ||
const { data: tree } = await clt.git.getTree({ | ||
owner: owner, | ||
repo: fensakCfgRepoName, | ||
tree_sha: sha, | ||
recursive: "true", | ||
}); | ||
const out: Record<string, string> = {}; | ||
for (const f of tree.tree) { | ||
if (!f.path || !f.sha) { | ||
continue; | ||
} | ||
out[f.path] = f.sha; | ||
} | ||
return out; | ||
} | ||
|
||
/** | ||
* Get config file name and sha in the repo by walking the repository tree. | ||
*/ | ||
function getConfigFinfo( | ||
repoTreeLookup: Record<string, string>, | ||
): IGitFileInfo | null { | ||
for (const fpath in repoTreeLookup) { | ||
const fpathBase = path.basename(fpath); | ||
const fpathExt = path.extname(fpathBase); | ||
if (fpathBase === `fensak${fpathExt}`) { | ||
const fsha = repoTreeLookup[fpath]; | ||
return { | ||
filename: fpath, | ||
gitSHA: fsha, | ||
}; | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
/** | ||
* Load the contents of the given file in the `.fensak` repository. | ||
*/ | ||
async function loadFileContents( | ||
clt: Octokit, | ||
owner: string, | ||
finfo: IGitFileInfo, | ||
): Promise<string> { | ||
const { data: file } = await clt.git.getBlob({ | ||
owner: owner, | ||
repo: fensakCfgRepoName, | ||
file_sha: finfo.gitSHA, | ||
}); | ||
|
||
if (file.encoding !== "base64") { | ||
throw new Error( | ||
`unknown encoding from github blob when retrieving ${finfo.filename}: ${file.encoding}`, | ||
); | ||
} | ||
const contentsBytes = base64.decode(file.content); | ||
return new TextDecoder().decode(contentsBytes); | ||
} | ||
|
||
/** | ||
* Load the referenced rule files in the org config from the `.fensak` repository so that they can be cached. | ||
*/ | ||
async function loadRuleFiles( | ||
clt: Octokit, | ||
owner: string, | ||
orgCfg: OrgConfig, | ||
fileSHALookup: Record<string, string>, | ||
): Promise<RuleLookup> { | ||
const ruleFilesToLoad: Record<string, RuleFnSourceLang> = {}; | ||
for (const repoName in orgCfg.repos) { | ||
const repoCfg = orgCfg.repos[repoName]; | ||
if (ruleFilesToLoad[repoCfg.ruleFile]) { | ||
// Skip because it is already accounted for | ||
continue; | ||
} | ||
|
||
// This is redundant and unnecessary, but it makes the compiler happy. | ||
if (!repoCfg.ruleLang) { | ||
repoCfg.ruleLang = getRuleLang(repoCfg.ruleFile); | ||
} | ||
|
||
ruleFilesToLoad[repoCfg.ruleFile] = repoCfg.ruleLang; | ||
} | ||
|
||
const out: RuleLookup = {}; | ||
for (const fname in ruleFilesToLoad) { | ||
const sha = fileSHALookup[fname]; | ||
if (!sha) { | ||
throw new Error( | ||
`could not find referenced rule file '${fname}' in '.fensak' repository`, | ||
); | ||
} | ||
|
||
const ruleLang = ruleFilesToLoad[fname]; | ||
|
||
// NOTE | ||
// Ideally we can asynchronously fetch the contents here, but GitHub API is very strict about parallel calls and | ||
// rate limiting, so we have to resort to a lousy loop here. | ||
const contents = await loadFileContents(clt, owner, { | ||
filename: fname, | ||
gitSHA: sha, | ||
}); | ||
const compiledContents = compileRuleFn(contents, ruleLang); | ||
|
||
out[fname] = { | ||
sourceGitHash: sha, | ||
compiledRule: compiledContents, | ||
}; | ||
} | ||
return out; | ||
} |
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,51 @@ | ||
import { assert, assertEquals } from "../test_deps.ts"; | ||
import { config, Octokit } from "../deps.ts"; | ||
|
||
import { RuleFnSourceLang } from "../udr/mod.ts"; | ||
|
||
import { loadConfigFromGitHub } from "./loader_github.ts"; | ||
|
||
const token = config.get("github.apiToken"); | ||
let octokit: Octokit; | ||
if (token) { | ||
octokit = new Octokit({ auth: token }); | ||
} else { | ||
octokit = new Octokit(); | ||
} | ||
|
||
Deno.test("loadConfigFromGitHub for fensak-test example repo", async () => { | ||
const cfg = await loadConfigFromGitHub(octokit, "fensak-test"); | ||
assertEquals(cfg.gitSHA, "4c35fe73411fd4a57cd45b0621d63638536425fc"); | ||
assertEquals(cfg.orgConfig, { | ||
repos: { | ||
"test-github-webhooks": { | ||
ruleFile: "subfolder/allow_readme_changes.js", | ||
ruleLang: RuleFnSourceLang.ES6, | ||
}, | ||
"test-fensak-rules-engine": { | ||
ruleFile: "app_deploy_rule.ts", | ||
ruleLang: RuleFnSourceLang.Typescript, | ||
}, | ||
}, | ||
}); | ||
|
||
// Test that the ruleLookup record contains rules for the expected rule functions. | ||
// | ||
// Ideally we can use the assertion functions to test this, but for some bizarre reason, even though the object | ||
// materializes, the assertion functions see that the object is undefined. | ||
const appDeployRule = cfg.ruleLookup["app_deploy_rule.ts"]; | ||
if (!appDeployRule) { | ||
assert(false, "The rule app_deploy_rule.ts was not successfully compiled"); | ||
} | ||
const allowReadmeChanges = | ||
cfg.ruleLookup["subfolder/allow_readme_changes.js"]; | ||
if (!allowReadmeChanges) { | ||
assert( | ||
false, | ||
"The rule subfolder/allow_readme_changes.js was not successfully compiled", | ||
); | ||
} | ||
|
||
// TODO | ||
// add some basic testing for the compiled rule source | ||
}); |
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 |
---|---|---|
@@ -1,6 +1,7 @@ | ||
export { | ||
assert, | ||
assertEquals, | ||
assertExists, | ||
assertFalse, | ||
assertNotEquals, | ||
assertRejects, | ||
|