Skip to content

Commit 611be54

Browse files
author
GitHub Actions Bot
committed
feat: add automatic mermaid update scripts for GQMs
1 parent c60e6d8 commit 611be54

File tree

7 files changed

+1934
-78
lines changed

7 files changed

+1934
-78
lines changed

.github/workflows/gqm_update.yml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
name: Update GQM
2+
on:
3+
push:
4+
branches: [main]
5+
paths:
6+
- 'measuring/goals/**'
7+
- 'measuring/questions/**'
8+
- 'measuring/metrics/**'
9+
10+
workflow_dispatch:
11+
jobs:
12+
generateDiagram:
13+
name: Generate Diagram
14+
runs-on: ubuntu-latest
15+
steps:
16+
- name: Checkout
17+
uses: actions/checkout@v4
18+
- name: Use Node.js
19+
uses: actions/setup-node@v3
20+
with:
21+
node-version: '21.x'
22+
- run: npm install --prefix ./scripts/gqm_gen
23+
- run: npm run coverage-report --prefix ./scripts/gqm_gen
24+
- name: Report NYC coverage
25+
uses: sidx1024/[email protected]
26+
with:
27+
coverage_file: ".nyc_output/nyc-coverage-report/coverage-summary.json"
28+
- run: npm run --silent start --prefix ./scripts/gqm_gen > ./new_gqm.md.tmp
29+
- run: sh ./scripts/gqm_gen/gqm_update.sh ./new_gqm.md.tmp ./measuring/use_gqm.md > ./measuring/use_gqm.md.tmp
30+
- run: rm -f ./new_gqm.md.tmp
31+
- run: mv -f ./measuring/use_gqm.md.tmp ./measuring/use_gqm.md
32+
- run: sh ./scripts/gqm_gen/gqm_commit.sh

scripts/gqm_gen/gqm_commit.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/bin/bash
2+
if cmp -s ./measuring/use_gqm.md.tmp ./measuring/use_gqm.md; then
3+
echo "No changes in GQM diagram"
4+
exit 0
5+
fi
6+
7+
git --version
8+
git config user.name "GitHub Actions Bot"
9+
git config user.email "<>"
10+
git add ./measuring/use_gqm.md
11+
git commit -m "feat: gqm diagram updated by GitHub Actions"
12+
git push

scripts/gqm_gen/gqm_update.sh

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
#!/bin/bash
2+
3+
# Check if the correct number of arguments are provided
4+
if [ "$#" -ne 2 ]; then
5+
echo "Usage: $0 <path-to-new-mermaid-markdown-file> <path-to-exising-use_gqm-markdown-file>"
6+
exit 1
7+
fi
8+
9+
# File paths
10+
NEW_MERMAID_MARKDOWN_FILE="$1"
11+
EXISTING_USE_GQM_MARKDOWN_FILE="$2"
12+
13+
# Check if the new content file exists
14+
if [ ! -f "$NEW_MERMAID_MARKDOWN_FILE" ]; then
15+
echo "Error: New mermaid markdown file not found at $NEW_MERMAID_MARKDOWN_FILE"
16+
exit 1
17+
fi
18+
19+
# Check if the markdown file exists
20+
if [ ! -f "$EXISTING_USE_GQM_MARKDOWN_FILE" ]; then
21+
echo "Error: Existing use_gqm markdown file not found at $EXISTING_USE_GQM_MARKDOWN_FILE"
22+
exit 1
23+
fi
24+
25+
# The pattern to match the mermaid block
26+
START_PATTERN='```mermaid'
27+
END_PATTERN='```'
28+
29+
# Replace the entire mermaid block including the delimiters
30+
sed -e "/$START_PATTERN/,/$END_PATTERN/{
31+
/$START_PATTERN/{
32+
r $NEW_MERMAID_MARKDOWN_FILE
33+
d
34+
}
35+
/$END_PATTERN/d
36+
d
37+
}" "$EXISTING_USE_GQM_MARKDOWN_FILE"

scripts/gqm_gen/index.test.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import * as Commonmark from "commonmark";
2+
import * as fs from "fs";
23
import test from "node:test";
34
import assert from "node:assert";
45
import { FileLink, Graph, LinkType } from "./types";
56
import {
67
getLinks,
78
getFileLinks,
89
generateMermaidDiagram,
9-
getGQMFileLinks,
10+
getGQMFileLinks
1011
} from "./index";
1112

1213
test("can get links", () => {
@@ -29,13 +30,11 @@ test("can generate mermaid diagram", () => {
2930
edges: [],
3031
};
3132
const diagram = generateMermaidDiagram(graph);
32-
assert(diagram.indexOf("graph TB") > 1);
33+
assert(diagram.indexOf("graph LR") > 1);
3334
});
3435

35-
test("can generate mermaid diagram from file", { only: true }, () => {
36-
const graph = getGQMFileLinks();
37-
36+
test("can generate mermaid diagram from file", () => {
37+
const graph = getGQMFileLinks('../../measuring/');
3838
const diagram = generateMermaidDiagram(graph);
39-
console.log(diagram);
4039
assert(diagram.indexOf("graph LR;") > 1);
4140
});

scripts/gqm_gen/index.ts

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,25 @@ import * as path from "path";
44
import { Node, Edge, Graph, Link, LinkType, NodeShape, ArrowType } from "./types";
55
import { FileLink } from "./types";
66

7-
const graph = getGQMFileLinks();
7+
const mdFilePath = process.env.npm_config_markDownFilePath || "../../measuring";
8+
9+
const graph = getGQMFileLinks(mdFilePath);
810

911
export function getLinkUrl(linkType: LinkType, file: string) {
1012
const measuringUrl = "https://github.com/InnerSourceCommons/managing-inner-source-projects/blob/main/measuring/";
1113
const url = `${measuringUrl}/${linkType.toLowerCase()}s/${file}`
1214
return url;
1315
}
1416

15-
export function getGQMFileLinks() {
17+
export function getGQMFileLinks(mdFilePath: string) {
1618
const graph: Graph = {
1719
nodes: [],
1820
edges: [],
1921
};
2022

21-
const goalsPath = "../../measuring/goals/";
22-
const questionsPath = "../../measuring/questions/";
23-
const metricsPath = "../../measuring/metrics/";
23+
const goalsPath = `${mdFilePath}/goals/`;
24+
const questionsPath = `${mdFilePath}/questions/`;
25+
const metricsPath = `${mdFilePath}/metrics/`;
2426

2527
const goalFileLinks: FileLink[] = getFileLinks(LinkType.GOAL, goalsPath);
2628
appendToGraph(graph, goalFileLinks);
@@ -144,8 +146,7 @@ export function generateMermaidDiagram(graph: Graph) {
144146
const nodes = graph.nodes;
145147
const edges = graph.edges;
146148

147-
let mermaidSyntax = `\`\`\`mermaid\n
148-
graph LR;\n
149+
let mermaidSyntax = `\`\`\`mermaid\ngraph LR;\n
149150
subgraph GQM[Goals, Questions, Metrics]\n
150151
`;
151152

@@ -156,7 +157,7 @@ export function generateMermaidDiagram(graph: Graph) {
156157

157158
edges.forEach((edge) => {
158159
const arrowSyntax: string = ArrowType.ARROW;
159-
mermaidSyntax += `${edge.from}${arrowSyntax}${edge.to}\n`;
160+
mermaidSyntax += ` ${edge.from}${arrowSyntax}${edge.to}\n`;
160161
});
161162

162163
const goalsList = nodes.filter(n => n.type == LinkType.GOAL).map(n => `${n.id}`).join(',');

0 commit comments

Comments
 (0)