|
1 | | -import {execSync} from 'child_process'; |
| 1 | +import { execSync } from 'child_process'; |
2 | 2 | import * as core from '@actions/core'; |
3 | 3 |
|
4 | | -function run() { |
5 | | - try { |
6 | | - const subprojectPrefixes = core.getInput('project_prefixes')?.split(",") ?? []; |
7 | | - const requiredProjects = core.getInput('required_projects')?.split(",") ?? []; |
8 | | - |
9 | | - core.debug("executing git fetch"); |
10 | | - execSync('git fetch --unshallow', {encoding: 'utf-8'}); |
11 | | - |
12 | | - const githubSha = process.env.GITHUB_SHA; |
13 | | - if (!githubSha) { |
14 | | - core.setFailed('GITHUB_SHA not set') |
15 | | - } |
16 | | - const diffCmd = `git diff --name-only HEAD~1..${githubSha}`; |
17 | | - |
18 | | - core.debug(`Executing: ${diffCmd}`); |
19 | | - core.debug(`Git Status: ${execSync(`git status`, {encoding: 'utf-8'}).trim()}`); |
20 | | - core.debug(`SHA Exists: ${execSync(`git cat-file -e ${githubSha}`, {encoding: 'utf-8'}).trim()}`); |
21 | | - |
22 | | - let modifiedProjects = execSync(diffCmd, {encoding: 'utf8'}); |
23 | | - core.debug("Modified Projects:" + modifiedProjects) |
24 | | - core.debug("Required Projects:" + requiredProjects) |
25 | | - if (modifiedProjects.includes('buildSrc/') && !modifiedProjects.includes('ktor-')) { |
26 | | - core.debug("only buildSrc has modified"); |
27 | | - modifiedProjects = "buildSrc"; |
28 | | - } else { |
29 | | - const subprojectPrefixesPattern = subprojectPrefixes.join("|") |
30 | | - core.debug("subprojectPrefixesPattern: " + subprojectPrefixesPattern); |
31 | | - const regex = subprojectPrefixes.length > 0 |
32 | | - ? new RegExp(`^(${subprojectPrefixesPattern})`) |
33 | | - : null; |
34 | | - let modifiedProjectsArray = modifiedProjects.split('\n') |
35 | | - .filter(line => { |
36 | | - return regex ? regex.test(line) : true; |
37 | | - }) |
38 | | - .map(line => line.split('/', 1)[0]) |
39 | | - .sort() |
40 | | - .filter((value, index, self) => self.indexOf(value) === index) |
41 | | - modifiedProjects = [...new Set(modifiedProjectsArray.concat(requiredProjects))].join(','); |
42 | | - } |
43 | | - if (modifiedProjects) { |
44 | | - core.info(`Modified subprojects including required projects: ${modifiedProjects}`); |
45 | | - core.setOutput('modified_projects', modifiedProjects); |
46 | | - } else { |
47 | | - core.info("No modified subprojects"); |
48 | | - } |
49 | | - |
50 | | - } catch (error) { |
51 | | - core.setFailed(`Action failed with error: ${error}`); |
| 4 | +function sh(cmd: string): string { |
| 5 | + return execSync(cmd, { encoding: 'utf-8' }).trim(); |
| 6 | +} |
| 7 | + |
| 8 | +function trySh(cmd: string): string { |
| 9 | + try { |
| 10 | + return sh(cmd); |
| 11 | + } catch { |
| 12 | + return ''; |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +/** |
| 17 | + * Determine a sensible base for diff: |
| 18 | + * - On pull_request events: origin/<base_branch> |
| 19 | + * - On push events: HEAD~1 (previous commit on same branch) if it exists |
| 20 | + * - Fallback: merge-base with origin/main (adjust default branch name if needed) |
| 21 | + */ |
| 22 | +function resolveBaseRef(): string { |
| 23 | + const eventName = process.env.GITHUB_EVENT_NAME || ''; |
| 24 | + const prBase = process.env.GITHUB_BASE_REF; // set on pull_request events |
| 25 | + |
| 26 | + if (eventName.startsWith('pull_request') && prBase) { |
| 27 | + // Ensure we have the PR base locally |
| 28 | + trySh(`git fetch origin ${prBase} --force`); |
| 29 | + return `origin/${prBase}`; |
| 30 | + } |
| 31 | + |
| 32 | + // Push builds: previous commit if available |
| 33 | + const prev = trySh('git rev-parse HEAD~1'); |
| 34 | + if (prev) return prev; |
| 35 | + |
| 36 | + // Fallback: merge-base with origin/main (change "main" if your default is different) |
| 37 | + trySh('git fetch origin main --force'); |
| 38 | + const mergeBase = trySh('git merge-base HEAD origin/main'); |
| 39 | + return mergeBase || 'HEAD'; |
| 40 | +} |
| 41 | + |
| 42 | +function run(): void { |
| 43 | + try { |
| 44 | + const subprojectPrefixes = (core.getInput('project_prefixes') || '') |
| 45 | + .split(',') |
| 46 | + .map(s => s.trim()) |
| 47 | + .filter(Boolean); |
| 48 | + const requiredProjects = (core.getInput('required_projects') || '') |
| 49 | + .split(',') |
| 50 | + .map(s => s.trim()) |
| 51 | + .filter(Boolean); |
| 52 | + |
| 53 | + // We assume Actions checkout used fetch-depth: 0 and fetch-tags: true. |
| 54 | + // Only refresh tags/prune; do NOT attempt to unshallow here. |
| 55 | + trySh('git fetch --tags --force --prune'); |
| 56 | + |
| 57 | + const githubSha = process.env.GITHUB_SHA; |
| 58 | + if (!githubSha) { |
| 59 | + core.setFailed('GITHUB_SHA not set'); |
| 60 | + return; |
| 61 | + } |
| 62 | + |
| 63 | + const base = resolveBaseRef(); |
| 64 | + const diffCmd = `git diff --name-only ${base}..${githubSha}`; |
| 65 | + |
| 66 | + core.debug(`Executing: ${diffCmd}`); |
| 67 | + core.debug(`Git Status:\n${trySh('git status')}`); |
| 68 | + |
| 69 | + const changed = trySh(diffCmd); |
| 70 | + core.debug('Changed files:\n' + (changed || '(none)')); |
| 71 | + core.debug('Required Projects: ' + JSON.stringify(requiredProjects)); |
| 72 | + |
| 73 | + let modifiedProjects = ''; |
| 74 | + |
| 75 | + if (changed.includes('buildSrc/') && !changed.includes('ktor-')) { |
| 76 | + core.debug('Only buildSrc modified → limiting to buildSrc'); |
| 77 | + modifiedProjects = 'buildSrc'; |
| 78 | + } else { |
| 79 | + const pattern = subprojectPrefixes.join('|'); |
| 80 | + core.debug('subprojectPrefixesPattern: ' + pattern); |
| 81 | + |
| 82 | + const regex = subprojectPrefixes.length > 0 ? new RegExp(`^(${pattern})`) : null; |
| 83 | + |
| 84 | + const modifiedProjectsArray = changed |
| 85 | + .split('\n') |
| 86 | + .filter(Boolean) |
| 87 | + .filter(line => (regex ? regex.test(line) : true)) |
| 88 | + .map(line => line.split('/', 1)[0]) // project dir |
| 89 | + .filter(Boolean) |
| 90 | + .sort() |
| 91 | + .filter((v, i, a) => a.indexOf(v) === i); // uniq |
| 92 | + |
| 93 | + const merged = Array.from(new Set([...modifiedProjectsArray, ...requiredProjects])); |
| 94 | + modifiedProjects = merged.join(','); |
| 95 | + } |
| 96 | + |
| 97 | + if (modifiedProjects) { |
| 98 | + core.info(`Modified subprojects (including required): ${modifiedProjects}`); |
| 99 | + core.setOutput('modified_projects', modifiedProjects); |
| 100 | + } else { |
| 101 | + core.info('No modified subprojects'); |
| 102 | + core.setOutput('modified_projects', ''); |
52 | 103 | } |
| 104 | + } catch (error: any) { |
| 105 | + core.setFailed(`Action failed with error: ${error?.message ?? String(error)}`); |
| 106 | + } |
53 | 107 | } |
54 | 108 |
|
55 | 109 | run(); |
0 commit comments