Skip to content

Instantly share code, notes, and snippets.

@rosinghal
Created May 11, 2020 12:56
Show Gist options
  • Save rosinghal/9c4ca522ba840e073e4352ee8a20c12f to your computer and use it in GitHub Desktop.
Save rosinghal/9c4ca522ba840e073e4352ee8a20c12f to your computer and use it in GitHub Desktop.
Mass update all Gitlab repositories inside group to have same integration (eg, Slack)
const Axios = require("axios");
const ACCESS_TOKEN = "YOUR_ACCESS_TOKEN";
const GROUP_ID = "GITLAB_GROUP_ID";
const WEBHOOK_URL = "YOUR_WEBHOOK_URL";
const gitlabInstance = Axios.create({
baseURL: "https://gitlab.com/api/v4",
headers: {
Authorization: `Bearer ${ACCESS_TOKEN}`
}
});
const fetchSubgroups = groupId => {
console.log(`Fetching subgroups for group #${groupId}`);
return gitlabInstance.get(`/groups/${groupId}/subgroups`);
};
const fetchProjects = groupId => {
console.log(`Fetching projects for group #${groupId}`);
return gitlabInstance.get(`/groups/${groupId}/projects`);
};
const updateSlackService = (service, projectId) => {
console.log(`Updating ${service} service for project #${projectId}`);
return gitlabInstance.put(`/projects/${projectId}/services/${service}`, {
webhook: WEBHOOK_URL,
username: "",
channel: "",
notify_only_broken_pipelines: false,
branches_to_be_notified: "all",
commit_events: true,
push_channel: "",
push_events: true,
job_events: true,
issue_channel: "",
issues_events: true,
confidential_issue_channel: "",
confidential_issues_events: true,
merge_request_channel: "",
merge_request_events: true,
note_channel: "",
note_events: true,
confidential_note_channel: "",
confidential_note_events: true,
tag_push_channel: "",
tag_push_events: true,
pipeline_channel: "",
pipeline_events: true,
wiki_page_channel: "",
wiki_page_events: true,
deployment_channel: "",
deployment_events: true
});
};
fetchSubgroups(GROUP_ID).then(subgroups => {
subgroups.data.forEach(subgroup => {
fetchProjects(subgroup.id).then(projects => {
projects.data.forEach(project => {
updateSlackService("slack", project.id);
});
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment