Skip to content

Instantly share code, notes, and snippets.

@skippednote
Created June 14, 2020 13:19
Show Gist options
  • Save skippednote/46046777390573aa06367d8b85ef9e14 to your computer and use it in GitHub Desktop.
Save skippednote/46046777390573aa06367d8b85ef9e14 to your computer and use it in GitHub Desktop.
const fetch = require('node-fetch');
const request = async (url, data = {}) => {
const response = await fetch(`https://gitlab.com/api/v4/${url}`, {
headers: {
'PRIVATE-TOKEN': process.env.GITLAB_TOKEN,
...data.headers,
},
...data,
});
return response;
};
const getProjects = async (id) => {
const response = await request(`groups/${id}`);
const project = response.json();
return project;
};
const createBranch = async (id, ref, name) => {
await request(
`projects/${id}/repository/branches?branch=${name}&ref=${ref}`,
{
method: 'POST',
}
);
};
const defaultBranch = async (id, name) => {
await request(`projects/${id}`, {
method: 'PUT',
body: `default_branch=${name}`,
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
},
});
};
const deleteBranch = async (id, ref) => {
await request(`projects/${id}/repository/branches/${ref}`, {
method: 'DELETE',
});
};
async function main() {
try {
const { projects } = await getProjects(23);
for (const { id, default_branch } of projects) {
if (default_branch === 'master') {
await createBranch(id, default_branch, 'main');
await defaultBranch(id, 'main');
await deleteBranch(id, 'master');
}
}
} catch (e) {
console.error(e);
}
}
main();
@skippednote
Copy link
Author

You'd need to install npm i node-fetch before you can run this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment