Skip to content

Commit

Permalink
add intput-to-json and filter-paths actions
Browse files Browse the repository at this point in the history
  • Loading branch information
pngwn committed Aug 3, 2024
1 parent 6c3f9df commit 56e8a5b
Show file tree
Hide file tree
Showing 7 changed files with 1,365 additions and 947 deletions.
19 changes: 19 additions & 0 deletions packages/filter-paths/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: "filter paths"

inputs:
name:
description: "Name of the filter to apply"
required: true
path:
description: "Path to the json file containing the filters"
required: true
token:
description: "GitHub token to read pull requests"
required: true
outputs:
match:
description: "Boolean indicating if the filter matched"

runs:
using: "node20"
main: "dist/index.js"
72 changes: 72 additions & 0 deletions packages/filter-paths/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { getInput, setOutput } from "@actions/core";
import { context, getOctokit } from "@actions/github";
import { readFileSync } from "fs";
import { join } from "path";

import picomatch from "picomatch";

function match_filter(patterns: string[], files: string[]) {
return files.some((file) => picomatch.isMatch(file, patterns));
}

async function run() {
const filter_name = getInput("filter");
const path = getInput("path") || ".github/filters.json";
const token = getInput("token");

const octokit = getOctokit(token);

const full_path = join(process.cwd(), path);
const filter_file = readFileSync(full_path, "utf-8");

const filters = JSON.parse(filter_file) as Record<string, string[]>;
if (!filters.hasOwnProperty(filter_name)) {
throw new Error(`Unknown filter: ${filter_name}`);
}
const filter = filters[filter_name];

let files: any[] = [];
for await (const response of octokit.paginate.iterator(
octokit.rest.pulls.listFiles,
{
owner: "octokit",
repo: "rest.js",
pull_number: context.payload.pull_request?.number as number,
per_page: 100,
}
)) {
files = [...files, ...parse_data(response.data)];
}

const result = match_filter(filter, files);

console.log(result);

setOutput("match", result);
}

run();

function parse_data(data: any): any[] {
// If the data is an array, return that
if (Array.isArray(data)) {
return data;
}

// Some endpoints respond with 204 No Content instead of empty array
// when there is no data. In that case, return an empty array.
if (!data) {
return [];
}

// Otherwise, the array of items that we want is in an object
// Delete keys that don't include the array of items
delete data.incomplete_results;
delete data.repository_selection;
delete data.total_count;
// Pull out the array of items
const namespaceKey = Object.keys(data)[0];
data = data[namespaceKey];

return data;
}
17 changes: 17 additions & 0 deletions packages/filter-paths/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@gradio-action/filter-paths",
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {
"@types/picomatch": "^3.0.0",
"picomatch": "^4.0.2"
}
}
13 changes: 13 additions & 0 deletions packages/input-to-json/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
name: "Convert inputs to a json file"

inputs:
path:
description: "Path to the json file"
required: true
outputs:
json:
description: "The json file"

runs:
using: "node20"
main: "dist/index.js"
40 changes: 40 additions & 0 deletions packages/input-to-json/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { getInput, setOutput } from "@actions/core";
import { writeFileSync } from "fs";
import { join } from "path";

async function run() {
const path = getInput("path");
try {
const inputs: Record<string, any> = {};
for (const key in process.env) {
if (key.startsWith("INPUT_")) {
const input_name = key.slice(6).toLowerCase();
const parts = input_name.split(".");

let current = inputs;
for (let i = 0; i < parts.length; i++) {
const part = parts[i];
if (i === parts.length - 1) {
current[part] = process.env[key];
} else {
if (!current[part]) {
current[part] = {};
}
current = current[part];
}
}
}
}

const json = JSON.stringify(inputs, null, 2);
const full_path = join(process.cwd(), path);

writeFileSync(full_path, json, "utf-8");
setOutput("path", full_path);
} catch (error) {
console.error(error);
setOutput("path", "");
}
}

run();
14 changes: 14 additions & 0 deletions packages/input-to-json/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@gradio-action/input-to-json",
"version": "0.1.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": [],
"author": "",
"license": "ISC",
"type": "module",
"dependencies": {}
}
Loading

0 comments on commit 56e8a5b

Please sign in to comment.