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

Allow org level configuration #49

Open
wants to merge 3 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
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