name: "Diff Display and Update Cache" description: "Compare current top5 results with previous cache, display new additions, and update the cache file." inputs: currentResults: description: "The JSON string of current top5 results from malware-check action." required: true orgName: description: "The GitHub organization name." required: true runs: using: "composite" steps: - name: Compare current results with previous cache and update cache id: diff-update uses: actions/github-script@v7 env: INPUT_ORGNAME: ${{ inputs.orgName }} with: script: | const GREEN = "\x1b[32m"; const YELLOW = "\x1b[33m"; const CYAN = "\x1b[36m"; const RESET = "\x1b[0m"; const owner = context.repo.owner; const repo = context.repo.repo; // currentResults を入力から取得 const currentResults = JSON.parse(core.getInput('currentResults') || "{}"); let previousResults = {}; let fileSha = null; try { const fileResponse = await github.rest.repos.getContent({ owner, repo, path: 'top5_cache.json', ref: 'main' }); const content = Buffer.from(fileResponse.data.content, 'base64').toString('utf8'); previousResults = JSON.parse(content); fileSha = fileResponse.data.sha; } catch (error) { if (error.status === 404) { core.info("No previous cache found. Starting fresh."); previousResults = {}; } else { throw error; } } // 差分算出: 各対象リポジトリごとに、current にあって previous にない項目(新規追加)のみ抽出 const differences = {}; for (const repoName in currentResults) { const currList = currentResults[repoName] || []; const prevList = previousResults[repoName] || []; const prevNames = new Set(prevList.map(item => item.full_name)); const newItems = currList.filter(item => !prevNames.has(item.full_name)); if (newItems.length > 0) { differences[repoName] = newItems; } } if (Object.keys(differences).length === 0) { core.info(`${YELLOW}No new additions detected compared to previous state.${RESET}`); } else { for (const repoName in differences) { core.info(`\nFor organization repo: ${repoName}`); differences[repoName].forEach(item => { core.info(`${GREEN}New addition: ${item.full_name}${RESET} | ${CYAN}Created: ${item.created_at} | Stars: ${item.stargazers_count} | URL: ${item.html_url}`); }); } } // キャッシュの更新: currentResults を top5_cache.json として保存 const newContent = Buffer.from(JSON.stringify(currentResults, null, 2)).toString('base64'); const commitMessage = `Update top5 cache: ${new Date().toISOString()}`; await github.rest.repos.createOrUpdateFileContents({ owner, repo, path: 'top5_cache.json', message: commitMessage, content: newContent, sha: fileSha || undefined });