mirror of
https://github.com/hanzoai/firebase-js-sdk.git
synced 2026-10-30 14:46:17 +00:00
The fix has two parts: 1. Add `"types": ["node"]` to tsconfig. 2. Fix an incorrect type cast in `index.ts`. The parsed GitHub API response was being cast to the DOM `Response` type to access its `.body` property, which is wrong — `Response.body` is a `ReadableStream`, not a string. The actual value is the parsed JSON from the GitHub PR API, whose `body` field is `string | null` per the API schema. Updated the cast accordingly. Resolves #9947
120 lines
3.6 KiB
TypeScript
120 lines
3.6 KiB
TypeScript
/**
|
|
* @license
|
|
* Copyright 2020 Google LLC
|
|
*
|
|
* Licensed 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.
|
|
*/
|
|
|
|
import { ChangelogFunctions } from '@changesets/types';
|
|
import { getInfo } from '@changesets/get-github-info';
|
|
|
|
const changelogFunctions: ChangelogFunctions = {
|
|
getDependencyReleaseLine: async (
|
|
changesets,
|
|
dependenciesUpdated,
|
|
options
|
|
) => {
|
|
if (!options.repo) {
|
|
throw new Error(
|
|
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]'
|
|
);
|
|
}
|
|
if (dependenciesUpdated.length === 0) {
|
|
return '';
|
|
}
|
|
|
|
const changesetLink = `- Updated dependencies [${(
|
|
await Promise.all(
|
|
changesets.map(async cs => {
|
|
if (cs.commit) {
|
|
const { links } = await getInfo({
|
|
repo: options.repo,
|
|
commit: cs.commit
|
|
});
|
|
return links.commit;
|
|
}
|
|
})
|
|
)
|
|
)
|
|
.filter(_ => _)
|
|
.join(', ')}]:`;
|
|
|
|
const updatedDependenciesList = dependenciesUpdated.map(
|
|
dependency => ` - ${dependency.name}@${dependency.newVersion}`
|
|
);
|
|
|
|
return [changesetLink, ...updatedDependenciesList].join('\n');
|
|
},
|
|
getReleaseLine: async (changeset, type, options) => {
|
|
if (!options || !options.repo) {
|
|
throw new Error(
|
|
'Please provide a repo to this changelog generator like this:\n"changelog": ["@changesets/changelog-github", { "repo": "org/repo" }]'
|
|
);
|
|
}
|
|
const [firstLine, ...futureLines] = changeset.summary
|
|
.split('\n')
|
|
.map(l => l.trimRight());
|
|
|
|
if (changeset.commit) {
|
|
const { pull: pullNumber, links } = await getInfo({
|
|
repo: options.repo,
|
|
commit: changeset.commit
|
|
});
|
|
|
|
let fixedIssueLink = null;
|
|
// If the summary didn't mention any issue, we will look at the PR body to try to generate one automatically
|
|
if (!/issues\/[\d+]/i.test(changeset.summary) && pullNumber) {
|
|
fixedIssueLink = await getFixedIssueLink(pullNumber, options.repo);
|
|
}
|
|
|
|
return `\n\n- ${links.commit}${
|
|
links.pull === null ? '' : ` ${links.pull}`
|
|
}${
|
|
fixedIssueLink === null ? '' : ` ${fixedIssueLink}`
|
|
} - ${firstLine}\n${futureLines.map(l => ` ${l}`).join('\n')}`;
|
|
} else {
|
|
return `\n\n- ${firstLine}\n${futureLines.map(l => ` ${l}`).join('\n')}`;
|
|
}
|
|
}
|
|
};
|
|
|
|
const fixedIssueRegex =
|
|
/(close|closes|closed|fix|fixes|fixed|resolve|resolves|resolved) [^\s]*(#|issues\/)([\d]+)/i;
|
|
async function getFixedIssueLink(
|
|
prNumber: number,
|
|
repo: string
|
|
): Promise<string> {
|
|
const response = (await fetch(
|
|
`https://api.github.com/repos/${repo}/pulls/${prNumber}`,
|
|
{
|
|
method: 'GET',
|
|
headers: {
|
|
'Authorization': `Bearer ${process.env.GITHUB_TOKEN}`
|
|
}
|
|
}
|
|
).then(data => data.json())) as { body: string | null };
|
|
|
|
const body = response.body;
|
|
if (!body) {
|
|
return '';
|
|
}
|
|
const match = fixedIssueRegex.exec(body.toString());
|
|
if (!match) {
|
|
return '';
|
|
}
|
|
const issueNumber = match[3];
|
|
return `(fixes [#${issueNumber}](https://github.com/firebase/firebase-js-sdk/issues/${issueNumber}))`;
|
|
}
|
|
|
|
exports.default = changelogFunctions;
|