ToolJet/marketplace/plugins/github/lib/index.ts
Arpit f098f447d1
fixes: integrating plugins from marketplace (#5678)
* fixes: integrating plugins from marketplace to apps

* clean up

* update deps: markeplace to node 18

* adds plivo

* updates plivo icon

* fixes app crash on clicking sidebae datasources popup

* fixes app crash for rendering selected ds form from leftsidebar

* fixes app crash for loading installed plugins

* fixes: updated correct plugin id to testconection

* init

* updates s3 plugin name

* checking if marketplace flag is "true"

* updates github svg

* plugin icon fix

* fixes:components werent rendering if marketplace plugin queries were used

* github plugin: error message for invalid creds

* fixes: typos

* fixes multiple installation of the same plugin
2023-03-16 18:17:25 +05:30

79 lines
2.1 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;
console.log('---octakit operation', {
queryOptions
});
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;
}
}