Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,400 changes: 2,350 additions & 50 deletions tools/releaser/package-lock.json

Large diffs are not rendered by default.

12 changes: 10 additions & 2 deletions tools/releaser/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@
"bin": {
"plugin-releaser": "./dist/index.js"
},
"engines": {
"node": "^20.19.0 || >=22.12.0"
},
"scripts": {
"build": "tsc",
"start": "tsc && node dist/index.js",
"dev": "ts-node src/index.ts"
"dev": "ts-node src/index.ts",
"test": "vitest run",
"test:watch": "vitest",
Comment thread
skools-here marked this conversation as resolved.
"test:coverage": "vitest run --coverage"
},
"dependencies": {
"@octokit/rest": "^22.0.0",
Expand All @@ -26,7 +32,9 @@
"@types/inquirer": "^8.2.10",
"@types/node": "^20.11.16",
"@types/semver": "^7.5.6",
"@vitest/coverage-v8": "^3.0.5",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
"typescript": "^5.3.3",
"vitest": "^3.0.5"
Comment thread
skools-here marked this conversation as resolved.
}
}
129 changes: 129 additions & 0 deletions tools/releaser/src/utils/artifacthub.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import * as crypto from 'crypto';
import * as yaml from 'js-yaml';
import {
hasArtifactHubFile,
getArtifactHubPath,
readArtifactHubConfig,
updateArtifactHubConfig,
createArtifactHubTemplate
} from './artifacthub.js';
import * as pluginUtils from './plugin.js';
import * as githubUtils from './github.js';
Comment thread
skools-here marked this conversation as resolved.

vi.mock('fs');
vi.mock('crypto', () => {
const mock = {
createHash: vi.fn(),
};
return {
...mock,
default: mock,
};
});
vi.mock('js-yaml', () => ({
load: vi.fn(),
dump: vi.fn(),
}));
vi.mock('./plugin.js');
vi.mock('./github.js');

describe('artifacthub utilities', () => {
const mockPluginPath = '/mock/plugin/path';

beforeEach(() => {
vi.clearAllMocks();
vi.mocked(pluginUtils.getPluginPath).mockReturnValue(mockPluginPath);
vi.mocked(pluginUtils.getPluginInfo).mockReturnValue({ name: 'Mock Plugin', version: '1.2.3' });
vi.mocked(githubUtils.getOwnerAndRepo).mockReturnValue({ owner: 'owner', repo: 'repo' });
});

describe('hasArtifactHubFile', () => {
it('should return true if file exists', () => {
vi.mocked(fs.existsSync).mockReturnValue(true);
expect(hasArtifactHubFile('my-plugin')).toBe(true);
expect(fs.existsSync).toHaveBeenCalledWith(path.join(mockPluginPath, 'artifacthub-pkg.yml'));
});
});

describe('getArtifactHubPath', () => {
it('should return correct path', () => {
expect(getArtifactHubPath('my-plugin')).toBe(path.join(mockPluginPath, 'artifacthub-pkg.yml'));
});
});

describe('readArtifactHubConfig', () => {
it('should read and parse yaml content', () => {
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(fs.readFileSync).mockReturnValue('yaml content');
vi.mocked(yaml.load).mockReturnValue({ version: '1.0.0' });

const config = readArtifactHubConfig('my-plugin');
expect(config).toEqual({ version: '1.0.0' });
expect(yaml.load).toHaveBeenCalledWith('yaml content');
});

it('should return null if file missing', () => {
vi.mocked(fs.existsSync).mockReturnValue(false);
expect(readArtifactHubConfig('my-plugin')).toBeNull();
});
});

describe('updateArtifactHubConfig', () => {
it('should create new config if none exists', () => {
vi.mocked(fs.existsSync).mockReturnValue(false);
vi.mocked(fs.readFileSync).mockReturnValue(Buffer.from('mock buffer'));
const mockHash = { update: vi.fn().mockReturnThis(), digest: vi.fn().mockReturnValue('checksum123') };
vi.mocked(crypto.createHash).mockReturnValue(mockHash as any);
vi.mocked(yaml.dump).mockReturnValue('new yaml content');

updateArtifactHubConfig('my-plugin', '1.2.3', '/path/to/tarball.tgz');

expect(fs.writeFileSync).toHaveBeenCalledWith(expect.anything(), 'new yaml content');
expect(yaml.dump).toHaveBeenCalledWith(expect.objectContaining({
version: '1.2.3',
name: 'headlamp_my_plugin'
}), expect.anything());
});

it('should update existing config if it exists', () => {
vi.mocked(fs.existsSync).mockReturnValue(true);
vi.mocked(fs.readFileSync).mockReturnValue('existing content');
vi.mocked(yaml.load).mockReturnValue({
version: '1.0.0',
annotations: { 'headlamp/plugin/archive-url': 'old' }
});
vi.mocked(yaml.dump).mockReturnValue('updated yaml content');

const mockHash = { update: vi.fn().mockReturnThis(), digest: vi.fn().mockReturnValue('checksum456') };
vi.mocked(crypto.createHash).mockReturnValue(mockHash as any);

updateArtifactHubConfig('my-plugin', '1.2.3', '/path/to/tarball.tgz');

expect(yaml.dump).toHaveBeenCalledWith(expect.objectContaining({
version: '1.2.3',
annotations: expect.objectContaining({
'headlamp/plugin/archive-url': expect.stringContaining('my-plugin-1.2.3')
})
}), expect.anything());
});
});

describe('createArtifactHubTemplate', () => {
it('should create a template file', () => {
vi.mocked(fs.existsSync).mockReturnValue(false);
vi.mocked(yaml.dump).mockReturnValue('template yaml');

createArtifactHubTemplate('my-plugin');

expect(fs.writeFileSync).toHaveBeenCalledWith(expect.anything(), 'template yaml');
});

it('should throw if file already exists', () => {
vi.mocked(fs.existsSync).mockReturnValue(true);
expect(() => createArtifactHubTemplate('my-plugin')).toThrow('artifacthub-pkg.yml already exists');
});
});
});
240 changes: 240 additions & 0 deletions tools/releaser/src/utils/git.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import * as child_process from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import {
getRepoRoot,
getPluginVersion,
commitPluginVersionChange,
getVersionBumpCommit,
validateCommitSha,
isCommitPushedToRemote,
createReleaseTag,
pushTag,
checkGitStatus,
getLatestTagForPlugin,
hasChangesInPluginSinceTag,
getCommitsSinceTag,
getChangelogForPlugin,
tagExists
} from './git.js';
Comment thread
skools-here marked this conversation as resolved.

