diff --git a/.github/workflows/pull-request-tags-suggestions.yml b/.github/workflows/pull-request-tags-suggestions.yml index 81d1b85d1..7b3229630 100644 --- a/.github/workflows/pull-request-tags-suggestions.yml +++ b/.github/workflows/pull-request-tags-suggestions.yml @@ -1,7 +1,8 @@ name: Pull request tags suggestions on: - pull_request: + pull_request_target: + types: [opened, reopened] paths: - 'icons/*.json' @@ -14,7 +15,11 @@ jobs: pull-requests: write steps: + # We checkout the main branch of main repository for security reasons. + # This is to prevent the workflow from running on a forked repository. - uses: actions/checkout@v4 + with: + repository: lucide-icons/lucide - uses: pnpm/action-setup@v4 - uses: actions/setup-node@v4 with: diff --git a/package.json b/package.json index fd25fbbc1..b40fff3e5 100644 --- a/package.json +++ b/package.json @@ -24,7 +24,7 @@ "generate:contributors": "node ./scripts/updateContributors.mts icons/*.svg", "generate:nextJSAliases": "node ./scripts/generateNextJSAliases.mts", "suggest:tags": "node ./scripts/suggestTags.mts", - "suggest:tags:watch": "node --watch ./scripts/suggestTags.mts", + "suggest:tags:watch": "node --env-file .env --watch ./scripts/suggestTags.mts ", "postinstall": "husky install", "lint:es": "eslint .", "lint:format": "prettier \"**/*.{js,mjs,ts,jsx,tsx,html,css,scss,json,yml,yaml}\" --check", diff --git a/scripts/suggestTags.mts b/scripts/suggestTags.mts index f09b73b05..fd021f0d1 100644 --- a/scripts/suggestTags.mts +++ b/scripts/suggestTags.mts @@ -11,6 +11,7 @@ const octokit = new Octokit({ auth: process.env.GITHUB_TOKEN }); const pullRequestNumber = Number(process.env.PULL_REQUEST_NUMBER); const username = process.env.REVIEWER ?? 'github-actions[bot]'; const commitSha = process.env.COMMIT_SHA ?? "HEAD"; +const useFileSystem = process.env.USE_FILE_SYSYEM === 'true' || false; const owner = 'lucide-icons'; const repo = 'lucide'; @@ -40,10 +41,9 @@ if(hasUserReviews) { process.exit(0); } -const changedFiles = files - .map((file) => file.filename) - .filter((file) => file.startsWith('icons/') && file.includes('.json')) - .filter((file, idx, arr) => arr.indexOf(file) === idx);; +const changedFiles = files.filter( + ({filename}) => filename.startsWith('icons/') && filename.includes('.json') +) if (changedFiles.length === 0) { console.log('No changed icons found'); @@ -54,8 +54,8 @@ const client = new OpenAI({ apiKey: process.env.OPENAI_API_KEY, }); -const suggestionsByFile = changedFiles.map(async (file) => { - const filePath = file.replace('.json', ''); +const suggestionsByFile = changedFiles.map(async ({ filename, raw_url }) => { + const filePath = filename.replace('.json', ''); const iconName = filePath.split('/').pop(); const input = `Create a list of tags for a \`${iconName}\` icon. Don't include words like: 'icon' and preferably use single words.`; @@ -72,15 +72,26 @@ const suggestionsByFile = changedFiles.map(async (file) => { console.log(`Suggesting tags for ${iconName}:`, suggestedTags); - // const currentContent = require(`../${filePath}`); - const jsonFile = path.join(process.cwd(), file); - const currentFileContent = await fs.readFile(jsonFile, 'utf-8') as unknown as string; - const metaData = JSON.parse(currentFileContent); + let fileContent: string - console.log(`Current tags for ${iconName}:`, metaData.tags || []); + if( useFileSystem ) { + const jsonFile = path.join(process.cwd(), filename); + fileContent = await fs.readFile(jsonFile, 'utf-8') as unknown as string; + } else { + const fileGithubRequest = await octokit.request(`GET ${raw_url}`, { + headers: { + 'Accept': 'application/vnd.github.v3.raw', + }, + }); + fileContent = fileGithubRequest.data + } + + const metadata = JSON.parse(fileContent) + + console.log(`Current tags for ${iconName}:`, metadata.tags || []); const tagSuggestionsWithoutDuplicates = suggestedTags.filter((tag) => { - return !metaData.tags?.includes(tag) && tag !== iconName; + return !metadata.tags?.includes(tag) && tag !== iconName; }); console.log(`Tag suggestions for ${iconName} without duplicates:`, tagSuggestionsWithoutDuplicates); @@ -90,7 +101,7 @@ const suggestionsByFile = changedFiles.map(async (file) => { return Promise.resolve(null); } // Find the startLine in the json file - const startLine = currentFileContent.split('\n').findIndex((line) => line.includes('"tags":')) + 1; + const startLine = fileContent.split('\n').findIndex((line) => line.includes('"tags":')) + 1; const codeSuggestion = tagSuggestionsWithoutDuplicates.map((tag) => ` "${tag}"`).join(',\n'); @@ -100,7 +111,7 @@ Here are the suggestions: \`\`\`suggestion\n "tags": [\n${codeSuggestion},`; return { - path: file, + path: filename, line: startLine, body: message, }