Skip to content

Commit 352711f

Browse files
authored
Merge pull request #3 from DefinitelyTyped/autoApproval
Update prompts and add auto-approval
2 parents 40c6b07 + 48d1802 commit 352711f

File tree

8 files changed

+729
-546
lines changed

8 files changed

+729
-546
lines changed

package-lock.json

Lines changed: 0 additions & 146 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "focus-dt",
3-
"version": "1.0.2",
3+
"version": "1.0.3",
44
"description": "A simple command-line tool for running down PRs on DefinitelyTyped",
55
"bin": {
66
"focus-dt": "bin/focus-dt"
@@ -27,7 +27,6 @@
2727
"typescript": "^3.8.3"
2828
},
2929
"dependencies": {
30-
"@esfx/async-canceltoken": "^1.0.0-pre.13",
3130
"@octokit/rest": "^16.28.1",
3231
"equatable": "^1.2.0",
3332
"iterable-query": "^1.0.0-pre.15",

src/chrome.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { CancelToken } from "@esfx/async-canceltoken";
21
import { regQuery } from "./registry";
32
import { spawn, ChildProcess } from "child_process";
43
import { chromeConnection } from "vscode-chrome-debug-core";

src/github.ts

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export interface Project extends Github.ProjectsListForRepoResponseItem {}
1313
export interface Column extends Github.ProjectsListColumnsResponseItem {}
1414
export interface Card extends Github.ProjectsListCardsResponseItem {}
1515
export interface Pull extends Github.PullsGetResponse {
16-
approved?: boolean;
16+
approvedByMe?: boolean;
17+
approvedByAll?: boolean;
1718
}
1819

1920
export interface GetPullSuccessResult {
@@ -124,9 +125,9 @@ export class ProjectService<K extends string = "Check and Merge" | "Review"> {
124125
...this._ownerAndRepo,
125126
pull_number: pull.number
126127
});
127-
const review = reviews.data?.find(review => review.user.id === id);
128-
const approved = review?.state === "APPROVED";
129-
return { error: false, pull: { ...pull, approved }, labels };
128+
const approvedByAll = (reviews.data?.length > 0 && reviews.data.every(review => review.state === "APPROVED")) ?? false;
129+
const approvedByMe = reviews.data?.some(review => review.user.id === id && review.state === "APPROVED") ?? false;
130+
return { error: false, pull: { ...pull, approvedByMe, approvedByAll }, labels };
130131
}
131132

132133
async whoAmiI(): Promise<number | undefined> {
@@ -140,19 +141,39 @@ export class ProjectService<K extends string = "Check and Merge" | "Review"> {
140141
return this._userId;
141142
}
142143

143-
async approvePull(pull: Pull): Promise<void> {
144-
const auth = await this._github.users.getAuthenticated();
145-
if (!auth.data) {
146-
return;
144+
async isApprovedByAll(pull: Pull): Promise<boolean> {
145+
const reviews = await this._github.pulls.listReviews({ ...this._ownerAndRepo, pull_number: pull.number });
146+
let wasApproved = false;
147+
if (reviews.data) {
148+
for (const review of reviews.data) {
149+
if (review.state === "APPROVED") {
150+
wasApproved = true;
151+
}
152+
else {
153+
return false;
154+
}
155+
}
147156
}
157+
return wasApproved;
158+
}
148159

149-
const id = auth.data.id;
160+
async isApprovedByMe(pull: Pull): Promise<boolean> {
161+
const id = await this.whoAmiI();
150162
const reviews = await this._github.pulls.listReviews({ ...this._ownerAndRepo, pull_number: pull.number });
151163
if (reviews.data) {
152164
for (const review of reviews.data) {
153-
if (review.user.id === id && review.state === "APPROVED") return;
165+
if (review.state === "APPROVED" && review.user.id === id) {
166+
return true;
167+
}
154168
}
155169
}
170+
return false;
171+
}
172+
173+
async approvePull(pull: Pull): Promise<void> {
174+
if (await this.isApprovedByMe(pull)) {
175+
return;
176+
}
156177

157178
const review = await this._github.pulls.createReview({
158179
...this._ownerAndRepo,
@@ -167,7 +188,8 @@ export class ProjectService<K extends string = "Check and Merge" | "Review"> {
167188
review_id: review.data.id
168189
});
169190

170-
pull.approved = true;
191+
pull.approvedByMe = await this.isApprovedByMe(pull);
192+
pull.approvedByAll = await this.isApprovedByAll(pull);
171193
}
172194

173195
async mergePull(pull: Pull, method?: "merge" | "squash" | "rebase"): Promise<void> {

0 commit comments

Comments
 (0)