From c31dbbf62aeb09951b328d8eb62b774fe13f40d3 Mon Sep 17 00:00:00 2001 From: Yash Kumar Verma Date: Sat, 30 May 2020 22:21:00 +0530 Subject: [PATCH] Fix old url issues, Ref #390 --- fixStrangeUrls.js | 57 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 fixStrangeUrls.js diff --git a/fixStrangeUrls.js b/fixStrangeUrls.js new file mode 100644 index 0000000..3a85a5e --- /dev/null +++ b/fixStrangeUrls.js @@ -0,0 +1,57 @@ +const config = require('./config') +const secrets = config.secrets +const Sequelize = require('sequelize') +const db = require('./utils/db') +const fs = require('fs') + +function generateGenericUrl(url) { + const extractSingleUrl = url.trim().split(' ')[0].split('#')[0] + const genericUrl = new URL(extractSingleUrl).pathname.replace(/\/+$/, '') + return `https://github.com${genericUrl}` +} + +async function worker() { + // these two claims are done twice, with a space, so no other option than to delete it + const firstConflict = await db.Database.query( + `DELETE FROM "claims" WHERE "pullUrl" = ' https://github.com/coding-blocks/gondor/pull/61'` + ) + + const secondConflict = await db.Database.query( + `DELETE FROM "claims" WHERE "pullUrl" = 'https://github.com/coding-blocks/boss/pull/308#issuecomment-633161908'` + ) + + // `DELETE FROM "claims" WHERE "pullUrl" = 'https://github.com/coding-blocks/boss/pull/308#issuecomment-633161908'` + const response = await db.Database.query( + `SELECT "issueUrl", "pullUrl", "id" FROM "claims" ` + ) + + const rows = response[0] + console.log(rows) + for (let i = 0; i < rows.length; i += 1) { + try { + let pullUrl = rows[i].pullUrl + let issueUrl = rows[i].issueUrl + pullUrl = generateGenericUrl(pullUrl) + issueUrl = generateGenericUrl(issueUrl) + console.log(`Processing id #${rows[i].id} ...`) + const dbResponse = await db.Claim.update( + { + issueUrl: issueUrl, + pullUrl: pullUrl + }, + { + where: { + id: rows[i].id + }, + returning: true + } + ) + } catch (error) { + fs.appendFileSync('errors.txt', `${error.message} in ${rows[i].id}\n`) + console.log(`>>>>>>>${error.message}`) + console.log(error) + } + } +} + +worker()