<commit_before>/**
 * Copyright 2019 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const bootstrap = require('./bootstrap');
const InfoServer = require('./info_server');
const path = require('path');
const {GitHub, PullRequest, Team} = require('./src/api/github');
const {OwnersCheck} = require('./src/ownership/owners_check');

const NO_REQUEST_OWNERS_REGEX = /\b(WIP|work in progress|DO NOT (MERGE|SUBMIT|REVIEW))\b/i;

module.exports = app => {
  const {github, ownersBot, initialized} = bootstrap(app.log);

  /**
   * Listen for webhooks and provide handlers with a GitHub interface and the
   * event payload.
   *
   * @param {string|Array<string>} events event or list of events to listen to.
   * @param {!function} cb callback function.
   */
  function listen(events, cb) {
    app.on(events, async context => {
      await initialized;

      let handlerGithub;
      try {
        handlerGithub = GitHub.fromContext(context);
      } catch (e) {
        // Some webhooks do not provide a GitHub instance in their context.
        handlerGithub = github;
      }

      await cb(handlerGithub, context.payload);
    });
  }

  /**
   * Identify if a PR is in a state that reviewers should be assigned.
   *
   * @param {!object} pr GitHub PR payload.
   * @return {boolean} false if the PR is a draft or the title contains WIP/DNS.
   */
  function shouldAssignReviewers(pr) {
    return !(pr.draft || NO_REQUEST_OWNERS_REGEX.test(pr.title));
  }

  /** Probot request handlers **/
  listen(
    ['pull_request.opened', 'pull_request.ready_for_review'],
    async (github, payload) => {
      const pr = PullRequest.fromGitHubResponse(payload.pull_request);
      if (!payload.pull_request.draft) {
        await ownersBot.runOwnersCheck(
          github,
          pr,
          shouldAssignReviewers(payload.pull_request)
        );
      }
    }
  );

  listen('pull_request.synchronize', async (github, payload) => {
    const pr = PullRequest.fromGitHubResponse(payload.pull_request);
    await ownersBot.runOwnersCheck(github, pr);
  });

  listen('check_run.rerequested', async (github, payload) => {
    const prNumber = payload.check_run.check_suite.pull_requests[0].number;
    await ownersBot.runOwnersCheckOnPrNumber(github, prNumber);
  });

  listen('pull_request_review.submitted', async (github, payload) => {
    const prNumber = payload.pull_request.number;
    await ownersBot.runOwnersCheckOnPrNumber(github, prNumber);
  });

  listen(
    [
      'team.created',
      'team.deleted',
      'team.edited',
      'membership.added',
      'membership.removed',
    ],
    async (github, payload) => {
      const {id, slug} = payload.team;
      await ownersBot.syncTeam(new Team(id, github.owner, slug), github);
    }<commit_msg>Abort owners checks on all draft PRs (#715)<commit_after>/**
 * Copyright 2019 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS-IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

const bootstrap = require('./bootstrap');
const InfoServer = require('./info_server');
const path = require('path');
const {GitHub, PullRequest, Team} = require('./src/api/github');
const {OwnersCheck} = require('./src/ownership/owners_check');

const NO_REQUEST_OWNERS_REGEX = /\b(WIP|work in progress|DO NOT (MERGE|SUBMIT|REVIEW))\b/i;

module.exports = app => {
  const {github, ownersBot, initialized} = bootstrap(app.log);

  /**
   * Listen for webhooks and provide handlers with a GitHub interface and the
   * event payload.
   *
   * @param {string|Array<string>} events event or list of events to listen to.
   * @param {!function} cb callback function.
   */
  function listen(events, cb) {
    app.on(events, async context => {
      await initialized;

      let handlerGithub;
      try {
        handlerGithub = GitHub.fromContext(context);
      } catch (e) {
        // Some webhooks do not provide a GitHub instance in their context.
        handlerGithub = github;
      }

      await cb(handlerGithub, context.payload);
    });
  }

  /**
   * Identify if a PR is in a state that reviewers should be assigned.
   *
   * @param {!object} pr GitHub PR payload.
   * @return {boolean} false if the PR is a draft or the title contains WIP/DNS.
   */
  function shouldAssignReviewers(pr) {
    return !(pr.draft || NO_REQUEST_OWNERS_REGEX.test(pr.title));
  }

  /** Probot request handlers **/
  listen(
    ['pull_request.opened', 'pull_request.ready_for_review'],
    async (github, payload) => {
      const prPayload = payload.pull_request;
      if (prPayload.draft) return;

      await ownersBot.runOwnersCheck(
        github,
        PullRequest.fromGitHubResponse(prPayload),
        shouldAssignReviewers(prPayload)
      );
    }
  );

  listen('pull_request.synchronize', async (github, payload) => {
    const prPayload = payload.pull_request;
    if (prPayload.draft) return;

    const pr = PullRequest.fromGitHubResponse(prPayload);
    await ownersBot.runOwnersCheck(github, pr);
  });

  listen('check_run.rerequested', async (github, payload) => {
    const prPayload = payload.check_run.check_suite.pull_requests[0];
    if (prPayload.draft) return;

    await ownersBot.runOwnersCheckOnPrNumber(github, prPayload.number);
  });

  listen('pull_request_review.submitted', async (github, payload) => {
    const prPayload = payload.pull_request;
    if (prPayload.draft) return;

    await ownersBot.runOwnersCheckOnPrNumber(github, prPayload.number);
  });

  listen(
    [
      'team.created',
      'team.deleted',
      'team.edited',
      'membership.added',
      'membership.removed',
    ],
    async (github, payload) => {
      const {id, slug} = payload.team;
      await ownersBot.syncTeam(new Team(id, github.owner, slug), github);
    }
{'fraction_non_alphanumeric': 0.11707968541326606, 'fraction_numerical': 0.0031161893456002373, 'mean_word_length': 3.2902609802673455, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '865291', 'n_tokens_mistral': 2043, 'n_tokens_neox': 1987, 'n_words': 580}