forked from salute-developers/plasma
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
2,420 additions
and
50 deletions.
There are no files selected for viewing
59 changes: 13 additions & 46 deletions
59
.github/actions/processing-release-changelog/groupByHeadings.js
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
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(), | ||
}; | ||
} |
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
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
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' |
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
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(), | ||
}; | ||
} |
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
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(); |
Oops, something went wrong.