You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by GitBox <gi...@apache.org> on 2022/03/07 21:55:09 UTC

[GitHub] [beam] damccorm opened a new pull request #17034: [BEAM-13925] Add weekly automation to update our reviewer config

damccorm opened a new pull request #17034:
URL: https://github.com/apache/beam/pull/17034


   Right now, we don't have a well defined automated system for staying on top of pull request reviews - we rely on contributors being able to find the correct OWNERS file and committers manually triaging/calling attention to old pull requests. This is the continuation of an effort to improve our automation around this. [Design doc for the full effort here](https://docs.google.com/document/d/1FhRPRD6VXkYlLAPhNfZB7y2Yese2FCWBzjx67d3TjBo/edit#).
   
   This specific change adds automation to automatically find and suggest reviewers for different areas **and** remove inactive reviewers from the configuration
   
   I mostly tested this locally to make sure it came up with the correct configuration on this repo, I also did test that it correctly creates the pr here - https://github.com/damccorm/beam-pr-bot-demo/pull/11
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [ ] [**Choose reviewer(s)**](https://beam.apache.org/contribute/#make-your-change) and mention them in a comment (`R: @username`).
    - [x] Format the pull request title like `[BEAM-XXX] Fixes bug in ApproximateQuantiles`, where you replace `BEAM-XXX` with the appropriate JIRA issue, if applicable. This will automatically link the pull request to the issue.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [x] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/#make-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [beam] damccorm commented on a change in pull request #17034: [BEAM-13925] Add weekly automation to update our reviewer config

Posted by GitBox <gi...@apache.org>.
damccorm commented on a change in pull request #17034:
URL: https://github.com/apache/beam/pull/17034#discussion_r822627130



##########
File path: scripts/ci/pr-bot/shared/reviewerConfig.ts
##########
@@ -22,10 +22,24 @@ import { Label } from "./githubUtils";
 
 export class ReviewerConfig {
   private config: any;
+  private configPath: string;
   constructor(pathToConfigFile) {
     this.config = yaml.load(
       fs.readFileSync(pathToConfigFile, { encoding: "utf-8" })
     );
+    this.configPath = pathToConfigFile;
+  }
+
+  // Returns all possible reviewers for each label configured.
+  getReviewersForAllLabels(): { [key: string]: string[] } {
+    var labelObjects = this.config.labels;

Review comment:
       Yikes - fixed!

##########
File path: .github/REVIEWERS.yml
##########
@@ -13,9 +13,17 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+# If you would like to be excluded from consideration for reviewing a certain label,
+# add yourself to that label's exclusionList
+# FallbackReviewers is for reviewers who can review any area of the code base that might
+# not receive a label. These should generally be more experienced committers.
 labels:
-- name: "Go"
-  reviewers: ["damccorm", "lostluck", "jrmccluskey", "youngoli", "riteshghorse"]
-  exclusionList: [] # These users will never be suggested as reviewers
-# I don't know the other areas well enough to assess who the normal committers/contributors who might want to be reviewers are
-fallbackReviewers: [] # List of committers to use when no label matches
+  - name: Go
+    reviewers:
+      - damccorm
+      - lostluck
+      - jrmccluskey
+      - youngoli
+      - riteshghorse
+    exclusionList: []
+fallbackReviewers: []

Review comment:
       I went ahead and updated to the new format that this file will get saved as to reduce the diff the first time this runs.

##########
File path: scripts/ci/pr-bot/updateReviewers.ts
##########
@@ -0,0 +1,286 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 exec = require("@actions/exec");
+const github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const {
+  REPO_OWNER,
+  REPO,
+  BOT_NAME,
+  PATH_TO_CONFIG_FILE,
+} = require("./shared/constants");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+
+async function getPullsFromLastThreeMonths(): Promise<any[]> {
+  const cutoffDate = new Date();
+  cutoffDate.setMonth(cutoffDate.getMonth() - 3);
+  console.log(`Getting PRs newer than ${cutoffDate}`);
+  const githubClient = github.getGitHubClient();
+  let result = await githubClient.rest.pulls.list({
+    owner: REPO_OWNER,
+    repo: REPO,
+    state: "closed",
+    per_page: 100, // max allowed
+  });
+  let page = 2;
+  let retries = 0;
+  let pulls = result.data;
+  while (
+    result.data.length > 0 &&
+    new Date(result.data[result.data.length - 1].created_at) > cutoffDate
+  ) {
+    if (retries === 0) {
+      console.log(`Getting PRs, page: ${page}`);
+      console.log(
+        `Current oldest PR = ${new Date(
+          result.data[result.data.length - 1].created_at
+        )}`
+      );
+    }
+    try {
+      result = await githubClient.rest.pulls.list({
+        owner: REPO_OWNER,
+        repo: REPO,
+        state: "closed",
+        per_page: 100, // max allowed
+        page: page,
+      });
+      pulls = pulls.concat(result.data);
+      page++;
+      retries = 0;
+    } catch (err) {
+      if (retries >= 3) {
+        throw err;
+      }
+      retries++;
+    }
+  }
+  return pulls;
+}
+
+interface pullActivity {
+  reviews: number;
+  pullsAuthored: number;
+}
+
+interface reviewerActivity {
+  reviewers: { [reviewer: string]: pullActivity };
+}
+
+async function getReviewersForPull(pull: any): Promise<string[]> {
+  let reviewers = new Set<string>();
+  const githubClient = github.getGitHubClient();
+  let comments = (
+    await githubClient.rest.issues.listComments({
+      owner: REPO_OWNER,
+      repo: REPO,
+      issue_number: pull.number,
+    })
+  ).data;
+  const reviewComments = (
+    await githubClient.rest.pulls.listReviewComments({
+      owner: REPO_OWNER,
+      repo: REPO,
+      pull_number: pull.number,
+    })
+  ).data;
+  comments = comments.concat(reviewComments);
+
+  for (const comment of comments) {
+    if (
+      comment.user.login &&
+      comment.user.login !== pull.user.login &&
+      comment.user.login !== BOT_NAME
+    ) {
+      reviewers.add(comment.user.login.toLowerCase());
+    }
+  }
+
+  return [...reviewers];
+}
+
+function addReviewerActivity(
+  reviewerActivity: reviewerActivity,
+  reviewers: string[],
+  author: string
+): reviewerActivity {
+  if (!reviewerActivity) {
+    reviewerActivity = {
+      reviewers: {},
+    };
+  }
+
+  if (author in reviewerActivity.reviewers) {
+    reviewerActivity.reviewers[author].pullsAuthored++;
+  } else {
+    reviewerActivity.reviewers[author] = {
+      reviews: 0,
+      pullsAuthored: 1,
+    };
+  }
+
+  for (const reviewer of reviewers) {
+    if (reviewer !== author) {
+      let curReviewerActivity = {
+        reviews: 1,
+        pullsAuthored: 0,
+      };
+      if (reviewer in reviewerActivity.reviewers) {
+        reviewerActivity.reviewers[reviewer].reviews++;
+      } else {
+        reviewerActivity.reviewers[author] = {
+          reviews: 1,
+          pullsAuthored: 0,
+        };
+      }
+    }
+  }
+
+  return reviewerActivity;
+}
+
+async function getReviewerActivityByLabel(
+  pulls: any[]
+): Promise<{ [label: string]: reviewerActivity }> {
+  let reviewerActivityByLabel: { [label: string]: reviewerActivity } = {};
+  for (const pull of pulls) {
+    console.log(`Processing PR ${pull.number}`);
+    const author = pull.user.login.toLowerCase();
+    if (author !== BOT_NAME) {
+      const reviewers = await getReviewersForPull(pull);
+      const labels = pull.labels;
+      for (const label of labels) {
+        const labelName = label.name.toLowerCase();
+        reviewerActivityByLabel[labelName] = addReviewerActivity(
+          reviewerActivityByLabel[labelName],
+          reviewers,
+          author
+        );
+      }
+    }
+  }
+
+  return reviewerActivityByLabel;
+}
+
+interface configUpdates {
+  reviewersAddedForLabels: { [reviewer: string]: string[] };
+  reviewersRemovedForLabels: { [reviewer: string]: string[] };
+}
+
+function updateReviewerConfig(
+  reviewerActivityByLabel: { [label: string]: reviewerActivity },
+  reviewerConfig: typeof ReviewerConfig
+): configUpdates {
+  let updates: configUpdates = {
+    reviewersAddedForLabels: {},
+    reviewersRemovedForLabels: {},
+  };
+  const currentReviewersForLabels = reviewerConfig.getReviewersForAllLabels();
+  for (const label of Object.keys(currentReviewersForLabels)) {
+    // Remove any reviewers with no reviews or pulls created
+    let reviewers = currentReviewersForLabels[label];
+    const exclusionList = reviewerConfig.getExclusionListForLabel(label);
+    for (let i = 0; i < reviewers.length; i++) {
+      if (!reviewerActivityByLabel[label].reviewers[reviewers[i]]) {
+        if (reviewers[i] in updates.reviewersRemovedForLabels) {
+          updates.reviewersRemovedForLabels[reviewers[i]].push(label);
+        } else {
+          updates.reviewersRemovedForLabels[reviewers[i]] = [label];
+        }
+        reviewers.splice(i, 1);
+        i--;
+      }
+    }

Review comment:
       Good call - updated

##########
File path: scripts/ci/pr-bot/updateReviewers.ts
##########
@@ -0,0 +1,286 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 exec = require("@actions/exec");
+const github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const {
+  REPO_OWNER,
+  REPO,
+  BOT_NAME,
+  PATH_TO_CONFIG_FILE,
+} = require("./shared/constants");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+
+async function getPullsFromLastThreeMonths(): Promise<any[]> {
+  const cutoffDate = new Date();
+  cutoffDate.setMonth(cutoffDate.getMonth() - 3);
+  console.log(`Getting PRs newer than ${cutoffDate}`);
+  const githubClient = github.getGitHubClient();
+  let result = await githubClient.rest.pulls.list({
+    owner: REPO_OWNER,
+    repo: REPO,
+    state: "closed",
+    per_page: 100, // max allowed
+  });
+  let page = 2;
+  let retries = 0;
+  let pulls = result.data;
+  while (
+    result.data.length > 0 &&
+    new Date(result.data[result.data.length - 1].created_at) > cutoffDate
+  ) {
+    if (retries === 0) {
+      console.log(`Getting PRs, page: ${page}`);
+      console.log(
+        `Current oldest PR = ${new Date(
+          result.data[result.data.length - 1].created_at
+        )}`
+      );
+    }
+    try {
+      result = await githubClient.rest.pulls.list({
+        owner: REPO_OWNER,
+        repo: REPO,
+        state: "closed",
+        per_page: 100, // max allowed
+        page: page,
+      });
+      pulls = pulls.concat(result.data);
+      page++;
+      retries = 0;
+    } catch (err) {
+      if (retries >= 3) {
+        throw err;
+      }
+      retries++;
+    }
+  }
+  return pulls;
+}
+
+interface pullActivity {
+  reviews: number;
+  pullsAuthored: number;
+}
+
+interface reviewerActivity {
+  reviewers: { [reviewer: string]: pullActivity };
+}
+
+async function getReviewersForPull(pull: any): Promise<string[]> {
+  let reviewers = new Set<string>();
+  const githubClient = github.getGitHubClient();
+  let comments = (
+    await githubClient.rest.issues.listComments({
+      owner: REPO_OWNER,
+      repo: REPO,
+      issue_number: pull.number,
+    })
+  ).data;
+  const reviewComments = (
+    await githubClient.rest.pulls.listReviewComments({
+      owner: REPO_OWNER,
+      repo: REPO,
+      pull_number: pull.number,
+    })
+  ).data;
+  comments = comments.concat(reviewComments);
+
+  for (const comment of comments) {
+    if (
+      comment.user.login &&
+      comment.user.login !== pull.user.login &&
+      comment.user.login !== BOT_NAME
+    ) {
+      reviewers.add(comment.user.login.toLowerCase());
+    }
+  }
+
+  return [...reviewers];
+}
+
+function addReviewerActivity(
+  reviewerActivity: reviewerActivity,
+  reviewers: string[],
+  author: string
+): reviewerActivity {
+  if (!reviewerActivity) {
+    reviewerActivity = {
+      reviewers: {},
+    };
+  }
+
+  if (author in reviewerActivity.reviewers) {
+    reviewerActivity.reviewers[author].pullsAuthored++;
+  } else {
+    reviewerActivity.reviewers[author] = {
+      reviews: 0,
+      pullsAuthored: 1,
+    };
+  }
+
+  for (const reviewer of reviewers) {
+    if (reviewer !== author) {
+      let curReviewerActivity = {
+        reviews: 1,
+        pullsAuthored: 0,
+      };
+      if (reviewer in reviewerActivity.reviewers) {
+        reviewerActivity.reviewers[reviewer].reviews++;
+      } else {
+        reviewerActivity.reviewers[author] = {
+          reviews: 1,
+          pullsAuthored: 0,
+        };
+      }
+    }
+  }
+
+  return reviewerActivity;
+}
+
+async function getReviewerActivityByLabel(
+  pulls: any[]
+): Promise<{ [label: string]: reviewerActivity }> {
+  let reviewerActivityByLabel: { [label: string]: reviewerActivity } = {};
+  for (const pull of pulls) {
+    console.log(`Processing PR ${pull.number}`);
+    const author = pull.user.login.toLowerCase();
+    if (author !== BOT_NAME) {
+      const reviewers = await getReviewersForPull(pull);
+      const labels = pull.labels;
+      for (const label of labels) {
+        const labelName = label.name.toLowerCase();
+        reviewerActivityByLabel[labelName] = addReviewerActivity(
+          reviewerActivityByLabel[labelName],
+          reviewers,
+          author
+        );
+      }
+    }
+  }
+
+  return reviewerActivityByLabel;
+}
+
+interface configUpdates {
+  reviewersAddedForLabels: { [reviewer: string]: string[] };
+  reviewersRemovedForLabels: { [reviewer: string]: string[] };
+}
+
+function updateReviewerConfig(
+  reviewerActivityByLabel: { [label: string]: reviewerActivity },
+  reviewerConfig: typeof ReviewerConfig
+): configUpdates {
+  let updates: configUpdates = {
+    reviewersAddedForLabels: {},
+    reviewersRemovedForLabels: {},
+  };
+  const currentReviewersForLabels = reviewerConfig.getReviewersForAllLabels();
+  for (const label of Object.keys(currentReviewersForLabels)) {
+    // Remove any reviewers with no reviews or pulls created
+    let reviewers = currentReviewersForLabels[label];
+    const exclusionList = reviewerConfig.getExclusionListForLabel(label);
+    for (let i = 0; i < reviewers.length; i++) {
+      if (!reviewerActivityByLabel[label].reviewers[reviewers[i]]) {
+        if (reviewers[i] in updates.reviewersRemovedForLabels) {
+          updates.reviewersRemovedForLabels[reviewers[i]].push(label);
+        } else {
+          updates.reviewersRemovedForLabels[reviewers[i]] = [label];
+        }
+        reviewers.splice(i, 1);
+        i--;
+      }
+    }
+
+    // Add any reviewers who have at least 5 combined reviews + pulls authored
+    for (const reviewer of Object.keys(
+      reviewerActivityByLabel[label].reviewers
+    )) {
+      const reviewerContributions =
+        reviewerActivityByLabel[label].reviewers[reviewer].reviews +
+        reviewerActivityByLabel[label].reviewers[reviewer].pullsAuthored;
+      if (
+        reviewerContributions >= 5 &&
+        reviewers.indexOf(reviewer) < 0 &&
+        exclusionList.indexOf(reviewer) < 0
+      ) {
+        reviewers.push(reviewer);
+        if (reviewer in updates.reviewersAddedForLabels) {
+          updates.reviewersAddedForLabels[reviewer].push(label);
+        } else {
+          updates.reviewersAddedForLabels[reviewer] = [label];
+        }
+      }
+    }
+
+    reviewerConfig.updateReviewerForLabel(label, reviewers);
+  }
+
+  return updates;
+}
+
+async function openPull(updates: configUpdates) {
+  const curDate = new Date();
+  const branch = `pr-bot-${
+    curDate.getMonth() + 1
+  }-${curDate.getDay()}-${curDate.getFullYear()}-${curDate.getHours()}-${curDate.getMinutes()}-${curDate.getSeconds()}`;

Review comment:
       I don't think there's a need to convert it back and forth - I chose not to go with toIsoString because I didn't want more special characters in the branch name. Representing days/months with a single digit is fine - the goal is just to make this an identifiable branch name (and to avoid conflicts with future branches)




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [beam] lostluck merged pull request #17034: [BEAM-13925] Add weekly automation to update our reviewer config

Posted by GitBox <gi...@apache.org>.
lostluck merged pull request #17034:
URL: https://github.com/apache/beam/pull/17034


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [beam] damccorm commented on pull request #17034: [BEAM-13925] Add weekly automation to update our reviewer config

Posted by GitBox <gi...@apache.org>.
damccorm commented on pull request #17034:
URL: https://github.com/apache/beam/pull/17034#issuecomment-1061820016


   R: @KonradJanica for TS changes


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [beam] KonradJanica commented on a change in pull request #17034: [BEAM-13925] Add weekly automation to update our reviewer config

Posted by GitBox <gi...@apache.org>.
KonradJanica commented on a change in pull request #17034:
URL: https://github.com/apache/beam/pull/17034#discussion_r822135287



##########
File path: scripts/ci/pr-bot/shared/reviewerConfig.ts
##########
@@ -22,10 +22,24 @@ import { Label } from "./githubUtils";
 
 export class ReviewerConfig {
   private config: any;
+  private configPath: string;
   constructor(pathToConfigFile) {
     this.config = yaml.load(
       fs.readFileSync(pathToConfigFile, { encoding: "utf-8" })
     );
+    this.configPath = pathToConfigFile;
+  }
+
+  // Returns all possible reviewers for each label configured.
+  getReviewersForAllLabels(): { [key: string]: string[] } {
+    var labelObjects = this.config.labels;

Review comment:
       Avoid `var`.

##########
File path: scripts/ci/pr-bot/updateReviewers.ts
##########
@@ -0,0 +1,286 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 exec = require("@actions/exec");
+const github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const {
+  REPO_OWNER,
+  REPO,
+  BOT_NAME,
+  PATH_TO_CONFIG_FILE,
+} = require("./shared/constants");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+
+async function getPullsFromLastThreeMonths(): Promise<any[]> {
+  const cutoffDate = new Date();
+  cutoffDate.setMonth(cutoffDate.getMonth() - 3);
+  console.log(`Getting PRs newer than ${cutoffDate}`);
+  const githubClient = github.getGitHubClient();
+  let result = await githubClient.rest.pulls.list({
+    owner: REPO_OWNER,
+    repo: REPO,
+    state: "closed",
+    per_page: 100, // max allowed
+  });
+  let page = 2;
+  let retries = 0;
+  let pulls = result.data;
+  while (
+    result.data.length > 0 &&
+    new Date(result.data[result.data.length - 1].created_at) > cutoffDate
+  ) {
+    if (retries === 0) {
+      console.log(`Getting PRs, page: ${page}`);
+      console.log(
+        `Current oldest PR = ${new Date(
+          result.data[result.data.length - 1].created_at
+        )}`
+      );
+    }
+    try {
+      result = await githubClient.rest.pulls.list({
+        owner: REPO_OWNER,
+        repo: REPO,
+        state: "closed",
+        per_page: 100, // max allowed
+        page: page,
+      });
+      pulls = pulls.concat(result.data);
+      page++;
+      retries = 0;
+    } catch (err) {
+      if (retries >= 3) {
+        throw err;
+      }
+      retries++;
+    }
+  }
+  return pulls;
+}
+
+interface pullActivity {
+  reviews: number;
+  pullsAuthored: number;
+}
+
+interface reviewerActivity {
+  reviewers: { [reviewer: string]: pullActivity };
+}
+
+async function getReviewersForPull(pull: any): Promise<string[]> {
+  let reviewers = new Set<string>();
+  const githubClient = github.getGitHubClient();
+  let comments = (
+    await githubClient.rest.issues.listComments({
+      owner: REPO_OWNER,
+      repo: REPO,
+      issue_number: pull.number,
+    })
+  ).data;
+  const reviewComments = (
+    await githubClient.rest.pulls.listReviewComments({
+      owner: REPO_OWNER,
+      repo: REPO,
+      pull_number: pull.number,
+    })
+  ).data;
+  comments = comments.concat(reviewComments);
+
+  for (const comment of comments) {
+    if (
+      comment.user.login &&
+      comment.user.login !== pull.user.login &&
+      comment.user.login !== BOT_NAME
+    ) {
+      reviewers.add(comment.user.login.toLowerCase());
+    }
+  }
+
+  return [...reviewers];
+}
+
+function addReviewerActivity(
+  reviewerActivity: reviewerActivity,
+  reviewers: string[],
+  author: string
+): reviewerActivity {
+  if (!reviewerActivity) {
+    reviewerActivity = {
+      reviewers: {},
+    };
+  }
+
+  if (author in reviewerActivity.reviewers) {
+    reviewerActivity.reviewers[author].pullsAuthored++;
+  } else {
+    reviewerActivity.reviewers[author] = {
+      reviews: 0,
+      pullsAuthored: 1,
+    };
+  }
+
+  for (const reviewer of reviewers) {
+    if (reviewer !== author) {
+      let curReviewerActivity = {
+        reviews: 1,
+        pullsAuthored: 0,
+      };
+      if (reviewer in reviewerActivity.reviewers) {
+        reviewerActivity.reviewers[reviewer].reviews++;
+      } else {
+        reviewerActivity.reviewers[author] = {
+          reviews: 1,
+          pullsAuthored: 0,
+        };
+      }
+    }
+  }
+
+  return reviewerActivity;
+}
+
+async function getReviewerActivityByLabel(
+  pulls: any[]
+): Promise<{ [label: string]: reviewerActivity }> {
+  let reviewerActivityByLabel: { [label: string]: reviewerActivity } = {};
+  for (const pull of pulls) {
+    console.log(`Processing PR ${pull.number}`);
+    const author = pull.user.login.toLowerCase();
+    if (author !== BOT_NAME) {
+      const reviewers = await getReviewersForPull(pull);
+      const labels = pull.labels;
+      for (const label of labels) {
+        const labelName = label.name.toLowerCase();
+        reviewerActivityByLabel[labelName] = addReviewerActivity(
+          reviewerActivityByLabel[labelName],
+          reviewers,
+          author
+        );
+      }
+    }
+  }
+
+  return reviewerActivityByLabel;
+}
+
+interface configUpdates {
+  reviewersAddedForLabels: { [reviewer: string]: string[] };
+  reviewersRemovedForLabels: { [reviewer: string]: string[] };
+}
+
+function updateReviewerConfig(
+  reviewerActivityByLabel: { [label: string]: reviewerActivity },
+  reviewerConfig: typeof ReviewerConfig
+): configUpdates {
+  let updates: configUpdates = {
+    reviewersAddedForLabels: {},
+    reviewersRemovedForLabels: {},
+  };
+  const currentReviewersForLabels = reviewerConfig.getReviewersForAllLabels();
+  for (const label of Object.keys(currentReviewersForLabels)) {
+    // Remove any reviewers with no reviews or pulls created
+    let reviewers = currentReviewersForLabels[label];
+    const exclusionList = reviewerConfig.getExclusionListForLabel(label);
+    for (let i = 0; i < reviewers.length; i++) {
+      if (!reviewerActivityByLabel[label].reviewers[reviewers[i]]) {
+        if (reviewers[i] in updates.reviewersRemovedForLabels) {
+          updates.reviewersRemovedForLabels[reviewers[i]].push(label);
+        } else {
+          updates.reviewersRemovedForLabels[reviewers[i]] = [label];
+        }
+        reviewers.splice(i, 1);
+        i--;
+      }
+    }

Review comment:
       Editing the reviewers array while looping is a code smell. Creating a new `reviewers` array would be cleaner.

##########
File path: scripts/ci/pr-bot/updateReviewers.ts
##########
@@ -0,0 +1,286 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 exec = require("@actions/exec");
+const github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const {
+  REPO_OWNER,
+  REPO,
+  BOT_NAME,
+  PATH_TO_CONFIG_FILE,
+} = require("./shared/constants");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+
+async function getPullsFromLastThreeMonths(): Promise<any[]> {
+  const cutoffDate = new Date();
+  cutoffDate.setMonth(cutoffDate.getMonth() - 3);
+  console.log(`Getting PRs newer than ${cutoffDate}`);
+  const githubClient = github.getGitHubClient();
+  let result = await githubClient.rest.pulls.list({
+    owner: REPO_OWNER,
+    repo: REPO,
+    state: "closed",
+    per_page: 100, // max allowed
+  });
+  let page = 2;
+  let retries = 0;
+  let pulls = result.data;
+  while (
+    result.data.length > 0 &&
+    new Date(result.data[result.data.length - 1].created_at) > cutoffDate
+  ) {
+    if (retries === 0) {
+      console.log(`Getting PRs, page: ${page}`);
+      console.log(
+        `Current oldest PR = ${new Date(
+          result.data[result.data.length - 1].created_at
+        )}`
+      );
+    }
+    try {
+      result = await githubClient.rest.pulls.list({
+        owner: REPO_OWNER,
+        repo: REPO,
+        state: "closed",
+        per_page: 100, // max allowed
+        page: page,
+      });
+      pulls = pulls.concat(result.data);
+      page++;
+      retries = 0;
+    } catch (err) {
+      if (retries >= 3) {
+        throw err;
+      }
+      retries++;
+    }
+  }
+  return pulls;
+}
+
+interface pullActivity {
+  reviews: number;
+  pullsAuthored: number;
+}
+
+interface reviewerActivity {
+  reviewers: { [reviewer: string]: pullActivity };
+}
+
+async function getReviewersForPull(pull: any): Promise<string[]> {
+  let reviewers = new Set<string>();
+  const githubClient = github.getGitHubClient();
+  let comments = (
+    await githubClient.rest.issues.listComments({
+      owner: REPO_OWNER,
+      repo: REPO,
+      issue_number: pull.number,
+    })
+  ).data;
+  const reviewComments = (
+    await githubClient.rest.pulls.listReviewComments({
+      owner: REPO_OWNER,
+      repo: REPO,
+      pull_number: pull.number,
+    })
+  ).data;
+  comments = comments.concat(reviewComments);
+
+  for (const comment of comments) {
+    if (
+      comment.user.login &&
+      comment.user.login !== pull.user.login &&
+      comment.user.login !== BOT_NAME
+    ) {
+      reviewers.add(comment.user.login.toLowerCase());
+    }
+  }
+
+  return [...reviewers];
+}
+
+function addReviewerActivity(
+  reviewerActivity: reviewerActivity,
+  reviewers: string[],
+  author: string
+): reviewerActivity {
+  if (!reviewerActivity) {
+    reviewerActivity = {
+      reviewers: {},
+    };
+  }
+
+  if (author in reviewerActivity.reviewers) {
+    reviewerActivity.reviewers[author].pullsAuthored++;
+  } else {
+    reviewerActivity.reviewers[author] = {
+      reviews: 0,
+      pullsAuthored: 1,
+    };
+  }
+
+  for (const reviewer of reviewers) {
+    if (reviewer !== author) {
+      let curReviewerActivity = {
+        reviews: 1,
+        pullsAuthored: 0,
+      };
+      if (reviewer in reviewerActivity.reviewers) {
+        reviewerActivity.reviewers[reviewer].reviews++;
+      } else {
+        reviewerActivity.reviewers[author] = {
+          reviews: 1,
+          pullsAuthored: 0,
+        };
+      }
+    }
+  }
+
+  return reviewerActivity;
+}
+
+async function getReviewerActivityByLabel(
+  pulls: any[]
+): Promise<{ [label: string]: reviewerActivity }> {
+  let reviewerActivityByLabel: { [label: string]: reviewerActivity } = {};
+  for (const pull of pulls) {
+    console.log(`Processing PR ${pull.number}`);
+    const author = pull.user.login.toLowerCase();
+    if (author !== BOT_NAME) {
+      const reviewers = await getReviewersForPull(pull);
+      const labels = pull.labels;
+      for (const label of labels) {
+        const labelName = label.name.toLowerCase();
+        reviewerActivityByLabel[labelName] = addReviewerActivity(
+          reviewerActivityByLabel[labelName],
+          reviewers,
+          author
+        );
+      }
+    }
+  }
+
+  return reviewerActivityByLabel;
+}
+
+interface configUpdates {
+  reviewersAddedForLabels: { [reviewer: string]: string[] };
+  reviewersRemovedForLabels: { [reviewer: string]: string[] };
+}
+
+function updateReviewerConfig(
+  reviewerActivityByLabel: { [label: string]: reviewerActivity },
+  reviewerConfig: typeof ReviewerConfig
+): configUpdates {
+  let updates: configUpdates = {
+    reviewersAddedForLabels: {},
+    reviewersRemovedForLabels: {},
+  };
+  const currentReviewersForLabels = reviewerConfig.getReviewersForAllLabels();
+  for (const label of Object.keys(currentReviewersForLabels)) {
+    // Remove any reviewers with no reviews or pulls created
+    let reviewers = currentReviewersForLabels[label];
+    const exclusionList = reviewerConfig.getExclusionListForLabel(label);
+    for (let i = 0; i < reviewers.length; i++) {
+      if (!reviewerActivityByLabel[label].reviewers[reviewers[i]]) {
+        if (reviewers[i] in updates.reviewersRemovedForLabels) {
+          updates.reviewersRemovedForLabels[reviewers[i]].push(label);
+        } else {
+          updates.reviewersRemovedForLabels[reviewers[i]] = [label];
+        }
+        reviewers.splice(i, 1);
+        i--;
+      }
+    }
+
+    // Add any reviewers who have at least 5 combined reviews + pulls authored
+    for (const reviewer of Object.keys(
+      reviewerActivityByLabel[label].reviewers
+    )) {
+      const reviewerContributions =
+        reviewerActivityByLabel[label].reviewers[reviewer].reviews +
+        reviewerActivityByLabel[label].reviewers[reviewer].pullsAuthored;
+      if (
+        reviewerContributions >= 5 &&
+        reviewers.indexOf(reviewer) < 0 &&
+        exclusionList.indexOf(reviewer) < 0
+      ) {
+        reviewers.push(reviewer);
+        if (reviewer in updates.reviewersAddedForLabels) {
+          updates.reviewersAddedForLabels[reviewer].push(label);
+        } else {
+          updates.reviewersAddedForLabels[reviewer] = [label];
+        }
+      }
+    }
+
+    reviewerConfig.updateReviewerForLabel(label, reviewers);
+  }
+
+  return updates;
+}
+
+async function openPull(updates: configUpdates) {
+  const curDate = new Date();
+  const branch = `pr-bot-${
+    curDate.getMonth() + 1
+  }-${curDate.getDay()}-${curDate.getFullYear()}-${curDate.getHours()}-${curDate.getMinutes()}-${curDate.getSeconds()}`;

Review comment:
       Should days/months less than 10 be represented with a single digit? Otherwise some logic is required to prepend '0'. Would [toIsoString](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/toISOString) work here? That way you can easily revert it back into a date object when needed.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org