Skip to content

Commit

Permalink
feat: option requiresReviewRequest
Browse files Browse the repository at this point in the history
  • Loading branch information
christophehurpeau committed Mar 8, 2019
1 parent fc3e05c commit 6149bf3
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .env.example
Expand Up @@ -7,5 +7,7 @@ PRIVATE_KEY_PATH="private_key/private-key.pem"
# Use `trace` to get verbose logging or `info` to show less
LOG_LEVEL=debug

NAME=reviewflow

# Go to https://smee.io/new set this to the URL that you are redirected to.
WEBHOOK_PROXY_URL=
10 changes: 6 additions & 4 deletions lib/actions/lintPR.js
Expand Up @@ -41,14 +41,16 @@ exports.lintPR = async (repoContext, context) => {
context.repo({
ref: pr.head.sha,
})
)).data.check_runs.find((check) => check.name === 'reviewflow/lint-pr');
)).data.check_runs.find(
(check) => check.name === `${process.env.NAME}/lint-pr`
);

return Promise.all(
[
...statuses.map(({ name, error, info }) =>
context.github.repos.createStatus(
context.repo({
context: `reviewflow/${name}`,
context: `${process.env.NAME}/${name}`,
sha: pr.head.sha,
state: error ? 'failure' : 'success',
target_url: error ? undefined : info.url,
Expand All @@ -59,7 +61,7 @@ exports.lintPR = async (repoContext, context) => {
hasLintPrCheck &&
context.github.checks.create(
context.repo({
name: 'reviewflow/lint-pr',
name: `${process.env.NAME}/lint-pr`,
head_sha: pr.head.sha,
status: 'completed',
conclusion: errorRule ? 'failure' : 'success',
Expand All @@ -76,7 +78,7 @@ exports.lintPR = async (repoContext, context) => {
!hasLintPrCheck &&
context.github.repos.createStatus(
context.repo({
context: 'reviewflow/lint-pr',
context: `${process.env.NAME}/lint-pr`,
sha: pr.head.sha,
state: errorRule ? 'failure' : 'success',
target_url: undefined,
Expand Down
36 changes: 33 additions & 3 deletions lib/context/repoContext.js
@@ -1,3 +1,5 @@
/* eslint-disable max-lines */

'use strict';

const teamConfigs = require('../teamconfigs');
Expand All @@ -11,12 +13,19 @@ const initRepoContext = async (context, config) => {

const labels = await initRepoLabels(context, config);
const labelsValues = Object.values(labels);
const needsReviewLabelIds = Object.keys(config.labels.review)
const reviewKeys = Object.keys(config.labels.review);

const needsReviewLabelIds = reviewKeys
.map((key) => config.labels.review[key].needsReview)
.filter(Boolean)
.map((name) => labels[name].id);

const approvedReviewLabelIds = Object.keys(config.labels.review)
const requestedReviewLabelIds = reviewKeys
.map((key) => config.labels.review[key].requested)
.filter(Boolean)
.map((name) => labels[name].id);

const approvedReviewLabelIds = reviewKeys
.map((key) => config.labels.review[key].approved)
.filter(Boolean)
.map((name) => labels[name].id);
Expand All @@ -28,7 +37,7 @@ const initRepoContext = async (context, config) => {

return context.github.checks.create(
context.repo({
name: 'reviewflow',
name: process.env.NAME,
head_sha: pr.head.sha,
...statusInfo,
})
Expand All @@ -40,6 +49,18 @@ const initRepoContext = async (context, config) => {
status: 'in_progress',
});

const createFailedStatusCheck = (context, message) =>
addStatusCheck(context, {
status: 'completed',
conclusion: 'failure',
started_at: context.payload.pull_request.created_at,
completed_at: new Date(),
output: {
title: message,
summary: '',
},
});

const createDoneStatusCheck = (context) =>
addStatusCheck(context, {
status: 'completed',
Expand All @@ -56,6 +77,8 @@ const initRepoContext = async (context, config) => {

const hasNeedsReview = (labels) =>
labels.some((label) => needsReviewLabelIds.includes(label.id));
const hasRequestedReview = (labels) =>
labels.some((label) => requestedReviewLabelIds.includes(label.id));
const hasApprovesReview = (labels) =>
labels.some((label) => approvedReviewLabelIds.includes(label.id));

Expand All @@ -70,6 +93,13 @@ const initRepoContext = async (context, config) => {
});

if (hasNeedsReview(labels)) {
console.log(config.requiresReviewRequest, !hasRequestedReview(labels));
if (config.requiresReviewRequest && !hasRequestedReview(labels)) {
return createFailedStatusCheck(
context,
'You need to request someone to review the PR'
);
}
return createInProgressStatusCheck(context);
} else if (hasApprovesReview(labels)) {
return createDoneStatusCheck(context);
Expand Down
2 changes: 2 additions & 0 deletions lib/index.js
Expand Up @@ -7,6 +7,8 @@ const { autoAssignPRToCreator } = require('./actions/autoAssignPRToCreator');
const { editOpenedPR } = require('./actions/editOpenedPR');
const { lintPR } = require('./actions/lintPR');

if (!process.env.NAME) process.env.NAME = 'reviewflow';

// const getConfig = require('probot-config')
// const { MongoClient } = require('mongodb');

Expand Down
1 change: 1 addition & 0 deletions lib/teamconfigs/christophehurpeau.js
Expand Up @@ -3,6 +3,7 @@
module.exports = {
autoAssignToCreator: true,
trimTitle: true,
requiresReviewRequest: false,
prLint: {
title: [
{
Expand Down
1 change: 1 addition & 0 deletions lib/teamconfigs/ornikar.js
Expand Up @@ -4,6 +4,7 @@ module.exports = {
slackToken: process.env.ORNIKAR_SLACK_TOKEN,
autoAssignToCreator: true,
trimTitle: true,
requiresReviewRequest: true,
prLint: {
title: [
{
Expand Down

0 comments on commit 6149bf3

Please sign in to comment.