KonradJanica commented on a change in pull request #17001:
URL: https://github.com/apache/beam/pull/17001#discussion_r821072062



##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {

Review comment:
       ```
   return pull.labels.some(label => label.name.toLowerCase() === 
labelName.toLowerCase())
   ```

##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+function getTwoWeekdaysAgo(): Date {
+  let twoWeekDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);

Review comment:
       Please store this duration in a constant with appropriate naming, e.g. 
`TWO_DAYS_MS`

##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+function getTwoWeekdaysAgo(): Date {
+  let twoWeekDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);

Review comment:
       `let` => `const`

##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+function getTwoWeekdaysAgo(): Date {
+  let twoWeekDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
+  const currentDay = new Date(Date.now()).getDay();
+  // If Saturday, sunday, monday, or tuesday, add extra time to account for 
weekend.
+  if (currentDay === 6) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 1);
+  }
+  if (currentDay <= 2) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 2);
+  }
+
+  return twoWeekDaysAgo;
+}
+
+async function isSlowReview(pull: any): Promise<boolean> {
+  if (!hasLabel(pull, "Next Action: Reviewers")) {
+    return false;
+  }
+  const lastModified = new Date(pull.updated_at);
+  const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);

Review comment:
       Reuse constant mentioned in comment above.

##########
File path: scripts/ci/pr-bot/shared/commentStrings.ts
##########
@@ -70,3 +70,33 @@ export function reviewersAlreadyAssigned(reviewers: 
string[]): string {
 export function noLegalReviewers(): string {
   return "No reviewers could be found from any of the labels on the PR or in 
the fallback reviewers list. Check the config file to make sure reviewers are 
configured";
 }
+
+export function assignNewReviewer(labelToReviewerMapping: any): string {
+  let commentString =
+    "Assigning new set of reviewers because Pr has gone too long without 
review. If you would like to opt out of this review, comment `assign to next 
reviewer`:\n\n";
+
+  for (let label in labelToReviewerMapping) {
+    let reviewer = labelToReviewerMapping[label];
+    if (label == "no-matching-label") {

Review comment:
       Triple equals for string matching. Should `"no-matching-label"` be a 
reusable constant?

##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+function getTwoWeekdaysAgo(): Date {
+  let twoWeekDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
+  const currentDay = new Date(Date.now()).getDay();
+  // If Saturday, sunday, monday, or tuesday, add extra time to account for 
weekend.
+  if (currentDay === 6) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 1);
+  }
+  if (currentDay <= 2) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 2);
+  }
+
+  return twoWeekDaysAgo;
+}
+
+async function isSlowReview(pull: any): Promise<boolean> {
+  if (!hasLabel(pull, "Next Action: Reviewers")) {
+    return false;
+  }
+  const lastModified = new Date(pull.updated_at);
+  const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
+  if (lastModified.getTime() < sevenDaysAgo.getTime()) {
+    return true;
+  }
+
+  // If it has no comments from a non-bot/author in the last 2 weekdays, 
return true
+  const twoWeekDaysAgo = getTwoWeekdaysAgo();
+  if (lastModified.getTime() < twoWeekDaysAgo.getTime()) {
+    const pullAuthor = pull.user.login;
+    const githubClient = github.getGitHubClient();
+    const comments = (
+      await githubClient.rest.issues.listComments({
+        owner: REPO_OWNER,
+        repo: REPO,
+        issue_number: pull.number,
+      })
+    ).data;
+    for (let i = 0; i < comments.length; i++) {
+      const comment = comments[i];
+      if (
+        comment.user.login !== pullAuthor &&
+        comment.user.login !== BOT_NAME
+      ) {
+        return false;
+      }
+    }
+
+    const reviewComments = (
+      await githubClient.rest.pulls.listReviewComments({
+        owner: REPO_OWNER,
+        repo: REPO,
+        pull_number: pull.number,
+      })
+    ).data;
+    for (let i = 0; i < reviewComments.length; i++) {
+      const comment = reviewComments[i];
+      if (
+        comment.user.login !== pullAuthor &&
+        comment.user.login !== BOT_NAME
+      ) {
+        return false;
+      }
+    }
+
+    return true;
+  }
+
+  return false;
+}
+
+async function assignToNewReviewers(
+  pull: any,
+  reviewerConfig: typeof ReviewerConfig,

Review comment:
       Is `typeof` needed here?

##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+function getTwoWeekdaysAgo(): Date {
+  let twoWeekDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
+  const currentDay = new Date(Date.now()).getDay();
+  // If Saturday, sunday, monday, or tuesday, add extra time to account for 
weekend.
+  if (currentDay === 6) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 1);
+  }
+  if (currentDay <= 2) {

Review comment:
       These conditions are mutually exclusive, `if` => `else if`

##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+function getTwoWeekdaysAgo(): Date {
+  let twoWeekDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
+  const currentDay = new Date(Date.now()).getDay();
+  // If Saturday, sunday, monday, or tuesday, add extra time to account for 
weekend.
+  if (currentDay === 6) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 1);
+  }
+  if (currentDay <= 2) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 2);
+  }
+
+  return twoWeekDaysAgo;
+}
+
+async function isSlowReview(pull: any): Promise<boolean> {
+  if (!hasLabel(pull, "Next Action: Reviewers")) {
+    return false;
+  }
+  const lastModified = new Date(pull.updated_at);
+  const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
+  if (lastModified.getTime() < sevenDaysAgo.getTime()) {
+    return true;
+  }
+
+  // If it has no comments from a non-bot/author in the last 2 weekdays, 
return true
+  const twoWeekDaysAgo = getTwoWeekdaysAgo();
+  if (lastModified.getTime() < twoWeekDaysAgo.getTime()) {
+    const pullAuthor = pull.user.login;
+    const githubClient = github.getGitHubClient();
+    const comments = (
+      await githubClient.rest.issues.listComments({
+        owner: REPO_OWNER,
+        repo: REPO,
+        issue_number: pull.number,
+      })
+    ).data;
+    for (let i = 0; i < comments.length; i++) {
+      const comment = comments[i];
+      if (
+        comment.user.login !== pullAuthor &&
+        comment.user.login !== BOT_NAME
+      ) {
+        return false;
+      }
+    }
+
+    const reviewComments = (
+      await githubClient.rest.pulls.listReviewComments({
+        owner: REPO_OWNER,
+        repo: REPO,
+        pull_number: pull.number,
+      })
+    ).data;
+    for (let i = 0; i < reviewComments.length; i++) {
+      const comment = reviewComments[i];
+      if (
+        comment.user.login !== pullAuthor &&
+        comment.user.login !== BOT_NAME
+      ) {
+        return false;
+      }
+    }
+
+    return true;
+  }
+
+  return false;
+}
+
+async function assignToNewReviewers(
+  pull: any,
+  reviewerConfig: typeof ReviewerConfig,
+  stateClient: typeof PersistentState

Review comment:
       Is `typeof` needed here?

##########
File path: scripts/ci/pr-bot/processPrUpdate.ts
##########
@@ -46,6 +76,11 @@ async function processPrComment(
 ) {
   const commentContents = payload.comment.body;
   const commentAuthor = payload.sender.login;
+  const pullAuthor = getPullAuthorFromPayload(payload);
+  // If there's been a comment by a non-author, we can remove the slow review 
label
+  if (commentAuthor !== pullAuthor && commentAuthor !== BOT_NAME) {
+    payload = await removeSlowReviewLabel(payload);

Review comment:
       Don't override the input parameter, there is no benefit, the caller will 
still reference the existing object. I was expecting the `payload` to be 
returned but I didn't see that. On a side note, modifying the input parameter 
is okay for private/unexported methods, but it can be a code smell.

##########
File path: scripts/ci/pr-bot/processPrUpdate.ts
##########
@@ -84,6 +118,12 @@ async function processPrReview(
   stateClient: typeof PersistentState,
   reviewerConfig: typeof ReviewerConfig
 ) {
+  const reviewer = payload.sender.login;
+  const pullAuthor = getPullAuthorFromPayload(payload);
+  // If there's been a review by a non-author, we can remove the slow review 
label
+  if (reviewer !== pullAuthor) {
+    payload = await removeSlowReviewLabel(payload);

Review comment:
       Ditto, as above. Don't reassign `payload`.

##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+function getTwoWeekdaysAgo(): Date {
+  let twoWeekDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
+  const currentDay = new Date(Date.now()).getDay();
+  // If Saturday, sunday, monday, or tuesday, add extra time to account for 
weekend.
+  if (currentDay === 6) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 1);
+  }
+  if (currentDay <= 2) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 2);
+  }
+
+  return twoWeekDaysAgo;
+}
+
+async function isSlowReview(pull: any): Promise<boolean> {
+  if (!hasLabel(pull, "Next Action: Reviewers")) {
+    return false;
+  }
+  const lastModified = new Date(pull.updated_at);
+  const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
+  if (lastModified.getTime() < sevenDaysAgo.getTime()) {
+    return true;
+  }
+
+  // If it has no comments from a non-bot/author in the last 2 weekdays, 
return true
+  const twoWeekDaysAgo = getTwoWeekdaysAgo();
+  if (lastModified.getTime() < twoWeekDaysAgo.getTime()) {
+    const pullAuthor = pull.user.login;
+    const githubClient = github.getGitHubClient();
+    const comments = (
+      await githubClient.rest.issues.listComments({
+        owner: REPO_OWNER,
+        repo: REPO,
+        issue_number: pull.number,
+      })
+    ).data;
+    for (let i = 0; i < comments.length; i++) {
+      const comment = comments[i];
+      if (
+        comment.user.login !== pullAuthor &&
+        comment.user.login !== BOT_NAME
+      ) {
+        return false;
+      }
+    }
+
+    const reviewComments = (
+      await githubClient.rest.pulls.listReviewComments({
+        owner: REPO_OWNER,
+        repo: REPO,
+        pull_number: pull.number,
+      })
+    ).data;
+    for (let i = 0; i < reviewComments.length; i++) {
+      const comment = reviewComments[i];
+      if (
+        comment.user.login !== pullAuthor &&
+        comment.user.login !== BOT_NAME
+      ) {
+        return false;
+      }
+    }
+
+    return true;
+  }
+
+  return false;
+}
+
+async function assignToNewReviewers(
+  pull: any,
+  reviewerConfig: typeof ReviewerConfig,
+  stateClient: typeof PersistentState
+) {
+  let prState = await stateClient.getPrState(pull.number);
+  let reviewerStateToUpdate = {};
+  const labelObjects = pull.labels;
+  let reviewersToExclude = Object.values(prState.reviewersAssignedForLabels);
+  reviewersToExclude.push(pull.user.login);
+  const reviewersForLabels: { [key: string]: string[] } =
+    reviewerConfig.getReviewersForLabels(labelObjects, reviewersToExclude);
+  for (const labelObject of labelObjects) {
+    const label = labelObject.name;
+    let availableReviewers = reviewersForLabels[label];
+    if (availableReviewers && availableReviewers.length > 0) {
+      let reviewersState = await stateClient.getReviewersForLabelState(label);
+      let chosenReviewer = await reviewersState.assignNextCommitter(
+        availableReviewers
+      );
+      reviewerStateToUpdate[label] = reviewersState;
+      prState.reviewersAssignedForLabels[label] = chosenReviewer;
+    }
+  }
+
+  console.log(`Assigning new reviewers for pr ${pull.number}`);
+  await github.addPrComment(
+    pull.number,
+    commentStrings.assignNewReviewer(prState.reviewersAssignedForLabels)
+  );
+
+  await stateClient.writePrState(pull.number, prState);
+  let labelsToUpdate = Object.keys(reviewerStateToUpdate);
+  for (const label of labelsToUpdate) {
+    await stateClient.writeReviewersForLabelState(
+      label,
+      reviewerStateToUpdate[label]
+    );
+  }
+}
+
+// Flag any prs that have been awaiting reviewer action at least 7 days,
+// or have been awaiting initial review for at least 2 days and ping the 
reviewers
+// If there's still no action after 2 more days, assign a new set of 
committers.
+async function processPull(
+  pull: any,
+  reviewerConfig: typeof ReviewerConfig,
+  stateClient: typeof PersistentState
+) {
+  const prState = await stateClient.getPrState(pull.number);
+  if (prState.stopReviewerNotifications) {
+    console.log(`Skipping PR ${pull.number} - notifications silenced`);
+    return;
+  }
+  if (hasLabel(pull, SLOW_REVIEW_LABEL)) {
+    const lastModified = new Date(pull.updated_at);
+    const twoWeekDaysAgo = getTwoWeekdaysAgo();
+    console.log(
+      `PR ${pull.number} has the slow review label. Last modified 
${lastModified}`
+    );
+    if (lastModified.getTime() < twoWeekDaysAgo.getTime()) {
+      console.log(
+        `PR ${pull.number} still awaiting action - assigning new reviewers`
+      );
+      await assignToNewReviewers(pull, reviewerConfig, stateClient);
+      await github.getGitHubClient().rest.issues.removeLabel({
+        owner: REPO_OWNER,
+        repo: REPO,
+        issue_number: pull.number,
+        name: SLOW_REVIEW_LABEL,
+      });
+    }
+
+    return;
+  }
+
+  if (await isSlowReview(pull)) {
+    const client = github.getGitHubClient();
+    const currentReviewers = prState.reviewersAssignedForLabels;
+    if (currentReviewers && Object.values(currentReviewers).length > 0) {
+      console.log(
+        `Flagging pr ${pull.number} as slow. Tagging reviewers 
${currentReviewers}`
+      );
+      await github.addPrComment(
+        pull.number,
+        commentStrings.slowReview(Object.values(currentReviewers))
+      );
+      await client.rest.issues.addLabels({
+        owner: REPO_OWNER,
+        repo: REPO,
+        issue_number: pull.number,
+        labels: [SLOW_REVIEW_LABEL],
+      });
+    }
+  }
+}
+
+async function processOldPrs() {
+  const githubClient = github.getGitHubClient();
+  let reviewerConfig = new ReviewerConfig(PATH_TO_CONFIG_FILE);
+  let stateClient = new PersistentState();
+
+  let openPulls = await githubClient.paginate(
+    "GET /repos/{owner}/{repo}/pulls",
+    {
+      owner: REPO_OWNER,
+      repo: REPO,
+    }
+  );
+
+  for (let i = 0; i < openPulls.length; i++) {

Review comment:
       Index unused => `for (const pull of openPulls)`

##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+function getTwoWeekdaysAgo(): Date {
+  let twoWeekDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
+  const currentDay = new Date(Date.now()).getDay();
+  // If Saturday, sunday, monday, or tuesday, add extra time to account for 
weekend.
+  if (currentDay === 6) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 1);
+  }
+  if (currentDay <= 2) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 2);
+  }
+
+  return twoWeekDaysAgo;
+}
+
+async function isSlowReview(pull: any): Promise<boolean> {
+  if (!hasLabel(pull, "Next Action: Reviewers")) {
+    return false;
+  }
+  const lastModified = new Date(pull.updated_at);
+  const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
+  if (lastModified.getTime() < sevenDaysAgo.getTime()) {
+    return true;
+  }
+
+  // If it has no comments from a non-bot/author in the last 2 weekdays, 
return true
+  const twoWeekDaysAgo = getTwoWeekdaysAgo();
+  if (lastModified.getTime() < twoWeekDaysAgo.getTime()) {
+    const pullAuthor = pull.user.login;
+    const githubClient = github.getGitHubClient();
+    const comments = (
+      await githubClient.rest.issues.listComments({
+        owner: REPO_OWNER,
+        repo: REPO,
+        issue_number: pull.number,
+      })
+    ).data;
+    for (let i = 0; i < comments.length; i++) {
+      const comment = comments[i];
+      if (
+        comment.user.login !== pullAuthor &&
+        comment.user.login !== BOT_NAME
+      ) {
+        return false;
+      }
+    }
+
+    const reviewComments = (
+      await githubClient.rest.pulls.listReviewComments({
+        owner: REPO_OWNER,
+        repo: REPO,
+        pull_number: pull.number,
+      })
+    ).data;
+    for (let i = 0; i < reviewComments.length; i++) {
+      const comment = reviewComments[i];
+      if (
+        comment.user.login !== pullAuthor &&
+        comment.user.login !== BOT_NAME
+      ) {
+        return false;
+      }

Review comment:
       ```
   if (reviewComments.some(({user: login}) => login !== pullAuthor && login !== 
BOT_NAME)) {
      return false;
   }
   ```
   
   After unnesting, this becomes the last return.

##########
File path: scripts/ci/pr-bot/shared/commentStrings.ts
##########
@@ -70,3 +70,33 @@ export function reviewersAlreadyAssigned(reviewers: 
string[]): string {
 export function noLegalReviewers(): string {
   return "No reviewers could be found from any of the labels on the PR or in 
the fallback reviewers list. Check the config file to make sure reviewers are 
configured";
 }
+
+export function assignNewReviewer(labelToReviewerMapping: any): string {

Review comment:
       `labelToReviewerMapping` shouldn't use `any`, is there a defined type?

##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+function getTwoWeekdaysAgo(): Date {
+  let twoWeekDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
+  const currentDay = new Date(Date.now()).getDay();
+  // If Saturday, sunday, monday, or tuesday, add extra time to account for 
weekend.
+  if (currentDay === 6) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 1);
+  }
+  if (currentDay <= 2) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 2);
+  }
+
+  return twoWeekDaysAgo;
+}
+
+async function isSlowReview(pull: any): Promise<boolean> {
+  if (!hasLabel(pull, "Next Action: Reviewers")) {
+    return false;
+  }
+  const lastModified = new Date(pull.updated_at);
+  const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
+  if (lastModified.getTime() < sevenDaysAgo.getTime()) {
+    return true;
+  }
+
+  // If it has no comments from a non-bot/author in the last 2 weekdays, 
return true
+  const twoWeekDaysAgo = getTwoWeekdaysAgo();
+  if (lastModified.getTime() < twoWeekDaysAgo.getTime()) {

Review comment:
       Perhaps unnest this block and directly return false. i.e.
   ```
   if (lastModified.getTime() >= twoWeekDaysAgo.getTime()) {
      return false;
   }
   ```

##########
File path: scripts/ci/pr-bot/findPrsNeedingAttention.ts
##########
@@ -0,0 +1,228 @@
+/*
+ * 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 github = require("./shared/githubUtils");
+const commentStrings = require("./shared/commentStrings");
+const { ReviewerConfig } = require("./shared/reviewerConfig");
+const { PersistentState } = require("./shared/persistentState");
+const {
+  BOT_NAME,
+  REPO_OWNER,
+  REPO,
+  PATH_TO_CONFIG_FILE,
+  SLOW_REVIEW_LABEL,
+} = require("./shared/constants");
+
+function hasLabel(pull: any, labelName: string): boolean {
+  const labels = pull.labels;
+  for (const label of labels) {
+    if (label.name.toLowerCase() === labelName.toLowerCase()) {
+      return true;
+    }
+  }
+  return false;
+}
+
+function getTwoWeekdaysAgo(): Date {
+  let twoWeekDaysAgo = new Date(Date.now() - 2 * 24 * 60 * 60 * 1000);
+  const currentDay = new Date(Date.now()).getDay();
+  // If Saturday, sunday, monday, or tuesday, add extra time to account for 
weekend.
+  if (currentDay === 6) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 1);
+  }
+  if (currentDay <= 2) {
+    twoWeekDaysAgo.setDate(twoWeekDaysAgo.getDate() - 2);
+  }
+
+  return twoWeekDaysAgo;
+}
+
+async function isSlowReview(pull: any): Promise<boolean> {
+  if (!hasLabel(pull, "Next Action: Reviewers")) {
+    return false;
+  }
+  const lastModified = new Date(pull.updated_at);
+  const sevenDaysAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000);
+  if (lastModified.getTime() < sevenDaysAgo.getTime()) {
+    return true;
+  }
+
+  // If it has no comments from a non-bot/author in the last 2 weekdays, 
return true
+  const twoWeekDaysAgo = getTwoWeekdaysAgo();
+  if (lastModified.getTime() < twoWeekDaysAgo.getTime()) {
+    const pullAuthor = pull.user.login;
+    const githubClient = github.getGitHubClient();
+    const comments = (
+      await githubClient.rest.issues.listComments({
+        owner: REPO_OWNER,
+        repo: REPO,
+        issue_number: pull.number,
+      })
+    ).data;
+    for (let i = 0; i < comments.length; i++) {
+      const comment = comments[i];
+      if (
+        comment.user.login !== pullAuthor &&
+        comment.user.login !== BOT_NAME
+      ) {
+        return false;
+      }

Review comment:
       ```
   if (comments.some(({user: login}) => login !== pullAuthor && login !== 
BOT_NAME)) {
      return false;
   }
   ```

##########
File path: scripts/ci/pr-bot/shared/commentStrings.ts
##########
@@ -70,3 +70,33 @@ export function reviewersAlreadyAssigned(reviewers: 
string[]): string {
 export function noLegalReviewers(): string {
   return "No reviewers could be found from any of the labels on the PR or in 
the fallback reviewers list. Check the config file to make sure reviewers are 
configured";
 }
+
+export function assignNewReviewer(labelToReviewerMapping: any): string {
+  let commentString =
+    "Assigning new set of reviewers because Pr has gone too long without 
review. If you would like to opt out of this review, comment `assign to next 
reviewer`:\n\n";
+
+  for (let label in labelToReviewerMapping) {
+    let reviewer = labelToReviewerMapping[label];
+    if (label == "no-matching-label") {
+      commentString += `R: @${reviewer} added as fallback since no labels 
match configuration\n`;
+    } else {
+      commentString += `R: @${reviewer} for label ${label}.\n`;
+    }
+  }
+
+  commentString += `
+Available commands:
+- \`stop reviewer notifications\` - opt out of the automated review tooling
+- \`remind me after tests pass\` - tag the comment author after tests pass
+- \`waiting on author\` - shift the attention set back to the author (any 
comment or push by the author will return the attention set to the reviewers)`;
+  return commentString;
+}
+
+export function slowReview(reviewers: string[]): string {
+  let commentString = `Reminder, please take a look at this pr: `;
+  reviewers.forEach((reviewer) => {

Review comment:
       `for (const reviewer of reviewers)` is the preferred syntax.




-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to