|
1 | | -version https://git-lfs.github.com/spec/v1 |
2 | | -oid sha256:8ebd148b5a695da6518569147d9648c6bf7173445e74c3cf0265d6f588873220 |
3 | | -size 2024 |
| 1 | +const fetch = require('node-fetch'); |
| 2 | +const fs = require('fs'); |
| 3 | +const { execSync } = require('child_process'); |
| 4 | + |
| 5 | +async function updateMetadata() { |
| 6 | + // Get repository URL from git config |
| 7 | + const remoteUrl = execSync('git config --get remote.origin.url').toString().trim(); |
| 8 | + |
| 9 | + // Parse owner/repo from URL |
| 10 | + const urlRegex = /github\.com[:\/]([^\/]+)\/([^\.]+)(\.git)?/; |
| 11 | + const match = remoteUrl.match(urlRegex); |
| 12 | + |
| 13 | + if (!match) { |
| 14 | + console.error('Could not parse GitHub repository URL'); |
| 15 | + return; |
| 16 | + } |
| 17 | + |
| 18 | + const [, owner, repo] = match; |
| 19 | + |
| 20 | + // Fetch repository metadata from GitHub API |
| 21 | + const response = await fetch(`https://api.github.com/repos/${owner}/${repo}`, { |
| 22 | + headers: { |
| 23 | + 'Accept': 'application/vnd.github+json', |
| 24 | + 'Authorization': process.env.GITHUB_TOKEN ? `Bearer ${process.env.GITHUB_TOKEN}` : '', |
| 25 | + } |
| 26 | + }); |
| 27 | + |
| 28 | + if (!response.ok) { |
| 29 | + console.error(`GitHub API error: ${response.status}`); |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const repoData = await response.json(); |
| 34 | + |
| 35 | + // Read current metadata template |
| 36 | + const metadataTemplate = JSON.parse(fs.readFileSync('metadata_template.json', 'utf8')); |
| 37 | + |
| 38 | + // Update fields with repository data |
| 39 | + metadataTemplate.title = repoData.name; |
| 40 | + metadataTemplate.description = repoData.description || metadataTemplate.description; |
| 41 | + metadataTemplate.version = repoData.pushed_at ? new Date(repoData.pushed_at).toISOString().split('T')[0] : metadataTemplate.version; |
| 42 | + |
| 43 | + // Update keywords with topics |
| 44 | + if (repoData.topics && repoData.topics.length > 0) { |
| 45 | + metadataTemplate.keywords = repoData.topics; |
| 46 | + } |
| 47 | + |
| 48 | + // Update related identifier |
| 49 | + if (metadataTemplate.related_identifiers && metadataTemplate.related_identifiers.length > 0) { |
| 50 | + metadataTemplate.related_identifiers[0].identifier = repoData.html_url; |
| 51 | + } |
| 52 | + |
| 53 | + // Write updated metadata back to file |
| 54 | + fs.writeFileSync('CITATION.cff', JSON.stringify(metadataTemplate, null, 2)); |
| 55 | + |
| 56 | + console.log('Metadata successfully updated with repository information'); |
| 57 | +} |
| 58 | + |
| 59 | +updateMetadata().catch(console.error); |
| 60 | + |
0 commit comments