Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Add redirect to github org invitation page #129

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add redirect to github org invitation page
  • Loading branch information
ThomasKranitsas committed Jan 21, 2019
commit 7d476dc2cba638c50827c849c233385ef9006a44
Binary file modified .DS_Store
Binary file not shown.
4 changes: 2 additions & 2 deletions TopcoderXDeploy.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,12 @@ To this:

## Local DNS setup

For login to work, your local Topcoder-X-UI deployment needs to have a `*.topcoder-dev.com` DNS name. Our development environment uses `x.topcoder-dev.com`
For login to work, your local Topcoder-X-UI deployment needs to have a `*.topcoder-dev.com` DNS name. Our development environment uses `x.topcoder-dev.com`. But for local setup it's better to use another one to not interfere with the one deployed on DEV Topcoder environment. So better to use `topcoderx.topcoder-dev.com` which is already configured in `config.js`.

You can make this change in your local `/etc/hosts` file.

```
127.0.0.1 x.topcoder-dev.com
127.0.0.1 topcoderx.topcoder-dev.com
```

You can login with one of these sample accounts:
Expand Down
17 changes: 15 additions & 2 deletions src/controllers/GithubController.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
* @version 1.0
*/

const _ = require('lodash');
const superagent = require('superagent');
const superagentPromise = require('superagent-promise');
const helper = require('../common/helper');
Expand Down Expand Up @@ -168,8 +169,20 @@ async function addUserToTeamCallback(req, res) {
githubUserId: githubUser.id,
});
}
// redirect to success page
res.redirect(`${constants.USER_ADDED_TO_TEAM_SUCCESS_URL}/github`);

// check if user is already in the team or not yet
if (githubUser.state === 'active') {
// redirect user to the success page, to let user know that he is already in the team
res.redirect(`${constants.USER_ADDED_TO_TEAM_SUCCESS_URL}/github`);
} else {
// redirect user to organization invitation page
const teamDetails = await GithubService.getTeamDetails(team.ownerToken, team.teamId);
const organizationLogin = _.get(teamDetails, 'organization.login');
if (!organizationLogin) {
throw new errors.ValidationError(`Couldn't get organization of the team with id '${team.teamId}'.`);
}
res.redirect(`https://github.com/orgs/${organizationLogin}/invitation?via_email=1`);
}
}

module.exports = {
Expand Down
5 changes: 2 additions & 3 deletions src/front/src/app/members/member.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ <h2>{{title}}</h2>
<div class="ibox float-e-margins">
<div class="ibox-content">
<div class="text-center m-t-lg" ng-if="provider==='github'">
<h3>You were successfully invited to the team!</h3>
<p>An invitation email will be sent to you if you are not already in the team.</p>
<p>You are already in the team!</p>
</div>
<div class="text-center m-t-lg" ng-if="provider==='gitlab'">
<p>You were successfully added to the group!</p>
Expand All @@ -22,4 +21,4 @@ <h3>You were successfully invited to the team!</h3>
</div>
</div>
</div>
</div>
</div>
38 changes: 35 additions & 3 deletions src/services/GithubService.js
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ getTeamRegistrationUrl.schema = Joi.object().keys({
async function addTeamMember(teamId, ownerUserToken, normalUserToken) {
let username;
let id;
let state;
try {
// get normal user name
const githubNormalUser = new GitHub({
Expand All @@ -197,7 +198,8 @@ async function addTeamMember(teamId, ownerUserToken, normalUserToken) {
token: ownerUserToken,
});
const team = github.getTeam(teamId);
await team.addMembership(username);
const membershipResponse = await team.addMembership(username);
state = _.get(membershipResponse, 'data.state')
} catch (err) {
// if error is already exists discard
if (_.chain(err).get('body.errors').countBy({
Expand All @@ -208,8 +210,8 @@ async function addTeamMember(teamId, ownerUserToken, normalUserToken) {
throw helper.convertGitHubError(err, 'Failed to add team member');
}
}
// return github username
return {username, id};
// return github username and its state
return {username, id, state};
}

addTeamMember.schema = Joi.object().keys({
Expand Down Expand Up @@ -240,12 +242,42 @@ getUserIdByUsername.schema = Joi.object().keys({
username: Joi.string().required(),
});

/**
* Get team detailed data
*
* @param {String} token user owner token
* @param {String|Number} teamId team id
*
* @returns {Object} team object, see https://developer.github.com/v3/teams/#get-team
*/
async function getTeamDetails(token, teamId) {
const teamIdAsNumber = !_.isNumber(teamId) ? parseInt(teamId, 10) : teamId
let team;

try {
const github = new GitHub({token});
const teamResponse = await github.getTeam(teamIdAsNumber).getTeam();

team = teamResponse.data;
} catch (err) {
throw helper.convertGitHubError(err, `Failed to get team with id '${teamId}'.`);
}

return team;
}

getTeamDetails.schema = Joi.object().keys({
token: Joi.string().required(),
teamId: Joi.alternatives().try(Joi.string(), Joi.number()).required(),
});

module.exports = {
ensureOwnerUser,
listOwnerUserTeams,
getTeamRegistrationUrl,
addTeamMember,
getUserIdByUsername,
getTeamDetails,
};

helper.buildService(module.exports);