From 68352b848f5390f166608b54a8af0874ce6264a2 Mon Sep 17 00:00:00 2001 From: Dark Steveneq Date: Tue, 20 Jan 2026 09:43:56 +0100 Subject: [PATCH] Port to Gitea-js --- README.md | 3 +++ package-lock.json | 12 ++++++++++++ package.json | 1 + src/interface.ts | 6 ------ src/issue.ts | 45 +++++++++++++++++++++------------------------ src/main.ts | 31 ++++++++++++------------------- src/pr.ts | 11 ++++------- 7 files changed, 53 insertions(+), 56 deletions(-) delete mode 100644 src/interface.ts diff --git a/README.md b/README.md index de0b164..afc187c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,6 @@ +[!INFO] +This repo is a scuffed Gitea port of the [original](https://github.com/oke-py/npm-audit-action) project + # npm audit action [![Coverage Status](https://coveralls.io/repos/github/oke-py/npm-audit-action/badge.svg?branch=main)](https://coveralls.io/github/oke-py/npm-audit-action?branch=main) diff --git a/package-lock.json b/package-lock.json index 0b6fa50..bdf7542 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@actions/core": "^1.11.1", "@actions/github": "^6.0.1", "@octokit/rest": "^22.0.0", + "gitea-js": "^1.23.0", "strip-ansi": "^7.1.0" }, "devDependencies": { @@ -4126,6 +4127,12 @@ "url": "https://github.com/privatenumber/get-tsconfig?sponsor=1" } }, + "node_modules/gitea-js": { + "version": "1.23.0", + "resolved": "https://registry.npmjs.org/gitea-js/-/gitea-js-1.23.0.tgz", + "integrity": "sha512-f4+UPoWgDetZeZ+Awo5iI1nVdO5bjxA8+2QCeLo3oYWUYxKyzLfXgbW1EPD635wb8hLgS0DRBu5XhtiuYKEeUA==", + "license": "MIT" + }, "node_modules/glob-parent": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", @@ -9493,6 +9500,11 @@ "resolve-pkg-maps": "^1.0.0" } }, + "gitea-js": { + "version": "1.23.0", + "resolved": "https://registry.npmjs.org/gitea-js/-/gitea-js-1.23.0.tgz", + "integrity": "sha512-f4+UPoWgDetZeZ+Awo5iI1nVdO5bjxA8+2QCeLo3oYWUYxKyzLfXgbW1EPD635wb8hLgS0DRBu5XhtiuYKEeUA==" + }, "glob-parent": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", diff --git a/package.json b/package.json index 7246000..c991e15 100644 --- a/package.json +++ b/package.json @@ -37,6 +37,7 @@ "@actions/core": "^1.11.1", "@actions/github": "^6.0.1", "@octokit/rest": "^22.0.0", + "gitea-js": "^1.23.0", "strip-ansi": "^7.1.0" }, "devDependencies": { diff --git a/src/interface.ts b/src/interface.ts deleted file mode 100644 index e16e064..0000000 --- a/src/interface.ts +++ /dev/null @@ -1,6 +0,0 @@ -export interface IssueOption { - title: string - body: string - assignees?: string[] - labels?: string[] -} diff --git a/src/issue.ts b/src/issue.ts index c857eba..ab43908 100644 --- a/src/issue.ts +++ b/src/issue.ts @@ -1,20 +1,28 @@ import * as core from '@actions/core' -import { IssueOption } from './interface.js' +import * as github from '@actions/github' +import { Api, CreateIssueOption, HttpResponse, Issue } from 'gitea-js' -export function getIssueOption(body: string): IssueOption { +export async function getIssueOption(api: Api, body: string): Promise { let assignees: string[] | undefined - let labels: string[] | undefined + let labels: number[] | undefined if (core.getInput('issue_assignees')) { assignees = core.getInput('issue_assignees').replace(/\s+/g, '').split(',') } if (core.getInput('issue_labels')) { - labels = core - .getInput('issue_labels') - .split(',') - .map((label) => label.trim()) + labels = []; + const labelNames = core + .getInput('issue_labels') + .split(',') + .map((label) => label.trim()); + (await api.repos.issueListLabels(github.context.repo.owner, github.context.repo.repo)).data.forEach(label => { + if (label.name && label.id && labelNames.includes(label.name)) { + labels?.push(label.id); + } + }) } + return { title: core.getInput('issue_title'), body, @@ -23,28 +31,17 @@ export function getIssueOption(body: string): IssueOption { } } -export type GetIssuesFunc = (options: { - owner: string - repo: string - state: 'open' | 'closed' | 'all' | undefined - [key: string]: string | undefined // Allow additional properties -}) => Promise<{ data: Array<{ title: string; number: number }> }> - export async function getExistingIssueNumber( - getIssues: GetIssuesFunc, + issues: HttpResponse, repo: { owner: string repo: string } ): Promise { - const { data: issues } = await getIssues({ - ...repo, - state: 'open' + issues.data.forEach(iss => { + if (iss.title == core.getInput('issue_title')) { + return iss.number; + } }) - - const iss = issues - .filter((i) => i.title === core.getInput('issue_title')) - .shift() - - return iss?.number ?? null + return null; } diff --git a/src/main.ts b/src/main.ts index 7353c70..f2d4191 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,11 +1,10 @@ import * as core from '@actions/core' import * as github from '@actions/github' -import { Octokit } from '@octokit/rest' import { Audit } from './audit.js' -import { IssueOption } from './interface.js' import * as issue from './issue.js' import * as pr from './pr.js' import * as workdir from './workdir.js' +import { CreateIssueOption, giteaApi } from 'gitea-js' export async function run(): Promise { try { @@ -70,10 +69,11 @@ export async function run(): Promise { // vulnerabilities are found // get GitHub information - const ctx = JSON.parse(core.getInput('github_context')) - const token: string = core.getInput('github_token', { required: true }) - const octokit = new Octokit({ - auth: token + const ctx = JSON.parse(core.getInput('gitea_context')) + const baseUrl: string = core.getInput('gitea_url', { required: true }) + const token: string = core.getInput('gitea_token', { required: true }) + const api = giteaApi(baseUrl, { + token: token }) if (ctx.event_name === 'pull_request') { @@ -84,7 +84,7 @@ export async function run(): Promise { if (createPRComments === 'true') { await pr.createComment( - octokit, + api, github.context.repo.owner, github.context.repo.repo, ctx.event.number, @@ -107,28 +107,21 @@ export async function run(): Promise { // remove control characters and create a code block const issueBody = audit.strippedStdout() - const option: IssueOption = issue.getIssueOption(issueBody) + const option: CreateIssueOption = await issue.getIssueOption(api, issueBody) const existingIssueNumber = core.getInput('dedupe_issues') === 'true' ? await issue.getExistingIssueNumber( - octokit.issues.listForRepo, + await api.repos.issueListIssues(github.context.repo.owner, github.context.repo.repo, {state: "all"}), github.context.repo ) : null if (existingIssueNumber !== null) { - const { data: createdComment } = await octokit.issues.createComment({ - ...github.context.repo, - issue_number: existingIssueNumber, - body: option.body - }) - core.debug(`comment ${createdComment.url}`) + const { data: createdComment } = await api.repos.issueCreateComment(github.context.repo.owner, github.context.repo.repo, existingIssueNumber, {body: option.body ?? ""}) + core.debug(`comment ${createdComment.issue_url}`) } else { - const { data: createdIssue } = await octokit.issues.create({ - ...github.context.repo, - ...option - }) + const { data: createdIssue } = await api.repos.issueCreateIssue(github.context.repo.owner, github.context.repo.repo, option) core.debug(`#${createdIssue.number}`) } core.setFailed('This repo has some vulnerabilities') diff --git a/src/pr.ts b/src/pr.ts index 1ffd8e2..50c7417 100644 --- a/src/pr.ts +++ b/src/pr.ts @@ -1,16 +1,13 @@ -import { Octokit } from '@octokit/rest' +import { Api } from "gitea-js"; export async function createComment( - octokit: Octokit, + api: Api, owner: string, repo: string, prNumber: number, body: string ): Promise { - await octokit.issues.createComment({ - owner, - repo, - issue_number: prNumber, + await api.repos.issueCreateComment(owner, repo, prNumber, { body - }) + }); }