@samitouri / QOS-React-2 / commits / 8951c5fc9d

[DevTools][BE] Read username using gh in release script (#25270)

* [DevTools][BE] Read username using gh in release script * better regex & fix lint

Tianyu Yao committed Sep 15, 2022 at 18:12 UTC 8951c5fc9dc8445747706605860421eed4b11296
1 file changed +24 -6
scripts/devtools/prepare-release.js
+24 -6
@@ -99,28 +99,46 @@ async function getCommitLog(sha) {
99 let shortLog = '';
100 let formattedLog = '';
101
102 + const hasGh = await hasGithubCLI();
103 const rawLog = await execRead(`
104 git log --topo-order --pretty=format:'%s' ${sha}...HEAD -- packages/react-devtools*
105 `);
105 - rawLog.split('\n').forEach(line => {
106 - line = line.replace('[DevTools] ', '');
107 -
106 + const lines = rawLog.split('\n');
107 + for (let i = 0; i < lines.length; i++) {
108 + const line = lines[i].replace(/^\[devtools\] */i, '');
109 const match = line.match(/(.+) \(#([0-9]+)\)/);
110 if (match !== null) {
111 const title = match[1];
112 const pr = match[2];
112 -
113 - formattedLog += `\n* ${title} ([USERNAME](https://github.com/USERNAME) in [#${pr}](${PULL_REQUEST_BASE_URL}${pr}))`;
113 + let username;
114 + if (hasGh) {
115 + const response = await execRead(
116 + `gh api /repos/facebook/react/pulls/${pr}`
117 + );
118 + const {user} = JSON.parse(response);
119 + username = `[${user.login}](${user.html_url})`;
120 + } else {
121 + username = '[USERNAME](https://github.com/USERNAME)';
122 + }
123 + formattedLog += `\n* ${title} (${username} in [#${pr}](${PULL_REQUEST_BASE_URL}${pr}))`;
124 shortLog += `\n* ${title}`;
125 } else {
126 formattedLog += `\n* ${line}`;
127 shortLog += `\n* ${line}`;
128 }
119 - });
129 + }
130
131 return [shortLog, formattedLog];
132 }
133
134 +async function hasGithubCLI() {
135 + try {
136 + await exec('which gh');
137 + return true;
138 + } catch (_) {}
139 + return false;
140 +}
141 +
142 async function getPreviousCommitSha() {
143 const choices = [];
144