Как правильно обрабатывать обновление GitHub?Javascript

Форум по Javascript
Ответить
Anonymous
 Как правильно обрабатывать обновление GitHub?

Сообщение Anonymous »

Я относительно новичок в github и пытаюсь запрограммировать систему для моего бота discord.js, которая будет обновлять код бота с помощью моих частных репозиториев. Для этой системы у меня есть 3 репозитория. Первый authCodeRepoUrl — это репозиторий, который я создал для общего кода бота (команды, события и т. д.), поэтому он всегда должен загружать новейший код из этого репозитория. Второй — authDataRepoUrl, который предназначен для данных, которые мой бот хранит в файлах .json. Он должен проверять, какой файл новее, и в зависимости от этого изменять локальные или удаленные файлы данных. Третий вариант authBackupRepoUrl заключается в том, что я делаю резервную копию всей папки с любого устройства в хранилище, чтобы всегда иметь возможность проверить папку.
Теперь я пытаюсь это сделать и когда у меня возникали проблемы, я просил ИИ помочь мне решить проблемы, но я не уверен, действительно ли это помогло, поэтому хочу обратиться за помощью сюда (заранее извините).
Мой код выглядит так:

Код: Выделить всё

const { exec } = require('child_process');
const path = require('path');
const fs = require('fs');
require('dotenv').config();
const os = require('os');
const codeRepoUrl = process.env.CODE_REPO_URL;
const dataRepoUrl = process.env.DATA_REPO_URL;
const backupRepoUrl = process.env.BACKUP_REPO_URL;
const token = process.env.GITHUB_PAT;
const allowedHost = 'ncpi';
const currentHost = os.hostname();

if (!codeRepoUrl || !dataRepoUrl || !backupRepoUrl || !token) {
console.error('[ERROR] CODE_REPO_URL, DATA_REPO_URL, BACKUP_REPO_URL, or GITHUB_PAT is not set in .env');
process.exit(1);
}

const codeRepoPath = path.join(__dirname, './');
const dataRepoPath = path.join(__dirname, 'Data');
const logFilePath = path.join(dataRepoPath, 'sync_log.txt');

const authCodeRepoUrl = codeRepoUrl.replace('https://', `https://${token}@`);
const authDataRepoUrl = dataRepoUrl.replace('https://', `https://${token}@`);
const authBackupRepoUrl = backupRepoUrl.replace('https://', `https://${token}@`);

// Hilfsfunktionen für Git-Operationen
function execPromise(command) {
return new Promise((resolve, reject) => {
exec(command, (error, stdout, stderr) => {
if (error) {
reject(stderr || stdout);
} else {
resolve(stdout);
}
});
});
}

async function getDefaultBranch(repoPath) {
try {
const branchName = await execPromise(`git -C ${repoPath} symbolic-ref --short HEAD`);
return branchName.trim();
} catch {
return 'main';
}
}

async function syncDataRepo() {
try {
console.log('[INFO] Syncing data repository...');
if (!fs.existsSync(dataRepoPath)) {
console.log('[INFO] Cloning data repository...');
await execPromise(`git clone ${authDataRepoUrl} ${dataRepoPath}`);
} else {
console.log('[INFO] Pulling latest changes for the data repository...');
await execPromise(`git -C ${dataRepoPath} pull`);
}

await execPromise(`git -C ${dataRepoPath} branch --set-upstream-to=origin/main main`);

const localChanges = await execPromise(`git -C ${dataRepoPath} status --porcelain`);
if (localChanges.trim().length > 0) {
console.log('[INFO] Local changes detected. Pushing to remote repository...');
await execPromise(
`git -C ${dataRepoPath} add .  && git -C ${dataRepoPath} commit -m "Auto-sync: ${new Date().toISOString()}" && git -C ${dataRepoPath} push`
);
}

console.log('[INFO] Data repository synchronized.');
logSyncSuccess();
} catch (error) {
console.error('[ERROR] Failed to synchronize data repository:', error);
}
}

function logSyncSuccess() {
const timestamp = `[SUCCESS] Data repository synchronized at: ${new Date().toISOString()}\n\n`;
try {
fs.appendFileSync(logFilePath, timestamp, 'utf8');
console.log('[INFO] Sync timestamp logged.');
} catch (err) {
console.error('[ERROR] Failed to write sync log:', err);
}
}

