@samitouri / QOS-React / commits / 9358aeab34

[scripts] Add script to pull commit message from PR description

Sathya Gunasekaran committed Mar 19, 2024 at 11:23 UTC 9358aeab34e750c3d1e3fe86fd504a0450a43cbd
1 file changed +140
compiler/scripts/update-commit-message.js new
+140
@@ -0,0 +1,140 @@
1 +/*
2 + * Copyright (c) Meta Platforms, Inc. and affiliates.
3 + *
4 + * This source code is licensed under the MIT license found in the
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
12 + *
13 + * USAGE:
14 + * - $ git filter-branch -f --msg-filter "node script-filter-branch.mjs" 2364096862b72cf4d801ef2008c54252335a2df9..HEAD
15 + */
16 +
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 = "";
24 +
25 +const OWNER = "facebook";
26 +const REPO = "react-forget";
27 +const octokit = new Octokit({ auth: TOKEN });
28 +
29 +const fetchPullRequest = async (pullNumber) => {
30 + const response = await octokit.request(
31 + "GET /repos/{owner}/{repo}/pulls/{pull_number}",
32 + {
33 + owner: OWNER,
34 + repo: REPO,
35 + pull_number: pullNumber,
36 + headers: {
37 + "X-GitHub-Api-Version": "2022-11-28",
38 + },
39 + }
40 + );
41 + return { body: response.data.body, title: response.data.title };
42 +};
43 +
44 +function formatCommitMessage(str) {
45 + let formattedStr = "";
46 + let line = "";
47 +
48 + const trim = str.replace(/(\r\n|\n|\r)/gm, " ").trim();
49 + if (!trim) {
50 + return "";
51 + }
52 +
53 + // Split the string into words
54 + const words = trim.split(" ");
55 + // Iterate over each word
56 + for (let i = 0; i < words.length; i++) {
57 + // If adding the next word doesn't exceed the line length limit, add it to the line
58 + if ((line + words[i]).length <= 80) {
59 + line += words[i] + " ";
60 + } else {
61 + // Otherwise, add the line to the formatted string and start a new line
62 + formattedStr += line + "\n";
63 + line = words[i] + " ";
64 + }
65 + }
66 + // Add the last line to the formatted string
67 + formattedStr += line;
68 + return formattedStr;
69 +}
70 +
71 +function filterMsg(response) {
72 + const { body, title } = response;
73 +
74 + const msgs = body.split("\n\n").flatMap((x) => x.split("\r\n"));
75 +
76 + const newMessage = [];
77 +
78 + // Add title
79 + msgs.unshift(title);
80 +
81 + for (const msg of msgs) {
82 + // remove "Stack from [ghstack] blurb"
83 + if (msg.startsWith("Stack from ")) {
84 + continue;
85 + }
86 +
87 + // remove "* #1234"
88 + if (msg.startsWith("* #")) {
89 + continue;
90 + }
91 +
92 + // remove "* __->__ #1234"
93 + if (msg.startsWith("* __")) {
94 + continue;
95 + }
96 +
97 + const formattedStr = formatCommitMessage(msg);
98 + if (!formattedStr) {
99 + continue;
100 + }
101 + newMessage.push(formattedStr);
102 + }
103 +
104 + const updatedMsg = newMessage.join("\n\n");
105 + return updatedMsg;
106 +}
107 +
108 +function parsePullRequestNumber(text) {
109 + if (!text) {
110 + return null;
111 + }
112 + const regex = /https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/pull\/(\d+)/;
113 + const match = text.match(regex);
114 + return match ? match[1] : null;
115 +}
116 +
117 +async function main() {
118 + const data = fs.readFileSync(0, "utf-8");
119 + const pr = parsePullRequestNumber(data);
120 +
121 + if (pr) {
122 + try {
123 + const response = await fetchPullRequest(pr);
124 + if (!response.body) {
125 + console.log(data);
126 + return;
127 + }
128 + const newMessage = filterMsg(response);
129 + console.log(newMessage);
130 + return;
131 + } catch (e) {
132 + console.log(data);
133 + return;
134 + }
135 + }
136 +
137 + console.log(data);
138 +}
139 +
140 +main();