-
Notifications
You must be signed in to change notification settings - Fork 388
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Abhinav Khanna
authored
Feb 11, 2021
1 parent
869f1a1
commit c9fbf78
Showing
10 changed files
with
282 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,4 @@ system-test/fixtures/gh/* | |
__pycache__ | ||
package-lock.json | ||
debug.sh | ||
.idea/ |
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,13 @@ | ||
exports['ChartYaml updateContent updates version in Chart.yaml 1'] = ` | ||
name: helm-test-repo | ||
version: 1.1.0 | ||
apiVersion: v2 | ||
appVersion: 2.0.0 | ||
dependencies: | ||
- name: another-repo | ||
version: 0.15.3 | ||
repository: linkToHelmChartRepo | ||
maintainers: | ||
- Abhinav Khanna | ||
` |
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,144 @@ | ||
// 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 {ReleasePR, ReleaseCandidate} from '../release-pr'; | ||
|
||
import {ConventionalCommits} from '../conventional-commits'; | ||
import {GitHub, GitHubTag, GitHubFileContents} from '../github'; | ||
import {checkpoint, CheckpointType} from '../util/checkpoint'; | ||
import {Update} from '../updaters/update'; | ||
import {Commit} from '../graphql-to-commits'; | ||
|
||
// Generic | ||
import {Changelog} from '../updaters/changelog'; | ||
import * as yaml from 'js-yaml'; | ||
// helm | ||
import {ChartYaml} from '../updaters/helm/chart-yaml'; | ||
|
||
export class Helm extends ReleasePR { | ||
static releaserName = 'helm'; | ||
|
||
protected async _run(): Promise<number | undefined> { | ||
// Make an effort to populate packageName from the contents of | ||
// the package.json, rather than forcing this to be set: | ||
const contents: GitHubFileContents = await this.gh.getFileContents( | ||
this.addPath('Chart.yaml') | ||
); | ||
const file = yaml.load(contents.parsedContent, {json: true}); | ||
if (file === null || file === undefined) { | ||
return undefined; | ||
} | ||
const pkg = JSON.parse(JSON.stringify(file)); | ||
if (pkg.name) { | ||
this.packageName = pkg.name; | ||
// we've rewritten the package name, recalculate the package prefix | ||
this.packagePrefix = this.coercePackagePrefix(pkg.name); | ||
} | ||
|
||
const latestTag: GitHubTag | undefined = await this.latestTag( | ||
this.monorepoTags ? `${this.packagePrefix}-` : undefined | ||
); | ||
const commits: Commit[] = await this.commits({ | ||
sha: latestTag ? latestTag.sha : undefined, | ||
path: this.path, | ||
}); | ||
|
||
const cc = new ConventionalCommits({ | ||
commits, | ||
githubRepoUrl: this.repoUrl, | ||
bumpMinorPreMajor: this.bumpMinorPreMajor, | ||
changelogSections: this.changelogSections, | ||
}); | ||
const candidate: ReleaseCandidate = await this.coerceReleaseCandidate( | ||
cc, | ||
latestTag | ||
); | ||
const changelogEntry: string = await cc.generateChangelogEntry({ | ||
version: candidate.version, | ||
currentTag: `v${candidate.version}`, | ||
previousTag: candidate.previousTag, | ||
}); | ||
|
||
// don't create a release candidate until user facing changes | ||
// (fix, feat, BREAKING CHANGE) have been made; a CHANGELOG that's | ||
// one line is a good indicator that there were no interesting commits. | ||
if (this.changelogEmpty(changelogEntry)) { | ||
checkpoint( | ||
`no user facing commits found since ${ | ||
latestTag ? latestTag.sha : 'beginning of time' | ||
}`, | ||
CheckpointType.Failure | ||
); | ||
return undefined; | ||
} | ||
|
||
const updates: Update[] = []; | ||
|
||
updates.push( | ||
new Changelog({ | ||
path: this.addPath('CHANGELOG.md'), | ||
changelogEntry, | ||
version: candidate.version, | ||
packageName: this.packageName, | ||
}) | ||
); | ||
|
||
updates.push( | ||
new ChartYaml({ | ||
path: this.addPath('Chart.yaml'), | ||
changelogEntry, | ||
version: candidate.version, | ||
packageName: this.packageName, | ||
contents, | ||
}) | ||
); | ||
|
||
return await this.openPR({ | ||
sha: commits[0].sha!, | ||
changelogEntry: `${changelogEntry}\n---\n`, | ||
updates, | ||
version: candidate.version, | ||
includePackageName: this.monorepoTags, | ||
}); | ||
} | ||
|
||
// A releaser can implement this method to automatically detect | ||
// the release name when creating a GitHub release, for instance by returning | ||
// name in package.json, or setup.py. | ||
static async lookupPackageName( | ||
gh: GitHub, | ||
path?: string | ||
): Promise<string | undefined> { | ||
// Make an effort to populate packageName from the contents of | ||
// the package.json, rather than forcing this to be set: | ||
const contents: GitHubFileContents = await gh.getFileContents( | ||
this.addPathStatic('Chart.yaml', path) | ||
); | ||
const file = yaml.load(contents.parsedContent, {json: true}); | ||
if (file === null || file === undefined) { | ||
return undefined; | ||
} | ||
const pkg = JSON.parse(JSON.stringify(file)); | ||
if (pkg.name) return pkg.name; | ||
else return undefined; | ||
} | ||
|
||
// Parse the package prefix for releases from the full package name | ||
// The package name usually looks like `@[group]/[library]` | ||
protected coercePackagePrefix(packageName: string): string { | ||
return packageName.match(/^@[\w-]+\//) | ||
? packageName.split('/')[1] | ||
: packageName; | ||
} | ||
} |
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,50 @@ | ||
// 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'; | ||
import * as yaml from 'js-yaml'; | ||
|
||
export class ChartYaml 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 data = yaml.load(content, {json: true}); | ||
if (data === null || data === undefined) { | ||
return ''; | ||
} | ||
const parsed = JSON.parse(JSON.stringify(data)); | ||
checkpoint( | ||
`updating ${this.path} from ${parsed.version} to ${this.version}`, | ||
CheckpointType.Success | ||
); | ||
parsed.version = this.version; | ||
return yaml.dump(parsed); | ||
} | ||
} |
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,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 {ChartYaml} from '../../src/updaters/helm/chart-yaml'; | ||
|
||
const fixturesPath = './test/updaters/fixtures'; | ||
|
||
describe('ChartYaml', () => { | ||
describe('updateContent', () => { | ||
it('updates version in Chart.yaml', async () => { | ||
const oldContent = readFileSync( | ||
resolve(fixturesPath, './helm/Chart.yaml'), | ||
'utf8' | ||
).replace(/\r\n/g, '\n'); | ||
const version = new ChartYaml({ | ||
path: './helm/Chart.yaml', | ||
changelogEntry: '', | ||
version: '1.1.0', | ||
packageName: '', | ||
}); | ||
const newContent = version.updateContent(oldContent); | ||
snapshot(newContent); | ||
}); | ||
}); | ||
}); |
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,24 @@ | ||
# 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 | ||
# | ||
# https://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. | ||
|
||
name: helm-test-repo | ||
version: 1.0.0 | ||
apiVersion: v2 | ||
appVersion: 2.0.0 | ||
dependencies: | ||
- name: another-repo | ||
version: 0.15.3 | ||
repository: "linkToHelmChartRepo" | ||
maintainers: | ||
- Abhinav Khanna |