Skip to content

Commit

Permalink
Display draft HIPs for Jekyll build (#1093)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Garber <michael.garber@swirldslabs.com>
Signed-off-by: mgarbs <michael.garber@hashgraph.com>
Signed-off-by: Michael Heinrichs <netopyr@users.noreply.github.com>
Co-authored-by: mgarbs <michael.garber@hashgraph.com>
Co-authored-by: Michael Heinrichs <netopyr@users.noreply.github.com>
  • Loading branch information
3 people authored Jan 14, 2025
1 parent d8ac9e5 commit 6fddbca
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 54 deletions.
118 changes: 118 additions & 0 deletions .github/workflows/update-draft-hips.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
name: Update Draft HIPs Data

on:
schedule:
- cron: "0 */6 * * *" # Runs every 6 hours
workflow_dispatch: # Allows manual triggering

jobs:
update-draft-hips:
if: github.ref == 'refs/heads/main' # Only run on main branch
runs-on: ubuntu-latest
permissions:
contents: write
pull-requests: read
steps:
- uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab # v3.5.2

- name: Setup Node.js
uses: actions/setup-node@64ed1c7eab4cce3362f8c340dee64e5eaeef8f7c # v3.6.0
with:
node-version: "20"

- name: Create Script
run: |
mkdir -p _data
cat << 'EOF' > fetch-draft-hips.js
const https = require('https');
async function makeGraphQLRequest(query, token) {
return new Promise((resolve, reject) => {
const options = {
hostname: 'api.github.com',
path: '/graphql',
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
'User-Agent': 'Node.js'
}
};
const req = https.request(options, (res) => {
let data = '';
res.on('data', chunk => { data += chunk; });
res.on('end', () => resolve(JSON.parse(data)));
});
req.on('error', reject);
req.write(JSON.stringify({ query }));
req.end();
});
}
async function getAllPRs() {
const query = `
query {
repository(name: "hedera-improvement-proposal", owner: "hashgraph") {
pullRequests(first: 100, states: [OPEN], orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
title
number
url
headRefOid
files(first: 100) {
edges {
node {
path
additions
deletions
}
}
}
author {
login
}
}
}
}
}
`;
try {
const result = await makeGraphQLRequest(query, process.env.GITHUB_TOKEN);
if (result.errors) {
console.error('GraphQL errors:', result.errors);
process.exit(1);
}
return result.data.repository.pullRequests.nodes;
} catch (error) {
console.error('Error fetching PRs:', error);
throw error;
}
}
// Run the main function
getAllPRs().then(prs => {
const fs = require('fs');
fs.writeFileSync('_data/draft_hips.json', JSON.stringify(prs, null, 2));
}).catch(error => {
console.error('Failed to fetch PRs:', error);
process.exit(1);
});
EOF
- name: Run Script
run: node fetch-draft-hips.js
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Commit and Push Changes
run: |
git config --local user.email "github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
git add _data/draft_hips.json
git commit -m "Update draft HIPs data [skip ci]" || echo "No changes to commit"
git push origin main || echo "No changes to push"
3 changes: 3 additions & 0 deletions _config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ defaults:
values:
layout: "hip"

include:
- _data

# Exclude from processing.
# The following items will not be processed, by default. Create a custom list
# to override the default setting.
Expand Down
1 change: 0 additions & 1 deletion _includes/head.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<head>
<meta name="github-token" content="{{ secrets.DRAFT_HIPS }}">
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<meta charset="UTF-8">
Expand Down
69 changes: 16 additions & 53 deletions assets/js/pr-integration.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,51 +69,14 @@ class HIPPRIntegration {

async fetchPRData() {
try {
const token = document.querySelector('meta[name="github-token"]').content;

const query = {
query: `query {
repository(name: "hedera-improvement-proposal", owner: "hashgraph") {
pullRequests(first: 100, orderBy: {field: CREATED_AT, direction: DESC}, states: [OPEN]) {
nodes {
title
number
url
headRefOid
files(last: 100) {
edges {
node {
path
additions
deletions
}
}
}
author {
login
}
}
}
}
}`
};

const response = await fetch('https://api.github.com/graphql', {
method: 'POST',
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json',
},
body: JSON.stringify(query)
});
// Try with site.baseurl if it's set in _config.yml
const baseUrl = document.querySelector('meta[name="site-baseurl"]')?.content || '';
const response = await fetch(`${baseUrl}/_data/draft_hips.json`);

const data = await response.json();

if (!response.ok || data.errors) {
console.error('GraphQL errors:', data.errors);
return null;
if (!response.ok) {
throw new Error('Failed to fetch draft HIPs data');
}
return data.data.repository.pullRequests.nodes;
return await response.json();
} catch (error) {
console.error('Error in fetchPRData:', error);
throw error;
Expand All @@ -123,39 +86,39 @@ class HIPPRIntegration {
async filterHIPPRs(prs) {
const validHips = [];
const seenPRs = new Set();

for (const pr of prs) {
if (seenPRs.has(pr.number)) continue;

const mdFiles = pr.files.edges.filter(file => file.node.path.endsWith('.md'));
let bestMetadata = null;
let bestFile = null;

// Try all MD files and pick the one with the most complete metadata
for (const file of mdFiles) {
try {
const contentUrl = `https://raw.githubusercontent.com/hashgraph/hedera-improvement-proposal/${pr.headRefOid}/${file.node.path}`;
const response = await fetch(contentUrl);
const content = await response.text();
const metadata = this.parseHIPMetadata(content);

// Skip template files and empty metadata
if (file.node.path.includes('template') || !metadata.title) {
continue;
}

// If we don't have metadata yet, or this file has a better title
if (!bestMetadata ||
(metadata.title && metadata.title.length > 3 &&
(!bestMetadata.title || metadata.title.length > bestMetadata.title.length))) {
if (!bestMetadata ||
(metadata.title && metadata.title.length > 3 &&
(!bestMetadata.title || metadata.title.length > bestMetadata.title.length))) {
bestMetadata = metadata;
bestFile = file;
}
} catch (error) {
console.error(`Error checking file ${file.node.path}:`, error);
}
}

// If we found valid metadata, add it to the results
if (bestMetadata && bestFile) {
validHips.push({
Expand All @@ -166,7 +129,7 @@ class HIPPRIntegration {
seenPRs.add(pr.number);
}
}

return validHips;
}

Expand Down

0 comments on commit 6fddbca

Please sign in to comment.