From a0eff5a837eb0e4bdf8b51e211af425156a20390 Mon Sep 17 00:00:00 2001 From: Anand Chowdhary Date: Fri, 13 Nov 2020 18:36:06 +0530 Subject: [PATCH] :sparkles: Add GitHub module --- src/app.module.ts | 2 ++ src/config/configuration.interface.ts | 7 ++++++- src/config/configuration.ts | 4 ++++ src/providers/github/github.module.ts | 10 ++++++++++ src/providers/github/github.service.ts | 20 ++++++++++++++++++++ 5 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 src/providers/github/github.module.ts create mode 100644 src/providers/github/github.service.ts diff --git a/src/app.module.ts b/src/app.module.ts index 6392b605a..966ec1834 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -34,6 +34,7 @@ import { DnsModule } from './providers/dns/dns.module'; import { ElasticSearchModule } from './providers/elasticsearch/elasticsearch.module'; import { FirebaseModule } from './providers/firebase/firebase.module'; import { GeolocationModule } from './providers/geolocation/geolocation.module'; +import { GitHubModule } from './providers/github/github.module'; import { MailModule } from './providers/mail/mail.module'; import { PrismaModule } from './providers/prisma/prisma.module'; import { S3Module } from './providers/s3/s3.module'; @@ -74,6 +75,7 @@ import { TasksModule } from './providers/tasks/tasks.module'; S3Module, CloudinaryModule, FirebaseModule, + GitHubModule, ], providers: [ { diff --git a/src/config/configuration.interface.ts b/src/config/configuration.interface.ts index e332dcc3b..508a525aa 100644 --- a/src/config/configuration.interface.ts +++ b/src/config/configuration.interface.ts @@ -113,7 +113,12 @@ export interface Configuration { firebase: { serviceAccountKey: | string - | { projectId?: string; clientEmail?: string; privateKey?: string }; + | { projectId?: string; clientEmail?: string; privateKey?: string }; databaseUrl: string; }; + + github: { + auth: string; + userAgent?: string; + }; } diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 7eb5d6d14..d5435f5fe 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -130,6 +130,10 @@ const configuration: Configuration = { : process.env.FIREBASE_SERVICE_ACCOUNT_KEY, databaseUrl: process.env.FIREBASE_DATABASE_URL, }, + github: { + auth: process.env.GITHUB_AUTH, + userAgent: process.env.GITHUB_USER_AGENT, + }, }; const configFunction: ConfigFactory = () => configuration; diff --git a/src/providers/github/github.module.ts b/src/providers/github/github.module.ts new file mode 100644 index 000000000..12e15308d --- /dev/null +++ b/src/providers/github/github.module.ts @@ -0,0 +1,10 @@ +import { Module } from '@nestjs/common'; +import { ConfigModule } from '@nestjs/config'; +import { GitHubService } from './github.service'; + +@Module({ + imports: [ConfigModule], + providers: [GitHubService], + exports: [GitHubService], +}) +export class GitHubModule {} diff --git a/src/providers/github/github.service.ts b/src/providers/github/github.service.ts new file mode 100644 index 000000000..1bebe1960 --- /dev/null +++ b/src/providers/github/github.service.ts @@ -0,0 +1,20 @@ +import { Injectable, Logger } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { Octokit } from '@octokit/rest'; +import { Configuration } from '../../config/configuration.interface'; + +@Injectable() +export class GitHubService { + private logger = new Logger(GitHubService.name); + octokit: Octokit; + + constructor(private configService: ConfigService) { + const config = this.configService.get('github'); + if (config.auth) + this.octokit = new Octokit({ + auth: config.auth, + userAgent: config.userAgent ?? 'staart', + }); + else this.logger.warn('GitHub API key not found'); + } +}