Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ocaml): Improve OCaml releaser for monorepos #867

Merged
merged 3 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions __snapshots__/dune-project.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
exports['DuneProject updateContent updates version in dune-project file 1'] = `
(lang dune 2.0)
(name sample)
(version 0.6.0)
`
50 changes: 0 additions & 50 deletions __snapshots__/ocaml.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,6 @@ filename: CHANGELOG.md
* **deps:** update dependency com.google.cloud:google-cloud-spanner to v1.50.0 ([1f9663c](https://www.github.com/phated/ocaml-sample-repo/commit/1f9663cf08ab1cf3b68d95dee4dc99b7c4aac373))
* **deps:** update dependency com.google.cloud:google-cloud-storage to v1.120.0 ([fcd1c89](https://www.github.com/phated/ocaml-sample-repo/commit/fcd1c890dc1526f4d62ceedad561f498195c8939))


### Miscellaneous Chores

* update common templates ([3006009](https://www.github.com/phated/ocaml-sample-repo/commit/3006009a2b1b2cb4bd5108c0f469c410759f3a6a))

`

exports['OCaml run creates a release PR for non-monorepo (esy.json + sample.opam): options'] = `
Expand All @@ -83,11 +78,6 @@ description: :robot: I have created a release \\*beep\\* \\*boop\\*

* **deps:** update dependency com.google.cloud:google-cloud-spanner to v1.50.0 ([1f9663c](https://www.github.com/phated/ocaml-sample-repo/commit/1f9663cf08ab1cf3b68d95dee4dc99b7c4aac373))
* **deps:** update dependency com.google.cloud:google-cloud-storage to v1.120.0 ([fcd1c89](https://www.github.com/phated/ocaml-sample-repo/commit/fcd1c890dc1526f4d62ceedad561f498195c8939))


### Miscellaneous Chores

* update common templates ([3006009](https://www.github.com/phated/ocaml-sample-repo/commit/3006009a2b1b2cb4bd5108c0f469c410759f3a6a))
---


Expand Down Expand Up @@ -142,11 +132,6 @@ filename: CHANGELOG.md
* **deps:** update dependency com.google.cloud:google-cloud-spanner to v1.50.0 ([1f9663c](https://www.github.com/phated/ocaml-sample-repo/commit/1f9663cf08ab1cf3b68d95dee4dc99b7c4aac373))
* **deps:** update dependency com.google.cloud:google-cloud-storage to v1.120.0 ([fcd1c89](https://www.github.com/phated/ocaml-sample-repo/commit/fcd1c890dc1526f4d62ceedad561f498195c8939))


### Miscellaneous Chores

* update common templates ([3006009](https://www.github.com/phated/ocaml-sample-repo/commit/3006009a2b1b2cb4bd5108c0f469c410759f3a6a))

`

exports['OCaml run creates a release PR for non-monorepo (esy.json): options'] = `
Expand All @@ -164,11 +149,6 @@ description: :robot: I have created a release \\*beep\\* \\*boop\\*

* **deps:** update dependency com.google.cloud:google-cloud-spanner to v1.50.0 ([1f9663c](https://www.github.com/phated/ocaml-sample-repo/commit/1f9663cf08ab1cf3b68d95dee4dc99b7c4aac373))
* **deps:** update dependency com.google.cloud:google-cloud-storage to v1.120.0 ([fcd1c89](https://www.github.com/phated/ocaml-sample-repo/commit/fcd1c890dc1526f4d62ceedad561f498195c8939))


### Miscellaneous Chores

* update common templates ([3006009](https://www.github.com/phated/ocaml-sample-repo/commit/3006009a2b1b2cb4bd5108c0f469c410759f3a6a))
---


Expand Down Expand Up @@ -223,11 +203,6 @@ filename: CHANGELOG.md
* **deps:** update dependency com.google.cloud:google-cloud-spanner to v1.50.0 ([1f9663c](https://www.github.com/phated/ocaml-sample-repo/commit/1f9663cf08ab1cf3b68d95dee4dc99b7c4aac373))
* **deps:** update dependency com.google.cloud:google-cloud-storage to v1.120.0 ([fcd1c89](https://www.github.com/phated/ocaml-sample-repo/commit/fcd1c890dc1526f4d62ceedad561f498195c8939))


### Miscellaneous Chores

* update common templates ([3006009](https://www.github.com/phated/ocaml-sample-repo/commit/3006009a2b1b2cb4bd5108c0f469c410759f3a6a))

`

exports['OCaml run creates a release PR for non-monorepo (package.json): options'] = `
Expand All @@ -245,11 +220,6 @@ description: :robot: I have created a release \\*beep\\* \\*boop\\*

* **deps:** update dependency com.google.cloud:google-cloud-spanner to v1.50.0 ([1f9663c](https://www.github.com/phated/ocaml-sample-repo/commit/1f9663cf08ab1cf3b68d95dee4dc99b7c4aac373))
* **deps:** update dependency com.google.cloud:google-cloud-storage to v1.120.0 ([fcd1c89](https://www.github.com/phated/ocaml-sample-repo/commit/fcd1c890dc1526f4d62ceedad561f498195c8939))


### Miscellaneous Chores

* update common templates ([3006009](https://www.github.com/phated/ocaml-sample-repo/commit/3006009a2b1b2cb4bd5108c0f469c410759f3a6a))
---


Expand Down Expand Up @@ -292,11 +262,6 @@ filename: CHANGELOG.md
* **deps:** update dependency com.google.cloud:google-cloud-spanner to v1.50.0 ([1f9663c](https://www.github.com/phated/ocaml-sample-repo/commit/1f9663cf08ab1cf3b68d95dee4dc99b7c4aac373))
* **deps:** update dependency com.google.cloud:google-cloud-storage to v1.120.0 ([fcd1c89](https://www.github.com/phated/ocaml-sample-repo/commit/fcd1c890dc1526f4d62ceedad561f498195c8939))


### Miscellaneous Chores

* update common templates ([3006009](https://www.github.com/phated/ocaml-sample-repo/commit/3006009a2b1b2cb4bd5108c0f469c410759f3a6a))

`

exports['OCaml run creates a release PR for non-monorepo (sample.opam): options'] = `
Expand All @@ -314,11 +279,6 @@ description: :robot: I have created a release \\*beep\\* \\*boop\\*

* **deps:** update dependency com.google.cloud:google-cloud-spanner to v1.50.0 ([1f9663c](https://www.github.com/phated/ocaml-sample-repo/commit/1f9663cf08ab1cf3b68d95dee4dc99b7c4aac373))
* **deps:** update dependency com.google.cloud:google-cloud-storage to v1.120.0 ([fcd1c89](https://www.github.com/phated/ocaml-sample-repo/commit/fcd1c890dc1526f4d62ceedad561f498195c8939))


### Miscellaneous Chores

* update common templates ([3006009](https://www.github.com/phated/ocaml-sample-repo/commit/3006009a2b1b2cb4bd5108c0f469c410759f3a6a))
---


Expand All @@ -342,11 +302,6 @@ filename: CHANGELOG.md
* **deps:** update dependency com.google.cloud:google-cloud-spanner to v1.50.0 ([1f9663c](https://www.github.com/phated/ocaml-sample-repo/commit/1f9663cf08ab1cf3b68d95dee4dc99b7c4aac373))
* **deps:** update dependency com.google.cloud:google-cloud-storage to v1.120.0 ([fcd1c89](https://www.github.com/phated/ocaml-sample-repo/commit/fcd1c890dc1526f4d62ceedad561f498195c8939))


### Miscellaneous Chores

* update common templates ([3006009](https://www.github.com/phated/ocaml-sample-repo/commit/3006009a2b1b2cb4bd5108c0f469c410759f3a6a))

`

exports['OCaml run skips JSON files that don\'t contain a "version" field: options'] = `
Expand All @@ -364,11 +319,6 @@ description: :robot: I have created a release \\*beep\\* \\*boop\\*

* **deps:** update dependency com.google.cloud:google-cloud-spanner to v1.50.0 ([1f9663c](https://www.github.com/phated/ocaml-sample-repo/commit/1f9663cf08ab1cf3b68d95dee4dc99b7c4aac373))
* **deps:** update dependency com.google.cloud:google-cloud-storage to v1.120.0 ([fcd1c89](https://www.github.com/phated/ocaml-sample-repo/commit/fcd1c890dc1526f4d62ceedad561f498195c8939))


### Miscellaneous Chores

* update common templates ([3006009](https://www.github.com/phated/ocaml-sample-repo/commit/3006009a2b1b2cb4bd5108c0f469c410759f3a6a))
---


Expand Down
34 changes: 12 additions & 22 deletions src/releasers/ocaml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,38 +21,19 @@ import {Changelog} from '../updaters/changelog';
// OCaml
import {Opam} from '../updaters/ocaml/opam';
import {EsyJson} from '../updaters/ocaml/esy-json';
import {ReleasePRConstructorOptions} from '..';
import {DuneProject} from '../updaters/ocaml/dune-project';

const notEsyLock = (path: string) => !path.startsWith('esy.lock');

const CHANGELOG_SECTIONS = [
{type: 'feat', section: 'Features'},
{type: 'fix', section: 'Bug Fixes'},
{type: 'perf', section: 'Performance Improvements'},
{type: 'revert', section: 'Reverts'},
{type: 'docs', section: 'Documentation'},
{type: 'chore', section: 'Miscellaneous Chores'},
{type: 'refactor', section: 'Code Refactoring'},
{type: 'test', section: 'Tests'},
{type: 'build', section: 'Build System'},
{type: 'ci', section: 'Continuous Integration'},
];

export class OCaml extends ReleasePR {
constructor(options: ReleasePRConstructorOptions) {
super(options);
// FIXME: this was previously hard-coded, do allow overriding?
this.changelogSections = CHANGELOG_SECTIONS;
}

protected async buildUpdates(
changelogEntry: string,
candidate: ReleaseCandidate,
packageName: PackageName
): Promise<Update[]> {
const updates: Update[] = [];

const jsonPaths = await this.gh.findFilesByExtension('json');
const jsonPaths = await this.gh.findFilesByExtension('json', this.path);
for (const path of jsonPaths) {
if (notEsyLock(path)) {
const contents: GitHubFileContents = await this.gh.getFileContents(
Expand All @@ -73,7 +54,7 @@ export class OCaml extends ReleasePR {
}
}

const opamPaths = await this.gh.findFilesByExtension('opam');
const opamPaths = await this.gh.findFilesByExtension('opam', this.path);
opamPaths.filter(notEsyLock).forEach(path => {
updates.push(
new Opam({
Expand All @@ -85,6 +66,15 @@ export class OCaml extends ReleasePR {
);
});

updates.push(
new DuneProject({
path: this.addPath('dune-project'),
changelogEntry,
version: candidate.version,
packageName: packageName.name,
})
);

updates.push(
new Changelog({
path: this.addPath(this.changelogPath),
Expand Down
48 changes: 48 additions & 0 deletions src/updaters/ocaml/dune-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// Copyright 2021 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 {checkpoint, CheckpointType} from '../../util/checkpoint';
import {Update, UpdateOptions, VersionsMap} from '../update';
import {GitHubFileContents} from '../../github';

export class DuneProject implements Update {
path: string;
changelogEntry: string;
version: string;
versions?: VersionsMap;
packageName: string;
create: boolean;
contents?: GitHubFileContents;

constructor(options: UpdateOptions) {
this.create = false;
this.path = options.path;
this.changelogEntry = options.changelogEntry;
this.version = options.version;
this.packageName = options.packageName;
}
updateContent(content: string): string {
const oldVersion = content.match(/^\(version ([A-Za-z0-9_\-+.~]+)\)$/m);
if (oldVersion) {
checkpoint(
`updating ${this.path} from ${oldVersion[1]} to ${this.version}`,
CheckpointType.Success
);
}
return content.replace(
/^\(version ([A-Za-z0-9_\-+.~]+)\)$/m,
`(version ${this.version})`
);
}
}
40 changes: 40 additions & 0 deletions test/updaters/dune-project.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 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 {readFileSync} from 'fs';
import {resolve} from 'path';
import * as snapshot from 'snap-shot-it';
import {describe, it} from 'mocha';
import {DuneProject} from '../../src/updaters/ocaml/dune-project';

const fixturesPath = './test/updaters/fixtures';

describe('DuneProject', () => {
describe('updateContent', () => {
it('updates version in dune-project file', async () => {
const oldContent = readFileSync(
resolve(fixturesPath, './dune-project'),
'utf8'
).replace(/\r\n/g, '\n');
const version = new DuneProject({
path: 'dune-project',
changelogEntry: '',
version: '0.6.0',
packageName: '',
});
const newContent = version.updateContent(oldContent);
snapshot(newContent);
});
});
});
3 changes: 3 additions & 0 deletions test/updaters/fixtures/dune-project
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
(lang dune 2.0)
(name sample)
(version 0.5.0)