Skip to content

Commit

Permalink
fix: changes are commited when not wanted
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed May 26, 2022
1 parent 460f504 commit 313e864
Show file tree
Hide file tree
Showing 3 changed files with 126 additions and 31 deletions.
31 changes: 31 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function getCurrentVersionCsproj(csprojDocument) {
return undefined;
}

// eslint-disable-next-line sonarjs/cognitive-complexity
function getNewProjectContentCsproj(newVersion, csprojDocument) {
const doc = new DOMParser().parseFromString(
csprojDocument,
Expand All @@ -64,6 +65,7 @@ function getNewProjectContentCsproj(newVersion, csprojDocument) {
});
if (propertyGroupNodes.length === 1) {
const propertyGroupNode = propertyGroupNodes[0];

const versionNodes = Object.values(propertyGroupNode.childNodes).filter((node) => {
return node.nodeName === 'Version';
});
Expand All @@ -77,6 +79,30 @@ function getNewProjectContentCsproj(newVersion, csprojDocument) {
);
propertyGroupNode.appendChild(versionNode);
}

const packageVersionNodes = Object.values(propertyGroupNode.childNodes).filter((node) => {
return node.nodeName === 'PackageVersion';
});
if (packageVersionNodes.length === 1) {
const versionNode = packageVersionNodes[0];
versionNode.childNodes.item(0).data = newVersion;
}

const assemblyVersionNodes = Object.values(propertyGroupNode.childNodes).filter((node) => {
return node.nodeName === 'AssemblyVersion';
});
if (assemblyVersionNodes.length === 1) {
const versionNode = assemblyVersionNodes[0];
versionNode.childNodes.item(0).data = `${newVersion}.0`;
}

const fileVersionNodes = Object.values(propertyGroupNode.childNodes).filter((node) => {
return node.nodeName === 'FileVersion';
});
if (fileVersionNodes.length === 1) {
const versionNode = fileVersionNodes[0];
versionNode.childNodes.item(0).data = `${newVersion}.0`;
}
}
}
return new XMLSerializer().serializeToString(rootNode);
Expand Down Expand Up @@ -294,6 +320,11 @@ async function commitChanges(newVersion, skipCommit, skipTag, skipPush, commitMe
// to support "actions/checkout@v1"
if (!skipCommit) {
await runInWorkspace('git', ['commit', '-a', '-m', commitMessageToUse.replace(/{{version}}/g, newVersion)]);
} else {
console.warn(
'Skipping commit'
);
return;
}
} catch (e) {
console.warn(
Expand Down
65 changes: 65 additions & 0 deletions test/__snapshots__/utils.spec.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,68 @@ using System.Runtime.InteropServices;
[assembly: AssemblyVersion(\\"3.2.0.1\\")]
"
`;

exports[`Utils getNewProjectContentCsproj should return accurate version change for multiple version properties 1`] = `
"<Project Sdk=\\"Microsoft.NET.Sdk\\">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;netstandard2.1;net461</TargetFrameworks>
<RootNamespace>Asyncapi.Nats.Client</RootNamespace>
<Version>12.0.45</Version>
<PackageVersion>12.0.45</PackageVersion>
<AssemblyVersion>12.0.45.0</AssemblyVersion>
<FileVersion>12.0.45.0</FileVersion>
<RepositoryUrl>https://github.com/GamingAPI/rust-csharp-game-api.git</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<None Remove=\\"NATS.Client\\"/>
<None Remove=\\"System.Text.Json\\"/>
<None Remove=\\"Microsoft.CSharp\\"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include=\\"NATS.Client\\" Version=\\"0.12.0\\"/>
<PackageReference Include=\\"System.Text.Json\\" Version=\\"5.0.2\\"/>
<PackageReference Include=\\"Microsoft.CSharp\\" Version=\\"4.7.0\\"/>
</ItemGroup>
</Project>"
`;
exports[`Utils getNewProjectContentCsproj should return correctly when no version present 1`] = `
"<Project Sdk=\\"Microsoft.NET.Sdk\\">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Asyncapi.Nats.Client</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.1</Version></PropertyGroup>
<ItemGroup>
<None Remove=\\"NATS.Client\\"/>
<None Remove=\\"System.Text.Json\\"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include=\\"NATS.Client\\" Version=\\"0.12.0\\"/>
<PackageReference Include=\\"System.Text.Json\\" Version=\\"5.0.2\\"/>
</ItemGroup>
</Project>"
`;
exports[`Utils getNewProjectContentCsproj should return version 1`] = `
"<Project Sdk=\\"Microsoft.NET.Sdk\\">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Asyncapi.Nats.Client</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.1</Version>
</PropertyGroup>
<ItemGroup>
<None Remove=\\"NATS.Client\\"/>
<None Remove=\\"System.Text.Json\\"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include=\\"NATS.Client\\" Version=\\"0.12.0\\"/>
<PackageReference Include=\\"System.Text.Json\\" Version=\\"5.0.2\\"/>
</ItemGroup>
</Project>"
`;
61 changes: 30 additions & 31 deletions test/utils.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ describe('Utils', () => {
expect(newVersion).toEqual('0.0.1-pre2.0');
});
});

describe('analyseVersionChange', () => {
test('figure out to bump major version', () => {
const {doMajorVersion, doMinorVersion, doPatchVersion, doPreReleaseVersion} = analyseVersionChange('feat!', '', '', '', ['feat!: change request']);
Expand Down Expand Up @@ -98,7 +99,7 @@ describe('Utils', () => {
const version = getCurrentVersionCsproj(csproj);
expect(version).toEqual('1.0.0');
});

test('should return version 2', () => {
const csproj = `
<Project Sdk="Microsoft.NET.Sdk">
Expand Down Expand Up @@ -152,31 +153,42 @@ describe('Utils', () => {
});

describe('getNewProjectContentCsproj', () => {
test('should return version', () => {
const csproj = `<Project Sdk="Microsoft.NET.Sdk">
test('should return accurate version change for multiple version properties', () => {
const csproj = `
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<TargetFrameworks>netstandard2.0;netstandard2.1;net461</TargetFrameworks>
<RootNamespace>Asyncapi.Nats.Client</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.0</Version>
<Version>0.1.0</Version>
<PackageVersion>0.1.0</PackageVersion>
<AssemblyVersion>0.1.0.0</AssemblyVersion>
<FileVersion>0.1.0.0</FileVersion>
<RepositoryUrl>https://github.com/GamingAPI/rust-csharp-game-api.git</RepositoryUrl>
</PropertyGroup>
<ItemGroup>
<None Remove="NATS.Client"/>
<None Remove="System.Text.Json"/>
<None Remove="NATS.Client" />
<None Remove="System.Text.Json" />
<None Remove="Microsoft.CSharp" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="NATS.Client" Version="0.12.0"/>
<PackageReference Include="System.Text.Json" Version="5.0.2"/>
<PackageReference Include="NATS.Client" Version="0.12.0" />
<PackageReference Include="System.Text.Json" Version="5.0.2" />
<PackageReference Include="Microsoft.CSharp" Version="4.7.0" />
</ItemGroup>
</Project>`;
const content = getNewProjectContentCsproj('1.0.1', csproj);
expect(content).toEqual(`<Project Sdk="Microsoft.NET.Sdk">
</Project>
`;
const newProjectFile = getNewProjectContentCsproj('12.0.45', csproj);
expect(newProjectFile).toMatchSnapshot();
});
test('should return version', () => {
const csproj = `<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Asyncapi.Nats.Client</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.1</Version>
<Version>1.0.0</Version>
</PropertyGroup>
<ItemGroup>
Expand All @@ -187,7 +199,9 @@ describe('Utils', () => {
<PackageReference Include="NATS.Client" Version="0.12.0"/>
<PackageReference Include="System.Text.Json" Version="5.0.2"/>
</ItemGroup>
</Project>`);
</Project>`;
const content = getNewProjectContentCsproj('1.0.1', csproj);
expect(content).toMatchSnapshot();
});
test('should return correctly when no version present', () => {
const csproj = `<Project Sdk="Microsoft.NET.Sdk">
Expand All @@ -208,22 +222,7 @@ describe('Utils', () => {
</Project>
`;
const content = getNewProjectContentCsproj('1.0.1', csproj);
expect(content).toEqual(`<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
<RootNamespace>Asyncapi.Nats.Client</RootNamespace>
<GeneratePackageOnBuild>false</GeneratePackageOnBuild>
<Version>1.0.1</Version></PropertyGroup>
<ItemGroup>
<None Remove="NATS.Client"/>
<None Remove="System.Text.Json"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="NATS.Client" Version="0.12.0"/>
<PackageReference Include="System.Text.Json" Version="5.0.2"/>
</ItemGroup>
</Project>`);
expect(content).toMatchSnapshot();
});
});

Expand Down

0 comments on commit 313e864

Please sign in to comment.