ToolJet/marketplace/plugins/github/lib/index.ts
Arpit 636c4a62c5
[chore] Eslint fixes (#5988)
* lint fixes: frontend

* lint fixes: marketplace

* lint fixes: plugins-client js files

* typo fix

* eslint updates/fixes for marketplace

* eslint updates/fixes for plugins

* removing "@typescript-eslint/no-floating-promises for plugins"

* ignore client.js and server.js

* ignore client.js and server.js

* ignore client.js and server.tss

* Delete client.js

* ignore cypress-test/**
2023-04-11 15:34:58 +05:30

68 lines
2 KiB
TypeScript

import { QueryError, QueryResult, QueryService, ConnectionTestResult } from '@tooljet-marketplace/common';
import { SourceOptions, QueryOptions, Operation } from './types';
import { Octokit } from 'octokit';
import { getUserInfo, getRepo, getRepoIssues, getRepoPullRequests } from './query_operations';
export default class Github implements QueryService {
async run(sourceOptions: SourceOptions, queryOptions: QueryOptions, dataSourceId: string): Promise<QueryResult> {
const operation: Operation = queryOptions.operation;
const octokit: Octokit = await this.getConnection(sourceOptions);
let result = {};
try {
switch (operation) {
case Operation.GetUserInfo:
result = await getUserInfo(octokit, queryOptions);
break;
case Operation.GetRepo:
result = await getRepo(octokit, queryOptions);
break;
case Operation.GetRepoIssues:
result = await getRepoIssues(octokit, queryOptions);
break;
case Operation.GetRepoPullRequests:
result = await getRepoPullRequests(octokit, queryOptions);
break;
default:
throw new QueryError('Query could not be completed', 'Invalid operation', {});
}
} catch (error) {
throw new QueryError('Query could not be completed', error.message, {});
}
return {
status: 'ok',
data: result,
};
}
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
const octokit = await this.getConnection(sourceOptions);
try {
const { status } = await octokit.rest.users.getAuthenticated();
if (status) {
return {
status: 'ok',
};
}
} catch (error) {
return {
status: 'failed',
message: 'Invalid credentials',
};
}
}
async getConnection(sourceOptions: SourceOptions): Promise<any> {
const octokitClient = new Octokit({
auth: sourceOptions.personal_token,
});
return octokitClient;
}
}