From 4b1c3d000d6947a9a6fcaff549a33c047ad1650c Mon Sep 17 00:00:00 2001 From: Mark Wylde Date: Fri, 30 May 2025 22:31:06 +0100 Subject: [PATCH] Attempt to make this work --- src/github/api/client.ts | 8 ++++- src/github/api/gitea-client.ts | 5 ++++ src/github/validation/permissions.ts | 44 ++++++++++++++++++++++++---- 3 files changed, 51 insertions(+), 6 deletions(-) diff --git a/src/github/api/client.ts b/src/github/api/client.ts index 00655a8..08abc53 100644 --- a/src/github/api/client.ts +++ b/src/github/api/client.ts @@ -5,7 +5,13 @@ export type GitHubClient = { }; export function createClient(token: string): GitHubClient { + // Use the GITEA_API_URL environment variable if provided + const apiUrl = process.env.GITEA_API_URL; + console.log( + `Creating client with API URL: ${apiUrl || "default (https://api.github.com)"}`, + ); + return { - api: createGiteaClient(token), + api: apiUrl ? new GiteaApiClient(token, apiUrl) : createGiteaClient(token), }; } diff --git a/src/github/api/gitea-client.ts b/src/github/api/gitea-client.ts index 822afc0..3df1819 100644 --- a/src/github/api/gitea-client.ts +++ b/src/github/api/gitea-client.ts @@ -25,12 +25,17 @@ export class GiteaApiClient { this.baseUrl = baseUrl.replace(/\/+$/, ""); // Remove trailing slashes } + getBaseUrl(): string { + return this.baseUrl; + } + private async request( method: string, endpoint: string, body?: any, ): Promise> { const url = `${this.baseUrl}${endpoint}`; + console.log(`Making ${method} request to: ${url}`); const headers: Record = { "Content-Type": "application/json", diff --git a/src/github/validation/permissions.ts b/src/github/validation/permissions.ts index 1c0206c..c8de0e8 100644 --- a/src/github/validation/permissions.ts +++ b/src/github/validation/permissions.ts @@ -14,19 +14,53 @@ export async function checkWritePermissions( ): Promise { const { repository, actor } = context; + core.info( + `Environment check - GITEA_API_URL: ${process.env.GITEA_API_URL || "undefined"}`, + ); + core.info(`API client base URL: ${api.getBaseUrl?.() || "undefined"}`); + // For Gitea compatibility, check if we're in a non-GitHub environment + const giteaApiUrl = process.env.GITEA_API_URL?.trim(); const isGitea = - process.env.GITEA_API_URL && - !process.env.GITEA_API_URL.includes("api.github.com"); + giteaApiUrl && + giteaApiUrl !== "" && + !giteaApiUrl.includes("api.github.com") && + !giteaApiUrl.includes("github.com"); if (isGitea) { - core.info(`Detected Gitea environment, assuming actor has permissions`); + core.info( + `Detected Gitea environment (${giteaApiUrl}), assuming actor has permissions`, + ); + return true; + } + + // Also check if the API client base URL suggests we're using Gitea + const apiUrl = api.getBaseUrl?.() || ""; + if ( + apiUrl && + !apiUrl.includes("api.github.com") && + !apiUrl.includes("github.com") + ) { + core.info( + `Detected non-GitHub API URL (${apiUrl}), assuming actor has permissions`, + ); + return true; + } + + // If we're still here, we might be using GitHub's API, so attempt the permissions check + core.info( + `Proceeding with GitHub-style permission check for actor: ${actor}`, + ); + + // However, if the API client is clearly pointing to a non-GitHub URL, skip the check + if (apiUrl && apiUrl !== "https://api.github.com") { + core.info( + `API URL ${apiUrl} doesn't look like GitHub, assuming permissions and skipping check`, + ); return true; } try { - core.info(`Checking permissions for actor: ${actor}`); - // Check permissions directly using the permission endpoint const response = await api.customRequest( "GET",