diff --git a/src/app/models/enums.ts b/src/app/models/enums.ts index fe84d50..2c15ca5 100644 --- a/src/app/models/enums.ts +++ b/src/app/models/enums.ts @@ -27,7 +27,10 @@ export enum PullRequestActivityAction { Rescoped = 'RESCOPED', Commented = 'COMMENTED', Merged = 'MERGED', - Declined = 'DECLINED' + Declined = 'DECLINED', + + // virtual activity + Removed = 'REMOVED' } diff --git a/src/app/services/background.service.ts b/src/app/services/background.service.ts index 15d2e47..e32dc26 100644 --- a/src/app/services/background.service.ts +++ b/src/app/services/background.service.ts @@ -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; private settings!: ExtensionSettings; @@ -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 + }); + } + }); }); } diff --git a/src/app/services/notification.service.ts b/src/app/services/notification.service.ts index 26c685c..40b7e3d 100644 --- a/src/app/services/notification.service.ts +++ b/src/app/services/notification.service.ts @@ -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, { @@ -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; }