vi.mock('child_process', () => ({
execFileSync: vi.fn(),
execSync: vi.fn(),
}));
vi.mock('fs');

describe('git utilities', () => {
beforeEach(() => {
vi.restoreAllMocks();
vi.clearAllMocks();
});

describe('getRepoRoot', () => {
it('should return repo root from git command', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('/repo/root\n');
expect(getRepoRoot()).toBe('/repo/root');
expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['rev-parse', '--show-toplevel'], expect.anything());
});

it('should exit if not in a git repo', () => {
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit'); });
vi.mocked(child_process.execFileSync).mockImplementation(() => { throw new Error('not a git repo'); });
expect(() => getRepoRoot()).toThrow('exit');
expect(exitSpy).toHaveBeenCalledWith(1);
});
});

describe('getPluginVersion', () => {
it('should read version from package.json', () => {
vi.mocked(fs.readFileSync).mockReturnValue(JSON.stringify({ version: '1.2.3' }));
expect(getPluginVersion('/plugin/path')).toBe('1.2.3');
});

it('should exit on error', () => {
const exitSpy = vi.spyOn(process, 'exit').mockImplementation(() => { throw new Error('exit'); });
vi.mocked(fs.readFileSync).mockImplementation(() => { throw new Error('read error'); });
expect(() => getPluginVersion('/plugin/path')).toThrow('exit');
expect(exitSpy).toHaveBeenCalledWith(1);
});
});

describe('commitPluginVersionChange', () => {
it('should add files and commit', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
vi.mocked(fs.existsSync).mockReturnValue(true); // package-lock.json exists

commitPluginVersionChange('my-plugin', '1.2.3');

expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['add', expect.stringContaining('package.json')], expect.anything());
expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['add', expect.stringContaining('package-lock.json')], expect.anything());
expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['commit', '--signoff', '-m', 'my-plugin: Bump version to 1.2.3'], expect.anything());
});
});

describe('getVersionBumpCommit', () => {
it('should find commit SHA for version bump', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('abc123commitsha\n');
const sha = getVersionBumpCommit('my-plugin', '1.2.3');
expect(sha).toBe('abc123commitsha');
expect(child_process.execFileSync).toHaveBeenCalledWith(
'git',
['log', '--format=%H', '-S', '"version": "1.2.3"', '--', path.join('my-plugin', 'package.json')],
expect.anything()
);
});

it('should fallback to latest package.json change if exact match not found', () => {
vi.mocked(child_process.execFileSync)
.mockReturnValueOnce('/repo/root\n') // repo root
.mockReturnValueOnce('') // No -S match
.mockReturnValueOnce('fallbacksha\n'); // Latest change

const sha = getVersionBumpCommit('my-plugin', '1.2.3');
expect(sha).toBe('fallbacksha');
});

it('should return null if no commit found', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
expect(getVersionBumpCommit('my-plugin', '1.2.3')).toBeNull();
});
});

