Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add authorize handler to CLA bot #206

Merged
merged 1 commit into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions services/bots/src/cla-sign/cla-sign.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ import { ClaSignService, ServiceRequestError } from './cla-sign.service';
export class ClaSignController {
constructor(private readonly claSignService: ClaSignService) {}

@Post('/authorize')
async authorize(@Body() payload: { code: string }): Promise<void> {
if (!payload.code) {
throw new HttpException('Missing required data in payload', 400);
}
try {
return await this.claSignService.handleClaAuthorize(payload.code);
} catch (e) {
console.error(e);
if (e instanceof ServiceRequestError) {
throw new HttpException(e.message, 400);
}
throw e;
}
}

@Post()
async webhook(
@Headers() headers: Record<string, any>,
Expand Down
28 changes: 28 additions & 0 deletions services/bots/src/cla-sign/cla-sign.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { createAppAuth } from '@octokit/auth-app';
import { DynamoDB } from 'aws-sdk';
import fetch from 'node-fetch';
import { GithubClient } from '../github-webhook/github-webhook.model';

export class ServiceRequestError extends ServiceError {}
Expand All @@ -14,6 +15,8 @@ export class ClaSignService {
private ddbClient: DynamoDB;
private signersTableName: string;
private pendingSignersTableName: string;
private claSignId: string;
private claSignSecret: string;

constructor(configService: ConfigService) {
this.githubApiClient = new GithubClient({
Expand All @@ -29,6 +32,8 @@ export class ClaSignService {

this.signersTableName = configService.get('dynamodb.cla.signersTable');
this.pendingSignersTableName = configService.get('dynamodb.cla.pendingSignersTable');
this.claSignId = configService.get('github.claSignId');
this.claSignSecret = configService.get('github.claSignSecret');
}

async handleClaSignature(
Expand Down Expand Up @@ -114,4 +119,27 @@ export class ClaSignService {
labels: [ClaIssueLabel.CLA_RECHECK],
});
}

async handleClaAuthorize(code: string): Promise<void> {
const params = new URLSearchParams({
code,
client_id: this.claSignId,
client_secret: this.claSignSecret,
});

const resp = await fetch(`https://github.com/login/oauth/access_token?${params.toString()}`, {
method: 'POST',
headers: {
Accept: 'application/json',
},
});
const data = await resp.json();
if (!resp.ok || 'error' in data) {
throw new ServiceError(data?.error_description || 'Could not authorize', {
data: { data },
service: 'cla-sign-authorize',
});
}
return data;
}
}
10 changes: 10 additions & 0 deletions services/bots/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,16 @@ const conf = convict({
env: 'GITHUB_WEBHOOK_SECRET',
format: String,
},
claSignId: {
default: '',
env: 'GITHUB_CLA_SIGN_APP_ID',
format: String,
},
claSignSecret: {
default: '',
env: 'GITHUB_CLA_SIGN_APP_SECRET',
format: String,
},
},
discord: {
token: {
Expand Down
Loading