diff --git a/.github/workflow-scripts/__tests__/generateChangelog-test.js b/.github/workflow-scripts/__tests__/generateChangelog-test.js index 952c6be0241..346aa06cc70 100644 --- a/.github/workflow-scripts/__tests__/generateChangelog-test.js +++ b/.github/workflow-scripts/__tests__/generateChangelog-test.js @@ -7,11 +7,13 @@ * @format */ -const { generateChangelog, +const { + generateChangelog, _computePreviousVersionFrom, _generateChangelog, _pushCommit, - _createPR } = require('../generateChangelog'); + _createPR, +} = require('../generateChangelog'); const silence = () => {}; const mockGetNpmPackageInfo = jest.fn(); @@ -40,27 +42,29 @@ describe('Generate Changelog', () => { const receivedVersion = await _computePreviousVersionFrom(currentVersion); expect(receivedVersion).toEqual(expectedVersion); - }) + }); - it('returns previous rc version when rc is > 1', async () => { + it('returns previous rc version when rc is > 1', async () => { const currentVersion = '0.78.0-rc.5'; const expectedVersion = '0.78.0-rc.4'; const receivedVersion = await _computePreviousVersionFrom(currentVersion); expect(receivedVersion).toEqual(expectedVersion); - }) + }); it('returns previous patch version when rc is 0', async () => { const currentVersion = '0.78.0-rc.0'; const expectedVersion = '0.77.1'; - mockGetNpmPackageInfo.mockReturnValueOnce(Promise.resolve({version: '0.77.1'})); + mockGetNpmPackageInfo.mockReturnValueOnce( + Promise.resolve({version: '0.77.1'}), + ); const receivedVersion = await _computePreviousVersionFrom(currentVersion); expect(receivedVersion).toEqual(expectedVersion); - }) + }); it('returns patch 0 when patch is 1', async () => { const currentVersion = '0.78.1'; @@ -88,10 +92,12 @@ describe('Generate Changelog', () => { expect(receivedVersion).toBeNull(); }); - it('throws an error when the version can\'t be parsed', async () => { + it("throws an error when the version can't be parsed", async () => { const currentVersion = '0.78.0-rc0'; - await expect(_computePreviousVersionFrom(currentVersion)).rejects.toThrow(); + await expect( + _computePreviousVersionFrom(currentVersion), + ).rejects.toThrow(); }); }); @@ -121,7 +127,10 @@ describe('Generate Changelog', () => { expect(mockRun).toHaveBeenNthCalledWith(1, 'git checkout main'); expect(mockRun).toHaveBeenNthCalledWith(2, 'git fetch'); expect(mockRun).toHaveBeenNthCalledWith(3, 'git pull origin main'); - expect(mockRun).toHaveBeenNthCalledWith(4, `npx ${expectedCommandArgs.join(' ')}`); + expect(mockRun).toHaveBeenNthCalledWith( + 4, + `npx ${expectedCommandArgs.join(' ')}`, + ); }); }); @@ -132,10 +141,19 @@ describe('Generate Changelog', () => { _pushCommit(currentVersion); expect(mockRun).toHaveBeenCalledTimes(4); - expect(mockRun).toHaveBeenNthCalledWith(1, `git checkout -b changelog/v${currentVersion}`); + expect(mockRun).toHaveBeenNthCalledWith( + 1, + `git checkout -b changelog/v${currentVersion}`, + ); expect(mockRun).toHaveBeenNthCalledWith(2, 'git add CHANGELOG.md'); - expect(mockRun).toHaveBeenNthCalledWith(3, `git commit -m "[RN][Changelog] Add changelog for v${currentVersion}"`); - expect(mockRun).toHaveBeenNthCalledWith(4, `git push origin changelog/v${currentVersion}`); + expect(mockRun).toHaveBeenNthCalledWith( + 3, + `git commit -m "[RN][Changelog] Add changelog for v${currentVersion}"`, + ); + expect(mockRun).toHaveBeenNthCalledWith( + 4, + `git push origin changelog/v${currentVersion}`, + ); }); }); @@ -147,10 +165,10 @@ describe('Generate Changelog', () => { mockFetch.mockReturnValueOnce(Promise.resolve({status: 401})); const headers = { - 'Accept': 'Accept: application/vnd.github+json', + Accept: 'Accept: application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28', Authorization: `Bearer ${token}`, - } + }; const content = ` ## Summary @@ -160,7 +178,7 @@ Add Changelog for ${currentVersion} [Internal] - Add Changelog for ${currentVersion} ## Test Plan: -N/A` +N/A`; const body = { title: `[RN][Changelog] Add changelog for v${currentVersion}`, @@ -178,25 +196,26 @@ N/A` method: 'POST', headers: headers, body: JSON.stringify(body), - } + }, ); }); it('Returns the pr url', async () => { const currentVersion = '0.79.0-rc5'; const token = 'token'; - const expectedPrURL = 'https://github.com/facebook/react-native/pulls/1234'; + const expectedPrURL = + 'https://github.com/facebook/react-native/pulls/1234'; const returnedObject = { status: 201, json: () => Promise.resolve({html_url: expectedPrURL}), - } + }; mockFetch.mockReturnValueOnce(Promise.resolve(returnedObject)); const headers = { - 'Accept': 'Accept: application/vnd.github+json', + Accept: 'Accept: application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28', Authorization: `Bearer ${token}`, - } + }; const content = ` ## Summary @@ -206,7 +225,7 @@ Add Changelog for ${currentVersion} [Internal] - Add Changelog for ${currentVersion} ## Test Plan: -N/A` +N/A`; const body = { title: `[RN][Changelog] Add changelog for v${currentVersion}`, @@ -215,7 +234,7 @@ N/A` body: content, }; - const receivedPrURL = await _createPR(currentVersion, token) + const receivedPrURL = await _createPR(currentVersion, token); expect(mockFetch).toHaveBeenCalledTimes(1); expect(mockFetch).toHaveBeenCalledWith( @@ -224,7 +243,7 @@ N/A` method: 'POST', headers: headers, body: JSON.stringify(body), - } + }, ); expect(receivedPrURL).toEqual(expectedPrURL); }); diff --git a/.github/workflow-scripts/generateChangelog.js b/.github/workflow-scripts/generateChangelog.js index eb170fdce00..fc7a9c1794d 100644 --- a/.github/workflow-scripts/generateChangelog.js +++ b/.github/workflow-scripts/generateChangelog.js @@ -31,7 +31,9 @@ async function _computePreviousVersionFrom(version) { } else { if (Number(patch) === 0) { // No need to generate the changelog for 0.X.0 as we already generated it from RCs - log(`Skipping changelog generation for ${version} as we already have it from the RCs`); + log( + `Skipping changelog generation for ${version} as we already have it from the RCs`, + ); return null; } return `0.${minor}.${Number(patch) - 1}`; @@ -43,8 +45,7 @@ function _generateChangelog(previousVersion, version, token) { run('git checkout main'); run('git fetch'); run('git pull origin main'); - const generateChangelogComand = - `npx @rnx-kit/rn-changelog-generator --base v${previousVersion} --compare v${version} --repo . --changelog ./CHANGELOG.md --token ${token}`; + const generateChangelogComand = `npx @rnx-kit/rn-changelog-generator --base v${previousVersion} --compare v${version} --repo . --changelog ./CHANGELOG.md --token ${token}`; run(generateChangelogComand); } @@ -67,12 +68,12 @@ Add Changelog for ${version} [Internal] - Add Changelog for ${version} ## Test Plan: -N/A` +N/A`; const response = await fetch(url, { method: 'POST', headers: { - 'Accept': 'Accept: application/vnd.github+json', + Accept: 'Accept: application/vnd.github+json', 'X-GitHub-Api-Version': '2022-11-28', Authorization: `Bearer ${token}`, }, @@ -85,7 +86,9 @@ N/A` }); if (response.status !== 201) { - throw new Error(`Failed to create PR: ${response.status} ${response.statusText}`); + throw new Error( + `Failed to create PR: ${response.status} ${response.statusText}`, + ); } const data = await response.json(); @@ -114,4 +117,4 @@ module.exports = { _generateChangelog, _pushCommit, _createPR, -} +}; diff --git a/.github/workflows/create-changelog.yml b/.github/workflows/generate-changelog.yml similarity index 90% rename from .github/workflows/create-changelog.yml rename to .github/workflows/generate-changelog.yml index 174fdd5ab9c..9734ebd6417 100644 --- a/.github/workflows/create-changelog.yml +++ b/.github/workflows/generate-changelog.yml @@ -1,7 +1,6 @@ name: Generate Changelog on: - pull_request: workflow_call: jobs: @@ -25,5 +24,5 @@ jobs: with: script: | const {generateChangelog} = require('./.github/workflow-scripts/generateChangelog'); - const version = 'v0.79.0-rc.3' //"${{ github.ref_name }}"; + const version = '${{ github.ref_name }}'; await generateChangelog(version, '${{secrets.REACT_NATIVE_BOT_GITHUB_TOKEN}}'); diff --git a/.github/workflows/publish-release.yml b/.github/workflows/publish-release.yml index 3ad9cb32b48..905c183b0dc 100644 --- a/.github/workflows/publish-release.yml +++ b/.github/workflows/publish-release.yml @@ -237,3 +237,8 @@ jobs: const {verifyArtifactsAreOnMaven} = require('./.github/workflow-scripts/verifyArtifactsAreOnMaven.js'); const version = "${{ github.ref_name }}"; await verifyArtifactsAreOnMaven(version); + + create_changelog: + needs: build_npm_package + uses: ./.github/workflows/generate-changelog.yml + secrets: inherit