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/09 14:01:32 UTC

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

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