react-native-macos/scripts/auto-sync-reactnative-to-sd...

120 строки
4.5 KiB
JavaScript

let child_process = require('child_process');
const fs = require('fs');
const path = require('path');
const branchName = 'rn-to-sdx';
const vsoApiUrl = process.env.VSO_API_URL ? process.env.VSO_API_URL : 'https://office.visualstudio.com/DefaultCollection/_apis';
const ISSUrl = 'https://office.visualstudio.com/ISS/_git/';
const vsoRepoID = '304dd303-24df-4106-8e06-2b6e33135356';
let pullRequestTitle = 'auto-sync from ISS\\react-native to ISS\\sdx-platform';
let pullRequestDescription = pullRequestTitle + '. This pull request is auto-generated by script';
let myArgs = process.argv.slice(2);
let myToken = myArgs[0];
function cloneRepoAndCreatBranch(repo) {
child_process.execSync(`git clone ${ISSUrl}${repo} temp`, { stdio: [0, 1, 2] });
child_process.execSync(`git -C temp checkout -b ${branchName}`, { stdio: [0, 1, 2] });
child_process.execSync(`git -C temp push -u origin ${branchName}`, { stdio: [0, 1, 2] });
}
function syncSubtree(fromRepo, path) {
try {
child_process.execSync(`git -C temp subtree pull --prefix=src/${path} ${ISSUrl}${fromRepo} master`, { stdio: [0, 1, 2] });
}
catch (e) {
}
try {
child_process.execSync('git -C temp checkout --theirs .');
}
// workaround for the cases that files are deleted on react-native repo
catch (errors) {
for (let error of errors.toString().split('\n')) {
if (error.startsWith('error: path') && error.endsWith('does not have their version')) {
let deletedFile = error.substring(13, error.length - 29);
child_process.execSync(`git -C temp rm ${deletedFile}`, { stdio: [0, 1, 2] });
}
}
child_process.execSync('git -C temp checkout --theirs .', { stdio: [0, 1, 2] });
}
// ISS\react-native is still using sub-modules, this is a workaround for the auto sync
if (fromRepo === 'react-native') {
child_process.execSync('git -C temp reset src/react-native/Folly', { stdio: [0, 1, 2] });
child_process.execSync('git -C temp reset src/react-native/glog', { stdio: [0, 1, 2] });
child_process.execSync('git -C temp reset src/react-native/double-conversion', { stdio: [0, 1, 2] });
child_process.execSync('git -C temp reset src/react-native/jsc', { stdio: [0, 1, 2] });
child_process.execSync('git -C temp reset src/react-native/v8', { stdio: [0, 1, 2] });
child_process.execSync('git -C temp checkout --theirs .', { stdio: [0, 1, 2] });
}
child_process.execSync('git -C temp add .', { stdio: [0, 1, 2] });
}
// sync from ISS\react-native to ISS\sdx-platform
function syncRepo() {
cloneRepoAndCreatBranch('sdx-platform');
syncSubtree('react-native', 'react-native/');
syncSubtree('Folly', 'react-native/Folly');
syncSubtree('jsc', 'react-native/jsc');
syncSubtree('glog', 'react-native/glog');
syncSubtree('double-conversion', 'react-native/double-conversion');
syncSubtree('v8', 'react-native/v8');
// no change on sub-trees
if (child_process.execSync('git -C temp status --porcelain').toString().trim().length === 0) {
console.log('No change happens on all the sub-trees, no need to create pull request');
child_process.execSync(`git -C temp push -d origin ${branchName}`, { stdio: [0, 1, 2] });
return false;
}
try {
child_process.execSync(`git -C temp commit -m "${pullRequestTitle}"`, { stdio: [0, 1, 2] });
}
catch (e) {
}
child_process.execSync(`git -C temp push origin ${branchName}`, { stdio: [0, 1, 2] });
return true;
}
const AutoSync = require('./autoSync.js');
const autoSync = new AutoSync(vsoRepoID, vsoApiUrl, myToken);
//clean the possible temporary local git repo before we start sync
child_process.execSync('IF EXIST temp rd /s /q temp', { stdio: [0, 1, 2] });
let checkBranch = child_process.execSync(`git ls-remote --heads ${ISSUrl}sdx-platform ${branchName} | wc -l`).toString();
if (checkBranch.trim() === '1') {
console.log('pull request already exists, completes the pull request when all builds success');
autoSync.completePrWhenAllBuildsSuccess(pullRequestTitle);
}
else {
let shouldCreatePr = syncRepo();
if (shouldCreatePr) {
console.log('There are changes, create pull request');
autoSync.createPr(branchName, pullRequestTitle, pullRequestDescription);
}
//clean the possible temporary local git repo after the pull request
child_process.execSync('IF EXIST temp rd /s /q temp', { stdio: [0, 1, 2] });
}