Skip to content
This repository has been archived by the owner on Oct 27, 2022. It is now read-only.

feat: requestedChanges & isApproved #21

Merged
merged 7 commits into from
Nov 12, 2020
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
26 changes: 25 additions & 1 deletion .github/labels.json
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,11 @@
"color": "172875",
"description": "DevOPS awaiting review"
},
"devOpsApproved": {
"name": "DevOps - Approved",
"color": "172875",
"description": "DevOPS Approved - awaiting staging"
},
"devOpsStaging": {
"name": "DevOps - Staging",
"color": "172875",
Expand All @@ -220,7 +225,7 @@
"color": "bfdadc",
"description": "The contributor is developing this issue"
},
"comOpsAwaiting Review": {
"comOpsAwaitingReview": {
"name": "ComOps - Awaiting Review",
"color": "bfdadc",
"description": "The contributor is awaiting review"
Expand Down Expand Up @@ -1225,6 +1230,25 @@
"value": true
}
]
},
"devOpsRejected": {
"requires": 1,
"conditions": [
{
"type": "requestedChanges",
"value": true
}
]
},
"devOpsApproved": {
"requires": 1,
"conditions": [
{
"type": "isApproved",
"value": true,
"required": 1
}
]
}
}
}
1 change: 0 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ on:
issues:
types: [opened, edited, closed, reopened]
pull_request:
types: [opened, edited, closed, reopened, ready_for_review, synchronize]

jobs:
# check_for_tech_debt:
Expand Down
105 changes: 97 additions & 8 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -223013,6 +223013,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
Object.defineProperty(exports, "__esModule", ({ value: true }));
exports.labelAPI = void 0;
const utils_1 = __webpack_require__(71314);
var __1 = __webpack_require__(6144);
Object.defineProperty(exports, "log", ({ enumerable: true, get: function () { return __1.log; } }));
class labels {
add({ client, repo, IDNumber, label, dryRun }) {
return __awaiter(this, void 0, void 0, function* () {
Expand Down Expand Up @@ -223091,10 +223093,35 @@ class PullRequests {
return Additions + deletions;
});
}
pendingReview({ client, IDNumber, repo }, requested_reviews) {
listReviews({ client, IDNumber, repo }) {
return __awaiter(this, void 0, void 0, function* () {
const reviews = yield client.pulls.listReviews(Object.assign(Object.assign({}, repo), { pull_number: IDNumber, per_page: 100 }));
return reviews.data.length <= requested_reviews;
return reviews.data;
});
}
pendingReview(reviews, requested_reviews) {
return __awaiter(this, void 0, void 0, function* () {
return reviews <= requested_reviews;
});
}
requestedChanges(reviews) {
return __awaiter(this, void 0, void 0, function* () {
let changes = 0;
reviews.forEach(review => {
if (review.state == 'CHANGES_REQUESTED')
changes++;
});
return changes;
});
}
isApproved(reviews) {
return __awaiter(this, void 0, void 0, function* () {
let approved = 0;
reviews.forEach(review => {
if (review.state == 'APPROVED')
approved++;
});
return approved;
});
}
}
Expand Down Expand Up @@ -223252,21 +223279,47 @@ const filesMatch_1 = __importDefault(__webpack_require__(34667));
const isDraft_1 = __importDefault(__webpack_require__(9559));
const changesSize_1 = __importDefault(__webpack_require__(19216));
const pendingReview_1 = __importDefault(__webpack_require__(13437));
const requestedChanges_1 = __importDefault(__webpack_require__(97699));
const isApproved_1 = __importDefault(__webpack_require__(73953));
const __1 = __webpack_require__(26280);
const handlers = [
...__1.handlers,
branchMatches_1.default,
filesMatch_1.default,
isDraft_1.default,
changesSize_1.default,
pendingReview_1.default
pendingReview_1.default,
requestedChanges_1.default,
isApproved_1.default
];
exports.getPRConditionHandler = (condition) => {
const handler = handlers.find(handler => handler[0] === condition.type);
return handler === null || handler === void 0 ? void 0 : handler[1];
};


/***/ }),

/***/ 73953:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
const TYPE = 'isApproved';
const isApproved = (condition, pr) => {
let reviewers = [];
pr.reviews.forEach(review => {
if (reviewers.indexOf(review.user.login) == -1)
reviewers.push(review.user.login);
});
return (!pr.pendingReview &&
pr.approved >= reviewers.length &&
(condition.required ? pr.approved >= condition.required : true));
};
exports.default = [TYPE, isApproved];


/***/ }),

/***/ 9559:
Expand Down Expand Up @@ -223297,6 +223350,21 @@ const pendingReview = (condition, pr) => {
exports.default = [TYPE, pendingReview];


/***/ }),

/***/ 97699:
/***/ ((__unused_webpack_module, exports) => {

"use strict";

Object.defineProperty(exports, "__esModule", ({ value: true }));
const TYPE = 'requestedChanges';
const requestedChanges = (condition, pr) => {
return pr.requestedChanges > pr.approved;
};
exports.default = [TYPE, requestedChanges];


/***/ }),

