Skip to content

Commit

Permalink
Throw http exception for request errors in cla sign (#141)
Browse files Browse the repository at this point in the history
Co-authored-by: Paulus Schoutsen <paulus@home-assistant.io>
  • Loading branch information
ludeeus and balloob authored Jan 3, 2023
1 parent 08140f8 commit 700ff37
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
13 changes: 10 additions & 3 deletions services/bots/src/cla-sign/cla-sign.controller.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Body, Controller, Post, Headers } from '@nestjs/common';
import { ClaSignService } from './cla-sign.service';
import { Body, Controller, Post, Headers, HttpException } from '@nestjs/common';
import { ClaSignService, ServiceRequestError } from './cla-sign.service';

@Controller('/cla-sign')
export class ClaSignController {
Expand All @@ -10,6 +10,13 @@ export class ClaSignController {
@Headers() headers: Record<string, any>,
@Body() payload: Record<string, any>,
): Promise<void> {
await this.claSignService.handleClaSignature(headers, payload);
try {
await this.claSignService.handleClaSignature(headers, payload);
} catch (e) {
if (e instanceof ServiceRequestError) {
throw new HttpException(e.message, 400);
}
throw e;
}
}
}
12 changes: 10 additions & 2 deletions services/bots/src/cla-sign/cla-sign.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { createAppAuth } from '@octokit/auth-app';
import { DynamoDB } from 'aws-sdk';
import { GithubClient } from '../github-webhook/github-webhook.model';

export class ServiceRequestError extends ServiceError {}

@Injectable()
export class ClaSignService {
private githubApiClient: GithubClient;
Expand Down Expand Up @@ -42,7 +44,7 @@ export class ClaSignService {

// Check signData
if (!signData.github_username) {
throw new ServiceError('Missing required data in payload', {
throw new ServiceRequestError('Missing required data in payload', {
data: { signData },
service: 'cla-sign',
});
Expand All @@ -58,7 +60,13 @@ export class ClaSignService {
).Item;

if (!pendingRequest) {
throw new ServiceError('No pending request', { data: { signData }, service: 'cla-sign' });
throw new ServiceRequestError(
`No pending request found for ${signData.github_username}. Are you signing the CLA with the same GitHub user that created the commits in the Pull Request?`,
{
data: { signData },
service: 'cla-sign',
},
);
}

// Store signData
Expand Down

0 comments on commit 700ff37

Please sign in to comment.