Skip to content

Support for github enterprise server(self hosted github) #646

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions backend/analytics_server/mhq/exapi/github.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ class GithubRateLimitExceeded(Exception):


class GithubApiService:
def __init__(self, access_token: str):
def __init__(self, access_token: str, domain: Optional[str]):
self._token = access_token
self._g = Github(self._token, per_page=PAGE_SIZE)
self.base_url = "https://api.github.com"
self.base_url = self._get_api_url(domain)
self._g = Github(self._token, base_url=self.base_url, per_page=PAGE_SIZE)
self.headers = {"Authorization": f"Bearer {self._token}"}

def _get_api_url(self, domain: str) -> str:
if not domain:
return "https://api.github.com"
else:
return f"{domain}/api/v3"

@contextlib.contextmanager
def temp_config(self, per_page: int = 30):
self._g.per_page = per_page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import List, Dict, Optional, Tuple, Set

import pytz
from mhq.utils.github import get_custom_github_domain
from github.PaginatedList import PaginatedList as GithubPaginatedList
from github.PullRequest import PullRequest as GithubPullRequest
from github.PullRequestReview import PullRequestReview as GithubPullRequestReview
Expand Down Expand Up @@ -371,7 +372,7 @@ def _get_access_token():

return GithubETLHandler(
org_id,
GithubApiService(_get_access_token()),
GithubApiService(_get_access_token(), get_custom_github_domain(org_id)),
CodeRepoService(),
CodeETLAnalyticsService(),
get_revert_prs_github_sync_handler(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,29 +26,29 @@ def __init__(
self.custom_domain = custom_domain

def get_github_organizations(self):
github_api_service = GithubApiService(self.access_token)
github_api_service = GithubApiService(self.access_token, self.custom_domain)
try:
orgs: [GithubOrganization] = github_api_service.get_org_list()
except GithubException as e:
raise e
return orgs

def get_github_org_repos(self, org_login: str, page_size: int, page: int):
github_api_service = GithubApiService(self.access_token)
github_api_service = GithubApiService(self.access_token, self.custom_domain)
try:
return github_api_service.get_repos_raw(org_login, page_size, page)
except GithubException as e:
raise e

def get_github_personal_repos(self, page_size: int, page: int):
github_api_service = GithubApiService(self.access_token)
github_api_service = GithubApiService(self.access_token, self.custom_domain)
try:
return github_api_service.get_user_repos_raw(page_size, page)
except GithubException as e:
raise e

def get_repo_workflows(self, gh_org_name: str, gh_org_repo_name: str):
github_api_service = GithubApiService(self.access_token)
github_api_service = GithubApiService(self.access_token, self.custom_domain)
try:
workflows = github_api_service.get_repo_workflows(
gh_org_name, gh_org_repo_name
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import pytz

from mhq.utils.github import get_custom_github_domain
from mhq.exapi.github import GithubApiService
from mhq.service.workflows.sync.etl_provider_handler import WorkflowProviderETLHandler
from mhq.store.models import UserIdentityProvider
Expand Down Expand Up @@ -181,5 +182,7 @@ def _get_access_token():
return access_token

return GithubActionsETLHandler(
org_id, GithubApiService(_get_access_token()), WorkflowRepoService()
org_id,
GithubApiService(_get_access_token(), get_custom_github_domain(org_id)),
WorkflowRepoService(),
)
25 changes: 25 additions & 0 deletions backend/analytics_server/mhq/utils/github.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from queue import Queue
from threading import Thread
from typing import Optional

from github import Organization

from mhq.utils.log import LOG
from mhq.store.repos.core import CoreRepoService
from mhq.store.models import UserIdentityProvider


def github_org_data_multi_thread_worker(orgs: [Organization]) -> dict:
Expand Down Expand Up @@ -48,3 +51,25 @@ def run(self):
for worker in workers:
r.update(worker.results)
return r


def get_custom_github_domain(org_id: str) -> Optional[str]:
DEFAULT_DOMAIN = "https://api.github.com"
core_repo_service = CoreRepoService()
integrations = core_repo_service.get_org_integrations_for_names(
org_id, [UserIdentityProvider.GITHUB.value]
)

github_domain = (
integrations[0].provider_meta.get("custom_domain")
if integrations[0].provider_meta
else None
)

if not github_domain:
LOG.warn(
f"Custom domain not found for intergration for org {org_id} and provider {UserIdentityProvider.GITHUB.value}"
)
return DEFAULT_DOMAIN

return github_domain
Empty file.
40 changes: 40 additions & 0 deletions backend/analytics_server/tests/exapi/test_github.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import unittest
from unittest.mock import patch

from mhq.exapi.github import GithubApiService, PAGE_SIZE


class DummyGithub:
def __init__(self, token, base_url=None, per_page=None):
self.token = token
self.base_url = base_url
self.per_page = per_page


class TestGithubApiService(unittest.TestCase):

@patch("mhq.exapi.github.Github", new=DummyGithub)
def test_default_domain_sets_standard_api_url(self):
token = "deadpool"
service = GithubApiService(access_token=token, domain=None)
self.assertEqual(service.base_url, "https://api.github.com")
self.assertIsInstance(service._g, DummyGithub)
self.assertEqual(service._g.token, token)
self.assertEqual(service._g.base_url, "https://api.github.com")
self.assertEqual(service._g.per_page, PAGE_SIZE)

@patch("mhq.exapi.github.Github", new=DummyGithub)
def test_empty_string_domain_uses_default_url(self):
token = "deadpool"
service = GithubApiService(access_token=token, domain="")
self.assertEqual(service.base_url, "https://api.github.com")
self.assertEqual(service._g.base_url, "https://api.github.com")

@patch("mhq.exapi.github.Github", new=DummyGithub)
def test_custom_domain_appends_api_v3(self):
token = "deadpool"
custom_domain = "https://github.sujai.com"
service = GithubApiService(access_token=token, domain=custom_domain)
expected = f"{custom_domain}/api/v3"
self.assertEqual(service.base_url, expected)
self.assertEqual(service._g.base_url, expected)
1 change: 1 addition & 0 deletions web-server/jest.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
preset: 'ts-jest/presets/js-with-babel', // Use the TypeScript preset with Babel
testEnvironment: 'jsdom', // Use jsdom as the test environment (for browser-like behavior)
setupFiles: ['<rootDir>/jest.setup.js'],
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'json', 'node'],
testMatch: [
'**/__tests__/**/*.test.(ts|tsx|js|jsx)',
Expand Down
3 changes: 3 additions & 0 deletions web-server/jest.setup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const { TextEncoder, TextDecoder } = require('util');
global.TextEncoder = TextEncoder;
global.TextDecoder = TextDecoder;
84 changes: 84 additions & 0 deletions web-server/pages/api/internal/[org_id]/__tests__/github.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
jest.mock('@/utils/db', () => ({
db: jest.fn(),
}));

import { db } from '@/utils/db';
import * as githubUtils from '../utils';
import { DEFAULT_GH_URL } from '@/constants/urls';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Critical issue with DEFAULT_GH_URL constant

There appears to be a typo in the imported constant from @/constants/urls.ts. The constant is defined as 'https://api.githb.com' (missing an 'u'), which will cause all tests and production code to use an incorrect GitHub API URL.

#!/bin/bash
# Verify the constant definition in the urls.ts file
cat web-server/src/constants/urls.ts | grep DEFAULT_GH_URL


describe('GitHub URL utilities', () => {
afterEach(() => {
jest.resetAllMocks();
});

describe('getGitHubCustomDomain', () => {
it('returns custom_domain when present', async () => {
const mockMeta = [{ custom_domain: 'custom.sujai.com' }];
(db as jest.Mock).mockReturnValue({
where: jest.fn().mockReturnThis(),
then: jest.fn().mockResolvedValue(mockMeta)
});

const domain = await githubUtils.getGitHubCustomDomain();
expect(domain).toBe('custom.sujai.com');
});

it('returns null when no provider_meta found', async () => {
(db as jest.Mock).mockReturnValue({
where: jest.fn().mockReturnThis(),
then: jest.fn().mockResolvedValue([])
});

const domain = await githubUtils.getGitHubCustomDomain();
expect(domain).toBeNull();
});

it('returns null on db error and logs error', async () => {
const consoleSpy = jest.spyOn(console, 'error').mockImplementation();
(db as jest.Mock).mockImplementation(() => {
throw new Error('DB failure');
});

const domain = await githubUtils.getGitHubCustomDomain();
expect(domain).toBeNull();
expect(consoleSpy).toHaveBeenCalledWith(
'Error occured while getting custom domain from database:',
expect.any(Error)
);
});
});

describe('getGitHubRestApiUrl', () => {
it('uses default URL when no custom domain', async () => {
jest.spyOn(githubUtils, 'getGitHubCustomDomain').mockResolvedValue(null);
const url = await githubUtils.getGitHubRestApiUrl('path/to/repo');
expect(url).toBe(`${DEFAULT_GH_URL}/path/to/repo`);
});

it('uses custom domain when provided', async () => {
jest.spyOn(githubUtils, 'getGitHubCustomDomain').mockResolvedValue('git.sujai.com');
const url = await githubUtils.getGitHubRestApiUrl('repos/owner/repo');
expect(url).toBe('https://git.sujai.com/api/v3/repos/owner/repo');
});

it('normalizes multiple slashes in URL', async () => {
jest.spyOn(githubUtils, 'getGitHubCustomDomain').mockResolvedValue('git.sujai.com/');
const url = await githubUtils.getGitHubRestApiUrl('/repos//owner//repo');
expect(url).toBe('https://git.sujai.com/api/v3/repos/owner/repo');
});
});

describe('getGitHubGraphQLUrl', () => {
it('uses default GraphQL endpoint when no custom domain', async () => {
jest.spyOn(githubUtils, 'getGitHubCustomDomain').mockResolvedValue(null);
const url = await githubUtils.getGitHubGraphQLUrl();
expect(url).toBe(`${DEFAULT_GH_URL}/graphql`);
});

it('uses custom domain for GraphQL endpoint', async () => {
jest.spyOn(githubUtils, 'getGitHubCustomDomain').mockResolvedValue('api.github.local');
const url = await githubUtils.getGitHubGraphQLUrl();
expect(url).toBe('https://api.github.local/api/graphql');
});
});
});
39 changes: 35 additions & 4 deletions web-server/pages/api/internal/[org_id]/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { Row } from '@/constants/db';
import { Integration } from '@/constants/integrations';
import { BaseRepo } from '@/types/resources';
import { db } from '@/utils/db';

const GITHUB_API_URL = 'https://api.github.com/graphql';
import { DEFAULT_GH_URL } from '@/constants/urls';

type GithubRepo = {
name: string;
Expand Down Expand Up @@ -53,7 +52,7 @@ export const searchGithubRepos = async (
};

const searchRepoWithURL = async (searchString: string) => {
const apiUrl = `https://api.github.com/repos/${searchString}`;
const apiUrl = await getGitHubRestApiUrl(`repos/${searchString}`);
const response = await axios.get<GithubRepo>(apiUrl);
const repo = response.data;
return [
Expand Down Expand Up @@ -104,7 +103,9 @@ export const searchGithubReposWithNames = async (

const queryString = `${searchString} in:name fork:true`;

const response = await fetch(GITHUB_API_URL, {
const githubApiUrl = await getGitHubGraphQLUrl();

const response = await fetch(githubApiUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Expand Down Expand Up @@ -304,3 +305,33 @@ const replaceURL = async (url: string): Promise<string> => {

return url;
};

export const getGitHubCustomDomain = async (): Promise<string | null> => {
try {
const provider_meta = await db('Integration')
.where('name', Integration.GITHUB)
.then((r: Row<'Integration'>[]) => r.map((item) => item.provider_meta));

return head(provider_meta || [])?.custom_domain || null;
} catch (error) {
console.error('Error occured while getting custom domain from database:', error);
return null;
}
};

const normalizeSlashes = (url: string) =>
url.replace(/(?<!:)\/{2,}/g, '/');

export const getGitHubRestApiUrl = async (path: string) => {
const customDomain = await getGitHubCustomDomain();
const base = customDomain
? `${customDomain}/api/v3`
: DEFAULT_GH_URL;
return normalizeSlashes(`${base}/${path}`);
};


export const getGitHubGraphQLUrl = async (): Promise<string> => {
const customDomain = await getGitHubCustomDomain();
return customDomain ? `${customDomain}/api/graphql` : `${DEFAULT_GH_URL}/graphql`;
};
1 change: 1 addition & 0 deletions web-server/src/constants/urls.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_GH_URL = 'https://api.github.com';
Loading