Skip to content

Commit

Permalink
ci: processing changelog 2 [no ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
Yakutoc committed Dec 4, 2024
1 parent b38658c commit ac6fc5e
Show file tree
Hide file tree
Showing 13 changed files with 2,420 additions and 50 deletions.
59 changes: 13 additions & 46 deletions .github/actions/processing-release-changelog/groupByHeadings.js
Original file line number Diff line number Diff line change
@@ -1,54 +1,21 @@
export function groupByHeadings(tree) {
const groups = new Map();
const h2List = new Set();

let nodes = [...tree.children];
let currentGroup = null;
let currentNodes = [];

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

// Группируем узлы по заголовкам
for (const node of tree.children) {
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 (!h2List.has(headingValue)) {
currentNodes = [node];

h2List.add(headingValue);
} else {
currentNodes = [];
}
} else {
if (currentGroup) {
currentNodes.push(node);
}
currentHeading = node.children[0].value;
// При первом появлении заголовка создаем массив с ним
groups.set(currentHeading, [node]);
} else if (currentHeading) {
// Добавляем узлы к текущей группе
groups.get(currentHeading).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 };
return {
...tree,
children: [...groups.values()].flat(),
};
}
4 changes: 1 addition & 3 deletions .github/actions/processing-release-changelog/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ async function run() {

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

Expand Down
12 changes: 12 additions & 0 deletions .github/actions/write-changelog/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: write-changelog

description: ''

inputs:
token:
description: The GitHub token used to create an authenticated client.
required: true

runs:
using: 'node20'
main: 'index.js'
38 changes: 38 additions & 0 deletions .github/actions/write-changelog/groupHeadingsByDeep.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
export function groupByHeadings(tree) {
const initialState = {
groups: new Map(),
currentH2: null,
currentH3: null,
seenHeadings: new Set(),
};

const result = tree.children.reduce((state, node) => {
const { groups, currentH2, currentH3, seenHeadings } = state;

if (node.type !== 'heading') {
const currentHeading = currentH3 || currentH2;
const currentNodes = groups.get(currentHeading) || [];
groups.set(currentHeading, [...currentNodes, node]);
return state;
}

const value = node.children[0].value;
if (seenHeadings.has(value)) {
return state;
}

seenHeadings.add(value);

return {
...state,
currentH2: node.depth === 2 ? value : currentH2,
currentH3: node.depth === 3 ? value : node.depth === 2 ? null : currentH3,
groups: new Map([...groups, [value, [node]]]),
};
}, initialState);

return {
...tree,
children: [...result.groups.values()].flat(),
};
}
69 changes: 69 additions & 0 deletions .github/actions/write-changelog/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import { unified } from 'unified';
import remarkParse from 'remark-parse';
import remarkStringify from 'remark-stringify';
import { visit, SKIP } from 'unist-util-visit';

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

import { writeChangelog } from './utils.js';
import { swapSectionPlace } from './swapSectionPlace.js';
import { processingHeadingByPackages } from './processingHeadingByPackages.js';
import { rewriteHeadingValue } from './rewriteHeadingValue.js';
import { groupByHeadings } from './groupHeadingsByDeep.js';

async function run() {
try {
const token = core.getInput('token');

const octokit = new github.getOctokit(token);
// const context = github.context;
//
// if (context.payload.pull_request == null) {
// core.setFailed('No release pull request found');
//
// return;
// }

const pr = await octokit.rest.pulls.get({
owner: 'Yakutoc',
repo: 'plasma-dev-stage',
pull_number: 40,
});

const tree = unified().use(remarkParse).parse(pr.data.body);

const data = [];

visit(tree, (node, index, parent) => {
if (node.type === 'heading' && node.depth === 2) {
data.push(node.children[0].value);
}
});

// TODO: Как добавляем изменения про токены?
// TODO: Plasma-icons точно отдельным моментом
const packages = data.map((item) => item.toLowerCase()).filter((item) => !['mics', 'core'].includes(item));

for (const pkg of packages) {
const list = packages.filter((item) => pkg !== item);

const changelogMD = unified()
.use(remarkParse)
.use(() => swapSectionPlace(pkg))
.use(() => processingHeadingByPackages(list))
.use(() => (tree) => {
return groupByHeadings(tree);
})
.use(() => rewriteHeadingValue(pkg))
.use(remarkStringify)
.processSync(pr.data.body);

await writeChangelog(changelogMD.toString(), pkg);
}
} catch (error) {
core.setFailed(error.message);
}
}

run();
Loading

0 comments on commit ac6fc5e

Please sign in to comment.