Skip to content

Commit

Permalink
feat: support discussions for github server
Browse files Browse the repository at this point in the history
  • Loading branch information
setchy committed May 17, 2024
1 parent e96fca2 commit 113891a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/utils/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@ import type {
import { apiRequestAuth } from './request';

import { print } from 'graphql/language/printer';
import Constants from '../constants';
import { QUERY_SEARCH_DISCUSSIONS } from './graphql/discussions';
import { formatAsGitHubSearchSyntax } from './graphql/utils';
import { getGitHubAPIBaseUrl } from './utils';
import { getGitHubAPIBaseUrl, getGitHubGraphQLUrl } from './utils';

/**
* Get Hypermedia links to resources accessible in GitHub's REST API
Expand Down Expand Up @@ -247,7 +246,8 @@ export async function searchDiscussions(
notification: Notification,
token: string,
): AxiosPromise<GraphQLSearch<Discussion>> {
return apiRequestAuth(Constants.GITHUB_API_GRAPHQL_URL, 'POST', token, {
const url = getGitHubGraphQLUrl(notification.hostname);
return apiRequestAuth(url.toString(), 'POST', token, {
query: print(QUERY_SEARCH_DISCUSSIONS),
variables: {
queryStatement: formatAsGitHubSearchSyntax(
Expand Down
16 changes: 14 additions & 2 deletions src/utils/api/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getGitHubAPIBaseUrl } from './utils';
import { getGitHubAPIBaseUrl, getGitHubGraphQLUrl } from './utils';

describe('utils/api/utils.ts', () => {
describe('generateGitHubAPIUrl', () => {
describe('getGitHubAPIBaseUrl', () => {
it('should generate a GitHub API url - non enterprise', () => {
const result = getGitHubAPIBaseUrl('github.com');
expect(result.toString()).toBe('https://api.github.com/');
Expand All @@ -12,4 +12,16 @@ describe('utils/api/utils.ts', () => {
expect(result.toString()).toBe('https://github.manos.im/api/v3/');
});
});

describe('generateGitHubGraphQLAPIUrl', () => {
it('should generate a GitHub GraphQL url - non enterprise', () => {
const result = getGitHubGraphQLUrl('github.com');
expect(result.toString()).toBe('https://api.github.com/graphql');
});

it('should generate a GitHub GraphQL url - enterprise', () => {
const result = getGitHubGraphQLUrl('github.manos.im');
expect(result.toString()).toBe('https://github.manos.im/api/graphql');
});
});
});
11 changes: 11 additions & 0 deletions src/utils/api/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,14 @@ export function getGitHubAPIBaseUrl(hostname: string): URL {
}
return url;
}

export function getGitHubGraphQLUrl(hostname: string): URL {
const url = new URL(Constants.GITHUB_API_GRAPHQL_URL);

if (isEnterpriseHost(hostname)) {
url.hostname = hostname;
url.pathname = '/api/graphql';
}

return url;
}

0 comments on commit 113891a

Please sign in to comment.