diff --git a/action/src/index.ts b/action/src/index.ts index 8b9a274..25be21c 100644 --- a/action/src/index.ts +++ b/action/src/index.ts @@ -11,23 +11,38 @@ import doStuff from './do-stuff.js'; const config = await loadConfig(); const octokit = await loadOctokit(); - // Do stuff - await doStuff(); // Commit changes execSync('git config --global user.name "Replexica"'); execSync('git config --global user.email "support@replexica.com"'); execSync(`git config --global safe.directory ${process.cwd()}`); if (!config.isPullRequestMode) { + // Do stuff + await doStuff(); execSync('git add .'); execSync(`git commit -m "${config.commitMessage}"`); execSync('git push'); } else { // Calculate automated branch name const prBranchName = `replexica/${config.currentBranchName}`; - - // Create branch - execSync(`git checkout -b ${prBranchName}`); + const branchExists = await octokit.rest.repos.getBranch({ + owner: config.repositoryOwner, + repo: config.repositoryName, + branch: prBranchName, + }).then(({ status }) => status === 200); + + if (branchExists) { + execSync(`git checkout ${prBranchName}`); + execSync('git pull'); + // merge current branch into pr branch + execSync(`git merge ${config.currentBranchName}`); + } else { + // Create branch + execSync(`git checkout -b ${prBranchName}`); + } + + // Do stuff + await doStuff(); execSync('git add .'); execSync(`git commit -m "${config.commitMessage}"`); execSync(`git push --set-upstream origin "${prBranchName}"`);