|
| 1 | +function writeComment(context, github, commentBody) { |
| 2 | + github.rest.issues.createComment({ |
| 3 | + owner: context.repo.owner, |
| 4 | + repo: context.repo.repo, |
| 5 | + issue_number: context.payload.issue.number, |
| 6 | + body: commentBody, |
| 7 | + }); |
| 8 | +} |
| 9 | + |
| 10 | +function handleFailure(context, github) { |
| 11 | + writeComment( |
| 12 | + context, |
| 13 | + github, |
| 14 | + "⚠️ Sorry but I couldn't understand your request." |
| 15 | + ); |
| 16 | +} |
| 17 | + |
| 18 | +function handleError(context, github, message, consoleOutput) { |
| 19 | + console.log(`❌ Error: ${message}`); |
| 20 | + let composedMessage = `❌ I failed to process your request with this error:\n\`\`\`\n${message}\n\`\`\``; |
| 21 | + if (consoleOutput) { |
| 22 | + composedMessage += `\n**console output:**\n\`\`\`\n${consoleOutput}\n\`\`\``; |
| 23 | + } |
| 24 | + writeComment(context, github, composedMessage); |
| 25 | +} |
| 26 | + |
| 27 | +// First parse the command as if it is a react-native-bot merge <SHA> <branch> |
| 28 | +function handleMergeCommand(context, github, commentBody) { |
| 29 | + const [botName, command, sha, branch] = commentBody.trim().split(" "); |
| 30 | + if ( |
| 31 | + botName !== "@react-native-bot" || |
| 32 | + command !== "merge" || |
| 33 | + !sha || |
| 34 | + !branch |
| 35 | + ) { |
| 36 | + handleFailure(context, github); |
| 37 | + return; |
| 38 | + } |
| 39 | + const execSync = require("child_process").execSync; |
| 40 | + |
| 41 | + const tokenSecret = process.env.REACT_NATIVE_BOT_GITHUB_TOKEN; |
| 42 | + |
| 43 | + try { |
| 44 | + console.log(`▶️ Cloning react-native repository...`); |
| 45 | + execSync( |
| 46 | + `git clone -b main --single-branch https://${tokenSecret}@github.com/facebook/react-native.git`, |
| 47 | + { |
| 48 | + stdio: "inherit", |
| 49 | + } |
| 50 | + ); |
| 51 | + } catch (error) { |
| 52 | + handleError( |
| 53 | + context, |
| 54 | + github, |
| 55 | + `Failed to clone with: ${error.status} with '${error.message}'` |
| 56 | + ); |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + try { |
| 61 | + console.log(`▶️ Checking out to the requested branch: ${branch}`); |
| 62 | + execSync(`cd react-native; git fetch origin ${branch}:${branch}`, { |
| 63 | + stdio: "inherit", |
| 64 | + }); |
| 65 | + execSync(`cd react-native; git checkout ${branch}`, { |
| 66 | + stdio: "inherit", |
| 67 | + }); |
| 68 | + } catch (error) { |
| 69 | + handleError( |
| 70 | + context, |
| 71 | + github, |
| 72 | + `Failed to checkout branch ${branch} with: ${error.status} with '${error.message}'` |
| 73 | + ); |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + try { |
| 78 | + console.log(`▶️ Attempting to cherry pick: ${sha}`); |
| 79 | + execSync(`git config --global user.email "[email protected]"`); |
| 80 | + execSync(`git config --global user.name "React Native Bot"`); |
| 81 | + execSync(`cd react-native; git cherry-pick ${sha}`, { |
| 82 | + stdio: "inherit", |
| 83 | + }); |
| 84 | + } catch (error) { |
| 85 | + let consoleOutput = execSync(`cd react-native; git status`).toString(); |
| 86 | + console.log("Console output is ", consoleOutput); |
| 87 | + handleError( |
| 88 | + context, |
| 89 | + github, |
| 90 | + `Failed to cherry-pick ${sha} on branch ${branch} with: ${error.status} with '${error.message}'`, |
| 91 | + consoleOutput |
| 92 | + ); |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + console.log(`▶️ Attempting to push on the remote branch: ${branch}`); |
| 98 | + execSync(`cd react-native; git push origin HEAD`, { |
| 99 | + stdio: "inherit", |
| 100 | + }); |
| 101 | + } catch (error) { |
| 102 | + handleError( |
| 103 | + context, |
| 104 | + github, |
| 105 | + `Failed to push the pick on branch ${branch} with: ${error.status} with '${error.message}'` |
| 106 | + ); |
| 107 | + return; |
| 108 | + } |
| 109 | + |
| 110 | + writeComment( |
| 111 | + context, |
| 112 | + github, |
| 113 | + `✅ Successfully picked up your commit https://github.com/facebook/react-native/commit/${sha} on the branch https://github.com/facebook/react-native/commits/${branch}` |
| 114 | + ); |
| 115 | + |
| 116 | + github.rest.issues.update({ |
| 117 | + owner: context.repo.owner, |
| 118 | + repo: context.repo.repo, |
| 119 | + issue_number: context.payload.issue.number, |
| 120 | + state: "closed", |
| 121 | + }); |
| 122 | +} |
| 123 | + |
| 124 | +module.exports = ({ github, context }) => { |
| 125 | + const commentBody = context.payload.comment.body; |
| 126 | + if (commentBody.trim() === "@react-native-bot help") { |
| 127 | + writeComment( |
| 128 | + context, |
| 129 | + github, |
| 130 | + "ℹ️ Usage: `react-native-bot merge <SHA> <branch>`" |
| 131 | + ); |
| 132 | + } else if (commentBody.trim().startsWith("@react-native-bot merge")) { |
| 133 | + handleMergeCommand(context, github, commentBody); |
| 134 | + } else { |
| 135 | + writeComment( |
| 136 | + context, |
| 137 | + github, |
| 138 | + "⚠️ Sorry, I didn't understand that. Please try again or type `@react-native-bot help` for more information." |
| 139 | + ); |
| 140 | + } |
| 141 | +}; |
0 commit comments