describe('validateCommitSha', () => {
it('should return true for valid SHA', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
expect(validateCommitSha('abc123sha')).toBe(true);
});

it('should return false for invalid SHA', () => {
vi.mocked(child_process.execFileSync).mockImplementation(() => { throw new Error('invalid sha'); });
expect(validateCommitSha('invalid')).toBe(false);
});
});

describe('isCommitPushedToRemote', () => {
it('should return true if commit is ancestor of remote HEAD', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
expect(isCommitPushedToRemote('sha')).toBe(true);
});

it('should return false if merge-base fails', () => {
vi.mocked(child_process.execFileSync)
.mockReturnValueOnce('/repo/root\n') // repo root
.mockReturnValueOnce('') // cat-file success
.mockImplementationOnce(() => { throw new Error('not an ancestor'); }); // merge-base
expect(isCommitPushedToRemote('sha')).toBe(false);
});
});

describe('createReleaseTag', () => {
it('should create an annotated tag', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
const tag = createReleaseTag('my-plugin', '1.2.3');
expect(tag).toBe('my-plugin-1.2.3');
expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['tag', '-a', 'my-plugin-1.2.3', '-m', 'my-plugin 1.2.3'], expect.anything());
});
});

describe('pushTag', () => {
it('should push tag to origin', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
pushTag('my-plugin-1.2.3');
expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['push', 'origin', 'my-plugin-1.2.3'], expect.anything());
});
});

describe('checkGitStatus', () => {
it('should return true if status is empty', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
expect(checkGitStatus()).toBe(true);
});

it('should return false if status is not empty', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('M file.txt');
expect(checkGitStatus()).toBe(false);
});
});

describe('getLatestTagForPlugin', () => {
it('should return the newest tag', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('my-plugin-1.1.0\nmy-plugin-1.0.0\n');
expect(getLatestTagForPlugin('my-plugin')).toBe('my-plugin-1.1.0');
});

it('should return null if no tags found', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
expect(getLatestTagForPlugin('my-plugin')).toBeNull();
});
});

describe('hasChangesInPluginSinceTag', () => {
it('should return true if diff has entries', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('modified-file.ts\n');
expect(hasChangesInPluginSinceTag('my-plugin', 'v1.0.0')).toBe(true);
});

it('should return false if diff is empty', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
expect(hasChangesInPluginSinceTag('my-plugin', 'v1.0.0')).toBe(false);
});
});

describe('getCommitsSinceTag', () => {
it('should return array of commit lines', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('hash1 commit message 1\nhash2 commit message 2\n');
const commits = getCommitsSinceTag('my-plugin', 'v1.0.0');
expect(commits).toHaveLength(2);
expect(commits[0]).toBe('hash1 commit message 1');
});

it('should return empty array if no commits', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
expect(getCommitsSinceTag('my-plugin', 'v1.0.0')).toEqual([]);
});
});

describe('getChangelogForPlugin', () => {
it('should generate changelog based on commits since tag', () => {
vi.mocked(child_process.execFileSync)
.mockReturnValueOnce('/repo/root\n') // rev-parse for latest tag
.mockReturnValueOnce('my-plugin-1.0.0\n') // latest tag
.mockReturnValueOnce('/repo/root\n') // rev-parse for commits since tag
.mockReturnValueOnce('hash1 message\n'); // commits since tag

const changelog = getChangelogForPlugin('my-plugin');
expect(changelog).toBe('hash1 message');
});

it('should handle case with no previous tag', () => {
vi.mocked(child_process.execFileSync)
.mockReturnValueOnce('/repo/root\n') // rev-parse for latest tag
.mockReturnValueOnce('') // no latest tag
.mockReturnValueOnce('/repo/root\n') // rev-parse for all commits
.mockReturnValueOnce('all commits message\n'); // all commits

const changelog = getChangelogForPlugin('my-plugin');
expect(changelog).toBe('all commits message');
});
});

describe('tagExists', () => {
it('should check local tag existence', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
expect(tagExists('my-tag')).toBe(true);
expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['rev-parse', '--verify', 'my-tag'], expect.anything());
});

it('should check remote tag existence', () => {
vi.mocked(child_process.execFileSync).mockReturnValue('');
expect(tagExists('my-tag', true)).toBe(true);
expect(child_process.execFileSync).toHaveBeenCalledWith('git', ['ls-remote', '--tags', 'origin', 'my-tag'], expect.anything());
});

it('should return false if command fails', () => {
vi.mocked(child_process.execFileSync).mockImplementation(() => { throw new Error('not found'); });
expect(tagExists('non-existent')).toBe(false);
});
});
});
Loading