Skip to content

Commit 6488305

Browse files
authored
Initial version of the bot (#807)
1 parent bcddfde commit 6488305

File tree

2 files changed

+174
-0
lines changed

2 files changed

+174
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Handle @react-native-bot commands
2+
3+
on:
4+
issue_comment:
5+
types: [created]
6+
7+
jobs:
8+
publish-message:
9+
runs-on: ubuntu-latest
10+
steps:
11+
# Checkout the repo
12+
- name: Checkout the repo
13+
uses: actions/checkout@v4
14+
- name: Run React Native Bot
15+
uses: actions/github-script@v5
16+
env:
17+
REACT_NATIVE_BOT_GITHUB_TOKEN: ${{ secrets.REACT_NATIVE_BOT_GITHUB_TOKEN }}
18+
with:
19+
github-token: ${{ secrets.REACT_NATIVE_BOT_GITHUB_TOKEN }}
20+
script: |
21+
// List of allowed GitHub users to interact with @react-native-bot
22+
const allowedUsers = [
23+
'cortinico',
24+
'cipolleschi'
25+
];
26+
27+
const commentBody = context.payload.comment.body;
28+
const commenterUsername = context.payload.comment.user.login;
29+
30+
if (allowedUsers.includes(commenterUsername) && commentBody.includes('@react-native-bot')) {
31+
const script = require('./.github/workflows/scripts/react-native-bot.js')
32+
script({github, context})
33+
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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

Comments
 (0)