Skip to content

Commit 3208848

Browse files
authored
fix: merge updates when creating a changeset (#2601)
* fix: merge updates when creating a change set The node-workspace plugin's manifest file update was overwriting the default change made in manifest.ts with no changes. b/444669596
1 parent 2ec56e7 commit 3208848

File tree

2 files changed

+65
-1
lines changed

2 files changed

+65
-1
lines changed

src/github.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import {Logger} from 'code-suggester/build/src/types';
5050
import {HttpsProxyAgent} from 'https-proxy-agent';
5151
import {HttpProxyAgent} from 'http-proxy-agent';
5252
import {PullRequestOverflowHandler} from './util/pull-request-overflow-handler';
53+
import {mergeUpdates} from './updaters/composite';
5354

5455
// Extract some types from the `request` package.
5556
type RequestBuilderType = typeof request;
@@ -1287,8 +1288,12 @@ export class GitHub {
12871288
updates: Update[],
12881289
defaultBranch: string
12891290
): Promise<ChangeSet> {
1291+
// Sometimes multiple updates are proposed for the same file,
1292+
// such as when the manifest file is additionally changed by the
1293+
// node-workspace plugin. We need to merge these updates.
1294+
const mergedUpdates = mergeUpdates(updates);
12901295
const changes = new Map();
1291-
for (const update of updates) {
1296+
for (const update of mergedUpdates) {
12921297
let content: GitHubFileContents | undefined;
12931298
try {
12941299
content = await this.getFileContentsOnBranch(

test/github.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {PullRequestBody} from '../src/util/pull-request-body';
3737
import {PullRequestTitle} from '../src/util/pull-request-title';
3838
import * as codeSuggester from 'code-suggester';
3939
import {RawContent} from '../src/updaters/raw-content';
40+
import {ReleasePleaseManifest} from '../src/updaters/release-please-manifest';
4041
import {HttpsProxyAgent} from 'https-proxy-agent';
4142
import {HttpProxyAgent} from 'http-proxy-agent';
4243
import {Commit} from '../src/commit';
@@ -1062,6 +1063,64 @@ describe('GitHub', () => {
10621063
});
10631064
});
10641065

1066+
describe('buildChangeSet', () => {
1067+
it('should merge updates for the same file', async () => {
1068+
const manifestPath = '.release-please-manifest.json';
1069+
const initialContent = JSON.stringify(
1070+
{
1071+
path1: '1.0.0',
1072+
path2: '2.0.0',
1073+
},
1074+
null,
1075+
2
1076+
);
1077+
sandbox
1078+
.stub(github, 'getFileContentsOnBranch')
1079+
.withArgs(manifestPath, 'main')
1080+
.resolves({
1081+
sha: 'abc123',
1082+
content: Buffer.from(initialContent).toString('base64'),
1083+
parsedContent: initialContent,
1084+
mode: '100644',
1085+
});
1086+
1087+
const updates = [
1088+
{
1089+
path: manifestPath,
1090+
createIfMissing: false,
1091+
updater: new ReleasePleaseManifest({
1092+
version: Version.parse('1.1.0'),
1093+
versionsMap: new Map([['path1', Version.parse('1.1.0')]]),
1094+
}),
1095+
},
1096+
{
1097+
path: manifestPath,
1098+
createIfMissing: false,
1099+
updater: new ReleasePleaseManifest({
1100+
version: Version.parse('1.1.0'),
1101+
versionsMap: new Map(),
1102+
}),
1103+
},
1104+
];
1105+
1106+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
1107+
const changes = await (github as any).buildChangeSet(updates, 'main');
1108+
1109+
expect(changes).to.not.be.undefined;
1110+
expect(changes!.size).to.eql(1);
1111+
const updatedContent = changes!.get(manifestPath)!.content;
1112+
const expectedContent = JSON.stringify(
1113+
{
1114+
path1: '1.1.0',
1115+
path2: '2.0.0',
1116+
},
1117+
null,
1118+
2
1119+
);
1120+
expect(updatedContent).to.eql(expectedContent);
1121+
});
1122+
});
1123+
10651124
describe('createFileOnNewBranch', () => {
10661125
it('forks a new branch if the branch does not exist', async () => {
10671126
req = req

0 commit comments

Comments
 (0)