-
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.
add intput-to-json and filter-paths actions
- Loading branch information
Showing
7 changed files
with
1,365 additions
and
947 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,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" |
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,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; | ||
} |
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 @@ | ||
{ | ||
"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" | ||
} | ||
} |
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,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" |
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,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(); |
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,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": {} | ||
} |
Oops, something went wrong.