This repository has been archived by the owner on Jul 10, 2024. It is now read-only.
generated from Bullrich/parity-action-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added logic to read all the commands available in a directory and validate them against a schema. The schema is simple, but it will be slowly growing. Resolves #7 and resolves #10
- Loading branch information
Showing
13 changed files
with
580 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name: Example command | ||
description: This command is just used for examples | ||
commandStart: 'echo "Hello World!"' |
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,15 @@ | ||
name: Command-Action | ||
|
||
on: | ||
pull_request: | ||
|
||
jobs: | ||
cmd-check: | ||
runs-on: ubuntu-latest | ||
name: Bot | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: paritytech/cmd-action@main | ||
with: | ||
commands-directory: '.github/scripts' | ||
GITHUB_TOKEN: ${{ github.token }} |
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,35 @@ | ||
import { readFile } from "fs/promises"; | ||
import { parse } from "yaml"; | ||
|
||
import { ActionLogger } from "./github/types"; | ||
import { Command } from "./schema/command"; | ||
import { validateConfig } from "./schema/validator"; | ||
import { findFilesWithExtension } from "./util"; | ||
|
||
/** The 'commander' of the command actions */ | ||
export class Commander { | ||
private commands?: Command[]; | ||
constructor( | ||
private readonly scriptsDiretory: string, | ||
private readonly logger: ActionLogger, | ||
) {} | ||
|
||
/** Get all the commands from a specific directory and validates them */ | ||
async getCommands(): Promise<Command[]> { | ||
if (this.commands) { | ||
return this.commands; | ||
} | ||
const files = await findFilesWithExtension(this.scriptsDiretory, "yml"); | ||
const commands: Command[] = []; | ||
for (const file of files) { | ||
const content = await readFile(file, "utf-8"); | ||
const command = parse(content) as Command; | ||
this.logger.info(`Parsing ${file}`); | ||
validateConfig(command); | ||
commands.push(command); | ||
} | ||
|
||
this.commands = commands; | ||
return commands; | ||
} | ||
} |
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,7 @@ | ||
export interface Command { | ||
name: string; | ||
description?: string; | ||
machine?: string[]; | ||
timeout?: number; | ||
commandStart: string; | ||
} |
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,17 @@ | ||
import { validate } from "@eng-automation/js"; | ||
import Joi from "joi"; | ||
|
||
import { Command } from "./command"; | ||
|
||
const commandSchema = Joi.object<Command>().keys({ | ||
name: Joi.string().required(), | ||
description: Joi.string().optional(), | ||
machine: Joi.array().items(Joi.string()).optional(), | ||
timeout: Joi.number().min(1).optional(), | ||
commandStart: Joi.string().required(), | ||
}); | ||
|
||
export const validateConfig = (config: Command): Command | never => | ||
validate<Command>(config, commandSchema, { | ||
message: "Command file is invalid", | ||
}); |
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,3 +1,10 @@ | ||
import { findFilesWithExtension } from "../util"; | ||
|
||
test("adds 1 + 2 to equal 3", () => { | ||
expect(1 + 2).toBe(3); | ||
}); | ||
|
||
test("find files", async () => { | ||
const files = await findFilesWithExtension(".github/scripts", "yml"); | ||
expect(files.length).toBeGreaterThan(0); | ||
}); |
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,21 @@ | ||
import { Command } from "../schema/command"; | ||
import { validateConfig } from "../schema/validator"; | ||
|
||
test("test good command", () => { | ||
const goodCommand: Command = { | ||
name: "Hi", | ||
timeout: 10, | ||
commandStart: "./bin", | ||
}; | ||
validateConfig(goodCommand); | ||
}); | ||
|
||
test("test bad command", () => { | ||
const badCommand = { | ||
timeout: -10, | ||
commandStart: "./bin", | ||
}; | ||
expect(() => validateConfig(badCommand as Command)).toThrow( | ||
"Command file is invalid", | ||
); | ||
}); |
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,7 +1,27 @@ | ||
import { debug, error, info, warning } from "@actions/core"; | ||
import fs from "fs/promises"; | ||
import { resolve } from "path"; | ||
|
||
import { ActionLogger } from "./github/types"; | ||
|
||
export function generateCoreLogger(): ActionLogger { | ||
return { info, debug, warn: warning, error }; | ||
} | ||
|
||
export async function findFilesWithExtension( | ||
dir: string, | ||
ext: string, | ||
): Promise<string[]> { | ||
const files: string[] = []; | ||
const dirents = await fs.readdir(dir, { withFileTypes: true }); | ||
for (const dirent of dirents) { | ||
const res = resolve(dir, dirent.name); | ||
if (dirent.isDirectory()) { | ||
files.push(...(await findFilesWithExtension(res, ext))); | ||
} else if (res.endsWith(ext)) { | ||
files.push(res); | ||
} | ||
} | ||
|
||
return files; | ||
} |
Oops, something went wrong.