Created
June 14, 2020 13:19
-
-
Save skippednote/46046777390573aa06367d8b85ef9e14 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You'd need to install
npm i node-fetch
before you can run this.