Skip to content

Commit

Permalink
feat/fix-exisiting-branch update branch logic
Browse files Browse the repository at this point in the history
  • Loading branch information
hminaee-tc committed Jul 17, 2024
1 parent f9524c6 commit 7db908e
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions create-pr-action/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ async function createPr(repoFullName, forkStatus, token, octokit, upstreamFilePa
const fileName = upstreamFilePath;
const targetBranch = targetBranchToMergeTo;
const commitMessage = botCommitMessage;
let upstreamFileContentOutdated = false;

// Check if the file exists in the target branch
core.info(`Checking if ${fileName} exists in ${targetBranch} branch of ${repoFullName}`);
Expand All @@ -91,7 +92,17 @@ async function createPr(repoFullName, forkStatus, token, octokit, upstreamFilePa
ref: targetBranch,
});
core.info(`${fileName} already exists in ${targetBranch} branch. No PR will be created.`);
return { url: null, number: null, upstreamFileAlreadyExists: true };
// Decode the content from base64
const existingContent = Buffer.from(fileContent.content, 'base64').toString('utf-8');

if (existingContent.trim() === forkStatus.trim()) {
core.info(`The content of ${fileName} in ${targetBranch} branch is the same as the provided content. No PR will be created.`);
return { url: null, number: null, upstreamFileAlreadyExists: true };
} else {
upstreamFileContentOutdated = true;
core.info(`The content of ${fileName} in ${targetBranch} branch is different. Proceeding with update and upstreamFileContentOutdated is set to ${upstreamFileContentOutdated}.`);

}
} catch (error) {
if (error.status === 404) {
core.info(`${fileName} does not exist in ${targetBranch} branch. Proceeding with PR creation.`);
Expand Down Expand Up @@ -120,9 +131,9 @@ async function createPr(repoFullName, forkStatus, token, octokit, upstreamFilePa
const matchingPRs = pullRequests.filter(pr => pr.title === commitMessage);

// Debugging: Log the filtered PRs
console.log(`Matching PRs with title '${commitMessage}':`, matchingPRs);
core.info(`Matching PRs with title '${commitMessage}':`, matchingPRs);

if (matchingPRs.length > 0) {
if (matchingPRs.length > 0 && !upstreamFileContentOutdated) {
core.info(`An open PR from ${newBranch} to ${targetBranch} already exists. No further action taken.`);
return { url: matchingPRs[0].html_url, number: matchingPRs[0].number, branchExists: true, openPrExists: true };
} else {
Expand Down

0 comments on commit 7db908e

Please sign in to comment.