feat: add support for custom github apiBaseUrl

Signed-off-by: Talita Gregory Nunes Freire <talita.freire@dazn.com>
This commit is contained in:
Talita Gregory Nunes Freire
2022-04-27 11:15:26 +02:00
parent 09286fefa7
commit 4a6ffbf3ad
@@ -14,23 +14,34 @@
* limitations under the License.
*/
import { Octokit } from '@octokit/rest';
import { useApi, githubAuthApiRef } from '@backstage/core-plugin-api';
import {
useApi,
githubAuthApiRef,
configApiRef,
} from '@backstage/core-plugin-api';
import { readGitHubIntegrationConfigs } from '@backstage/integration';
let octokit: any;
export const useOctokitGraphQl = <T>() => {
const auth = useApi(githubAuthApiRef);
const config = useApi(configApiRef);
const baseUrl = readGitHubIntegrationConfigs(
config.getOptionalConfigArray('providers.github') ?? [],
)[0].apiBaseUrl;
return (path: string, options?: any): Promise<T> =>
auth.getAccessToken(['repo'])
auth
.getAccessToken(['repo'])
.then((token: string) => {
if(!octokit) {
octokit = new Octokit({ auth: token })
if (!octokit) {
octokit = new Octokit({ auth: token, ...(baseUrl && { baseUrl }) });
}
return octokit
return octokit;
})
.then(octokitInstance => {
return octokitInstance.graphql(path, options)
return octokitInstance.graphql(path, options);
});
};