Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GitHub ID verifier #50

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
.serverless
serverless.yml
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"lint": "eslint \"src/*.js\"",
"prettier:fix": "prettier --write \"{src,spec}/**/*.js\"",
"serverless": "serverless",
"serverless-setup-credentials": "serverless config credentials --provider aws"
"serverless-setup-credentials": "serverless config credentials --provider aws",
"deploy": "serverless deploy"
},
"author": "",
"license": "Apache-2.0"
Expand Down
22 changes: 18 additions & 4 deletions src/committerFinder.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,27 @@ const getUniqueCommitters = arr =>
self.findIndex(c => c.email == value.email) === index
);

const getUserPayload = userData => {
let ghId = undefined;
if (userData.length > 0) {
if (userData[0].identities) {
ghIdentity = userData[0].identities.filter(identity => identity.provider == 'github');
if (ghIdentity.length > 0) {
ghId = ghIdentity[0].extern_uid;
}
}
return {
...userData[0],
login: userData[0].username,
gitHubId: ghId
}
} else return undefined;
}

const hydrateGitlabUserInfo = async (usersToVerify, token) =>
Promise.all(
usersToVerify.map(user =>
gitlabRequest(getUserInfo(user.email), token).then(response => ({
...user,
login: response.length > 0 ? response[0].username : undefined
}))
gitlabRequest(getUserInfo(user.email), token).then(response => getUserPayload(response))
)
);

Expand Down
18 changes: 12 additions & 6 deletions src/contributionVerifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ function partition(array, isValid) {

const domainFromEmail = email => "@" + email.split("@")[1];

const contributorArrayVerifier = contributors => committers => {
const contributorArrayVerifier = (contributors, useGitHubIDs) => committers => {
const lowerCaseContributors = contributors.map(c => c.toLowerCase());
const [
emailVerification,
Expand All @@ -36,8 +36,14 @@ const contributorArrayVerifier = contributors => committers => {
return true;
}
}
if (usernameVerification.includes(c.login.toLowerCase())) {
return true;
if (useGitHubIDs) {
if (usernameVerification.includes(c.gitHubId)) {
return true;
}
} else {
if (usernameVerification.includes(c.login.toLowerCase())) {
return true;
}
}
return false;
};
Expand All @@ -50,7 +56,7 @@ const configFileFromUrlVerifier = contributorListUrl => committers =>
requestp({
url: contributorListUrl,
json: true
}).then(contributors => contributorArrayVerifier(contributors)(committers));
}).then(contributors => contributorArrayVerifier(contributors, false)(committers));

const webhookVerifier = webhookUrl => committers =>
Promise.all(
Expand All @@ -67,7 +73,7 @@ const webhookVerifier = webhookUrl => committers =>
const contributors = responses
.filter(r => r.isContributor)
.map(r => r.username);
return contributorArrayVerifier(contributors)(committers);
return contributorArrayVerifier(contributors, false)(committers);
});

module.exports = config => {
Expand All @@ -78,7 +84,7 @@ module.exports = config => {
logger.info(
"Checking contributors against the list supplied in the .clabot file"
);
return contributorArrayVerifier(configCopy.contributors);
return contributorArrayVerifier(configCopy.contributors, configCopy.useGitHubIDs);
} else if (
is.url(configCopy.contributors) &&
configCopy.contributors.indexOf("?") !== -1
Expand Down
12 changes: 12 additions & 0 deletions src/gitlabApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@ exports.getUserInfo = emailAddress => ({
method: "GET"
});

exports.searchNamespaceByName = nsName => ({
// This assumes master branch, might need to rework this at some point
url: `https://${process.env.GITLAB_INSTANCE_DOMAIN}/api/v4/namespaces?search=${nsName}`,
method: "GET"
});

exports.getOrgConfigProject = nsId => ({
// This assumes master branch, might need to rework this at some point
url: `https://${process.env.GITLAB_INSTANCE_DOMAIN}/api/v4/groups/${nsId}/search?scope=projects&search=clabot-config`,
method: "GET"
});

exports.getProjectClaFile = projectId => ({
// This assumes master branch, might need to rework this at some point
url: `https://${process.env.GITLAB_INSTANCE_DOMAIN}/api/v4/projects/${projectId}/repository/files/%2Eclabot/raw?ref=master`,
Expand Down
25 changes: 22 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ const gitLabInfo = webhook =>
webhook.object_kind === "note"
? {
projectId: webhook.project.id,
namespace: webhook.project.path_with_namespace.split("/")[0],
mergeRequestId: webhook.merge_request.iid,
projectUrl: webhook.project.web_url
}
: {
projectId: webhook.project.id,
namespace: webhook.project.path_with_namespace.split("/")[0],
mergeRequestId: webhook.object_attributes.iid,
projectUrl: webhook.project.web_url
};
Expand Down Expand Up @@ -87,12 +89,14 @@ exports.Handler = constructHandler(async webhook => {

logger.info("The cla-bot has been summoned by a comment");
}

const token = obtainToken(webhook);
const {
addComment,
getMergeRequest,
getProjectClaFile,
searchNamespaceByName,
getOrgConfigProject,
setCommitStatus,
getProjectLabels,
createProjectLabel,
Expand All @@ -101,6 +105,7 @@ exports.Handler = constructHandler(async webhook => {

const {
projectId: projectId,
namespace: namespace,
mergeRequestId: mergeRequestId,
projectUrl: projectUrl
} = gitLabInfo(webhook);
Expand All @@ -115,8 +120,22 @@ exports.Handler = constructHandler(async webhook => {
const MRInfo = await getMergeRequest(projectId, mergeRequestId);
const headSha = MRInfo.sha;

// TODO : Investigate group level .clabot file.
let claConfig = await getProjectClaFile(projectId);
// Try to load project clabot-config, from
// the same project namespace (aka GitLab Group)
let ns = await searchNamespaceByName(namespace);
let nsId = ns[0].id;
logger.info(`Namespace is ${namespace}, ID ${nsId}`);
let orgConfigProject = await getOrgConfigProject(nsId);
let claConfig = null;
if (orgConfigProject) {
logger.info(`Found clabot-config project, ID is ${orgConfigProject[0].id}`);
claConfig = await getProjectClaFile(orgConfigProject[0].id);
}

if (!is.json(claConfig)) {
claConfig = await getProjectClaFile(projectId);
}

if (!is.json(claConfig)) {
logger.error("The .clabot file is not valid JSON");
await setCommitStatus(projectId, headSha, "failed", botName);
Expand Down