forked from googleapis/release-please
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add strategy for R packages. Closes googleapis#2151
- Loading branch information
Showing
8 changed files
with
298 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2024 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 { BaseStrategy, BuildUpdatesOptions, BaseStrategyOptions } from './base'; | ||
import { Update } from '../update'; | ||
import { News } from '../updaters/r/news'; | ||
import { Version } from '../version'; | ||
import { DescriptionUpdater } from '../updaters/r/description'; | ||
import { FileNotFoundError } from '../errors'; | ||
import { filterCommits } from '../util/filter-commits'; | ||
|
||
const CHANGELOG_SECTIONS = [ | ||
{ type: 'feat', section: 'Features' }, | ||
{ type: 'fix', section: 'Bug Fixes' }, | ||
{ type: 'perf', section: 'Performance Improvements' }, | ||
{ type: 'deps', section: 'Dependencies' }, | ||
{ type: 'revert', section: 'Reverts' }, | ||
{ type: 'docs', section: 'Documentation' }, | ||
{ type: 'style', section: 'Styles', hidden: true }, | ||
{ type: 'chore', section: 'Miscellaneous Chores', hidden: true }, | ||
{ type: 'refactor', section: 'Code Refactoring', hidden: true }, | ||
{ type: 'test', section: 'Tests', hidden: true }, | ||
{ type: 'build', section: 'Build System', hidden: true }, | ||
{ type: 'ci', section: 'Continuous Integration', hidden: true }, | ||
]; | ||
|
||
export class R extends BaseStrategy { | ||
constructor(options: BaseStrategyOptions) { | ||
options.changelogPath = options.changelogPath ?? 'NEWS.md'; | ||
options.changelogSections = options.changelogSections ?? CHANGELOG_SECTIONS; | ||
super(options); | ||
} | ||
|
||
protected async buildUpdates( | ||
options: BuildUpdatesOptions | ||
): Promise<Update[]> { | ||
const updates: Update[] = []; | ||
const version = options.newVersion; | ||
|
||
updates.push({ | ||
path: this.addPath(this.changelogPath), | ||
createIfMissing: true, | ||
updater: new News({ | ||
version, | ||
changelogEntry: options.changelogEntry, | ||
}), | ||
}); | ||
|
||
updates.push({ | ||
path: this.addPath('DESCRIPTION'), | ||
createIfMissing: false, | ||
updater: new DescriptionUpdater({ | ||
version, | ||
}), | ||
}); | ||
|
||
return updates; | ||
} | ||
|
||
protected async getPackageNameFromDescription(): Promise<string | null> { | ||
try { | ||
const descriptionContent = ( | ||
await this.github.getFileContentsOnBranch( | ||
this.addPath('DESCRIPTION'), | ||
this.targetBranch | ||
) | ||
).parsedContent; | ||
|
||
const match = descriptionContent.match(/^Package:\s*(.+)\s*$/m); | ||
return match ? match[1] : null; | ||
} catch (e) { | ||
if (e instanceof FileNotFoundError) { | ||
return null; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
protected initialReleaseVersion(): Version { | ||
return Version.parse('0.1.0'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright 2024 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 { DefaultUpdater } from '../default'; | ||
|
||
/** | ||
* Updates the DESCRIPTION file of an R package. | ||
*/ | ||
export class DescriptionUpdater extends DefaultUpdater { | ||
/** | ||
* Given initial file contents, return updated contents. | ||
* @param {string} content The initial content | ||
* @returns {string} The updated content | ||
*/ | ||
updateContent(content: string): string { | ||
return content.replace( | ||
/^Version:\s*[0-9]+\.[0-9]+\.[0-9]+(?:\.[0-9]+)?\s*$/m, | ||
`Version: ${this.version}` | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2024 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 { DefaultUpdater, UpdateOptions } from '../default'; | ||
|
||
interface ChangelogOptions extends UpdateOptions { | ||
changelogEntry: string; | ||
versionHeaderRegex?: string; | ||
} | ||
|
||
const DEFAULT_VERSION_HEADER_REGEX = '\n### v?[0-9[]'; | ||
|
||
export class News extends DefaultUpdater { | ||
changelogEntry: string; | ||
readonly versionHeaderRegex: RegExp; | ||
|
||
constructor(options: ChangelogOptions) { | ||
super(options); | ||
this.changelogEntry = options.changelogEntry; | ||
this.versionHeaderRegex = new RegExp( | ||
options.versionHeaderRegex ?? DEFAULT_VERSION_HEADER_REGEX, | ||
's' | ||
); | ||
} | ||
|
||
updateContent(content: string | undefined): string { | ||
content = content || ''; | ||
const lastEntryIndex = content.search(this.versionHeaderRegex); | ||
if (lastEntryIndex === -1) { | ||
if (content) { | ||
return `${this.changelogEntry}\n\n${adjustHeaders( | ||
content | ||
).trim()}\n`; | ||
} else { | ||
return `${this.changelogEntry}\n`; | ||
} | ||
} else { | ||
const before = content.slice(0, lastEntryIndex); | ||
const after = content.slice(lastEntryIndex); | ||
return `${before}\n${this.changelogEntry}\n${after}`.trim() + '\n'; | ||
} | ||
} | ||
} | ||
|
||
// Helper to increase markdown H1 headers to H2 | ||
function adjustHeaders(content: string): string { | ||
return content.replace(/^#(\s)/gm, '##$1'); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import {afterEach, beforeEach, describe, it} from 'mocha'; | ||
import {GitHub} from '../../src'; | ||
import * as sinon from 'sinon'; | ||
import { | ||
assertHasUpdate, | ||
assertHasUpdates, | ||
buildMockConventionalCommit, | ||
} from '../helpers'; | ||
import {News} from '../../src/updaters/r/news'; | ||
import {Generic} from '../../src/updaters/generic'; | ||
import {DescriptionUpdater} from '../../src/updaters/r/description'; | ||
import {R} from '../../src/strategies/r'; | ||
import {TagName} from '../../src/util/tag-name'; | ||
import {Version} from '../../src/version'; | ||
import {expect} from 'chai'; | ||
|
||
const sandbox = sinon.createSandbox(); | ||
|
||
const COMMITS = [ | ||
...buildMockConventionalCommit('fix(deps): update dependency'), | ||
...buildMockConventionalCommit('chore: update common templates'), | ||
]; | ||
|
||
describe('R', () => { | ||
let github: GitHub; | ||
beforeEach(async () => { | ||
github = await GitHub.create({ | ||
owner: 'googleapis', | ||
repo: 'r-test-repo', | ||
defaultBranch: 'main', | ||
}); | ||
}); | ||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
describe('buildReleasePullRequest', () => { | ||
it('updates DESCRIPTION and NEWS.md files', async () => { | ||
const strategy = new R({ | ||
targetBranch: 'main', | ||
github, | ||
changelogPath: 'NEWS.md', | ||
}); | ||
|
||
sandbox | ||
.stub(github, 'findFilesByFilenameAndRef') | ||
.withArgs('DESCRIPTION', 'main') | ||
.resolves(['DESCRIPTION']); | ||
|
||
const release = await strategy.buildReleasePullRequest( | ||
COMMITS, | ||
undefined | ||
); | ||
|
||
expect(release?.version?.toString()).to.eql('0.1.0'); | ||
|
||
const updates = release!.updates; | ||
assertHasUpdate(updates, 'NEWS.md', News); | ||
assertHasUpdates(updates, 'DESCRIPTION', DescriptionUpdater); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
Package: mypackage | ||
Title: What the Package Does (One Line, Title Case) | ||
Version: 0.0.1 | ||
Authors@R: | ||
person("First", "Last", , "first.last@example.com", role = c("aut", "cre"), | ||
comment = c(ORCID = "YOUR-ORCID-ID")) | ||
Description: What the package does (one paragraph). | ||
License: GPL-3 | ||
Encoding: UTF-8 | ||
Roxygen: list(markdown = TRUE) | ||
RoxygenNote: 7.3.2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// Copyright 2024 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 { DescriptionUpdater } from '../../src/updaters/r/description'; | ||
import { Version } from '../../src/version'; | ||
|
||
const fixturesPath = './test/updaters/fixtures'; | ||
|
||
describe('DESCRIPTION', () => { | ||
describe('updateContent', () => { | ||
it('updates version in DESCRIPTION file', async () => { | ||
const oldContent = readFileSync( | ||
resolve(fixturesPath, './r/DESCRIPTION'), | ||
'utf8' | ||
).replace(/\r\n/g, '\n'); | ||
const version = new DescriptionUpdater({ | ||
version: Version.parse('1.2.3'), | ||
}); | ||
const newContent = version.updateContent(oldContent); | ||
snapshot(newContent); | ||
}); | ||
}); | ||
}); |