Skip to content
Merged
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
19 changes: 11 additions & 8 deletions lib/backport.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,27 @@ module.exports = async function (context, targets, logger) {
const commitsList = target.commits.length > 0 ? target.commits : commits
logger.debug('Backporting commits', commitsList.join(', '), 'to', target.branch)

const branch = await git(context, token, commitsList, target.branch, logger)
const { backportBranch, conflicts } = await git(context, token, commitsList, target.branch, logger)

// Check conflicts
// Compare diffs
const oldChanges = await pullRequest.getChanges(context)
const newChanges = await pullRequest.getChanges(context, newPrId)
const conflicts = oldChanges.additions !== newChanges.additions || oldChanges.deletions !== newChanges.deletions
logger.debug('Checking conflicts', oldChanges, newChanges)
const diffChanges = oldChanges.additions !== newChanges.additions || oldChanges.deletions !== newChanges.deletions
logger.debug('Comparing diffs', oldChanges, newChanges)

// Open PR
const newPR = await pullRequest.newReady(context, pr.data.number, pr.data.title, target.branch, branch, conflicts)
const newPR = await pullRequest.newReady(context, pr.data.number, pr.data.title, target.branch, backportBranch, conflicts || diffChanges)
newPrId = newPR.data.number

// Add labels
await pullRequest.addLabels(context, labels, newPrId)

if (conflicts) {
logger.warn('Conflicts when cherry-picking from', oldBranch, 'to branch', branch)
await pullRequest.updatePRBody(context, newPrId, `- [ ] :warning: This backport had conflicts and is incomplete\n\nbackport of #${pr.data.number}`)
logger.warn('Conflicts when cherry-picking from ' + oldBranch + ' to branch ' + backportBranch + ' prevented with --strategy=ours')
await pullRequest.updatePRBody(context, newPrId, `- [ ] :warning: This backport had conflicts that were resolved with the 'ours' merge strategy and is likely incomplete\n\nbackport of #${pr.data.number}`)
} else if (diffChanges) {
logger.warn('Diff changes when cherry-picking from ' + oldBranch + ' to branch ' + backportBranch)
await pullRequest.updatePRBody(context, newPrId, `- [ ] :warning: This backport's changes differ from the original and might be incomplete\n\nbackport of #${pr.data.number}`)
}

// Set available milestone
Expand All @@ -101,7 +104,7 @@ module.exports = async function (context, targets, logger) {
await pullRequest.setMilestone(context, milestoneId, newPrId)
}

logger.info('Successfully created backport from', oldBranch, 'for', target.branch, 'in', branch)
logger.info('Successfully created backport from', oldBranch, 'for', target.branch, 'in', backportBranch)
} catch (e) {
logger.debug(e)
logger.error('Backport to', target.branch, 'failed')
Expand Down
31 changes: 22 additions & 9 deletions lib/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,27 @@ module.exports = async function (context, token, commits, target, logger) {
target
)

let conflicts = false
for (let i = 0; i < commits.length; i++) {
logger.debug('Cherry picking', commits[i])
// Cherry picking commits while discarding conflicts
await git.raw([
'cherry-pick',
commits[i],
'--strategy-option',
'ours'
], (err, result) => {
logger.info(err, result)
})
try {
await git.raw([
'cherry-pick',
commits[i],
])
} catch (error) {
conflicts = true

await git.raw([
'cherry-pick',
commits[i],
'--strategy-option',
'ours'
], (err, result) => {
logger.info(err, result)
})
}
}

logger.debug('Pushing to', backportBranch)
Expand All @@ -59,5 +69,8 @@ module.exports = async function (context, token, commits, target, logger) {
// Cleanup
fs.remove(gitRoot)

return backportBranch
return {
backportBranch,
conflicts,
}
}