Skip to content

Commit

Permalink
New Feature : conflicting claims : Fixes coding-blocks#260
Browse files Browse the repository at this point in the history
  • Loading branch information
YashKumarVerma committed May 18, 2020
1 parent 857e507 commit 6ad5b9f
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 77 deletions.
53 changes: 45 additions & 8 deletions routes/root.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,52 @@ route.get('/claims/add', auth.ensureLoggedInGithub, (req, res) => {
})
})

route.get('/claims/:id', auth.adminOnly, (req, res) => {
du.getClaimById(req.params.id)
.then(claim => {
if (!claim) throw new Error('No claim found')
res.render('pages/claims/id', { claim })
})
.catch(err => {
res.send('Error fetching claim id = ' + escapeHtml(req.params.id))
route.get('/claims/:id', auth.adminOnly, async (req, res) => {
try {
const claimInfo = await du.getClaimById(req.params.id)
if (!claimInfo) {
throw new Error('Error finding claim')
}

const prSlug = new URL(claimInfo.pullUrl).pathname.replace(/\/+$/, '')
let response, conflicts

/**
* the database has internal checks for duplicate items in the pull request column
* therefore the main task of this segment is to handle the conditions when
* 1. issue is passed in both fields, pull-request column and issue column
* 2. pull request is submitted with a tailing slash, or url modifications like
* https, or hashes, or language slugs */
if (prSlug.includes('pull')) {
/**
* this segment handles point number 2 mentioned above by checking only the
* useful and important , unchanged, and unique identity of a pull request
*/
response = await du.getConflictingClaimByPr(prSlug)
conflicts = response[0].filter((claim) => claim.id !== claimInfo.id)
} else {
/**
* this segment handles the point number 1 mentioned above, by checking the
* url of the issue passed into pull-request column of current claim uniquely
* across database table for similar entries in the issues column
*/
response = await du.getConflictingClaimByIssue(prSlug)
conflicts = response[0].filter((claim) => claim.id !== claimInfo.id)
}

/**
* now rendering the ui of the admin panel and populating it with conflicts data
* the following line displays the raw json used to build the page, use while debugging
* res.json({ claimInfo, conflicts })
*/
res.render('pages/claims/id', { claim: claimInfo, conflicts })
} catch (err) {
console.log(err.message)
res.status(500).json({
error: true,
message: 'There was some error processing your request'
})
}
})

route.post('/claims/add', auth.ensureLoggedInGithub, (req, res) => {
Expand Down
16 changes: 15 additions & 1 deletion utils/datautils.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ function getClaimById(claimId) {
return db.Claim.findById(claimId)
}

function getConflictingClaimByPr(pullRequestId) {
return db.Database.query(
`SELECT * FROM claims WHERE "pullUrl" LIKE '%${pullRequestId}%';`
)
}

function getConflictingClaimByIssue(issueId) {
return db.Database.query(
`SELECT * FROM claims WHERE "issueUrl" LIKE '%${issueId}%';`
)
}

function delClaim(claimId) {
if (isNaN(+claimId)) {
return res.send('ClaimId must be a number')
Expand Down Expand Up @@ -161,5 +173,7 @@ module.exports = {
getLeaderboard,
getClaimById,
updateClaim,
getCounts
getCounts,
getConflictingClaimByIssue,
getConflictingClaimByPr
}
176 changes: 108 additions & 68 deletions views/pages/claims/id.hbs
Original file line number Diff line number Diff line change
@@ -1,76 +1,116 @@
<div class="four wide item">
<div class="image">
<div class="ui statistic">
<div class="value">
{{claim.bounty}}
</div>
<div class="label">
BOUNTY POINTS
</div>
</div>
</div>
<div class="middle aligned content">
<a class="header">{{claim.user}}</a>
<div class="meta">
<span>{{claim.repo}}</span>
</div>
<div class="description">
<p>
Issue : <a href="{{claim.issueUrl}}">{{claim.issueUrl}}</a>
</p>
<p>
Pull : <a href="{{claim.pullUrl}}">{{claim.pullUrl}}</a>
</p>
</div>
<div class="extra">
{{#equal claim.status "claimed"}}
<div class="ui label yellow">{{claim.status}}</div>
{{/equal}}
{{#equal claim.status "accepted"}}
<div class="ui label green">{{claim.status}}</div>
{{/equal}}
{{#equal claim.status "rejected"}}
<div class="ui label red">{{claim.status}}</div>
{{/equal}}
</div>
</div>
<form class="ui form" style="padding: 3em" method="post" action="/claims/{{claim.id}}/update">
<div class="four wide field">
<label>Final Bounty Points</label>
<input name="bounty" value="{{claim.bounty}}">
<label>Update Status</label>
<select name="status" class="ui dropdown" id="status">
<option value="claimed">Claimed</option>
<option value="rejected">Rejected</option>
<option value="accepted">Accepted</option>
<option value="disputed">Disputed</option>
<option value="revoked">Revoked</option>
</select>
</div>
<div class="ui container">
<div class="ui two column grid">
<div class="column">
<div class="four wide item">
<div class="image">
<div class="ui statistic">
<div class="value">
{{claim.bounty}}
</div>
<div class="label">
BOUNTY POINTS
</div>
</div>
</div>
<div class="middle aligned content">
<a class="header">{{claim.user}}</a>
<div class="meta">
<span>{{claim.repo}}</span>
</div>
<div class="description">
<p>
Issue : <a href="{{claim.issueUrl}}">{{claim.issueUrl}}</a>
</p>
<p>
Pull : <a href="{{claim.pullUrl}}">{{claim.pullUrl}}</a>
</p>
</div>
<div class="extra">
{{#equal claim.status "claimed"}}
<div class="ui label yellow">{{claim.status}}</div>
{{/equal}}
{{#equal claim.status "accepted"}}
<div class="ui label green">{{claim.status}}</div>
{{/equal}}
{{#equal claim.status "rejected"}}
<div class="ui label red">{{claim.status}}</div>
{{/equal}}
</div>
</div>
<form class="ui form" style="padding: 3em" method="post" action="/claims/{{claim.id}}/update">
<div class="four wide field">
<label>Final Bounty Points</label>
<input name="bounty" value="{{claim.bounty}}">
<label>Update Status</label>
<select name="status" class="ui dropdown" id="status">
<option value="claimed">Claimed</option>
<option value="rejected">Rejected</option>
<option value="accepted">Accepted</option>
<option value="disputed">Disputed</option>
<option value="revoked">Revoked</option>
</select>
</div>

<label>Reason </label>
<textarea name="reason" id="reason"> {{claim.reason}} </textarea>
<br />
<button class="ui button green" type="submit">Submit</button>

<label>Reason </label>
<textarea name="reason" id="reason"> {{claim.reason}} </textarea>
<br/>
<button class="ui button green" type="submit">Submit</button>

</form>
<script>
</form>
<script>
$('select.dropdown').dropdown();
$('select.dropdown').dropdown();
{ {#unless claim.reason } }
$('#reason').hide();
{
{
/unless}}
{{#unless claim.reason}}
$('#reason').hide();
{{/unless}}
$('select').on('change', function (e) {
let status = $('#status option:selected').text();
if (['Rejected', 'Disputed', 'Revoked'].includes(status))
$('#reason').show();
else
$('#reason').html('').hide();
$('select').on('change', function (e){
let status = $('#status option:selected').text();
if( ['Rejected' , 'Disputed' , 'Revoked'].includes(status) )
$('#reason').show();
else
$('#reason').html('').hide();
});
});
</script>
</div>
</div>
<div class="column">
<h2>Conflicts</h2>
{{#if conflicts}}
This following claims collides with the current claim
<div class="ui two column grid">
{{#conflicts}}
<div class="column">
<div class="ui fluid card">
<div class="content">
<div class="header">Claim ID : {{id}}</div>
<div class="meta">Bounty : {{bounty}}</div>
<div class="meta">By : {{user}}</div>
<div class="description">
<div class="ui two column grid">
<div class="column">
<a href="{{issueUrl}}">View Issue</a>
</div>
<div class="column">
<a href="{{pullUrl}}">View PR</a>
</div>
</div>
</div>
</div>

</script>
</div>
</div>
</div>
{{/conflicts}}
</div>
{{else}}
There are no conflicting claims
{{/if}}
</div>
</div>
</div>

0 comments on commit 6ad5b9f

Please sign in to comment.