Skip to content

Instantly share code, notes, and snippets.

@emilhein
Last active March 25, 2022 12:32
Show Gist options
  • Save emilhein/2013d3cddc45f9ab1f8b0ae86bbac0d4 to your computer and use it in GitHub Desktop.
Save emilhein/2013d3cddc45f9ab1f8b0ae86bbac0d4 to your computer and use it in GitHub Desktop.
const fetch = require('node-fetch');
const getPostObj = ({ branch = 'main', msg = 'auto commit', content = '', file_path = null, action = 'update' }) => ({
branch,
commit_message: msg,
"actions": [
{
action,
file_path,
content
},
]
})
const commit = async () => {
let project = 'YOUR_PROJECT_ID'
let fileContent = 'IM just a text file'
let obj = getPostObj({
content: JSON.stringify(fileContent, null, 2),
file_path: 'PATH_IN_YOUR_REPO.txt'
})
let headers = {
'PRIVATE-TOKEN': 'YOUR_PRIVATE_TOKEN',
'Content-Type': 'application/json'
}
let url = `https://gitlab.com/api/v4/projects/${project}/repository/commits`
let res = await fetch(url, {
method: 'POST', // or 'PUT'
headers: { ...headers },
body: JSON.stringify(obj, null, 2),
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment