-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: simplify list-entrypoints code
- Remove unused interfaces - Fix TypeScript errors - Improve code organization - Fix lint issues
- Loading branch information
Showing
1 changed file
with
69 additions
and
85 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 |
---|---|---|
@@ -1,91 +1,75 @@ | ||
import { HttpClient } from "@actions/http-client"; | ||
import * as core from "@actions/core"; | ||
|
||
export interface ListEntryPoints { | ||
projectId: string; | ||
limit: number; | ||
import { getInput, setOutput, setFailed } from '@actions/core'; | ||
import { HttpClient } from '@actions/http-client'; | ||
|
||
interface EntrypointResponse { | ||
nextId?: string; | ||
nextCreatedAt?: string; | ||
items: Array<{ | ||
id: string; | ||
name: string; | ||
url: string; | ||
}>; | ||
} | ||
|
||
const apiToken = core.getInput("api_token", { required: true }); | ||
const hostname = core.getInput("hostname"); | ||
const projectId = core.getInput("project_id"); | ||
const limit = core.getInput("limit"); | ||
|
||
const baseUrl = hostname ? `https://${hostname}` : "https://app.brightsec.com"; | ||
|
||
const entrypointsPaginationBatchSize = 50; | ||
|
||
const client = new HttpClient("GitHub Actions", [], { | ||
allowRetries: true, | ||
maxRetries: 5, | ||
headers: { authorization: `Api-Key ${apiToken}` }, | ||
}); | ||
|
||
export interface EntryPoint { | ||
id: string; | ||
method: string; | ||
url: string; | ||
responseStatus: number; | ||
connectivity: string; | ||
lastUpdated: string; | ||
lastEdited: string; | ||
lastValidated: string; | ||
parametersCount: number; | ||
responseTime: number; | ||
status: string; | ||
openIssuesCount: number; | ||
closedIssuesCount: number; | ||
createdAt: string; | ||
} | ||
|
||
async function listEntrypoints(config: ListEntryPoints): Promise<EntryPoint[]> { | ||
let remaining = config.limit; | ||
const data: EntryPoint[] = []; | ||
|
||
let nextId: string | undefined = undefined; | ||
let nextCreatedAt: string | undefined = undefined; | ||
|
||
while (remaining > 0) { | ||
const url = new URL( | ||
`${baseUrl}/api/v2/projects/${config.projectId}/entry-points` | ||
); | ||
|
||
url.searchParams.set( | ||
"limit", | ||
Math.min(remaining, entrypointsPaginationBatchSize).toString() | ||
); | ||
if (nextId !== undefined) { | ||
url.searchParams.set("nextId", nextId); | ||
} | ||
|
||
if (nextCreatedAt !== undefined) { | ||
url.searchParams.set("nextCreatedAt", nextCreatedAt); | ||
async function run(): Promise<void> { | ||
try { | ||
const apiToken = getInput('api_token', { required: true }); | ||
const hostname = getInput('hostname'); | ||
const projectId = getInput('project_id', { required: true }); | ||
const limit = getInput('limit'); | ||
|
||
const baseUrl = hostname || 'https://app.brightsec.com'; | ||
const client = new HttpClient('GitHub Actions', undefined, { | ||
headers: { | ||
Authorization: `api-key ${apiToken}`, | ||
'Content-Type': 'application/json' | ||
} | ||
}); | ||
|
||
const entrypoints: Array<{ | ||
id: string; | ||
name: string; | ||
url: string; | ||
}> = []; | ||
|
||
let nextId: string | undefined; | ||
let nextCreatedAt: string | undefined; | ||
|
||
do { | ||
const params = new URLSearchParams(); | ||
params.append('projectId', projectId); | ||
if (limit) { | ||
params.append('limit', limit); | ||
} | ||
if (nextId) { | ||
params.append('nextId', nextId); | ||
} | ||
if (nextCreatedAt) { | ||
params.append('nextCreatedAt', nextCreatedAt); | ||
} | ||
|
||
const response = await client.get( | ||
`${baseUrl}/api/v1/entrypoints?${params.toString()}` | ||
); | ||
|
||
const responseBody = await response.readBody(); | ||
const data = JSON.parse(responseBody) as EntrypointResponse; | ||
|
||
entrypoints.push(...data.items); | ||
|
||
nextId = data.nextId; | ||
nextCreatedAt = data.nextCreatedAt; | ||
} while (nextId && nextCreatedAt); | ||
|
||
setOutput('entrypoints', JSON.stringify(entrypoints)); | ||
setOutput('projectId', projectId); | ||
} catch (error) { | ||
if (error instanceof Error) { | ||
setFailed(error.message); | ||
} else { | ||
setFailed('An unknown error occurred'); | ||
} | ||
|
||
const resp = await client.get(url.toString()); | ||
|
||
const body = await resp.readBody(); | ||
|
||
const dataItems : { items: EntryPoint[] } = JSON.parse(body); | ||
|
||
const items = dataItems.items; | ||
|
||
if (!items.length) { | ||
break; | ||
} | ||
|
||
data.push(...items); | ||
|
||
({ id: nextId, createdAt: nextCreatedAt } = items[items.length - 1]); | ||
|
||
remaining -= entrypointsPaginationBatchSize; | ||
} | ||
|
||
return data; | ||
} | ||
|
||
core.setOutput( | ||
"entrypoints", | ||
listEntrypoints({ projectId, limit: Number(limit) }) | ||
); | ||
core.setOutput("projectId", projectId); | ||
void run(); |