forked from facebook/react
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-commit-message.js
147 lines (127 loc) · 3.51 KB
/
update-commit-message.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* INSTALLATION:
* - `$ npm install octokit
* - Get a token from https://github.com/settings/tokens for use in the command below,
* set the token value as the GITHUB_AUTH_TOKEN environment variable
*
* USAGE:
* - $ GITHUB_AUTH_TOKEN="..." git filter-branch -f --msg-filter "node update-commit-message.js" 2364096862b72cf4d801ef2008c54252335a2df9..HEAD
*/
const {Octokit, App} = require('octokit');
const fs = require('fs');
const OWNER = 'facebook';
const REPO = 'react-forget';
const octokit = new Octokit({auth: process.env.GITHUB_AUTH_TOKEN});
const fetchPullRequest = async pullNumber => {
const response = await octokit.request(
'GET /repos/{owner}/{repo}/pulls/{pull_number}',
{
owner: OWNER,
repo: REPO,
pull_number: pullNumber,
headers: {
'X-GitHub-Api-Version': '2022-11-28',
},
}
);
return {body: response.data.body, title: response.data.title};
};
function formatCommitMessage(str) {
let formattedStr = '';
let line = '';
const trim = str.replace(/(\r\n|\n|\r)/gm, ' ').trim();
if (!trim) {
return '';
}
// Split the string into words
const words = trim.split(' ');
// Iterate over each word
for (let i = 0; i < words.length; i++) {
// If adding the next word doesn't exceed the line length limit, add it to the line
if ((line + words[i]).length <= 80) {
line += words[i] + ' ';
} else {
// Otherwise, add the line to the formatted string and start a new line
formattedStr += line + '\n';
line = words[i] + ' ';
}
}
// Add the last line to the formatted string
formattedStr += line;
return formattedStr;
}
function filterMsg(response) {
const {body, title} = response;
const msgs = body.split('\n\n').flatMap(x => x.split('\r\n'));
const newMessage = [];
// Add title
msgs.unshift(title);
for (const msg of msgs) {
// remove "Stack from [ghstack] blurb"
if (msg.startsWith('Stack from ')) {
continue;
}
// remove "* #1234"
if (msg.startsWith('* #')) {
continue;
}
// remove "* __->__ #1234"
if (msg.startsWith('* __')) {
continue;
}
const formattedStr = formatCommitMessage(msg);
if (!formattedStr) {
continue;
}
newMessage.push(formattedStr);
}
const updatedMsg = newMessage.join('\n\n');
return updatedMsg;
}
function parsePullRequestNumber(text) {
if (!text) {
return null;
}
const ghstackUrlRegex =
/https:\/\/github\.com\/[\w.-]+\/[\w.-]+\/pull\/(\d+)/;
const ghstackMatch = text.match(ghstackUrlRegex);
if (ghstackMatch) {
return ghstackMatch[1];
}
const firstLine = text.split('\n').filter(text => text.trim().length > 0)[0];
if (firstLine == null) {
return null;
}
const prNumberRegex = /\(#(\d{3,})\)\s*$/;
const prNumberMatch = firstLine.match(prNumberRegex);
if (prNumberMatch) {
return prNumberMatch[1];
}
return null;
}
async function main() {
const data = fs.readFileSync(0, 'utf-8');
const pr = parsePullRequestNumber(data);
if (pr) {
try {
const response = await fetchPullRequest(pr);
if (!response.body) {
console.log(data);
return;
}
const newMessage = filterMsg(response);
console.log(newMessage);
return;
} catch (e) {
console.log(data);
return;
}
}
console.log(data);
}
main();