async function syncCodeRepo() {
if (currentHost !== allowedHost) {
console.log('[INFO] Code synchronization skipped: Not running on the allowed device.');
return;
}

try {
console.log('[INFO] Syncing code repository...');
const branchName = await getDefaultBranch(codeRepoPath);

if (!fs.existsSync(path.join(codeRepoPath, '.git'))) {
console.log('[INFO] Cloning code repository...');
await execPromise(`git clone ${authCodeRepoUrl} ${codeRepoPath}`);
} else {
console.log('[INFO] Pulling latest changes for the code repository...');
try {
await execPromise(`git -C ${codeRepoPath} remote add origin ${authCodeRepoUrl}`);
} catch (error) {
if (!error.includes('remote origin already exists')) {
throw error;
}
}
await execPromise(`git -C ${codeRepoPath} pull origin ${branchName}`);
}

await execPromise(`git -C ${codeRepoPath} branch --set-upstream-to=origin/${branchName} ${branchName}`);

console.log('[INFO] Code repository synchronized.');
} catch (error) {
console.error('[ERROR] Failed to synchronize code repository:', error);
}
}

async function backupToRemoteRepo() {
if (currentHost !== allowedHost) {
console.log('[INFO] Backup skipped: Not running on the allowed device.');
return;
}

try {
console.log('[INFO] Backing up directly to remote repository...');
const branchName = 'main';

if (!fs.existsSync(path.join(codeRepoPath, '.git'))) {
console.log('[INFO] Initializing repository for backup...');
await execPromise(`git -C ${codeRepoPath} init`);
await execPromise(`git -C ${codeRepoPath} checkout -b ${branchName}`);
await execPromise(`git -C ${codeRepoPath} remote add origin ${authBackupRepoUrl}`);
}

await execPromise(`git -C ${codeRepoPath} config user.email "email censored"`);
await execPromise(`git -C ${codeRepoPath} config user.name "github user censored"`);

await execPromise(`git -C ${codeRepoPath} add .`);
await execPromise(`git -C ${codeRepoPath} commit -m "Backup: ${new Date().toISOString()}"`);
await execPromise(`git -C ${codeRepoPath} push origin ${branchName} --force`);

console.log('[INFO] Backup to remote repository completed.');
} catch (error) {
console.error('[ERROR] Failed to backup to remote repository:', error);
}
}

async function checkAndUpdate() {
console.log('[INFO] Starting update process...');
await syncCodeRepo();
await syncDataRepo();
await backupToRemoteRepo();
console.log('[INFO] Update process completed.');
scheduleNextSync();
}

function scheduleNextSync() {
const now = new Date();
const minutes = now.getMinutes();
const nextInterval = 10 - (minutes % 10);
const delay = nextInterval * 60 * 1000;

console.log(`[INFO] Next sync scheduled in ${nextInterval} minutes (${new Date(now.getTime() + delay).toLocaleTimeString()}).`);

setTimeout(() =>  {
checkAndUpdate().then(scheduleRecurringSync);
}, delay);
}

function scheduleRecurringSync() {
console.log('[INFO] Scheduling recurring sync every 10 minutes.');
setInterval(checkAndUpdate, 10 * 60 * 1000);
}

module.exports = { checkAndUpdate };

Это данные журнала, которые появляются, когда я пробую код:

Код: Выделить всё

0| [ERROR] Failed to synchronize data repository: There is no tracking information for the current branch.
0| Please specify which branch you want to merge with.
0| See git-pull(1) for details.
0|     git pull 

0| If you wish to set tracking information for this branch you can do so with:
0|     git branch --set-upstream-to=/
master
0| [INFO] Backing up directly to remote repository...
0| [ERROR] Failed to backup to remote repository: error: src refspec main does not match any
0| error: failed to push some refs to 'https://github.com/censored/censored'
0| [INFO] Update process completed.
0| [INFO] Next sync scheduled in 10 minutes (04:10:37).
0| [INFO] Bot and data repositories are up-to-date. Starting bot...
0| [INFO] Starting update process...
0| [INFO] Syncing code repository...
0| [INFO] Pulling latest changes for the code repository...
0| [ERROR] Failed to synchronize code repository: fatal: couldn't find remote ref master
0| [INFO] Syncing data repository...
0| [INFO] Pulling latest changes for the data repository...
0| [ERROR] Failed to synchronize data repository: There is no tracking information for the current branch.
0| Please specify which branch you want to merge with.
0| See git-pull(1) for details.
0|     git pull 

0| If you wish to set tracking information for this branch you can do so with:
0|     git branch --set-upstream-to=/
master
0| [INFO] Backing up directly to remote repository...
0| [ERROR] Failed to backup to remote repository: On branch master
0| nothing to commit, working tree clean
0| [INFO] Update process completed.
0| [INFO] Next sync scheduled in 10 minutes (04:20:39).
0| [INFO] Scheduling recurring sync every 10 minutes.
Я ценю любую помощь, я просто хочу, чтобы это сработало.
Заранее спасибо.

Подробнее здесь: https://stackoverflow.com/questions/793 ... -correctly
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «Javascript»