From 5657189391ac94b952e83358503ed67b0e6cba68 Mon Sep 17 00:00:00 2001 From: Koen Van Looveren Date: Mon, 27 Nov 2023 22:11:06 +0100 Subject: [PATCH] fix: configure git repo --- index.js | 157 ++++++++++++++++++++++++++++--------------------------- 1 file changed, 81 insertions(+), 76 deletions(-) diff --git a/index.js b/index.js index 49530e3..1c83e94 100644 --- a/index.js +++ b/index.js @@ -25,6 +25,86 @@ if (githubToken == null || githubToken == "") { return } +try { + await configureGit() + await installDartDependencies(); + await executeScriptPreRun() + + await configurePubDevToken() + + const pubspec = getPubspec() + const event = github.context.payload + + const messages = event.commits.map(commit => commit.message + '\n' + commit.body) + + const commitMessage = 'version bump to' + const isVersionBump = messages.map(message => message.toLowerCase().includes(commitMessage)).includes(true) + if (isVersionBump) { + core.info('No action necessary!') + return + } + core.info(`\`${event.commits.length}\` commit(s) for this version bump.`) + + let versionType = 'patch' + if (messages.map(message => message.includes('BREAKING CHANGE') || message.includes('major')).includes(true)) { + versionType = 'major' + } else if (messages.map( + message => message.toLowerCase().startsWith('feat') || message.toLowerCase().includes('minor')).includes(true)) { + versionType = 'minor' + } + core.info(`${versionType} version bump!`) + + const currentVersion = pubspec.version.toString() + const newVersion = incrementVersion(currentVersion, versionType) + core.info(`Bumping version from ${currentVersion} to ${newVersion}`) + updatePubspec(newVersion) + + // Verification before publishing + await analyzeDartProject() + + // Setting up Git + await runInWorkspace('git', ['config', 'user.name', `"${process.env.GITHUB_USER || 'Dart Conventional Release'}"`]) + await runInWorkspace('git', ['config', 'user.email', `"${process.env.GITHUB_EMAIL || 'gh_action_dart_conventional_release@users.noreply.github.com'}"`]) + const remoteGitRepoUrl = `https://${githubToken}:x-oauth-basic@github.com/${process.env.GITHUB_REPOSITORY}.git` + await runInWorkspace('git', ['remote', 'set-url', 'origin', remoteGitRepoUrl]) + + + // Committing changes + await runInWorkspace('git', ['add', 'pubspec.yaml']) + await runInWorkspace('git', ['commit', '-m', `ci: ${commitMessage} ${newVersion}`]) + // Tagging the commit + const tag = `v${newVersion}` + await runInWorkspace('git', ['tag', tag]) + + // Pushing changes + await runInWorkspace('git', ['push', 'origin']) + await runInWorkspace('git', ['push', 'origin', '--tags']) + await uploadDartProject() +} catch (error) { + core.setFailed(`Action failed with error: ${error}`) +} + +// ===================================================================== +// ============================= Functions ============================= +// ===================================================================== + +function configureGit() { + return runInWorkspace('git', ['config', '--global', '--add', 'safe.directory', workspace]) +} + +function installDartDependencies() { + return runInWorkspace('dart', ['pub', 'get']) +} + +// Run a script before the action (only if configured) +async function executeScriptPreRun() { + const path = core.getInput('script-pre-run') + if (!path) { + return + } + await runInWorkspace('dart', ['run', path]) +} + // Helper function to read and parse the pubspec.yaml file function getPubspec() { const pubspecContent = fs.readFileSync('pubspec.yaml', 'utf8') @@ -67,79 +147,4 @@ async function analyzeDartProject() { async function uploadDartProject() { await runInWorkspace('dart', ['pub', 'publish']) -} - - -async function executeScriptPreRun() { - const path = core.getInput('script-pre-run') - if (!path) { - return - } - await runInWorkspace('dart', ['run', path]) -} - -async function run() { - try { - // Install dependencies - await runInWorkspace('dart', ['pub', 'get']) - - // Run a script before the action (only if configured) - await executeScriptPreRun() - - // Configure pub.dev token from github - await configurePubDevToken() - - const pubspec = getPubspec() - const event = github.context.payload - - const messages = event.commits.map(commit => commit.message + '\n' + commit.body) - - const commitMessage = 'version bump to' - const isVersionBump = messages.map(message => message.toLowerCase().includes(commitMessage)).includes(true) - if (isVersionBump) { - core.info('No action necessary!') - return - } - core.info(`\`${event.commits.length}\` commit(s) for this version bump.`) - - let versionType = 'patch' - if (messages.map(message => message.includes('BREAKING CHANGE') || message.includes('major')).includes(true)) { - versionType = 'major' - } else if (messages.map( - message => message.toLowerCase().startsWith('feat') || message.toLowerCase().includes('minor')).includes(true)) { - versionType = 'minor' - } - core.info(`${versionType} version bump!`) - - const currentVersion = pubspec.version.toString() - const newVersion = incrementVersion(currentVersion, versionType) - core.info(`Bumping version from ${currentVersion} to ${newVersion}`) - updatePubspec(newVersion) - - // Verification before publishing - await analyzeDartProject() - - // Setting up Git - await runInWorkspace('git', ['config', 'user.name', `"${process.env.GITHUB_USER || 'Dart Conventional Release'}"`]) - await runInWorkspace('git', ['config', 'user.email', `"${process.env.GITHUB_EMAIL || 'gh_action_dart_conventional_release@users.noreply.github.com'}"`]) - const remoteGitRepoUrl = `https://${githubToken}:x-oauth-basic@github.com/${process.env.GITHUB_REPOSITORY}.git` - await runInWorkspace('git', ['remote', 'set-url', 'origin', remoteGitRepoUrl]) - - - // Committing changes - await runInWorkspace('git', ['add', 'pubspec.yaml']) - await runInWorkspace('git', ['commit', '-m', `ci: ${commitMessage} ${newVersion}`]) - // Tagging the commit - const tag = `v${newVersion}` - await runInWorkspace('git', ['tag', tag]) - - // Pushing changes - await runInWorkspace('git', ['push', 'origin']) - await runInWorkspace('git', ['push', 'origin', '--tags']) - await uploadDartProject() - } catch (error) { - core.setFailed(`Action failed with error: ${error}`) - } -} - -run() +} \ No newline at end of file