Skip to content

Instantly share code, notes, and snippets.

@darkterminal
Last active July 1, 2022 13:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save darkterminal/bda55b52001bd520332ccdc95b99ba60 to your computer and use it in GitHub Desktop.
Save darkterminal/bda55b52001bd520332ccdc95b99ba60 to your computer and use it in GitHub Desktop.
Gitlab Release JS
const _importDynamic = new Function('modulePath', 'return import(modulePath)');
const fetch = async function (...args) {
const { default: fetch } = await _importDynamic('node-fetch');
return fetch(...args);
}
const pkg = require('./package.json')
const PROPERTIES = {
host: 'https://gitlab.com/api/v4',
token: '<GITLAB-ACCESS-TOKEN>',
projectId: '<PROJECT-ID>',
repositoryUrl: 'https://gitlab.example.com/<YOUR-GROUP>/<REPOSITORY-NAME>'
}
function endpoint(path) {
return `${PROPERTIES.host}${path}`
}
function makeCommitUrl(commit_id) {
return `[${commit_id}](${PROPERTIES.repositoryUrl}/-/commit/${commit_id})`
}
async function getTagBeforeLatest() {
let tagname = 'v' + pkg.version
let response = await fetch(endpoint(`/projects/${PROPERTIES.projectId}/repository/tags`), {
headers: {
"Private-Token": PROPERTIES.token
}
})
let data = await response.json()
let tags = data.filter(tag => tag.name != tagname)
let prevTag = tags[0].name
return prevTag
}
async function generateChangelog() {
let prevTag = await getTagBeforeLatest()
let response = await fetch(endpoint(`/projects/${PROPERTIES.projectId}/repository/commits?ref_name=${prevTag}...v${pkg.version}`), {
headers: {
"Private-Token": PROPERTIES.token
}
})
let data = await response.json()
let notes = {
feat: {
title: 'Features',
lists: []
},
fix: {
title: 'Fixes',
lists: []
},
docs: {
title: 'Docs',
lists: []
},
style: {
title: 'Styles',
lists: []
},
refactor: {
title: 'Refactors',
lists: []
},
pref: {
title: 'Prefs',
lists: []
},
build: {
title: 'Builds',
lists: []
},
chore: {
title: 'Chores',
lists: []
},
ci: {
title: 'CI',
lists: []
}
}
for (let i = 0; i < data.length; i++) {
if (data[i].title.startsWith('feat')) {
notes.feat.lists.push(`- ${data[i].title} - Commit: ${makeCommitUrl(data[i].short_id)} by ${data[i].committer_name}`)
}
if (data[i].title.startsWith('fix')) {
notes.fix.lists.push(`- ${data[i].title} - Commit: ${makeCommitUrl(data[i].short_id)} by ${data[i].committer_name}`)
}
if (data[i].title.startsWith('build')) {
notes.build.lists.push(`- ${data[i].title} - Commit: ${makeCommitUrl(data[i].short_id)} by ${data[i].committer_name}`)
}
if (data[i].title.startsWith('chore')) {
notes.chore.lists.push(`- ${data[i].title} - Commit: ${makeCommitUrl(data[i].short_id)} by ${data[i].committer_name}`)
}
if (data[i].title.startsWith('docs')) {
notes.docs.lists.push(`- ${data[i].title} - Commit: ${makeCommitUrl(data[i].short_id)} by ${data[i].committer_name}`)
}
if (data[i].title.startsWith('style')) {
notes.style.lists.push(`- ${data[i].title} - Commit: ${makeCommitUrl(data[i].short_id)} by ${data[i].committer_name}`)
}
if (data[i].title.startsWith('refactor')) {
notes.refactor.lists.push(`- ${data[i].title} - Commit: ${makeCommitUrl(data[i].short_id)} by ${data[i].committer_name}`)
}
if (data[i].title.startsWith('pref')) {
notes.pref.lists.push(`- ${data[i].title} - Commit: ${makeCommitUrl(data[i].short_id)} by ${data[i].committer_name}`)
}
if (data[i].title.startsWith('ci')) {
notes.ci.lists.push(`- ${data[i].title} - Commit: ${makeCommitUrl(data[i].short_id)} by ${data[i].committer_name}`)
}
}
// Genereate tamplate
var markdown = `# Changelog v${pkg.version}\n`
// Features notes
if (notes.feat.lists.length) {
markdown += `\n#### :triangular_flag_on_post: ${notes.feat.title}\n`
for (let i = 0; i < notes.feat.lists.length; i++) {
markdown += notes.feat.lists[i] + '\n'
}
}
// Fixes notes
if (notes.fix.lists.length) {
markdown += `\n#### :hammer_and_wrench: ${notes.fix.title}\n`
for (let i = 0; i < notes.fix.lists.length; i++) {
markdown += notes.fix.lists[i] + '\n'
}
}
// Docs notes
if (notes.docs.lists.length) {
markdown += `\n#### :notebook_with_decorative_cover: ${notes.docs.title}\n`
for (let i = 0; i < notes.docs.lists.length; i++) {
markdown += notes.docs.lists[i] + '\n'
}
}
// Styles notes
if (notes.style.lists.length) {
markdown += `\n#### :notebook_with_decorative_cover: ${notes.style.title}\n`
for (let i = 0; i < notes.style.lists.length; i++) {
markdown += notes.style.lists[i] + '\n'
}
}
// Refactors notes
if (notes.refactor.lists.length) {
markdown += `\n#### :notebook_with_decorative_cover: ${notes.refactor.title}\n`
for (let i = 0; i < notes.refactor.lists.length; i++) {
markdown += notes.refactor.lists[i] + '\n'
}
}
// Prefs notes
if (notes.pref.lists.length) {
markdown += `\n#### :paintbrush: ${notes.pref.title}\n`
for (let i = 0; i < notes.pref.lists.length; i++) {
markdown += notes.pref.lists[i] + '\n'
}
}
// Chores notes
if (notes.chore.lists.length) {
markdown += `\n#### :house: ${notes.chore.title}\n`
for (let i = 0; i < notes.chore.lists.length; i++) {
markdown += notes.chore.lists[i] + '\n'
}
}
// Build notes
if (notes.chore.lists.length) {
markdown += `\n#### :building_construction: ${notes.build.title}\n`
for (let i = 0; i < notes.build.lists.length; i++) {
markdown += notes.build.lists[i] + '\n'
}
}
// CI notes
if (notes.ci.lists.length) {
markdown += `\n#### :construction_worker: ${notes.ci.title}\n`
for (let i = 0; i < notes.ci.lists.length; i++) {
markdown += notes.ci.lists[i] + '\n'
}
}
return markdown
}
async function createRelease() {
try {
let tagname = 'v' + pkg.version
let releaseNotes = await generateChangelog()
let params = JSON.stringify({
name: "New release " + tagname,
tag_name: tagname,
description: releaseNotes
})
let response = await fetch(endpoint('/projects/' + PROPERTIES.projectId + '/releases'), {
body: params,
headers: {
"Content-Type": "application/json",
"Private-Token": PROPERTIES.token
},
method: "POST"
})
let data = await response.json()
console.log(data)
} catch (error) {
console.log(error)
}
}
createRelease()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment