diff --git a/lib/common.js b/lib/common.js index 6ff376d49..ca3090be7 100644 --- a/lib/common.js +++ b/lib/common.js @@ -59,6 +59,20 @@ function createLabel(name, description, color) { .then(response => response.data); } +function updateLabel(name, description, color) { + return axios.patch(`https://api.github.com/repos/ioBroker/ioBroker.repositories/labels/${name}`, + { + 'description': `${description}`, + 'color': `${color}` + }, + { + headers: { + Authorization: process.env.OWN_GITHUB_TOKEN ? `token ${process.env.OWN_GITHUB_TOKEN}` : 'none', + 'user-agent': 'Action script' + } + }) + .then(response => response.data); +} function addComment(prID, body) { return axios.post(`https://api.github.com/repos/ioBroker/ioBroker.repositories/issues/${prID}/comments`, {body}, { @@ -148,6 +162,7 @@ module.exports = { addLabel, createLabel, deleteLabel, + updateLabel, getGithub, getUrl, createIssue, diff --git a/lib/stableBrandNewReminder.js b/lib/stableBrandNewReminder.js index 2c830c53b..9912e668b 100644 --- a/lib/stableBrandNewReminder.js +++ b/lib/stableBrandNewReminder.js @@ -3,12 +3,13 @@ const { addLabel, createLabel, deleteLabel, + updateLabel, getLabels, getAllComments, getGithub, } = require('./common'); -async function doIt() { +async function cleanupLabels(){ let labels = await getLabels(''); labels.forEach( (l)=> { const result = /^(\d\d)\.(\d\d)\.(\d\d\d\d)$/g.exec(l.name); @@ -16,12 +17,28 @@ async function doIt() { let targetTs=new Date(result[3], result[2]-1, result[1], 0, 0, 0).getTime(); const nowTs = new Date().getTime(); if ( nowTs > targetTs + 2* 24*60*60*1000) { - console.log(`Label ${l.name} is outdated and will be removed`); + console.log(` ${l.name} is outdated and will be removed`); deleteLabel('', l.name); } }; }); - +} + +async function cleanupIssueLabels(){ + const issues = await getGithub(`https://api.github.com/repos/iobroker/ioBroker.repositories/issues`); + for (const issue of issues ) { + console.log(`cleanup PR ${issue.number}`); + issue.labels.forEach((l) => { + const result = /^(\d\d)\.(\d\d)\.(\d\d\d\d)$/g.exec(l.name); + if (result) { + console.log(` ${l.name} will be removed`); + deleteLabel(issue.number, l.name); + } + }) + } +} + +async function handleBrandNew(){ const issues = await getGithub(`https://api.github.com/repos/iobroker/ioBroker.repositories/issues`); for (const issue of issues ) { if (issue.labels.find(label => label.name === 'STABLE - brand new')) { @@ -31,7 +48,7 @@ async function doIt() { let found=false; let comment; - comment = comments.find( c => /created (\d+\.\d+\.\d+)/g.exec(c.body)); + comment = comments.findLast( c => /created (\d+\.\d+\.\d+)/g.exec(c.body)); if (comment) { const result = /created (\d+)\.(\d+)\.(\d+)/g.exec(comment.body); if (result) { @@ -39,43 +56,49 @@ async function doIt() { targetTs += (7 * 86400 * 1000); const dateStr = new Date(targetTs).toLocaleDateString(); const nowTs = new Date().getTime(); + const label = `${dateStr}`; + let labels = await getLabels(''); + labels = labels.filter( (f) => { return f.name===`${label}`} ); + if (!labels.length) { + console.log(` will create label $label}`); + await createLabel(`${label}`, `remind after ${dateStr}`, `ffffff`); + } if ( nowTs < targetTs ) { console.log(` will merged after ${dateStr}`); - const label = `${dateStr}`; - let labels = await getLabels(''); - labels = labels.filter( (f) => { return f.name===`${label}`} ); - if (!labels.length) { - console.log(` will create label $label}`); - await createLabel(`${label}`, `remind after ${dateStr}`, `ffffff`); - } + await updateLabel(`${label}`, `remind after ${dateStr}`, `ffffff`); await addLabel(issue.number, [`${label}`]); } else { console.log(` should be merged now (deadline ${dateStr})`); + await updateLabel(`${label}`, `remind after ${dateStr}`, `ff0000`); + await addLabel(issue.number, [`${label}`]); await addLabel(issue.number, ['⚠️check']); } } found=true; } - comment = comments.find( c => /reminder (\d+\.\d+\.\d+)/g.exec(c.body)); + comment = comments.findLast( c => /reminder (\d+\.\d+\.\d+)/g.exec(c.body)); if (comment) { const result = /reminder (\d+)\.(\d+)\.(\d+)/g.exec(comment.body); if (result) { let targetTs=new Date(result[3], result[2]-1, result[1], 0, 0, 0).getTime(); const dateStr = new Date(targetTs).toLocaleDateString(); const nowTs = Date.now(); + const label = `${dateStr}`; + let labels = await getLabels(''); + labels = labels.filter( (f) => { return f.name===`${label}`} ); + if (!labels.length) { + console.log(` will create label ${label}`); + await createLabel(`${label}`, `remind after ${dateStr}`, `ffffff`); + } if ( nowTs < targetTs ) { console.log(` will remind at ${dateStr}`); - const label = `${dateStr}`; - let labels = await getLabels(''); - labels = labels.filter( (f) => { return f.name===`${label}`} ); - if (!labels.length) { - console.log(` will create label ${label}`); - await createLabel(`${label}`, `remind after ${dateStr}`, `ffffff`); - } + await updateLabel(`${label}`, `remind after ${dateStr}`, `ffffff`); await addLabel(issue.number, [`${label}`]); } else { console.log(` should be checked now (deadline ${dateStr})`); + await updateLabel(`${label}`, `remind after ${dateStr}`, `ff0000`); + await addLabel(issue.number, [`${label}`]); await addLabel(issue.number, ['⚠️check']); } } @@ -88,6 +111,59 @@ async function doIt() { } } } +} + +async function handleOthers(){ + const issues = await getGithub(`https://api.github.com/repos/iobroker/ioBroker.repositories/issues`); + for (const issue of issues ) { + if (! issue.labels.find(label => label.name === 'STABLE - brand new')) { + console.log(`checking PR ${issue.number}`); + const comments= await getAllComments( issue.number ); + + let comment = comments.findLast( c => /reminder (\d+\.\d+\.\d+)/g.exec(c.body)); + if (comment) { + const result = /reminder (\d+)\.(\d+)\.(\d+)/g.exec(comment.body); + if (result) { + let targetTs=new Date(result[3], result[2]-1, result[1], 0, 0, 0).getTime(); + const dateStr = new Date(targetTs).toLocaleDateString(); + const nowTs = Date.now(); + const label = `${dateStr}`; + let labels = await getLabels(''); + labels = labels.filter( (f) => { return f.name===`${label}`} ); + if (!labels.length) { + console.log(` will create label ${label}`); + await createLabel(`${label}`, `remind after ${dateStr}`, `ffffff`); + } + if ( nowTs < targetTs ) { + console.log(` will remind at ${dateStr}`); + await updateLabel(`${label}`, `remind after ${dateStr}`, `ffffff`); + await addLabel(issue.number, [`${label}`]); + } else { + console.log(` should be checked now (deadline ${dateStr})`); + await updateLabel(`${label}`, `remind after ${dateStr}`, `ff0000`); + await addLabel(issue.number, [`${label}`]); + } + } + } + } + } +} + +async function doIt() { + // cleanup labels + console.log ('check for outdated labels'); + await cleanupLabels(); + + console.log ('cleanup labels already set'); + await cleanupIssueLabels(); + + // process STABLE-brand new tagged issues + console.log ('process STABLE-brand-new issues'); + await handleBrandNew(); + + // process other reminders + console.log ('process normal issues'); + await handleOthers(); return 'done'; }