Skip to content

Commit

Permalink
handle merged/declined/removed pr activities
Browse files Browse the repository at this point in the history
  • Loading branch information
Serghei Paduret committed Jul 8, 2021
1 parent 04e74e8 commit 744b16f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/app/models/enums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,10 @@ export enum PullRequestActivityAction {
Rescoped = 'RESCOPED',
Commented = 'COMMENTED',
Merged = 'MERGED',
Declined = 'DECLINED'
Declined = 'DECLINED',

// virtual activity
Removed = 'REMOVED'
}


Expand Down
39 changes: 37 additions & 2 deletions src/app/services/background.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,16 @@ import {BitbucketCommentAction, PullRequestActivityAction, PullRequestRole, Pull
import {ExtensionSettings, PullRequest} from '../models/models';
import {DataService} from './data.service';
import {NotificationService} from './notification.service';
import {catchError} from 'rxjs/operators';
import {of, throwError} from 'rxjs';
import Alarm = chrome.alarms.Alarm;
import {HttpErrorResponse} from '@angular/common/http';

// @ts-ignore
let chr: any = chrome;

@Injectable()
export class BackgroundService {
timer?: number;
interval: number;
public dataProcessed!: EventEmitter<PullRequestRole>;
private settings!: ExtensionSettings;
Expand Down Expand Up @@ -208,7 +210,40 @@ export class BackgroundService {
before
.filter(b => !now.some(n => n.id === b.id))
.forEach(b => {
// todo: check missing PR, was it merged / declined ?
this.bitbucketService
.getPullRequestActivities(b.fromRef.repository.project.key, b.fromRef.repository.slug, b.id)
.pipe(catchError(error => {
if (error instanceof HttpErrorResponse && error.status === 404) {
this.notificationService.sendNotification(
{
action: PullRequestActivityAction.Removed,
pullRequest: b
});

return of({values: []});
} else {
return throwError(error);
}
}))
.subscribe(data => {
let activities = data.values.filter(a =>
(a.action === PullRequestActivityAction.Merged || a.action === PullRequestActivityAction.Declined)
// make sure activity wasn't generated by myself
&& a.user.id !== this.settings?.bitbucket?.userId
// check if action occurred after last running time
&& a.createdDate >= this.lastRunningTime);

if (activities.length) {
// get recent activity
let act = activities.sort((one, two) => (one.createdDate > two.createdDate ? -1 : 1))[0];
this.notificationService.sendNotification(
{
action: act.action,
pullRequest: b,
activity: act
});
}
});
});
}

Expand Down
16 changes: 14 additions & 2 deletions src/app/services/notification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ export class NotificationService {
case PullRequestActivityAction.Approved:
body = 'pull request approved';
break;
case PullRequestActivityAction.Merged:
body = 'pull request merged';
break;
case PullRequestActivityAction.Declined:
body = 'pull request declined';
break;
case PullRequestActivityAction.Removed:
body = 'pull request removed';
break;
}

new Notification(options.pullRequest.title, {
Expand Down Expand Up @@ -123,10 +132,13 @@ export class NotificationService {
title = `:merged: @${options.activity?.user.name} merged pull request`;
break;
case PullRequestActivityAction.Declined:
title = `:_: @${options.activity?.user.name} declined pull request`;
title = `:heavy_multiplication_x: @${options.activity?.user.name} declined pull request`;
break;
case PullRequestActivityAction.Removed:
title = `:x: pull request was removed`;
break;
default:
title = `something happened: ${options.action}`;
title = `something happened with pull request: ${options.action}`;
break;
}

Expand Down

0 comments on commit 744b16f

Please sign in to comment.