Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Meta: GitHub-sourced Mindmap #1559

Merged
merged 1 commit into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"format:uncommitted": "nx format --fix --parallel --uncommitted",
"lint": "nx run-many --all --target=lint",
"prepublishOnly": "npm run build",
"mindmap": "cd packages/meta/src/mindmap/v2 && php -S 127.0.0.1:5269",
"preview": "nx preview playground-website",
"recompile:php:web": "nx recompile-php:light:all php-wasm-web && nx recompile-php:kitchen-sink:all php-wasm-web ",
"recompile:php:web:light": "nx recompile-php:light:all php-wasm-web ",
Expand Down
229 changes: 229 additions & 0 deletions packages/meta/src/mindmap/v1/fetch-mindmap-data.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,229 @@
const shouldRebuild =
new URLSearchParams(window.location.search).get('rebuild') === 'true';

let __moduleGithubToken = localStorage.getItem('GITHUB_TOKEN');
const repos = [
'wordpress/wordpress-playground',
'wordpress/playground-tools',
'wordpress/blueprints',
'wordpress/blueprints-library',
'adamziel/playground-docs-workflow',
'adamziel/site-transfer-protocol',
];

const comparableKey = (str) => str.toLowerCase();

const graphqlQuery = async (query, variables) => {
const headers = {
'Content-Type': 'application/json',
};
if (__moduleGithubToken) {
headers['Authorization'] = `Bearer ${__moduleGithubToken}`;
}
const response = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers,
body: JSON.stringify({ query, variables }),
});
return response.json();
};

async function* iterateIssuesPRs(repo, labels = []) {
const query = `
query GetProjects($cursor: String!, $query: String!) {
search(query: $query, first: 100, type: ISSUE, after: $cursor) {
pageInfo {
hasNextPage
endCursor
}
edges {
node {
... on Issue {
number
title
url
body
state
repository {
nameWithOwner
}
labels(first: 10) {
nodes {
name
}
}
}
... on PullRequest {
number
title
url
body
state
repository {
nameWithOwner
}
labels(first: 10) {
nodes {
name
}
}
}
}
}
}
}
`;

let cursor = '';
do {
const labelQuery = labels.map((label) => `"${label}"`).join(',');
const variables = {
cursor,
query:
`repo:${repo}` + (labels.length ? ` label:${labelQuery}` : ''),
};

const response = await graphqlQuery(query, variables);
const edges = response.data.search.edges;
for (const edge of edges) {
if (edge.node) {
const item = edge.node;
const key = comparableKey(
`${item.repository.nameWithOwner}#${item.number}`
);
item.key = key;
item.title = item.title.trim().replace(/^Tracking: /, '');
yield edge.node;
}
}
if (!response.data.search.pageInfo.hasNextPage) {
break;
}
cursor = response.data.search.pageInfo.endCursor;
} while (true);
}

const nodesCacheKey = 'nodes_cache';

const fetchData = async () => {
let allNodes = {};
const allNodesArray = [];
for (const repo of repos) {
for await (const item of iterateIssuesPRs(repo)) {
allNodes[item.key] = item;
}
for await (const item of iterateIssuesPRs(repo, [
'[Type] Mindmap Node',
'[Type] Mindmap Tree',
])) {
allNodes[item.key] = item;
}
}
return allNodes;
};

const getConnectedNodes = (issue) => {
const currentRepo = issue.repository.nameWithOwner;
let connections = [];

const regex1 =
/\bhttps:\/\/github.com\/([^\/]+)\/([^\/]+)\/(?:issues|pull)\/(\d+)\b/g;
const regex2 = /\b#(\d+)\b/g;
let match;

while ((match = regex1.exec(issue.body)) !== null) {
connections.push(`${match[1]}/${match[2]}#${match[3]}`);
}

while ((match = regex2.exec(issue.body)) !== null) {
connections.push(`${currentRepo}#${match[1]}`);
}

return connections.map(comparableKey);
};

const buildEdges = ({ allNodes, rootKey, isEdge }) => {
const seen = {};
const allEdges = {};

const preorderTraversal = (currentNode) => {
const relatedIssueKeys = getConnectedNodes(currentNode).filter(
(edgeKey) => isEdge(currentNode.key, edgeKey)
);

for (const relatedKey of relatedIssueKeys) {
if (!allNodes[relatedKey]) continue;
if (seen[relatedKey]) continue;
seen[relatedKey] = true;

if (!allEdges[currentNode.key]) {
allEdges[currentNode.key] = [];
}

allEdges[currentNode.key].push(relatedKey);
preorderTraversal(allNodes[relatedKey]);
}
};

preorderTraversal(allNodes[rootKey]);
return allEdges;
};

const buildTree = (allNodes, allEdges, rootKey) => {
const node = { ...allNodes[rootKey] };
const childrenKeys = allEdges[rootKey] || [];
node.children = childrenKeys.map((childKey) =>
buildTree(allNodes, allEdges, childKey)
);
return node;
};

export const fetchMindmapData = async ({ githubToken } = {}) => {
if (githubToken) {
__moduleGithubToken = githubToken;
}

const allNodes = await fetchData();
const isEdge = (fromKey, toKey) => {
if (mindmapTrees[fromKey]) {
return true;
}
if (mindmapNodes[fromKey] && mindmapNodes[toKey]) {
return true;
}
return false;
};

const mindmapTrees = {};
const mindmapNodes = {};
for (const key in allNodes) {
if (
allNodes[key].labels.nodes.some(
(label) => label.name === '[Type] Mindmap Tree'
)
) {
mindmapTrees[key] = allNodes[key];
mindmapNodes[key] = allNodes[key];
} else if (
allNodes[key].labels.nodes.some(
(label) => label.name === '[Type] Mindmap Node'
)
) {
mindmapNodes[key] = allNodes[key];
}
}

const rootKey = 'wordpress/wordpress-playground#525';
const allEdges = buildEdges({
allNodes,
rootKey,
isEdge,
});
const tree = buildTree(allNodes, allEdges, rootKey);
console.log({
allNodes,
tree,
});

return tree;
};
13 changes: 13 additions & 0 deletions packages/meta/src/mindmap/v1/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Mind Map</title>
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div id="mindmap"></div>
<script src="https://d3js.org/d3.v6.min.js"></script>
<script src="script.js"></script>
</body>
</html>
Loading
Loading