From 395403d18a18810d8500addcaefcbc6ff277f795 Mon Sep 17 00:00:00 2001 From: Yash Kumar Verma Date: Sat, 30 May 2020 22:20:33 +0530 Subject: [PATCH 1/2] Ensure new claims have clean urls, Fixes: #390 --- routes/api.js | 4 ++-- routes/root.js | 4 ++-- utils/datautils.js | 7 +++++++ 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/routes/api.js b/routes/api.js index 6e0ddbb..8d86241 100644 --- a/routes/api.js +++ b/routes/api.js @@ -69,8 +69,8 @@ route.post('/claims/add', auth.ensureLoggedInGithub, (req, res) => { du.createClaim( req.user.usergithub.username, - req.body.issue_url, - req.body.pull_url, + du.generateGenericUrl(req.body.issue_url), + du.generateGenericUrl(req.body.pull_url), req.body.bounty, config.CLAIM_STATUS.CLAIMED ) diff --git a/routes/root.js b/routes/root.js index a4b548d..a751701 100644 --- a/routes/root.js +++ b/routes/root.js @@ -248,8 +248,8 @@ route.post('/claims/add', auth.ensureLoggedInGithub, (req, res) => { du.createClaim( req.user.usergithub.username, // github username already valid - req.body.issue_url, - req.body.pull_url, + du.generateGenericUrl(req.body.issue_url), + du.generateGenericUrl(req.body.pull_url), req.body.bounty, config.CLAIM_STATUS.CLAIMED ) diff --git a/utils/datautils.js b/utils/datautils.js index 0efa24c..ff0a21d 100644 --- a/utils/datautils.js +++ b/utils/datautils.js @@ -5,6 +5,12 @@ const db = require('./db') const fs = require('fs') const consts = require('./consts') +function generateGenericUrl(url) { + const extractSingleUrl = url.trim().split(' ')[0].split('#')[0] + const genericUrl = new URL(extractSingleUrl).pathname.replace(/\/+$/, '') + return `https://github.com${genericUrl}` +} + function getContestPeriod(year) { if (year) return { @@ -180,5 +186,6 @@ module.exports = { getLoggedInUserStats, getClaimById, updateClaim, + generateGenericUrl, getCounts } From 123650953805ffe54a96ce9656e0e80aa17b5251 Mon Sep 17 00:00:00 2001 From: Yash Kumar Verma Date: Sat, 30 May 2020 22:21:00 +0530 Subject: [PATCH 2/2] Fix old url issues, Ref #390 --- fixStrangeUrls.js | 65 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 fixStrangeUrls.js diff --git a/fixStrangeUrls.js b/fixStrangeUrls.js new file mode 100644 index 0000000..2e2f4fd --- /dev/null +++ b/fixStrangeUrls.js @@ -0,0 +1,65 @@ +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'` + ) + + const thirdConflict = await db.Database.query( + `UPDATE "claims" SET "issueUrl"='https://github.com/coding-blocks/gondor/pull/22' WHERE "pullUrl" = 'https://github.com/coding-blocks/gondor/pull/22'` + ) + + const fourthConflict = await db.Database.query( + `UPDATE "claims" SET "issueUrl"='https://github.com/coding-blocks/make-a-pr/pull/717' WHERE "pullUrl" = 'https://github.com/coding-blocks/make-a-pr/pull/717'` + ) + + // `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()