From af9b7c519823d6fea16347776b560327fe47b1bb Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Tue, 22 Oct 2024 13:21:59 +0200 Subject: [PATCH 01/17] feat: Add support for remote GitHub owner and repo - Update default values for `commit-message`, `owner`, `repo`, `branch-name`, `branch-push-force`, and `tag-only-if-file-changes` - Add logging for getting info from context and setting branch according to input and context in `main.ts` - Update repository info fetching message with input owner and repo in `main.ts` --- action.yml | 14 +++++++++++--- src/main.ts | 14 ++++++++++++-- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/action.yml b/action.yml index d3c9df1..0bebd00 100644 --- a/action.yml +++ b/action.yml @@ -14,16 +14,24 @@ inputs: description: | Directory containing files to be committed. Default: GitHub workspace directory (root of repository). required: false - default: '' + default: "" commit-message: description: | Commit message for the file changes. required: false + owner: + description: | + GitHub repository owner (user or organization), defaults to the repo invoking the action. + required: false + repo: + description: | + GitHub repository name, defaults to the repo invoking the action. + required: false branch-name: description: | Branch to commit to. Default: Workflow triggered branch. required: false - default: '' + default: "" branch-push-force: description: | --force flag when running git push . @@ -33,7 +41,7 @@ inputs: description: | Push tag for the new/current commit. required: false - default: '' + default: "" tag-only-if-file-changes: description: | Push tag for new commit only when file changes present. diff --git a/src/main.ts b/src/main.ts index 4014982..d70534c 100644 --- a/src/main.ts +++ b/src/main.ts @@ -23,18 +23,28 @@ import { export async function run(): Promise { try { + core.info('Getting info from context') const { owner, repo, branch } = getContext() + + core.debug('Setting branch according to input and context') const inputBranch = getInput('branch-name') if (inputBranch && inputBranch !== branch) { await switchBranch(inputBranch) await pushCurrentBranch() } const currentBranch = inputBranch ? inputBranch : branch + + const inputOwner = getInput('owner') + const currentOwner = inputOwner ? inputOwner : owner + + const inputRepo = getInput('repo') + const currentRepo = inputRepo ? inputRepo : repo + const repository = await core.group( - `fetching repository info for owner: ${owner}, repo: ${repo}, branch: ${currentBranch}`, + `fetching repository info for owner: ${currentOwner}, repo: ${currentRepo}, branch: ${currentBranch}`, async () => { const startTime = Date.now() - const repositoryData = await getRepository(owner, repo, currentBranch) + const repositoryData = await getRepository(currentOwner, currentRepo, currentBranch) const endTime = Date.now() core.debug(`time taken: ${(endTime - startTime).toString()} ms`) return repositoryData From 756aef5d9653ff355fbeaa14c504faf5afe0264e Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Fri, 25 Oct 2024 11:53:56 +0200 Subject: [PATCH 02/17] Fixup: prettier --- action.yml | 6 +++--- src/main.ts | 6 +++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/action.yml b/action.yml index 0bebd00..690b6d8 100644 --- a/action.yml +++ b/action.yml @@ -14,7 +14,7 @@ inputs: description: | Directory containing files to be committed. Default: GitHub workspace directory (root of repository). required: false - default: "" + default: '' commit-message: description: | Commit message for the file changes. @@ -31,7 +31,7 @@ inputs: description: | Branch to commit to. Default: Workflow triggered branch. required: false - default: "" + default: '' branch-push-force: description: | --force flag when running git push . @@ -41,7 +41,7 @@ inputs: description: | Push tag for the new/current commit. required: false - default: "" + default: '' tag-only-if-file-changes: description: | Push tag for new commit only when file changes present. diff --git a/src/main.ts b/src/main.ts index d70534c..5d9bbf7 100644 --- a/src/main.ts +++ b/src/main.ts @@ -44,7 +44,11 @@ export async function run(): Promise { `fetching repository info for owner: ${currentOwner}, repo: ${currentRepo}, branch: ${currentBranch}`, async () => { const startTime = Date.now() - const repositoryData = await getRepository(currentOwner, currentRepo, currentBranch) + const repositoryData = await getRepository( + currentOwner, + currentRepo, + currentBranch + ) const endTime = Date.now() core.debug(`time taken: ${(endTime - startTime).toString()} ms`) return repositoryData From ef960487345977a26523ba45bac87331a600083c Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Fri, 25 Oct 2024 11:56:07 +0200 Subject: [PATCH 03/17] Fixup: Build Ts --- dist/index.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 258dd67..bfc7690 100644 --- a/dist/index.js +++ b/dist/index.js @@ -30387,16 +30387,22 @@ function run() { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; try { + core.info('Getting info from context'); const { owner, repo, branch } = (0, repo_1.getContext)(); + core.debug('Setting branch according to input and context'); const inputBranch = (0, input_1.getInput)('branch-name'); if (inputBranch && inputBranch !== branch) { yield (0, git_1.switchBranch)(inputBranch); yield (0, git_1.pushCurrentBranch)(); } const currentBranch = inputBranch ? inputBranch : branch; - const repository = yield core.group(`fetching repository info for owner: ${owner}, repo: ${repo}, branch: ${currentBranch}`, () => __awaiter(this, void 0, void 0, function* () { + const inputOwner = (0, input_1.getInput)('owner'); + const currentOwner = inputOwner ? inputOwner : owner; + const inputRepo = (0, input_1.getInput)('repo'); + const currentRepo = inputRepo ? inputRepo : repo; + const repository = yield core.group(`fetching repository info for owner: ${currentOwner}, repo: ${currentRepo}, branch: ${currentBranch}`, () => __awaiter(this, void 0, void 0, function* () { const startTime = Date.now(); - const repositoryData = yield (0, graphql_1.getRepository)(owner, repo, currentBranch); + const repositoryData = yield (0, graphql_1.getRepository)(currentOwner, currentRepo, currentBranch); const endTime = Date.now(); core.debug(`time taken: ${(endTime - startTime).toString()} ms`); return repositoryData; From 19f59fe4970e4b924ec1ad96b7b4227cb6b41c37 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Tue, 29 Oct 2024 23:03:50 +0100 Subject: [PATCH 04/17] Improve logging --- dist/index.js | 38 ++++++++++++++++++++++++-------------- src/main.ts | 45 +++++++++++++++++++++++++++++---------------- 2 files changed, 53 insertions(+), 30 deletions(-) diff --git a/dist/index.js b/dist/index.js index bfc7690..f8455f5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -30387,34 +30387,44 @@ function run() { return __awaiter(this, void 0, void 0, function* () { var _a, _b, _c, _d, _e, _f; try { - core.info('Getting info from context'); + core.info('Getting info from GH Worklfow context'); const { owner, repo, branch } = (0, repo_1.getContext)(); - core.debug('Setting branch according to input and context'); + core.info('Setting variables according to inputs and context'); + core.debug('* branch'); const inputBranch = (0, input_1.getInput)('branch-name'); - if (inputBranch && inputBranch !== branch) { - yield (0, git_1.switchBranch)(inputBranch); - yield (0, git_1.pushCurrentBranch)(); - } - const currentBranch = inputBranch ? inputBranch : branch; + const selectedBranch = inputBranch ? inputBranch : branch; + core.debug('* owner'); const inputOwner = (0, input_1.getInput)('owner'); - const currentOwner = inputOwner ? inputOwner : owner; + const selectedOwner = inputOwner ? inputOwner : owner; + core.debug('* repo'); const inputRepo = (0, input_1.getInput)('repo'); - const currentRepo = inputRepo ? inputRepo : repo; - const repository = yield core.group(`fetching repository info for owner: ${currentOwner}, repo: ${currentRepo}, branch: ${currentBranch}`, () => __awaiter(this, void 0, void 0, function* () { + const selectedRepo = inputRepo ? inputRepo : repo; + core.warning('Pushing local and current branch to remote before proceeding'); + if (selectedOwner == owner && + selectedRepo == repo && + selectedBranch !== branch) { + // Git commands + yield (0, git_1.switchBranch)(selectedBranch); + yield (0, git_1.pushCurrentBranch)(); + } + const repository = yield core.group(`fetching repository info for owner: ${selectedOwner}, repo: ${selectedRepo}, branch: ${selectedBranch}`, () => __awaiter(this, void 0, void 0, function* () { const startTime = Date.now(); - const repositoryData = yield (0, graphql_1.getRepository)(currentOwner, currentRepo, currentBranch); + const repositoryData = yield (0, graphql_1.getRepository)(selectedOwner, selectedRepo, selectedBranch); const endTime = Date.now(); core.debug(`time taken: ${(endTime - startTime).toString()} ms`); return repositoryData; })); + core.info('Checking remote branches'); if (!repository.ref) { - if (inputBranch && currentBranch == inputBranch) { + if (inputBranch) { throw new errors_1.InputBranchNotFound(inputBranch); } else { - throw new errors_1.BranchNotFound(currentBranch); + throw new errors_1.BranchNotFound(branch); } } + core.info('Processing to create signed commit'); + core.debug('Get last (current?) commit'); const currentCommit = (_b = (_a = repository.ref.target.history) === null || _a === void 0 ? void 0 : _a.nodes) === null || _b === void 0 ? void 0 : _b[0]; if (!currentCommit) { throw new errors_1.BranchCommitNotFound(repository.ref.name); @@ -30447,7 +30457,7 @@ function run() { const startTime = Date.now(); const commitData = yield (0, graphql_1.createCommitOnBranch)(currentCommit, commitMessage, { repositoryNameWithOwner: repository.nameWithOwner, - branchName: currentBranch, + branchName: selectedBranch, }, fileChanges); const endTime = Date.now(); core.debug(`time taken: ${(endTime - startTime).toString()} ms`); diff --git a/src/main.ts b/src/main.ts index 5d9bbf7..3a4099a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -23,31 +23,41 @@ import { export async function run(): Promise { try { - core.info('Getting info from context') + core.info('Getting info from GH Worklfow context') const { owner, repo, branch } = getContext() - core.debug('Setting branch according to input and context') + core.info('Setting variables according to inputs and context') + core.debug('* branch') const inputBranch = getInput('branch-name') - if (inputBranch && inputBranch !== branch) { - await switchBranch(inputBranch) - await pushCurrentBranch() - } - const currentBranch = inputBranch ? inputBranch : branch + const selectedBranch = inputBranch ? inputBranch : branch + core.debug('* owner') const inputOwner = getInput('owner') - const currentOwner = inputOwner ? inputOwner : owner + const selectedOwner = inputOwner ? inputOwner : owner + core.debug('* repo') const inputRepo = getInput('repo') - const currentRepo = inputRepo ? inputRepo : repo + const selectedRepo = inputRepo ? inputRepo : repo + + core.warning('Pushing local and current branch to remote before proceeding') + if ( + selectedOwner == owner && + selectedRepo == repo && + selectedBranch !== branch + ) { + // Git commands + await switchBranch(selectedBranch) + await pushCurrentBranch() + } const repository = await core.group( - `fetching repository info for owner: ${currentOwner}, repo: ${currentRepo}, branch: ${currentBranch}`, + `fetching repository info for owner: ${selectedOwner}, repo: ${selectedRepo}, branch: ${selectedBranch}`, async () => { const startTime = Date.now() const repositoryData = await getRepository( - currentOwner, - currentRepo, - currentBranch + selectedOwner, + selectedRepo, + selectedBranch ) const endTime = Date.now() core.debug(`time taken: ${(endTime - startTime).toString()} ms`) @@ -55,14 +65,17 @@ export async function run(): Promise { } ) + core.info('Checking remote branches') if (!repository.ref) { - if (inputBranch && currentBranch == inputBranch) { + if (inputBranch) { throw new InputBranchNotFound(inputBranch) } else { - throw new BranchNotFound(currentBranch) + throw new BranchNotFound(branch) } } + core.info('Processing to create signed commit') + core.debug('Get last (current?) commit') const currentCommit = repository.ref.target.history?.nodes?.[0] if (!currentCommit) { throw new BranchCommitNotFound(repository.ref.name) @@ -103,7 +116,7 @@ export async function run(): Promise { commitMessage, { repositoryNameWithOwner: repository.nameWithOwner, - branchName: currentBranch, + branchName: selectedBranch, }, fileChanges ) From 024ca0213c4eaabcd353749051d7cc49c42b0216 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Tue, 29 Oct 2024 23:04:38 +0100 Subject: [PATCH 05/17] Add support 'workspace' for all Git commands --- dist/index.js | 5 ++++- src/git.ts | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index f8455f5..6767301 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29989,7 +29989,10 @@ function execGit(args) { const debugOutput = []; const warningOutput = []; const errorOutput = []; - yield (0, exec_1.exec)('git', args, { + const workspace = (0, cwd_1.getWorkspace)(); + const gitArgs = ['-C', workspace]; + gitArgs.concat(args); + yield (0, exec_1.exec)('git', gitArgs, { silent: true, ignoreReturnCode: true, listeners: { diff --git a/src/git.ts b/src/git.ts index 64f2efa..1504a2b 100644 --- a/src/git.ts +++ b/src/git.ts @@ -14,7 +14,11 @@ async function execGit(args: string[]) { const warningOutput: string[] = [] const errorOutput: string[] = [] - await exec('git', args, { + const workspace = getWorkspace() + const gitArgs = ['-C', workspace] + gitArgs.concat(args) + + await exec('git', gitArgs, { silent: true, ignoreReturnCode: true, listeners: { @@ -55,7 +59,6 @@ export async function pushCurrentBranch() { export async function addFileChanges(globPatterns: string[]) { const workspace = getWorkspace() const workspacePaths = globPatterns.map((p) => join(workspace, p)) - await execGit(['add', '--', ...workspacePaths]) } From 88721f90625504a3db0548a3b5ef36c01be17347 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Fri, 25 Oct 2024 18:14:17 +0200 Subject: [PATCH 06/17] Fixup --- dist/index.js | 5 ++++- src/git.ts | 1 + src/main.ts | 7 ++++++- 3 files changed, 11 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 6767301..23573fc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29992,6 +29992,7 @@ function execGit(args) { const workspace = (0, cwd_1.getWorkspace)(); const gitArgs = ['-C', workspace]; gitArgs.concat(args); + core.debug(JSON.stringify(gitArgs)); yield (0, exec_1.exec)('git', gitArgs, { silent: true, ignoreReturnCode: true, @@ -30402,10 +30403,10 @@ function run() { core.debug('* repo'); const inputRepo = (0, input_1.getInput)('repo'); const selectedRepo = inputRepo ? inputRepo : repo; - core.warning('Pushing local and current branch to remote before proceeding'); if (selectedOwner == owner && selectedRepo == repo && selectedBranch !== branch) { + core.warning('Pushing local and current branch to remote before proceeding'); // Git commands yield (0, git_1.switchBranch)(selectedBranch); yield (0, git_1.pushCurrentBranch)(); @@ -30439,7 +30440,9 @@ function run() { } else { core.debug(`proceed with file commit, input: ${JSON.stringify(filePaths)}`); + core.debug('Adding files to git index according to "filePaths"'); yield (0, git_1.addFileChanges)(filePaths); + core.debug('Getting changed files'); const fileChanges = yield (0, git_1.getFileChanges)(); const fileCount = ((_d = (_c = fileChanges.additions) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) + ((_f = (_e = fileChanges.deletions) === null || _e === void 0 ? void 0 : _e.length) !== null && _f !== void 0 ? _f : 0); diff --git a/src/git.ts b/src/git.ts index 1504a2b..db92c5a 100644 --- a/src/git.ts +++ b/src/git.ts @@ -17,6 +17,7 @@ async function execGit(args: string[]) { const workspace = getWorkspace() const gitArgs = ['-C', workspace] gitArgs.concat(args) + core.debug(JSON.stringify(gitArgs)) await exec('git', gitArgs, { silent: true, diff --git a/src/main.ts b/src/main.ts index 3a4099a..57776d2 100644 --- a/src/main.ts +++ b/src/main.ts @@ -39,12 +39,14 @@ export async function run(): Promise { const inputRepo = getInput('repo') const selectedRepo = inputRepo ? inputRepo : repo - core.warning('Pushing local and current branch to remote before proceeding') if ( selectedOwner == owner && selectedRepo == repo && selectedBranch !== branch ) { + core.warning( + 'Pushing local and current branch to remote before proceeding' + ) // Git commands await switchBranch(selectedBranch) await pushCurrentBranch() @@ -90,7 +92,10 @@ export async function run(): Promise { `proceed with file commit, input: ${JSON.stringify(filePaths)}` ) + core.debug('Adding files to git index according to "filePaths"') await addFileChanges(filePaths) + + core.debug('Getting changed files') const fileChanges = await getFileChanges() const fileCount = (fileChanges.additions?.length ?? 0) + From 01cac32aa58fac5f49c9996f8336f0ea5dfb6921 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Fri, 25 Oct 2024 22:34:30 +0200 Subject: [PATCH 07/17] Fixup --- dist/index.js | 3 ++- src/git.ts | 4 +++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 23573fc..c00a3e5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29989,10 +29989,11 @@ function execGit(args) { const debugOutput = []; const warningOutput = []; const errorOutput = []; + core.debug('execGit() - args: ' + JSON.stringify(args)); const workspace = (0, cwd_1.getWorkspace)(); const gitArgs = ['-C', workspace]; gitArgs.concat(args); - core.debug(JSON.stringify(gitArgs)); + core.debug('execGit() - gitArgs: ' + JSON.stringify(gitArgs)); yield (0, exec_1.exec)('git', gitArgs, { silent: true, ignoreReturnCode: true, diff --git a/src/git.ts b/src/git.ts index db92c5a..e2b2256 100644 --- a/src/git.ts +++ b/src/git.ts @@ -14,10 +14,12 @@ async function execGit(args: string[]) { const warningOutput: string[] = [] const errorOutput: string[] = [] + core.debug('execGit() - args: ' + JSON.stringify(args)) + const workspace = getWorkspace() const gitArgs = ['-C', workspace] gitArgs.concat(args) - core.debug(JSON.stringify(gitArgs)) + core.debug('execGit() - gitArgs: ' + JSON.stringify(gitArgs)) await exec('git', gitArgs, { silent: true, From 809a7c0c069cf3a4c61b8a9c8e5f7068f1bd7b1c Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Fri, 25 Oct 2024 22:52:24 +0200 Subject: [PATCH 08/17] Fixup --- dist/index.js | 12 +++++++----- src/git.ts | 13 ++++++++----- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/dist/index.js b/dist/index.js index c00a3e5..0115668 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29989,12 +29989,14 @@ function execGit(args) { const debugOutput = []; const warningOutput = []; const errorOutput = []; - core.debug('execGit() - args: ' + JSON.stringify(args)); const workspace = (0, cwd_1.getWorkspace)(); - const gitArgs = ['-C', workspace]; - gitArgs.concat(args); - core.debug('execGit() - gitArgs: ' + JSON.stringify(gitArgs)); - yield (0, exec_1.exec)('git', gitArgs, { + const defaultArgs = ['-C', workspace]; + core.debug('execGit() - defaultArgs: ' + JSON.stringify(defaultArgs)); + core.debug('execGit() - args: ' + JSON.stringify(args)); + const mergedArgs = []; + mergedArgs.concat(defaultArgs).concat(args); + core.debug('execGit() - mergedArgs: ' + JSON.stringify(mergedArgs)); + yield (0, exec_1.exec)('git', mergedArgs, { silent: true, ignoreReturnCode: true, listeners: { diff --git a/src/git.ts b/src/git.ts index e2b2256..4325438 100644 --- a/src/git.ts +++ b/src/git.ts @@ -14,14 +14,17 @@ async function execGit(args: string[]) { const warningOutput: string[] = [] const errorOutput: string[] = [] + const workspace = getWorkspace() + const defaultArgs = ['-C', workspace] + core.debug('execGit() - defaultArgs: ' + JSON.stringify(defaultArgs)) + core.debug('execGit() - args: ' + JSON.stringify(args)) - const workspace = getWorkspace() - const gitArgs = ['-C', workspace] - gitArgs.concat(args) - core.debug('execGit() - gitArgs: ' + JSON.stringify(gitArgs)) + const mergedArgs: string[] = [] + mergedArgs.concat(defaultArgs).concat(args) + core.debug('execGit() - mergedArgs: ' + JSON.stringify(mergedArgs)) - await exec('git', gitArgs, { + await exec('git', mergedArgs, { silent: true, ignoreReturnCode: true, listeners: { From 5e0c80777f9eb5c4b2a6416be52bf25dccca67be Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Fri, 25 Oct 2024 22:55:50 +0200 Subject: [PATCH 09/17] Fixup --- dist/index.js | 3 +-- src/git.ts | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 0115668..950173d 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29993,8 +29993,7 @@ function execGit(args) { const defaultArgs = ['-C', workspace]; core.debug('execGit() - defaultArgs: ' + JSON.stringify(defaultArgs)); core.debug('execGit() - args: ' + JSON.stringify(args)); - const mergedArgs = []; - mergedArgs.concat(defaultArgs).concat(args); + const mergedArgs = defaultArgs.concat(args); core.debug('execGit() - mergedArgs: ' + JSON.stringify(mergedArgs)); yield (0, exec_1.exec)('git', mergedArgs, { silent: true, diff --git a/src/git.ts b/src/git.ts index 4325438..e444c68 100644 --- a/src/git.ts +++ b/src/git.ts @@ -20,8 +20,7 @@ async function execGit(args: string[]) { core.debug('execGit() - args: ' + JSON.stringify(args)) - const mergedArgs: string[] = [] - mergedArgs.concat(defaultArgs).concat(args) + const mergedArgs = defaultArgs.concat(args) core.debug('execGit() - mergedArgs: ' + JSON.stringify(mergedArgs)) await exec('git', mergedArgs, { From f10c232d82009eb5150176ca119af7518c680672 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Fri, 25 Oct 2024 23:07:45 +0200 Subject: [PATCH 10/17] Fixup --- dist/index.js | 5 +---- src/git.ts | 4 +--- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/dist/index.js b/dist/index.js index 950173d..c2d5c64 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29982,7 +29982,6 @@ exports.addFileChanges = addFileChanges; exports.getFileChanges = getFileChanges; const core = __importStar(__nccwpck_require__(7484)); const exec_1 = __nccwpck_require__(5236); -const node_path_1 = __nccwpck_require__(6760); const cwd_1 = __nccwpck_require__(9827); function execGit(args) { return __awaiter(this, void 0, void 0, function* () { @@ -30039,9 +30038,7 @@ function pushCurrentBranch() { } function addFileChanges(globPatterns) { return __awaiter(this, void 0, void 0, function* () { - const workspace = (0, cwd_1.getWorkspace)(); - const workspacePaths = globPatterns.map((p) => (0, node_path_1.join)(workspace, p)); - yield execGit(['add', '--', ...workspacePaths]); + yield execGit(['add', '--', ...globPatterns]); }); } function processFileChanges(output) { diff --git a/src/git.ts b/src/git.ts index e444c68..c5a01ef 100644 --- a/src/git.ts +++ b/src/git.ts @@ -62,9 +62,7 @@ export async function pushCurrentBranch() { } export async function addFileChanges(globPatterns: string[]) { - const workspace = getWorkspace() - const workspacePaths = globPatterns.map((p) => join(workspace, p)) - await execGit(['add', '--', ...workspacePaths]) + await execGit(['add', '--', ...globPatterns]) } function processFileChanges(output: string[]) { From 8b29d5e8bfe5997c7a096448a3e68fbd556c4a46 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Tue, 29 Oct 2024 14:39:43 +0100 Subject: [PATCH 11/17] Fixup tests --- __tests__/git.test.ts | 4 ++-- dist/index.js | 10 ++++++++-- src/git.ts | 14 ++++++++++---- 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index 4182e9b..dc5a368 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -190,7 +190,7 @@ describe('Git CLI', () => { await addFileChanges(['*.ts', '~/.bashrc']) expect(execMock).toHaveBeenCalledWith( 'git', - ['add', '--', '/test-workspace/*.ts', '/test-workspace/~/.bashrc'], + ['-C', '/test-workspace', 'add', '--', '*.ts', '~/.bashrc'], expect.objectContaining({ listeners: { stdline: expect.anything(), errline: expect.anything() }, }) @@ -283,7 +283,7 @@ describe('Git CLI', () => { const changes = await getFileChanges() expect(execMock).toHaveBeenCalledWith( 'git', - ['status', '-suno', '--porcelain'], + ['-C', '/test-workspace', 'status', '-suno', '--porcelain'], expect.objectContaining({ listeners: { stdline: expect.anything(), errline: expect.anything() }, }) diff --git a/dist/index.js b/dist/index.js index c2d5c64..00240a4 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29988,10 +29988,16 @@ function execGit(args) { const debugOutput = []; const warningOutput = []; const errorOutput = []; + const defaultArgs = []; + // Handle workspace const workspace = (0, cwd_1.getWorkspace)(); - const defaultArgs = ['-C', workspace]; + if (workspace !== '') { + defaultArgs.push('-C'); + defaultArgs.push(workspace); + core.debug('execGit() - Adding GHA parameter "workspace" to git cli args'); + } core.debug('execGit() - defaultArgs: ' + JSON.stringify(defaultArgs)); - core.debug('execGit() - args: ' + JSON.stringify(args)); + core.debug('execGit() - args parameter: ' + JSON.stringify(args)); const mergedArgs = defaultArgs.concat(args); core.debug('execGit() - mergedArgs: ' + JSON.stringify(mergedArgs)); yield (0, exec_1.exec)('git', mergedArgs, { diff --git a/src/git.ts b/src/git.ts index c5a01ef..e822a85 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,6 +1,5 @@ import * as core from '@actions/core' import { exec } from '@actions/exec' -import { join } from 'node:path' import { FileChanges, FileAddition, @@ -14,11 +13,18 @@ async function execGit(args: string[]) { const warningOutput: string[] = [] const errorOutput: string[] = [] + const defaultArgs: string[] = [] + + // Handle workspace const workspace = getWorkspace() - const defaultArgs = ['-C', workspace] - core.debug('execGit() - defaultArgs: ' + JSON.stringify(defaultArgs)) + if (workspace !== '') { + defaultArgs.push('-C') + defaultArgs.push(workspace) + core.debug('execGit() - Adding GHA parameter "workspace" to git cli args') + } - core.debug('execGit() - args: ' + JSON.stringify(args)) + core.debug('execGit() - defaultArgs: ' + JSON.stringify(defaultArgs)) + core.debug('execGit() - args parameter: ' + JSON.stringify(args)) const mergedArgs = defaultArgs.concat(args) core.debug('execGit() - mergedArgs: ' + JSON.stringify(mergedArgs)) From 9b0e643bf442db9b330485040ef39e4384c8d908 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Tue, 29 Oct 2024 15:13:20 +0100 Subject: [PATCH 12/17] Fixup: processFileChanges() --- dist/index.js | 24 +++++++++++++++--------- src/git.ts | 11 ++++++++--- src/main.ts | 17 +++++++++-------- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/dist/index.js b/dist/index.js index 00240a4..a63c461 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29982,6 +29982,7 @@ exports.addFileChanges = addFileChanges; exports.getFileChanges = getFileChanges; const core = __importStar(__nccwpck_require__(7484)); const exec_1 = __nccwpck_require__(5236); +const node_path_1 = __nccwpck_require__(6760); const cwd_1 = __nccwpck_require__(9827); function execGit(args) { return __awaiter(this, void 0, void 0, function* () { @@ -30050,22 +30051,24 @@ function addFileChanges(globPatterns) { function processFileChanges(output) { const additions = []; const deletions = []; + // Handle workspace + const workspace = (0, cwd_1.getWorkspace)(); for (const line of output) { const staged = line.charAt(0); const filePath = line.slice(3); switch (staged) { case 'D': { - deletions.push({ path: filePath }); + deletions.push({ path: (0, node_path_1.join)(workspace, filePath) }); break; } case '?': case 'A': case 'M': { - additions.push({ path: filePath, contents: '' }); + additions.push({ path: (0, node_path_1.join)(workspace, filePath), contents: '' }); break; } case 'R': { - const [from, to] = filePath.split('->'); + const [from, to] = (0, node_path_1.join)(workspace, filePath).split('->'); deletions.push({ path: from.trim() }); additions.push({ path: to.trim(), contents: '' }); break; @@ -30441,13 +30444,16 @@ function run() { let createdCommit; const filePaths = core.getMultilineInput('files'); if (filePaths.length <= 0) { - core.debug('skip file commit, empty files input'); + core.warning('skip file commit, empty files input'); } else { - core.debug(`proceed with file commit, input: ${JSON.stringify(filePaths)}`); - core.debug('Adding files to git index according to "filePaths"'); + core.info(`proceed with file commit, input: ${JSON.stringify(filePaths)}`); + core.info('Adding files to git index according to "filePaths"'); yield (0, git_1.addFileChanges)(filePaths); - core.debug('Getting changed files'); + core.info('Getting changed files'); + // NOTE: getFileChanges() returns a map of FileChanges (from '@octokit/graphql-schema'). + // The 'contents' property of each FileChange is hard coded to an empty string in processFileChanges(). + // This is expected. const fileChanges = yield (0, git_1.getFileChanges)(); const fileCount = ((_d = (_c = fileChanges.additions) === null || _c === void 0 ? void 0 : _c.length) !== null && _d !== void 0 ? _d : 0) + ((_f = (_e = fileChanges.deletions) === null || _e === void 0 ? void 0 : _e.length) !== null && _f !== void 0 ? _f : 0); @@ -30484,11 +30490,11 @@ function run() { } const tag = (0, input_1.getInput)('tag'); if (!tag) { - core.debug('skip commit tagging, empty tag input'); + core.notice('skip commit tagging, empty tag input'); } else { const tagCommit = createdCommit !== null && createdCommit !== void 0 ? createdCommit : currentCommit; - core.debug(`proceed with commit tagging, input: ${tag}, commit: ${tagCommit.oid}`); + core.info(`proceed with commit tagging, input: ${tag}, commit: ${tagCommit.oid}`); const tagResponse = yield core.group('tagging commit', () => __awaiter(this, void 0, void 0, function* () { const startTime = Date.now(); const tagData = yield (0, graphql_1.createTagOnCommit)(tagCommit, tag, repository.id); diff --git a/src/git.ts b/src/git.ts index e822a85..dc3236b 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,5 +1,6 @@ import * as core from '@actions/core' import { exec } from '@actions/exec' +import { join } from 'node:path' import { FileChanges, FileAddition, @@ -74,22 +75,26 @@ export async function addFileChanges(globPatterns: string[]) { function processFileChanges(output: string[]) { const additions: FileAddition[] = [] const deletions: FileDeletion[] = [] + + // Handle workspace + const workspace = getWorkspace() + for (const line of output) { const staged = line.charAt(0) const filePath = line.slice(3) switch (staged) { case 'D': { - deletions.push({ path: filePath }) + deletions.push({ path: join(workspace, filePath) }) break } case '?': case 'A': case 'M': { - additions.push({ path: filePath, contents: '' }) + additions.push({ path: join(workspace, filePath), contents: '' }) break } case 'R': { - const [from, to] = filePath.split('->') + const [from, to] = join(workspace, filePath).split('->') deletions.push({ path: from.trim() }) additions.push({ path: to.trim(), contents: '' }) break diff --git a/src/main.ts b/src/main.ts index 57776d2..db4ea0d 100644 --- a/src/main.ts +++ b/src/main.ts @@ -86,16 +86,17 @@ export async function run(): Promise { let createdCommit: Commit | undefined const filePaths = core.getMultilineInput('files') if (filePaths.length <= 0) { - core.debug('skip file commit, empty files input') + core.warning('skip file commit, empty files input') } else { - core.debug( - `proceed with file commit, input: ${JSON.stringify(filePaths)}` - ) + core.info(`proceed with file commit, input: ${JSON.stringify(filePaths)}`) - core.debug('Adding files to git index according to "filePaths"') + core.info('Adding files to git index according to "filePaths"') await addFileChanges(filePaths) - core.debug('Getting changed files') + core.info('Getting changed files') + // NOTE: getFileChanges() returns a map of FileChanges (from '@octokit/graphql-schema'). + // The 'contents' property of each FileChange is hard coded to an empty string in processFileChanges(). + // This is expected. const fileChanges = await getFileChanges() const fileCount = (fileChanges.additions?.length ?? 0) + @@ -141,10 +142,10 @@ export async function run(): Promise { const tag = getInput('tag') if (!tag) { - core.debug('skip commit tagging, empty tag input') + core.notice('skip commit tagging, empty tag input') } else { const tagCommit = createdCommit ?? currentCommit - core.debug( + core.info( `proceed with commit tagging, input: ${tag}, commit: ${tagCommit.oid as string}` ) const tagResponse = await core.group('tagging commit', async () => { From 537f41c5aa142b6b781ba67df9bf051ae4513f4d Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Tue, 29 Oct 2024 15:28:08 +0100 Subject: [PATCH 13/17] Fixup --- dist/index.js | 15 ++++++++------- src/git.ts | 11 +++-------- src/github/graphql.ts | 9 +++++++-- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/dist/index.js b/dist/index.js index a63c461..acb9ddc 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29982,7 +29982,6 @@ exports.addFileChanges = addFileChanges; exports.getFileChanges = getFileChanges; const core = __importStar(__nccwpck_require__(7484)); const exec_1 = __nccwpck_require__(5236); -const node_path_1 = __nccwpck_require__(6760); const cwd_1 = __nccwpck_require__(9827); function execGit(args) { return __awaiter(this, void 0, void 0, function* () { @@ -30051,24 +30050,22 @@ function addFileChanges(globPatterns) { function processFileChanges(output) { const additions = []; const deletions = []; - // Handle workspace - const workspace = (0, cwd_1.getWorkspace)(); for (const line of output) { const staged = line.charAt(0); const filePath = line.slice(3); switch (staged) { case 'D': { - deletions.push({ path: (0, node_path_1.join)(workspace, filePath) }); + deletions.push({ path: filePath }); break; } case '?': case 'A': case 'M': { - additions.push({ path: (0, node_path_1.join)(workspace, filePath), contents: '' }); + additions.push({ path: filePath, contents: '' }); break; } case 'R': { - const [from, to] = (0, node_path_1.join)(workspace, filePath).split('->'); + const [from, to] = filePath.split('->'); deletions.push({ path: from.trim() }); additions.push({ path: to.trim(), contents: '' }); break; @@ -30161,10 +30158,12 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.getRepository = getRepository; exports.createCommitOnBranch = createCommitOnBranch; exports.createTagOnCommit = createTagOnCommit; +const node_path_1 = __nccwpck_require__(6760); const core = __importStar(__nccwpck_require__(7484)); const graphql_1 = __nccwpck_require__(7); const client_1 = __nccwpck_require__(6584); const blob_1 = __nccwpck_require__(1408); +const cwd_1 = __nccwpck_require__(9827); function formatLogMessage(...params) { return Object.entries(Object.assign({}, ...params)) .map(([key, value]) => { @@ -30256,8 +30255,10 @@ const createCommitMutation = ` `; function createCommitOnBranch(currentCommit, commitMessage, branch, fileChanges) { return __awaiter(this, void 0, void 0, function* () { + // Handle workspace + const workspace = (0, cwd_1.getWorkspace)(); if (fileChanges.additions) { - const promises = fileChanges.additions.map((file) => (0, blob_1.getBlob)(file.path).load()); + const promises = fileChanges.additions.map((file) => (0, blob_1.getBlob)((0, node_path_1.join)(workspace, file.path)).load()); fileChanges.additions = yield Promise.all(promises); } const commitInput = { diff --git a/src/git.ts b/src/git.ts index dc3236b..e822a85 100644 --- a/src/git.ts +++ b/src/git.ts @@ -1,6 +1,5 @@ import * as core from '@actions/core' import { exec } from '@actions/exec' -import { join } from 'node:path' import { FileChanges, FileAddition, @@ -75,26 +74,22 @@ export async function addFileChanges(globPatterns: string[]) { function processFileChanges(output: string[]) { const additions: FileAddition[] = [] const deletions: FileDeletion[] = [] - - // Handle workspace - const workspace = getWorkspace() - for (const line of output) { const staged = line.charAt(0) const filePath = line.slice(3) switch (staged) { case 'D': { - deletions.push({ path: join(workspace, filePath) }) + deletions.push({ path: filePath }) break } case '?': case 'A': case 'M': { - additions.push({ path: join(workspace, filePath), contents: '' }) + additions.push({ path: filePath, contents: '' }) break } case 'R': { - const [from, to] = join(workspace, filePath).split('->') + const [from, to] = filePath.split('->') deletions.push({ path: from.trim() }) additions.push({ path: to.trim(), contents: '' }) break diff --git a/src/github/graphql.ts b/src/github/graphql.ts index 9d93147..30787f2 100644 --- a/src/github/graphql.ts +++ b/src/github/graphql.ts @@ -1,3 +1,4 @@ +import { join } from 'node:path' import * as core from '@actions/core' import { GraphqlResponseError } from '@octokit/graphql' import { RequestParameters } from '@octokit/types' @@ -12,8 +13,9 @@ import { } from '@octokit/graphql-schema' import { graphqlClient } from './client' +import { RepositoryWithCommitHistory } from './types' import { getBlob } from '../blob' -import { RepositoryWithCommitHistory } from '../github/types' +import { getWorkspace } from '../utils/cwd' function formatLogMessage(...params: Record[]): string { return Object.entries(Object.assign({}, ...params) as Record) @@ -125,9 +127,12 @@ export async function createCommitOnBranch( branch: CommittableBranch, fileChanges: FileChanges ): Promise { + // Handle workspace + const workspace = getWorkspace() + if (fileChanges.additions) { const promises = fileChanges.additions.map((file) => - getBlob(file.path).load() + getBlob(join(workspace, file.path)).load() ) fileChanges.additions = await Promise.all(promises) } From d2287e7ee179ee6fa524f3cdbdec832e0ef14d90 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Tue, 29 Oct 2024 16:17:07 +0100 Subject: [PATCH 14/17] Fixup createCommitOnBranch() --- dist/index.js | 19 ++++++++++--------- src/blob.ts | 18 +++++++++++++++--- src/github/graphql.ts | 7 +------ src/main.ts | 4 ++-- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/dist/index.js b/dist/index.js index acb9ddc..ea1aa2e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29857,7 +29857,12 @@ const base64_encoder_1 = __importDefault(__nccwpck_require__(5564)); class Blob { constructor(path) { const cwd = (0, cwd_1.getCwd)(); - this.absolutePath = path.startsWith(cwd) ? path : (0, node_path_1.join)(cwd, path); + const workspace = (0, cwd_1.getWorkspace)(); + core.debug('Blob.constructor() - Add workspace directory to filepath'); + const tmpPath = (0, node_path_1.join)(workspace, path); + // Add GHA cwd + this.absolutePath = tmpPath.startsWith(cwd) ? tmpPath : (0, node_path_1.join)(cwd, tmpPath); + // Remove GHA cwd this.path = path.startsWith(cwd) ? path.replace(new RegExp(cwd, 'g'), '') : path; @@ -29880,7 +29885,7 @@ class Blob { else if (typeof chunk === 'string') chunks.push(node_buffer_1.Buffer.from(chunk)); // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - core.debug(`received blob: ${chunk}`); + core.debug(`Blob.load() - filepath ${this.absolutePath}, received blob: ${chunk}`); }); stream.on('error', (err) => { throw new Error(`Read file failed, error: ${err.message}, path: ${this.absolutePath}`); @@ -30158,12 +30163,10 @@ Object.defineProperty(exports, "__esModule", ({ value: true })); exports.getRepository = getRepository; exports.createCommitOnBranch = createCommitOnBranch; exports.createTagOnCommit = createTagOnCommit; -const node_path_1 = __nccwpck_require__(6760); const core = __importStar(__nccwpck_require__(7484)); const graphql_1 = __nccwpck_require__(7); const client_1 = __nccwpck_require__(6584); const blob_1 = __nccwpck_require__(1408); -const cwd_1 = __nccwpck_require__(9827); function formatLogMessage(...params) { return Object.entries(Object.assign({}, ...params)) .map(([key, value]) => { @@ -30255,10 +30258,8 @@ const createCommitMutation = ` `; function createCommitOnBranch(currentCommit, commitMessage, branch, fileChanges) { return __awaiter(this, void 0, void 0, function* () { - // Handle workspace - const workspace = (0, cwd_1.getWorkspace)(); if (fileChanges.additions) { - const promises = fileChanges.additions.map((file) => (0, blob_1.getBlob)((0, node_path_1.join)(workspace, file.path)).load()); + const promises = fileChanges.additions.map((file) => (0, blob_1.getBlob)(file.path).load()); fileChanges.additions = yield Promise.all(promises); } const commitInput = { @@ -30415,7 +30416,7 @@ function run() { if (selectedOwner == owner && selectedRepo == repo && selectedBranch !== branch) { - core.warning('Pushing local and current branch to remote before proceeding'); + core.notice('Pushing local and current branch to remote before proceeding'); // Git commands yield (0, git_1.switchBranch)(selectedBranch); yield (0, git_1.pushCurrentBranch)(); @@ -30448,7 +30449,7 @@ function run() { core.warning('skip file commit, empty files input'); } else { - core.info(`proceed with file commit, input: ${JSON.stringify(filePaths)}`); + core.info(`Proceed with file commit, input: ${JSON.stringify(filePaths)}`); core.info('Adding files to git index according to "filePaths"'); yield (0, git_1.addFileChanges)(filePaths); core.info('Getting changed files'); diff --git a/src/blob.ts b/src/blob.ts index b6d598b..0064355 100644 --- a/src/blob.ts +++ b/src/blob.ts @@ -6,16 +6,26 @@ import { Readable } from 'node:stream' import { finished } from 'node:stream/promises' import { FileAddition } from '@octokit/graphql-schema' -import { getCwd } from './utils/cwd' +import { getCwd, getWorkspace } from './utils/cwd' import Base64Encoder from './stream/base64-encoder' export class Blob { + // Returned as a property of FileChange object path: string + // Used for content access absolutePath: string constructor(path: string) { const cwd = getCwd() - this.absolutePath = path.startsWith(cwd) ? path : join(cwd, path) + const workspace = getWorkspace() + + core.debug('Blob.constructor() - Add workspace directory to filepath') + const tmpPath = join(workspace, path) + + // Add GHA cwd + this.absolutePath = tmpPath.startsWith(cwd) ? tmpPath : join(cwd, tmpPath) + + // Remove GHA cwd this.path = path.startsWith(cwd) ? path.replace(new RegExp(cwd, 'g'), '') : path @@ -40,7 +50,9 @@ export class Blob { else if (typeof chunk === 'string') chunks.push(Buffer.from(chunk)) // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - core.debug(`received blob: ${chunk}`) + core.debug( + `Blob.load() - filepath ${this.absolutePath}, received blob: ${chunk}` + ) }) stream.on('error', (err) => { diff --git a/src/github/graphql.ts b/src/github/graphql.ts index 30787f2..e363d82 100644 --- a/src/github/graphql.ts +++ b/src/github/graphql.ts @@ -1,4 +1,3 @@ -import { join } from 'node:path' import * as core from '@actions/core' import { GraphqlResponseError } from '@octokit/graphql' import { RequestParameters } from '@octokit/types' @@ -15,7 +14,6 @@ import { import { graphqlClient } from './client' import { RepositoryWithCommitHistory } from './types' import { getBlob } from '../blob' -import { getWorkspace } from '../utils/cwd' function formatLogMessage(...params: Record[]): string { return Object.entries(Object.assign({}, ...params) as Record) @@ -127,12 +125,9 @@ export async function createCommitOnBranch( branch: CommittableBranch, fileChanges: FileChanges ): Promise { - // Handle workspace - const workspace = getWorkspace() - if (fileChanges.additions) { const promises = fileChanges.additions.map((file) => - getBlob(join(workspace, file.path)).load() + getBlob(file.path).load() ) fileChanges.additions = await Promise.all(promises) } diff --git a/src/main.ts b/src/main.ts index db4ea0d..34da28a 100644 --- a/src/main.ts +++ b/src/main.ts @@ -44,7 +44,7 @@ export async function run(): Promise { selectedRepo == repo && selectedBranch !== branch ) { - core.warning( + core.notice( 'Pushing local and current branch to remote before proceeding' ) // Git commands @@ -88,7 +88,7 @@ export async function run(): Promise { if (filePaths.length <= 0) { core.warning('skip file commit, empty files input') } else { - core.info(`proceed with file commit, input: ${JSON.stringify(filePaths)}`) + core.info(`Proceed with file commit, input: ${JSON.stringify(filePaths)}`) core.info('Adding files to git index according to "filePaths"') await addFileChanges(filePaths) From 8f57c46fdfe72f99abcbadfe7a487a58c641c353 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Tue, 29 Oct 2024 17:57:01 +0100 Subject: [PATCH 15/17] Fixup: Global for Git, Blog and Tests --- __tests__/git.test.ts | 38 +++++++++++++++++++++++++++++++++----- dist/index.js | 19 ++++++++----------- src/blob.ts | 15 ++++++++------- src/git.ts | 9 ++------- 4 files changed, 51 insertions(+), 30 deletions(-) diff --git a/__tests__/git.test.ts b/__tests__/git.test.ts index dc5a368..969ff59 100644 --- a/__tests__/git.test.ts +++ b/__tests__/git.test.ts @@ -15,13 +15,16 @@ describe('Git CLI', () => { }) describe('git checkout', () => { + beforeEach(() => { + jest.spyOn(cwd, 'getWorkspace').mockReturnValue('/test-workspace') + }) + it('should create new branch', async () => { const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0) - await switchBranch('new-branch') expect(execMock).toHaveBeenCalledWith( 'git', - ['checkout', '-b', 'new-branch'], + ['-C', '/test-workspace', 'checkout', '-b', 'new-branch'], expect.objectContaining({ listeners: { stdline: expect.anything(), errline: expect.anything() }, }) @@ -89,6 +92,10 @@ describe('Git CLI', () => { }) describe('git push', () => { + beforeEach(() => { + jest.spyOn(cwd, 'getWorkspace').mockReturnValue('/test-workspace') + }) + it('should push new branch', async () => { const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0) const getInput = jest @@ -98,7 +105,15 @@ describe('Git CLI', () => { await pushCurrentBranch() expect(execMock).toHaveBeenCalledWith( 'git', - ['push', '--porcelain', '--set-upstream', 'origin', 'HEAD'], + [ + '-C', + '/test-workspace', + 'push', + '--porcelain', + '--set-upstream', + 'origin', + 'HEAD', + ], expect.objectContaining({ listeners: { stdline: expect.anything(), errline: expect.anything() }, }) @@ -113,7 +128,16 @@ describe('Git CLI', () => { expect(execMock).toHaveBeenCalled() expect(execMock).toHaveBeenCalledWith( 'git', - ['push', '--force', '--porcelain', '--set-upstream', 'origin', 'HEAD'], + [ + '-C', + '/test-workspace', + 'push', + '--force', + '--porcelain', + '--set-upstream', + 'origin', + 'HEAD', + ], expect.objectContaining({ listeners: { stdline: expect.anything(), errline: expect.anything() }, }) @@ -184,7 +208,7 @@ describe('Git CLI', () => { jest.spyOn(cwd, 'getWorkspace').mockReturnValue('/test-workspace') }) - it('should ensure file paths are within curent working directory', async () => { + it('should ensure file paths are within current working directory', async () => { const execMock = jest.spyOn(exec, 'exec').mockResolvedValue(0) await addFileChanges(['*.ts', '~/.bashrc']) @@ -269,6 +293,10 @@ describe('Git CLI', () => { 'A tests/run.test.ts', ] + beforeEach(() => { + jest.spyOn(cwd, 'getWorkspace').mockReturnValue('/test-workspace') + }) + it('should parse ouput into file changes', async () => { const execMock = jest .spyOn(exec, 'exec') diff --git a/dist/index.js b/dist/index.js index ea1aa2e..bbae0c0 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29857,15 +29857,15 @@ const base64_encoder_1 = __importDefault(__nccwpck_require__(5564)); class Blob { constructor(path) { const cwd = (0, cwd_1.getCwd)(); - const workspace = (0, cwd_1.getWorkspace)(); - core.debug('Blob.constructor() - Add workspace directory to filepath'); - const tmpPath = (0, node_path_1.join)(workspace, path); // Add GHA cwd - this.absolutePath = tmpPath.startsWith(cwd) ? tmpPath : (0, node_path_1.join)(cwd, tmpPath); + this.absolutePath = path.startsWith(cwd) ? path : (0, node_path_1.join)(cwd, path); + core.debug('Blob.constructor() - this.absolutePath: ' + + JSON.stringify(this.absolutePath)); // Remove GHA cwd this.path = path.startsWith(cwd) ? path.replace(new RegExp(cwd, 'g'), '') : path; + core.debug('Blob.constructor() - this.path: ' + JSON.stringify(this.path)); } get streamable() { if (!fs.existsSync(this.absolutePath)) { @@ -29884,8 +29884,9 @@ class Blob { chunks.push(chunk); else if (typeof chunk === 'string') chunks.push(node_buffer_1.Buffer.from(chunk)); + core.debug( // eslint-disable-next-line @typescript-eslint/restrict-template-expressions - core.debug(`Blob.load() - filepath ${this.absolutePath}, received blob: ${chunk}`); + `Blob.load() - filepath ${this.absolutePath}, received blob: ${chunk}`); }); stream.on('error', (err) => { throw new Error(`Read file failed, error: ${err.message}, path: ${this.absolutePath}`); @@ -29993,14 +29994,10 @@ function execGit(args) { const debugOutput = []; const warningOutput = []; const errorOutput = []; - const defaultArgs = []; // Handle workspace const workspace = (0, cwd_1.getWorkspace)(); - if (workspace !== '') { - defaultArgs.push('-C'); - defaultArgs.push(workspace); - core.debug('execGit() - Adding GHA parameter "workspace" to git cli args'); - } + const defaultArgs = ['-C', workspace]; + core.debug('execGit() - Adding GHA parameter "workspace" to git cli args'); core.debug('execGit() - defaultArgs: ' + JSON.stringify(defaultArgs)); core.debug('execGit() - args parameter: ' + JSON.stringify(args)); const mergedArgs = defaultArgs.concat(args); diff --git a/src/blob.ts b/src/blob.ts index 0064355..a8b62a5 100644 --- a/src/blob.ts +++ b/src/blob.ts @@ -6,7 +6,7 @@ import { Readable } from 'node:stream' import { finished } from 'node:stream/promises' import { FileAddition } from '@octokit/graphql-schema' -import { getCwd, getWorkspace } from './utils/cwd' +import { getCwd } from './utils/cwd' import Base64Encoder from './stream/base64-encoder' export class Blob { @@ -17,18 +17,19 @@ export class Blob { constructor(path: string) { const cwd = getCwd() - const workspace = getWorkspace() - - core.debug('Blob.constructor() - Add workspace directory to filepath') - const tmpPath = join(workspace, path) // Add GHA cwd - this.absolutePath = tmpPath.startsWith(cwd) ? tmpPath : join(cwd, tmpPath) + this.absolutePath = path.startsWith(cwd) ? path : join(cwd, path) + core.debug( + 'Blob.constructor() - this.absolutePath: ' + + JSON.stringify(this.absolutePath) + ) // Remove GHA cwd this.path = path.startsWith(cwd) ? path.replace(new RegExp(cwd, 'g'), '') : path + core.debug('Blob.constructor() - this.path: ' + JSON.stringify(this.path)) } get streamable(): Readable { @@ -49,8 +50,8 @@ export class Blob { if (Buffer.isBuffer(chunk)) chunks.push(chunk) else if (typeof chunk === 'string') chunks.push(Buffer.from(chunk)) - // eslint-disable-next-line @typescript-eslint/restrict-template-expressions core.debug( + // eslint-disable-next-line @typescript-eslint/restrict-template-expressions `Blob.load() - filepath ${this.absolutePath}, received blob: ${chunk}` ) }) diff --git a/src/git.ts b/src/git.ts index e822a85..4480014 100644 --- a/src/git.ts +++ b/src/git.ts @@ -13,15 +13,10 @@ async function execGit(args: string[]) { const warningOutput: string[] = [] const errorOutput: string[] = [] - const defaultArgs: string[] = [] - // Handle workspace const workspace = getWorkspace() - if (workspace !== '') { - defaultArgs.push('-C') - defaultArgs.push(workspace) - core.debug('execGit() - Adding GHA parameter "workspace" to git cli args') - } + const defaultArgs: string[] = ['-C', workspace] + core.debug('execGit() - Adding GHA parameter "workspace" to git cli args') core.debug('execGit() - defaultArgs: ' + JSON.stringify(defaultArgs)) core.debug('execGit() - args parameter: ' + JSON.stringify(args)) From 1c89da080d6a92b6c00d327405540f30de724be7 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Tue, 29 Oct 2024 18:33:12 +0100 Subject: [PATCH 16/17] Fixup --- dist/index.js | 16 ++++++++++++++-- src/blob.ts | 16 +++++++++++++--- 2 files changed, 27 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index bbae0c0..e6c1e13 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29857,14 +29857,26 @@ const base64_encoder_1 = __importDefault(__nccwpck_require__(5564)); class Blob { constructor(path) { const cwd = (0, cwd_1.getCwd)(); + const workspace = (0, cwd_1.getWorkspace)(); // Add GHA cwd - this.absolutePath = path.startsWith(cwd) ? path : (0, node_path_1.join)(cwd, path); + if (cwd.includes(workspace)) { + this.absolutePath = path.startsWith(cwd) ? path : (0, node_path_1.join)(cwd, path); + } + else if (cwd === workspace) { + this.absolutePath = path.startsWith(cwd) ? path : (0, node_path_1.join)(cwd, path); + } + else { + this.absolutePath = (0, node_path_1.join)(cwd, workspace, path); + } core.debug('Blob.constructor() - this.absolutePath: ' + JSON.stringify(this.absolutePath)); // Remove GHA cwd - this.path = path.startsWith(cwd) + const tmpPath = path.startsWith(cwd) ? path.replace(new RegExp(cwd, 'g'), '') : path; + this.path = tmpPath.startsWith(workspace) + ? tmpPath.replace(new RegExp(workspace, 'g'), '') + : tmpPath; core.debug('Blob.constructor() - this.path: ' + JSON.stringify(this.path)); } get streamable() { diff --git a/src/blob.ts b/src/blob.ts index a8b62a5..02dcbbb 100644 --- a/src/blob.ts +++ b/src/blob.ts @@ -6,7 +6,7 @@ import { Readable } from 'node:stream' import { finished } from 'node:stream/promises' import { FileAddition } from '@octokit/graphql-schema' -import { getCwd } from './utils/cwd' +import { getCwd, getWorkspace } from './utils/cwd' import Base64Encoder from './stream/base64-encoder' export class Blob { @@ -17,18 +17,28 @@ export class Blob { constructor(path: string) { const cwd = getCwd() + const workspace = getWorkspace() // Add GHA cwd - this.absolutePath = path.startsWith(cwd) ? path : join(cwd, path) + if (cwd.includes(workspace)) { + this.absolutePath = path.startsWith(cwd) ? path : join(cwd, path) + } else if (cwd === workspace) { + this.absolutePath = path.startsWith(cwd) ? path : join(cwd, path) + } else { + this.absolutePath = join(cwd, workspace, path) + } core.debug( 'Blob.constructor() - this.absolutePath: ' + JSON.stringify(this.absolutePath) ) // Remove GHA cwd - this.path = path.startsWith(cwd) + const tmpPath = path.startsWith(cwd) ? path.replace(new RegExp(cwd, 'g'), '') : path + this.path = tmpPath.startsWith(workspace) + ? tmpPath.replace(new RegExp(workspace, 'g'), '') + : tmpPath core.debug('Blob.constructor() - this.path: ' + JSON.stringify(this.path)) } From 3e739727442065d4074b56debde56480b0069b25 Mon Sep 17 00:00:00 2001 From: Xavier Krantz Date: Tue, 29 Oct 2024 18:37:29 +0100 Subject: [PATCH 17/17] Fixup --- dist/index.js | 19 +++++++------------ src/blob.ts | 23 +++++++++-------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/dist/index.js b/dist/index.js index e6c1e13..ca94b1b 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29858,25 +29858,20 @@ class Blob { constructor(path) { const cwd = (0, cwd_1.getCwd)(); const workspace = (0, cwd_1.getWorkspace)(); - // Add GHA cwd - if (cwd.includes(workspace)) { - this.absolutePath = path.startsWith(cwd) ? path : (0, node_path_1.join)(cwd, path); - } - else if (cwd === workspace) { + if (cwd === workspace || cwd.includes(workspace)) { this.absolutePath = path.startsWith(cwd) ? path : (0, node_path_1.join)(cwd, path); + this.path = path.startsWith(cwd) + ? path.replace(new RegExp(cwd, 'g'), '') + : path; } else { this.absolutePath = (0, node_path_1.join)(cwd, workspace, path); + this.path = path.startsWith(workspace) + ? path.replace(new RegExp(workspace, 'g'), '') + : path; } core.debug('Blob.constructor() - this.absolutePath: ' + JSON.stringify(this.absolutePath)); - // Remove GHA cwd - const tmpPath = path.startsWith(cwd) - ? path.replace(new RegExp(cwd, 'g'), '') - : path; - this.path = tmpPath.startsWith(workspace) - ? tmpPath.replace(new RegExp(workspace, 'g'), '') - : tmpPath; core.debug('Blob.constructor() - this.path: ' + JSON.stringify(this.path)); } get streamable() { diff --git a/src/blob.ts b/src/blob.ts index 02dcbbb..3d460b9 100644 --- a/src/blob.ts +++ b/src/blob.ts @@ -10,35 +10,30 @@ import { getCwd, getWorkspace } from './utils/cwd' import Base64Encoder from './stream/base64-encoder' export class Blob { - // Returned as a property of FileChange object - path: string // Used for content access absolutePath: string + // Returned as a property of FileChange object + path: string constructor(path: string) { const cwd = getCwd() const workspace = getWorkspace() - // Add GHA cwd - if (cwd.includes(workspace)) { - this.absolutePath = path.startsWith(cwd) ? path : join(cwd, path) - } else if (cwd === workspace) { + if (cwd === workspace || cwd.includes(workspace)) { this.absolutePath = path.startsWith(cwd) ? path : join(cwd, path) + this.path = path.startsWith(cwd) + ? path.replace(new RegExp(cwd, 'g'), '') + : path } else { this.absolutePath = join(cwd, workspace, path) + this.path = path.startsWith(workspace) + ? path.replace(new RegExp(workspace, 'g'), '') + : path } core.debug( 'Blob.constructor() - this.absolutePath: ' + JSON.stringify(this.absolutePath) ) - - // Remove GHA cwd - const tmpPath = path.startsWith(cwd) - ? path.replace(new RegExp(cwd, 'g'), '') - : path - this.path = tmpPath.startsWith(workspace) - ? tmpPath.replace(new RegExp(workspace, 'g'), '') - : tmpPath core.debug('Blob.constructor() - this.path: ' + JSON.stringify(this.path)) }