From 02e38c05f6f19fba93fd1885ac00ae5340e5c3ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mislav=20Marohni=C4=87?= Date: Tue, 12 Sep 2023 16:32:21 +0200 Subject: [PATCH] :nail_care: --- src/main-test.ts | 35 +++++++++++++++++++++++++++++++++-- src/main.ts | 14 +++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/main-test.ts b/src/main-test.ts index 38dab29..9be8730 100644 --- a/src/main-test.ts +++ b/src/main-test.ts @@ -50,7 +50,7 @@ test('commitForRelease()', (t) => { ) }) -test('prepareEdit()', async (t) => { +test('prepareEdit() homebrew-core', async (t) => { const ctx = { sha: 'TAGSHA', ref: 'refs/tags/v0.8.2', @@ -60,6 +60,7 @@ test('prepareEdit()', async (t) => { }, } + process.env['GITHUB_REPOSITORY'] = 'monalisa/hello-world' process.env['INPUT_HOMEBREW-TAP'] = 'Homebrew/homebrew-core' process.env['INPUT_COMMIT-MESSAGE'] = 'Upgrade {{formulaName}} to {{version}}' @@ -85,7 +86,7 @@ test('prepareEdit()', async (t) => { t.is(opts.owner, 'Homebrew') t.is(opts.repo, 'homebrew-core') t.is(opts.branch, '') - t.is(opts.filePath, 'Formula/repo.rb') + t.is(opts.filePath, 'Formula/r/repo.rb') t.is(opts.commitMessage, 'Upgrade repo to 0.8.2') const oldFormula = ` @@ -109,3 +110,33 @@ test('prepareEdit()', async (t) => { opts.replace(oldFormula) ) }) + +test('prepareEdit() non-homebrew-core', async (t) => { + const ctx = { + sha: 'TAGSHA', + ref: 'refs/tags/v0.8.2', + repo: { + owner: 'OWNER', + repo: 'REPO', + }, + } + + process.env['GITHUB_REPOSITORY'] = 'monalisa/hello-world' + process.env['INPUT_HOMEBREW-TAP'] = 'myorg/homebrew-utils' + process.env['INPUT_COMMIT-MESSAGE'] = 'Upgrade {{formulaName}} to {{version}}' + process.env['INPUT_DOWNLOAD-SHA256'] = 'MOCK-SHA-256' + + const apiClient = api('ATOKEN', { + fetch: function (url: string) { + throw url + }, + logRequests: false, + }) + + const opts = await prepareEdit(ctx, apiClient, apiClient) + t.is(opts.owner, 'myorg') + t.is(opts.repo, 'homebrew-utils') + t.is(opts.branch, '') + t.is(opts.filePath, 'Formula/repo.rb') + t.is(opts.commitMessage, 'Upgrade repo to 0.8.2') +}) diff --git a/src/main.ts b/src/main.ts index 524dabb..d50e321 100644 --- a/src/main.ts +++ b/src/main.ts @@ -14,6 +14,17 @@ function tarballForRelease( return `https://github.com/${owner}/${repo}/archive/${tagName}.tar.gz` } +function formulaPath(owner: string, repo: string, formulaName: string): string { + if ( + owner.toLowerCase() == 'homebrew' && + repo.toLowerCase() == 'homebrew-core' + ) { + // respect formula sharding structure in `Homebrew/homebrew-core` + return `Formula/${formulaName.charAt(0)}/${formulaName}.rb` + } + return `Formula/${formulaName}.rb` +} + export function commitForRelease( messageTemplate: string, params: { [key: string]: string } = {} @@ -82,7 +93,8 @@ export async function prepareEdit( } const formulaName = getInput('formula-name') || ctx.repo.repo.toLowerCase() const branch = getInput('base-branch') - const filePath = getInput('formula-path') || `Formula/${formulaName.charAt(0)}/${formulaName}.rb` + const filePath = + getInput('formula-path') || formulaPath(owner, repo, formulaName) const version = tagName.replace(/^v(\d)/, '$1') const downloadUrl = getInput('download-url') ||