/***/ 2765:
Expand Down Expand Up @@ -223750,8 +223818,26 @@ class ContextHandler {
_1.log(`Error thrown while handling changes: ` + err, 5);
throw err;
});
const reviews = yield api_1.pullRequests
.listReviews({ client, repo, IDNumber })
.catch(err => {
_1.log(`Error thrown while handling reviews: ` + err, 5);
throw err;
});
const pendingReview = yield api_1.pullRequests
.pendingReview({ client, repo, IDNumber }, pr.requested_reviewers.length)
.pendingReview(reviews.length, pr.requested_reviewers.length)
.catch(err => {
_1.log(`Error thrown while handling reviews: ` + err, 5);
throw err;
});
const requestedChanges = yield api_1.pullRequests
.requestedChanges(reviews)
.catch(err => {
_1.log(`Error thrown while handling reviews: ` + err, 5);
throw err;
});
const approved = yield api_1.pullRequests
.isApproved(reviews)
.catch(err => {
_1.log(`Error thrown while handling reviews: ` + err, 5);
throw err;
Expand All @@ -223763,13 +223849,16 @@ class ContextHandler {
branch: pr.head.ref,
creator: pr.user.login,
description: pr.body || '',
files,
changes,
pendingReview,
isDraft: pr.draft,
locked: pr.locked,
state: pr.state,
title: pr.title
title: pr.title,
files,
changes,
reviews,
pendingReview,
requestedChanges,
approved
}
};
});
Expand Down
2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/api/labels.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IssueApiProps, ApiProps } from '.'
import { Label, Labels } from '../types'
import { utils } from '../utils'

export { log } from '..'
class labels {
async add({
client,
Expand Down
28 changes: 23 additions & 5 deletions src/api/pullRequests.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IssueApiProps } from '.'
import { Reviews } from '../types'

class PullRequests {
async list({ client, IDNumber, repo }: IssueApiProps) {
Expand All @@ -14,16 +15,33 @@ class PullRequests {
return Additions + deletions
}

async pendingReview(
{ client, IDNumber, repo }: IssueApiProps,
requested_reviews: number
): Promise<boolean> {
async listReviews({ client, IDNumber, repo }: IssueApiProps) {
const reviews = await client.pulls.listReviews({
...repo,
pull_number: IDNumber,
per_page: 100
})
return reviews.data.length <= requested_reviews
return reviews.data
}

async pendingReview(reviews: number, requested_reviews: number) {
return reviews <= requested_reviews
}

async requestedChanges(reviews: Reviews) {
let changes: number = 0
reviews.forEach(review => {
if (review.state == 'CHANGES_REQUESTED') changes++
})
return changes
}

async isApproved(reviews: Reviews) {
let approved: number = 0
reviews.forEach(review => {
if (review.state == 'APPROVED') approved++
})
return approved
}
}

Expand Down
10 changes: 8 additions & 2 deletions src/conditions/pr/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,29 @@ import filesMatch, { ConditionFilesMatch } from './filesMatch'
import isDraft, { ConditionIsDraft } from './isDraft'
import changesSize, { ConditionChangesSize } from './changesSize'
import pendingReview, { ConditionPendingReview } from './pendingReview'
import requestedChanges, { ConditionRequestedChanges } from './requestedChanges'
import isApproved, { ConditionisApproved } from './isApproved'
import { Condition, handlers as sharedHandlers } from '../'

export type PRCondition =
| Condition
| ConditionBranchMatches
| ConditionFilesMatch
| ConditionIsDraft
| ConditionChangesSize
| ConditionPendingReview
| Condition
| ConditionRequestedChanges
| ConditionisApproved

const handlers = [
...sharedHandlers,
branchMatches,
filesMatch,
isDraft,
changesSize,
pendingReview
pendingReview,
requestedChanges,
isApproved
]

export const getPRConditionHandler = (condition: PRCondition) => {
Expand Down
24 changes: 24 additions & 0 deletions src/conditions/pr/isApproved.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { PRProps } from '.'

const TYPE = 'isApproved'

export interface ConditionisApproved {
type: typeof TYPE
value: boolean
required?: number
}

const isApproved = (condition: ConditionisApproved, pr: PRProps) => {
let reviewers: string[] = []
pr.reviews.forEach(review => {
if (reviewers.indexOf(review.user.login) == -1)
reviewers.push(review.user.login)
})
return (
!pr.pendingReview &&
pr.approved >= reviewers.length &&
(condition.required ? pr.approved >= condition.required : true)
)
}

export default [TYPE, isApproved] as const
17 changes: 17 additions & 0 deletions src/conditions/pr/requestedChanges.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { PRProps } from '.'

const TYPE = 'requestedChanges'

export interface ConditionRequestedChanges {
type: typeof TYPE
value: boolean
}

const requestedChanges = (
condition: ConditionRequestedChanges,
pr: PRProps
) => {
return pr.requestedChanges > pr.approved
}

export default [TYPE, requestedChanges] as const
19 changes: 19 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ export interface PRProps extends Props {
branch: string
isDraft: boolean
files: string[]
reviews: Reviews
pendingReview: boolean
requestedChanges: number
approved: number
changes: number
}

Expand Down Expand Up @@ -73,3 +76,19 @@ export interface Options {
showLogs: boolean
dryRun: boolean
}

export type Reviews = Review[]

export interface Review {
id?: number
node_id?: string
user?: any
body?: string
state?: 'APPROVED' | '' | string
html_url?: string
pull_request_url?: string
author_association?: string
_links?: {}
submitted_at?: string
commit_id?: string
}
Loading