Skip to content

Commit

Permalink
ci: processing changelog [no ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakutoc committed Dec 4, 2024
1 parent c3725a9 commit 3e82d8c
Show file tree
Hide file tree
Showing 6 changed files with 2,252 additions and 20 deletions.
16 changes: 16 additions & 0 deletions .github/actions/processing-release-changelog/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: processing-release-changelog

description: 'Transform release changelog with group by heading'

inputs:
data:
description: "Raw data for processing"
required: true

outputs:
changelog:
description: "changelog after processing"

runs:
using: 'node20'
main: 'index.js'
56 changes: 56 additions & 0 deletions .github/actions/processing-release-changelog/groupByHeadings.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
export function groupByHeadings(tree) {
const groups = new Map();
let nodes = [...tree.children];

let currentGroup = null;
let currentNodes = [];
let isFirstMainHeading = new Set(); // Для отслеживания первого вхождения заголовка

for (let i = 0; i < nodes.length; i++) {
const node = nodes[i];

if (node.type === 'heading' && node.depth === 2) {
const headingValue = node.children[0].value;

if (currentGroup) {
if (!groups.has(currentGroup)) {
groups.set(currentGroup, []);
}

groups.get(currentGroup).push(...currentNodes);
}

currentGroup = headingValue;
// Добавляем заголовок в группу только если он встречается первый раз
if (!isFirstMainHeading.has(headingValue)) {
currentNodes = [node];
isFirstMainHeading.add(headingValue);
} else {
// Пропускаем повторный заголовок
currentNodes = [];
}
} else {
if (currentGroup) {
currentNodes.push(node);
}
}
}

// Сохраняем последнюю группу
if (currentGroup && currentNodes.length) {
if (!groups.has(currentGroup)) {
groups.set(currentGroup, []);
}

groups.get(currentGroup).push(...currentNodes);
}

// Создаем новое дерево
const newChildren = [];

for (const [_, nodes] of groups) {
newChildren.push(...nodes);
}

return { ...tree, children: newChildren };
}
27 changes: 27 additions & 0 deletions .github/actions/processing-release-changelog/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';

import * as core from '@actions/core';

import { groupByHeadings } from './groupByHeadings.js';

async function run() {
try {
const data = core.getInput('data', { required: true });

const changelog = await unified()
.use(remarkParse)
.use(() => (tree) => {
return groupByHeadings(tree);
})
.use(remarkStringify)
.process(data);

core.setOutput('changelog', changelog);
} catch (error) {
core.setFailed(error.message);
}
}

run();
Loading

0 comments on commit 3e82d8c

Please sign in to comment.