@samitouri / QOS-React-2 / commits / 0846daaa54

Commit message script captures non-ghstack messages

Tested by manually passing in the commit message from https://github.com/facebook/react-forget/commit/1028504dc809d29992954553d34cf6f55a1fefad and verifying that it grabbed the description from GH.

Joe Savona committed Apr 2, 2024 at 20:55 UTC 0846daaa54537d3df212903bf37bab92e44f9c07
1 file changed +25 -16
compiler/scripts/update-commit-message.js
+25 -16
@@ -5,26 +5,20 @@
5 * LICENSE file in the root directory of this source tree.
6 *
7 * INSTALLATION:
8 - * - $ npm install octokit
9 - * - Update TOKEN after creating token from
10 - * https://github.com/settings/tokens
11 - * - Update REPO_LOCAL_PATH to point to local Forget repo
8 + * - `$ npm install octokit
9 + * - Get a token from https://github.com/settings/tokens for use in the command below,
10 + * set the token value as the GITHUB_AUTH_TOKEN environment variable
11 *
12 * USAGE:
14 - * - $ git filter-branch -f --msg-filter "node script-filter-branch.mjs" 2364096862b72cf4d801ef2008c54252335a2df9..HEAD
13 + * - $ GITHUB_AUTH_TOKEN="..." git filter-branch -f --msg-filter "node update-commit-message.js" 2364096862b72cf4d801ef2008c54252335a2df9..HEAD
14 */
15
17 -import { Octokit, App } from "octokit";
18 -
19 -/*
20 - * UPDATE ${TOKEN} and ${REPO_LOCAL_PATH} before running this!
21 - */
22 -const TOKEN = "";
23 -const REPO_LOCAL_PATH = "";
16 +const { Octokit, App } = require("octokit");
17 +const fs = require("fs");
18
19 const OWNER = "facebook";
20 const REPO = "react-forget";
27 -const octokit = new Octokit({ auth: TOKEN });
21 +const octokit = new Octokit({ auth: process.env.GITHUB_AUTH_TOKEN });
22
23 const fetchPullRequest = async (pullNumber) => {
24 const response = await octokit.request(
@@ -109,9 +103,24 @@ function parsePullRequestNumber(text) {
103 if (!text) {
104 return null;
105 }
112 - const regex = /https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/pull\/(\d+)/;
113 - const match = text.match(regex);
114 - return match ? match[1] : null;
106 + const ghstackUrlRegex =
107 + /https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/pull\/(\d+)/;
108 + const ghstackMatch = text.match(ghstackUrlRegex);
109 + if (ghstackMatch) {
110 + return ghstackMatch[1];
111 + }
112 + const firstLine = text
113 + .split("\n")
114 + .filter((text) => text.trim().length > 0)[0];
115 + if (firstLine == null) {
116 + return null;
117 + }
118 + const prNumberRegex = /\(#(\d{3,})\)\s*$/;
119 + const prNumberMatch = firstLine.match(prNumberRegex);
120 + if (prNumberMatch) {
121 + return prNumberMatch[1];
122 + }
123 + return null;
124 }
125
126 async function main() {