Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
Use filename instead of name (#24)
Browse files Browse the repository at this point in the history
Changed action to use the filename of the file instead the name
parameter.

Added a raw example on how to run each command in the documentation.

Added a step to lint the project.

Added a test to check if the `commander` is properly fetching the
scripts of a repository.

This resolves #22

---------

Co-authored-by: Bullrich <Bullrich@users.noreply.github.com>
  • Loading branch information
Bullrich and Bullrich committed May 14, 2024
1 parent 6ae81c9 commit fb27fd4
Show file tree
Hide file tree
Showing 10 changed files with 136 additions and 90 deletions.
3 changes: 3 additions & 0 deletions .github/scripts/lint/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
name: Lint
description: This command lints the project
commandStart: 'yarn install && yarn run fix'
3 changes: 3 additions & 0 deletions .github/workflows/cmd-action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ jobs:
command: ${{ fromJson(needs.cmd-check.outputs.commands) }}
name: Run command
steps:
- uses: actions/setup-node@v4
with:
node-version: 20
- uses: paritytech/cmd-action/run@main
with:
branch: ${{ needs.cmd-check.outputs.branch }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:
FILE_NAME: ".github/workflows/cmd-action.yml"
- name: Validate that cmd-run points to main branch
run: |
BRANCH=$(yq '.jobs.cmd-run.steps[0].uses' $FILE_NAME | cut -d "@" -f2)
BRANCH=$(yq '.jobs.cmd-run.steps[1].uses' $FILE_NAME | cut -d "@" -f2)
# If the branch is not the main branch
if [ "$BRANCH" != "$GITHUB_BASE_REF" ]; then
echo "Action points to $BRANCH. It has to point to $GITHUB_BASE_REF instead!"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"homepage": "https://github.com/Bullrich/parity-action-template#readme",
"dependencies": {
"@actions/core": "^1.10.1",
"@actions/github": "^5.1.1",
"@actions/github": "^6.0.0",
"@eng-automation/integrations": "^4.3.0",
"@octokit/webhooks-types": "^7.5.1",
"joi": "^17.13.1",
Expand Down
13 changes: 8 additions & 5 deletions src/commander.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { summary } from "@actions/core";
import { readFile } from "fs/promises";
import { dirname } from "path";
import path from "path";
import { parse } from "yaml";

import { ActionLogger } from "./github/types";
Expand All @@ -25,9 +25,10 @@ export class Commander {
const commands: Command[] = [];
for (const file of files) {
const content = await readFile(file, "utf-8");
const command = parse(content) as Command;
command.location = dirname(file);
this.logger.info(`Parsing ${file}`);
const command = parse(content) as Command;
command.filename = path.basename(file, ".yml");
command.location = path.dirname(file);
validateConfig(command);
commands.push(command);
}
Expand All @@ -49,6 +50,8 @@ export class Commander {
text = text.addRaw(command.description).addEOL();
}
text = text
.addEOL()
.addRaw(`How to run: <code>/cmd ${command.filename}</code>`)
.addEOL()
.addDetails("File location", `<code>${command.location}</code>`)
.addEOL();
Expand Down Expand Up @@ -77,13 +80,13 @@ export class Commander {
const commands = await this.getCommands();
const outputs: { name: string; command: string }[] = [];
for (const comment of lines) {
// parse "/bot command"
// parse "/cmd command"
const [_, command] = comment.trim().split(" ");

this.logger.info(`Searching for command '${command}'`);

const matchingCommand = commands.findIndex(
({ name }) => name === command,
({ filename }) => filename === command,
);
if (matchingCommand < 0) {
throw new Error(
Expand Down
1 change: 1 addition & 0 deletions src/schema/command.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface Command {
name: string;
filename: string;
location: string;
description?: string;
machine?: string[];
Expand Down
4 changes: 3 additions & 1 deletion src/schema/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ const commandSchema = Joi.object<Command>().keys({
commandStart: Joi.string().required(),
});

export const validateConfig = (config: Command): Command | never =>
type JoiCommand = Omit<Command, "filename">;

export const validateConfig = (config: JoiCommand): JoiCommand | never =>
validate<Command>(config, commandSchema, {
message: "Command file is invalid",
});
8 changes: 8 additions & 0 deletions src/test/commander.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Commander } from "../commander";
import { generateCoreLogger } from "../util";

test("test command inside repository", async () => {
const commander = new Commander(".github/scripts", generateCoreLogger());
const commands = await commander.getCommands();
expect(commands.length).toBeGreaterThan(0);
});
2 changes: 1 addition & 1 deletion src/test/validator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Command } from "../schema/command";
import { validateConfig } from "../schema/validator";

test("test good command", () => {
const goodCommand: Command = {
const goodCommand: Omit<Command, "filename"> = {
name: "Hi",
timeout: 10,
location: "",
Expand Down
Loading

0 comments on commit fb27fd4

Please sign in to comment.