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/05 04:11:34 UTC

[GitHub] [beam] youngoli commented on a change in pull request #16985: [BEAM-13925] Add ability to get metrics on pr-bot performance

youngoli commented on a change in pull request #16985:
URL: https://github.com/apache/beam/pull/16985#discussion_r820022640



##########
File path: scripts/ci/pr-bot/gatherMetrics.ts
##########
@@ -0,0 +1,528 @@
+/*
+ * 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 fs = require("fs");
+const github = require("./shared/githubUtils");
+const {
+  REPO_OWNER,
+  REPO,
+  PATH_TO_METRICS_CSV,
+  BOT_NAME,
+} = require("./shared/constants");
+
+interface PrStats {
+  firstTimeContribution: boolean;
+  timeToFirstReview: number;
+  timeFromCreationToCompletion: number;
+  timeFromReviewersMentionedToCompletion: { [key: string]: number };
+  mergedDate: Date;
+}
+
+interface AggregatedMetrics {
+  prsCompleted: number;
+  prsCompletedByNewContributors: number;
+  averageTimeToFirstReview: number;
+  averageTimeToNewContributorFirstReview: number;
+  averageTimeCreationToCompletion: number;
+  numUsersPerformingReviews: number;
+  numCommittersPerformingReviews: number;
+  numNonCommittersPerformingReviews: number;
+  giniIndexCommittersPerformingReviews: number;
+  averageTimeFromCommitterAssignmentToPrMerge: number;
+}
+
+async function getCompletedPullsFromLastYear(): Promise<any[]> {
+  const cutoffDate = new Date();
+  cutoffDate.setFullYear(new Date().getFullYear() - 1);
+  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++;
+    }
+  }
+  console.log("Got all PRs, moving to the processing stage");
+  return pulls;
+}
+
+// Rather than check the whole repo history (expensive),
+// we'll just look at the last year's worth of contributions to check if they're a first time contributor.
+// If they contributed > 1 year ago, their experience is probably similar to a first time contributor anyways.
+function checkIfFirstTimeContributor(
+  pull: any,
+  pullsFromLastYear: any[]
+): boolean {
+  return !pullsFromLastYear.some(
+    (pullFromLastYear) =>
+      pullFromLastYear.created_at < pull.created_at &&
+      pullFromLastYear.user.login === pull.user.login
+  );
+}
+
+// Get time between pr creation and the first comment, approval, or merge that isn't done by:
+// (a) the author
+// (b) automated tooling
+function getTimeToFirstReview(
+  pull: any,
+  comments: any[],
+  reviews: any[],
+  creationDate: Date,
+  mergedDate: Date
+): number {
+  let timeToFirstReview = mergedDate.getTime() - creationDate.getTime();
+
+  const firstReviewed = reviews.find(
+    (review) => review.user.login == pull.user.login

Review comment:
       Shouldn't this be the following?
   ```suggestion
       (review) => review.user.login != pull.user.login
   ```




-- 
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