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



##########
File path: scripts/ci/pr-bot/gatherMetrics.ts
##########
@@ -0,0 +1,527 @@
+/*
+ * 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(
+    new Date().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 += 1;
+      retries = 0;
+    } catch (err) {
+      if (retries >= 3) {
+        throw err;
+      }
+      retries += 1;
+    }
+  }
+  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 {
+  for (const pullFromLastYear of pullsFromLastYear) {
+    if (
+      pullFromLastYear.created_at < pull.created_at &&
+      pullFromLastYear.user.login === pull.user.login
+    ) {
+      return false;
+    }
+  }
+
+  return true;
+}
+
+// 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();
+  let i = 0;
+  while (i < reviews.length && reviews[i].user.login === pull.user.login) {
+    i++;
+  }
+  if (i < reviews.length) {
+    const firstReviewDate = new Date(reviews[i].submitted_at);
+    timeToFirstReview = Math.min(
+      timeToFirstReview,
+      firstReviewDate.getTime() - creationDate.getTime()
+    );
+  }
+  for (const comment of comments) {
+    if (
+      comment.user.login != pull.user.login &&
+      comment.user.login != BOT_NAME
+    ) {
+      let commentTime = new Date(comment.created_at);
+      timeToFirstReview = Math.min(
+        timeToFirstReview,
+        commentTime.getTime() - creationDate.getTime()
+      );
+    }
+  }
+  return timeToFirstReview;
+}
+
+// Takes a R: @reviewer comment and extracts all reviewers tagged
+// Returns an empty list if no reviewer can be extracted
+function extractReviewersTaggedFromCommentBody(body: string): string[] {
+  if (!body) {
+    return [];
+  }
+  body = body.toLowerCase();
+  if (body.indexOf("r: @") < -1) {

Review comment:
       Actually, scratch that - it is specifically looking for R: @, not just 
@. I'll keep it but fix the condition




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