Skip to content

Commit

Permalink
GitHub API requests are upgraded to basic auth.
Browse files Browse the repository at this point in the history
Rate limit increased from 60 requests per hour to 5000 requests per hour
  • Loading branch information
techrube committed Feb 5, 2018
1 parent 4375a81 commit 8f8bc60
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions rules.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"use strict";
const request = require("request-promise")
const steem = require("steem");
const GITHUB_API = "https://api.github.com"
steem.api.setOptions({url: "https://api.steemit.com"});

var rules = {}
Expand Down Expand Up @@ -34,10 +33,11 @@ rules.githubCheck = (checkList) => {
for(let i=0; i<links.length; i++){
if(links[i].search("https:\/\/github\.com\/"+jsonMetaData.repository.full_name+"\/pull\/") == 0){
let prNo = links[i].split("/")[6]
let prUrl = GITHUB_API + "/repos/"+jsonMetaData.repository.full_name+"/pulls/"+prNo.toString()
let prUrl = "/repos/"+jsonMetaData.repository.full_name+"/pulls/"+prNo.toString()
getDataByUrl(prUrl)
.then((prJson) => {handlePR(JSON.parse(prJson))})
.then(() => resolve()).catch((err) => reject(err))
.then((prJson) => {handlePR(JSON.parse(prJson))})
.then(() => resolve())
.catch((err) => reject(err))
return false //stop searching, when the first pull request link is found on the contribution's body
}
}
Expand Down Expand Up @@ -70,7 +70,7 @@ rules.githubCheck = (checkList) => {
for(let i=0; i<links.length; i++){
if(links[i].search("https:\/\/github\.com\/" + jsonMetaData.repository.full_name + "\/commit\/") == 0){
let prNo = links[i].split("/")[6]
getDataByUrl(GITHUB_API + "/repos/" + jsonMetaData.repository.full_name + "/commits/" + prNo.toString())
getDataByUrl("/repos/" + jsonMetaData.repository.full_name + "/commits/" + prNo.toString())
.then((cmJson) => handleCommit(cmJson))
.then(() => resolve())
.catch((err) => reject(err))
Expand Down Expand Up @@ -104,7 +104,7 @@ rules.githubCheck = (checkList) => {
username = jsonMetaData.repository.owner.login
}
return new Promise((resolve, reject) => {
getDataByUrl(GITHUB_API + "/users/" + username).then((res) => {
getDataByUrl("/users/" + username).then((res) => {
let githubProfile = JSON.parse(res)
let rgx = new RegExp(utopianName, 'i')
if(githubProfile.name != null && (githubProfile.name).match(rgx) != null)
Expand All @@ -114,7 +114,7 @@ rules.githubCheck = (checkList) => {
if(githubProfile.bio != null && (githubProfile.bio).match(rgx) != null)
repo.name_check = true
resolve()
}).catch((err)=> reject(err))
}).catch((err) => reject(err))
})
}

Expand Down Expand Up @@ -149,17 +149,24 @@ rules.githubCheck = (checkList) => {
})
}

var getDataByUrl = (prUrl) => {
var getDataByUrl = (path) => {
return new Promise((resolve, reject) => {
let options = {
url: prUrl,
url: "https://api.github.com" + path,
headers: {
'User-Agent': 'scoutopian'
}
'User-Agent': 'scoutopian',
'Authorization': 'Basic ' + new Buffer(process.env.GITHUB_PAT).toString('base64')
},
resolveWithFullResponse: true,
}
request(options)
.then((result) => resolve(result))
.catch((err) => reject(err))
.then((response) => {
if(parseInt(response.headers['x-ratelimit-remaining']) <= 0)
reject("GitHub API rate limit exceeded! Limit will be reset " +
((parseInt(response.headers['x-ratelimit-reset'] + "000") - Date.now())/60000).toPrecision(3) + " mins later\n")
else
resolve(response.body)
}).catch((err) => reject(err))
})
}

Expand All @@ -181,8 +188,8 @@ rules.githubCheck = (checkList) => {

var fetchRepoData = () => {
return new Promise((resolve, reject) => {
Promise.all([getDataByUrl(GITHUB_API + "/repos/" + repoFullName + "/readme")
, getDataByUrl(GITHUB_API + "/repos/" + repoFullName)])
Promise.all([getDataByUrl("/repos/" + repoFullName + "/readme")
, getDataByUrl("/repos/" + repoFullName)])
.then((results) => {
let readme = results[0]
if(readme.message == undefined)
Expand Down Expand Up @@ -289,14 +296,16 @@ rules.completeRuleCheck = (url) => {
rules.getContribution(url)
.then((contribution) => {
checkList.contribution = contribution
})
.then(() =>
Promise.all([rules.githubCheck(checkList), rules.downvoteCheck(checkList.contribution.active_votes)])
.then((results) => {
checkList.github = results[0]
checkList.downvotes = results[1]
checkList.score = rules.score.value
resolve(checkList)
})
}).catch((err) => reject(err))
).catch((err) => reject(err))
})


Expand Down

0 comments on commit 8f8bc60

Please sign in to comment.