Skip to content

Commit

Permalink
add API to get the real name of contributor automatically from GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
xpdavid committed Jan 14, 2019
1 parent 254d0bd commit 9675ae2
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 19 deletions.
1 change: 1 addition & 0 deletions release-script/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ config.js
package-lock.json
npm-debug.log
node_modules/
.idea/
2 changes: 1 addition & 1 deletion release-script/config.template.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ module.exports = {
githubPassword: 'password',

// Directory of TEAMMATES project
teammatesDir: '../teammates',
teammatesDir: '../../teammates',

// Milestone number in GitHub, e.g. for V6.0.0 (https://github.com/TEAMMATES/teammates/milestone/254) => milestone number 254
milestone: 254,
Expand Down
7 changes: 4 additions & 3 deletions release-script/milestone-api.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const request = require('request');
const { githubUsername, milestone } = require('./config');
const { githubUsername, githubPassword, milestone } = require('./config');

if (!githubUsername || !milestone) {
console.log('Milestone API could not proceed. Please specify a milestone and your GitHub username for authentication.');
if (!githubUsername || !githubPassword || !milestone) {
console.log('Milestone API could not proceed. Please specify a milestone and your GitHub username & password for authentication.');
process.exit();
}

Expand All @@ -13,6 +13,7 @@ function createRequestObject(uri) {
uri,
headers: {
'User-Agent': githubUsername,
Authorization: `Basic ${Buffer.from(`${githubUsername}:${githubPassword}`).toString('base64')}`,
},
};
}
Expand Down
57 changes: 42 additions & 15 deletions release-script/pre-release.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const rl = require('readline-sync');
const { EOL: eol } = require('os');
const { teammatesDir } = require('./config');
const { getMilestoneInfo } = require('./milestone-api');
const { getDisplayNameOfUser } = require('./user-api');

const developersJsonDir = './src/main/webapp/data/developers.json';

Expand Down Expand Up @@ -139,27 +140,53 @@ function checkPrMetadata() {
});
}

function syncContributorsName(contributors, index, onComplete) {
if (contributors.length === index) {
onComplete();
return;
}
const currentContributor = contributors[index];
if (!currentContributor.username) {
syncContributorsName(contributors, index + 1, onComplete);
}

getDisplayNameOfUser(currentContributor.username, (name) => {
if (name) {
currentContributor.name = name;
}
// try not to exceed the rate limit of API
setTimeout(() => {
syncContributorsName(contributors, index + 1, onComplete);
}, 200);
});
}

function checkAndUpdateDevelopersJson() {
prsInMilestone.forEach(pr => checkUsername(pr.user.login));
if (newContributors.length) {
Object.keys(newContributors).forEach(i => allDevs.contributors.push({ name: '', username: newContributors[i] }));
fs.writeFile(developersJsonDir, JSON.stringify(allDevs, null, 2) + eol, 'UTF-8', () => {
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
const newContributorsDetail = [];
Object.keys(newContributors).forEach(i => newContributorsDetail.push({ username: newContributors[i] }));
syncContributorsName(newContributorsDetail, 0, () => {
allDevs.contributors.push(...newContributorsDetail);

fs.writeFile(developersJsonDir, JSON.stringify(allDevs, null, 2) + eol, 'UTF-8', () => {
function capitalize(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}

console.log('The execution is now paused and the username of new contributors have been temporarily added.');
console.log('Enter the real names of the new contributors manually, then press enter on the console to continue.');
rl.question();
console.log('Please proofread the real names of newly added contributors. '
+ 'Their real names may not be entered correctly on GitHub.');
rl.question();

allDevs = JSON.parse(fs.readFileSync(developersJsonDir, 'UTF-8'));
allDevs.contributors.sort((a, b) => {
const aName = a.name || capitalize(a.username);
const bName = b.name || capitalize(b.username);
return aName > bName ? 1 : -1;
});
allDevs = JSON.parse(fs.readFileSync(developersJsonDir, 'UTF-8'));
allDevs.contributors.sort((a, b) => {
const aName = a.name || capitalize(a.username);
const bName = b.name || capitalize(b.username);
return aName > bName ? 1 : -1;
});

updateDevelopersJson();
updateDevelopersJson();
});
});
} else if (needUpdate) {
updateDevelopersJson();
Expand Down
35 changes: 35 additions & 0 deletions release-script/user-api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
const request = require('request');
const { githubUsername, githubPassword } = require('./config');

if (!githubUsername || !githubPassword) {
console.log('User API could not proceed. Please specify your GitHub username & password for authentication.');
process.exit();
}

function createRequestObject(uri) {
return {
uri,
headers: {
'User-Agent': githubUsername,
Authorization: `Basic ${Buffer.from(`${githubUsername}:${githubPassword}`).toString('base64')}`,
},
};
}

function getDisplayNameOfUser(username, callback) {
const uri = createRequestObject(`https://api.github.com/users/${username}`);
request(uri, (error, response, body) => {
if (response.statusCode !== 200) {
console.log(`Error ${response.statusCode} when getting displayed name of user ${username}:`);
console.log(response.statusMessage);
return callback(undefined);
}

const userInfo = JSON.parse(body);
return callback(userInfo.name);
});
}

module.exports = {
getDisplayNameOfUser,
};

0 comments on commit 9675ae2

Please sign in to comment.