-
Notifications
You must be signed in to change notification settings - Fork 191
175 lines (152 loc) · 5.86 KB
/
Copy pathupdate-draft-hips.yml
File metadata and controls
175 lines (152 loc) · 5.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
name: Update Draft HIPs Data
on:
schedule:
- cron: "0 */6 * * *" # Runs every 6 hours
workflow_dispatch: # Allows manual triggering
permissions:
contents: read
jobs:
update-draft-hips:
if: ${{ github.ref == 'refs/heads/main' }} # Only run on main branch
runs-on: hiero-improvement-proposals-linux-medium
permissions:
contents: write
pull-requests: read
steps:
- name: Harden the runner (Audit all outbound calls)
uses: step-security/harden-runner@fe104658747b27e96e4f7e80cd0a94068e53901d # v2.16.1
with:
egress-policy: audit
- name: Checkout Code
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
token: ${{ secrets.GH_ACCESS_TOKEN }}
ref: 'main'
- name: Import GPG Key
id: gpg_importer
uses: step-security/ghaction-import-gpg@69c854a83c7f79463f8bdf46772ab09826c560cd # v6.3.1
with:
git_commit_gpgsign: true
git_tag_gpgsign: true
git_user_signingkey: true
gpg_private_key: ${{ secrets.GPG_KEY_CONTENTS }}
passphrase: ${{ secrets.GPG_KEY_PASSPHRASE }}
- name: Setup Node.js
uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0
with:
node-version: "20"
- name: Create Script
run: |
mkdir -p _data
cat << 'EOF' > fetch-draft-hips.js
const https = require('https');
const fs = require('fs');
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: "hiero-improvement-proposals", owner: "hiero-ledger") {
pullRequests(first: 100, states: [OPEN], orderBy: {field: CREATED_AT, direction: DESC}) {
nodes {
title
number
url
headRefOid
files(first: 100) {
edges {
node {
path
changeType
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);
}
// Check if data and the expected path to nodes exist
if (!result.data || !result.data.repository || !result.data.repository.pullRequests || !result.data.repository.pullRequests.nodes) {
console.error('Unexpected GraphQL response structure:', result);
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(allPRs => {
const draftHIPPRs = allPRs.filter(pr => {
if (!pr.files || !pr.files.edges) {
return false;
}
const hipFiles = pr.files.edges.filter(edge => {
const fileNode = edge.node;
const isNewHIPFile = /^HIP\/hip-[a-zA-Z0-9-]+\.md$/.test(fileNode.path);
return fileNode.changeType === 'ADDED' && isNewHIPFile;
}).map(edge => edge.node);
return hipFiles.length > 0;
});
const outputPath = '_data/draft_hips.json';
if (fs.existsSync(outputPath)) {
console.log(`Removing existing file: ${outputPath}`);
fs.unlinkSync(outputPath);
}
console.log(`Writing ${draftHIPPRs.length} filtered PRs to: ${outputPath}`);
fs.writeFileSync(outputPath, JSON.stringify(draftHIPPRs, null, 2));
}).catch(error => {
console.error('Failed to fetch and filter PRs:', error);
process.exit(1);
});
EOF
- name: Run Script
run: node fetch-draft-hips.js
env:
GITHUB_TOKEN: ${{ secrets.GH_ACCESS_TOKEN }}
- name: Commit and Push Changes
env:
GITHUB_USER_EMAIL: ${{ vars.GIT_USER_EMAIL }}
GITHUB_USER_NAME: ${{ vars.GIT_USER_NAME }}
run: |
set -e
git config --local user.email "$GITHUB_USER_EMAIL"
git config --local user.name "$GITHUB_USER_NAME"
git add _data/draft_hips.json
git diff --cached --quiet && echo "No changes to commit" && exit 0
git commit -s -S -m "Update draft HIPs data [skip ci]"
git push origin main
set +e