static async validateRepositoryAccess()

in app/lib/server/githubTokenService.ts [75:116]


  static async validateRepositoryAccess(
    token: string,
    repositoryUrl: string
  ): Promise<{ canAccess: boolean; isPrivate: boolean }> {
    try {
      const repoMatch = repositoryUrl.match(
        /github\.com[\/:]([^\/]+)\/([^\/\.]+)/
      );
      if (!repoMatch) {
        return { canAccess: false, isPrivate: false };
      }

      const [, owner, repo] = repoMatch;

      const response = await fetch(
        `${this.GITHUB_API_BASE}/repos/${owner}/${repo}`,
        {
          headers: {
            Authorization: `Bearer ${token}`,
            Accept: "application/vnd.github+json",
            "X-GitHub-Api-Version": "2022-11-28",
          },
        }
      );

      if (response.ok) {
        const repoData = await response.json();
        return {
          canAccess: true,
          isPrivate: repoData.private || false,
        };
      } else if (response.status === 404) {
        // Could be private repo without access, or non-existent repo
        return { canAccess: false, isPrivate: true }; // Assume private if not found
      } else {
        return { canAccess: false, isPrivate: false };
      }
    } catch (error) {
      console.error("Error validating repository access:", error);
      return { canAccess: false, isPrivate: false };
    }
  }