Skip to content

Commit

Permalink
add: check-pr-api
Browse files Browse the repository at this point in the history
  • Loading branch information
lucastozo committed Apr 12, 2024
1 parent e0567f2 commit b9a4346
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 18 deletions.
33 changes: 33 additions & 0 deletions api/check-pr-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const axios = require('axios');
module.exports = async (req, res) =>
{
const { dataMode } = req.body;
const token = process.env.DLBR_AUTO_GITHUB_TOKEN;
const owner = 'lucastozo';
const repo = 'DemonlistBR';
const branch = 'list-changes-commits';

// Obter a lista de pull requests
const { data: pullRequests } = await axios.get(`https://api.github.com/repos/${owner}/${repo}/pulls`, {
headers: {
'Authorization': `token ${token}`
}
});

// Verificar se algum pull request está aberto
let title;
switch (dataMode)
{
case 1:
title = 'List Changes';
break;
case 2:
title = 'Records Changes';
break;
default:
res.status(400).json({ message: 'Modo de dados inválido' });
return;
}
const openPullRequest = pullRequests.find(pr => pr.state === 'open' && pr.head.ref === branch && pr.title === title);
res.status(200).json({ openPullRequest: !!openPullRequest });
}
14 changes: 0 additions & 14 deletions api/send-changes-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,6 @@ module.exports = async (req, res) =>
const content = Buffer.from(changes).toString('base64');
const branch = 'list-changes-commits';

// Obter a lista de pull requests
const { data: pullRequests } = await axios.get(`https://api.github.com/repos/${owner}/${repo}/pulls`, {
headers: {
'Authorization': `token ${token}`
}
});

// Verificar se algum pull request está aberto com o título "List Changes"
const openPullRequest = pullRequests.find(pr => pr.state === 'open' && pr.head.ref === branch && pr.title === 'List Changes');
if (openPullRequest) {
res.status(400).json({ message: 'Já existe um pull request aberto. Aguarde a revisão para criar um novo request.' });
return;
}

// Deletar a branch 'list-changes-commits'
await axios.delete(`https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${branch}`, {
headers: {
Expand Down
15 changes: 14 additions & 1 deletion assets/script/LevelData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ fetch('/data/listvalues.json')
extendedListMaxPosition = data.Data[0].extendedList;
});

IniciarLevelData();
checkOpenPR(1).then(isOpen => {
if(isOpen) {
var confirmMessage = 'ATENÇÂO: Existem alterações pendentes na lista. Os dados atuais podem estar desatualizados.\n' +
'Você não será capaz de enviar alterações até que essas alterações sejam fechadas.\n' +
'É aconselhável contatar um administrador para solicitar a revisão das alterações.\n\n' +
'Deseja continuar mesmo assim?';
if(!confirm(confirmMessage)) {
window.location.href = '/';
return;
}
}
IniciarLevelData();
});

function IniciarLevelData()
{
document.getElementById('overlay').style.display = 'flex';
Expand Down
15 changes: 14 additions & 1 deletion assets/script/PlayerData.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,20 @@ fetch('/data/listvalues.json')
extendedListMaxPosition = data.Data[0].extendedList;
});

IniciarPlayerData();
checkOpenPR(2).then(isOpen => {
if(isOpen) {
var confirmMessage = 'ATENÇÂO: Existem alterações pendentes na lista. Os dados atuais podem estar desatualizados.\n' +
'Você não será capaz de enviar alterações até que essas alterações sejam fechadas.\n' +
'É aconselhável contatar um administrador para solicitar a revisão das alterações.\n\n' +
'Deseja continuar mesmo assim?';
if(!confirm(confirmMessage)) {
window.location.href = '/';
return;
}
}
IniciarPlayerData();
});

function IniciarPlayerData()
{
document.getElementById('overlay').style.display = 'flex';
Expand Down
21 changes: 21 additions & 0 deletions assets/script/checkPR.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function checkOpenPR(dataMode) {
return fetch('/api/check-pr-api', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({dataMode}),
})
.then(response => {
if (!response.ok) {
return false;
}
return response.json();
})
.then(data => {
return data.openPullRequest;
})
.catch(error => {
return false;
});
}
17 changes: 15 additions & 2 deletions assets/script/sendChanges.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ function sendLevelChanges()
const json = ExportarLevel(document.getElementById('level-table'));
const changes = JSON.stringify(json, null, 2);

fetchAPI(userKey, tokenHash, changelog, changes, dataMode);
sendChanges(userKey, tokenHash, changelog, changes, dataMode);
}

function sendRecordsChanges()
Expand Down Expand Up @@ -99,7 +99,20 @@ function sendRecordsChanges()
const json = ExportarRecord(document.getElementById('player-table'));
const changes = JSON.stringify(json, null, 2);

fetchAPI(userKey, tokenHash, changelog, changes, dataMode);
sendChanges(userKey, tokenHash, changelog, changes, dataMode);
}

function sendChanges(userKey, tokenHash, changelog, changes, dataMode)
{
checkOpenPR(dataMode).then(isOpen => {
if(isOpen)
{
sendButtonHandler(1);
errorMsgHandler("Já existe um pull request aberto. Aguarde a revisão para criar um novo request.", 2);
return;
}
fetchAPI(userKey, tokenHash, changelog, changes, dataMode);
});
}

function fetchAPI(userKey, tokenHash, changelog, changes, dataMode)
Expand Down
1 change: 1 addition & 0 deletions pages/leveldata.html
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ <h1 class="modal-title fs-5" id="send-changesLabel">Enviar mudanças</h1>
</div>

<script src="/assets/script/sendChanges.js"></script>
<script src="/assets/script/checkPR.js"></script>
<script src="/assets/script/LevelData.js"></script>
<script src="/assets/script/getLevelInfo.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
Expand Down
1 change: 1 addition & 0 deletions pages/playerdata.html
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ <h1 class="modal-title fs-5" id="send-changesLabel">Enviar mudanças</h1>
</div>

<script src="/assets/script/sendChanges.js"></script>
<script src="/assets/script/checkPR.js"></script>
<script src="/assets/script/PlayerData.js"></script>
<script src="/assets/script/getLevelInfo.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL" crossorigin="anonymous"></script>
Expand Down

0 comments on commit b9a4346

Please sign in to comment.