Skip to content

Commit

Permalink
Allow passing gcloudignore path and fix root computation (#376)
Browse files Browse the repository at this point in the history
- Fixes
#336
- Fixes
#357

---------

Co-authored-by: Eddie Lin <eddielin0926@gmail.com>
  • Loading branch information
sethvargo and eddielin0926 authored Aug 23, 2024
1 parent 94471cf commit aa65d0a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
10 changes: 10 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ inputs:
required: false
default: '100'

gcloudignore_path:
description: |-
Path to a gcloudignore file within the repository.
```yaml
gcloudignore_path: '.gcloudignore.dev'
```
required: false
default: '.gcloudignore'

process_gcloudignore:
description: |-
Process a `.gcloudignore` file present in the top-level of the repository.
Expand Down
11 changes: 6 additions & 5 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ export async function run(): Promise<void> {
const predefinedAcl =
predefinedAclInput === '' ? undefined : (predefinedAclInput as PredefinedAcl);
const headersInput = core.getInput('headers');
const gcloudIgnorePath = core.getInput('gcloudignore_path') || '.gcloudignore';
const processGcloudIgnore = parseBoolean(core.getInput('process_gcloudignore'));
const metadata = headersInput === '' ? {} : parseHeadersInput(headersInput);

Expand All @@ -89,18 +90,18 @@ export async function run(): Promise<void> {
// - Format all files to be posix relative to input.path
// - Filter out items that match
if (processGcloudIgnore) {
core.debug(`Processing gcloudignore`);
core.debug(`Processing gcloudignore at ${gcloudIgnorePath}`);

const ignores = ignore();

// Look for a .gcloudignore in the repository root.
const githubWorkspace = process.env.GITHUB_WORKSPACE;
if (githubWorkspace) {
const gcloudIgnorePath = path.join(githubWorkspace, '.gcloudignore');
const ignoreList = await parseGcloudIgnore(gcloudIgnorePath);
const gcloudIgnorePathAbs = path.join(githubWorkspace, gcloudIgnorePath);
const ignoreList = await parseGcloudIgnore(gcloudIgnorePathAbs);

if (ignoreList && ignoreList.length) {
core.debug(`Using .gcloudignore at: ${gcloudIgnorePath}`);
core.debug(`Using .gcloudignore at: ${gcloudIgnorePathAbs}`);
core.debug(`Parsed ignore list: ${JSON.stringify(ignoreList)}`);

ignores.add(ignoreList);
Expand All @@ -113,7 +114,7 @@ export async function run(): Promise<void> {
}

for (let i = 0; i < files.length; i++) {
const name = files[i];
const name = path.join(root, files[i]);
try {
if (ignores.ignores(name)) {
core.debug(`Ignoring ${name} because of ignore file`);
Expand Down
2 changes: 2 additions & 0 deletions tests/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { forceRemove, randomFilepath, writeSecureFile } from '@google-github-act

import { Client } from '../src/client';
import { Bucket, UploadOptions } from '@google-cloud/storage';
import { GoogleAuth } from 'google-auth-library';

import { mockUpload } from './helpers.test';

Expand Down Expand Up @@ -231,6 +232,7 @@ describe('Client', { concurrency: true }, async () => {
test('#upload', async (suite) => {
await suite.test('calls uploadFile', async (t) => {
const uploadMock = t.mock.method(Bucket.prototype, 'upload', mockUpload);
t.mock.method(GoogleAuth.prototype, 'getClient', () => {});

// Do the upload
const client = await Client.build();
Expand Down
40 changes: 39 additions & 1 deletion tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { promises as fs } from 'fs';
import * as core from '@actions/core';
import { clearEnv, forceRemove, setInputs } from '@google-github-actions/actions-utils';
import { Bucket, UploadOptions } from '@google-cloud/storage';
import { GoogleAuth } from 'google-auth-library';

import { mockUpload } from './helpers.test';

Expand All @@ -47,6 +48,9 @@ test('#run', { concurrency: true }, async (suite) => {
suite.mock.method(core, 'endGroup', () => {});
suite.mock.method(core, 'addPath', () => {});
suite.mock.method(core, 'exportVariable', () => {});

// We do not care about authentication in the unit tests
suite.mock.method(GoogleAuth.prototype, 'getClient', () => {});
});

suite.beforeEach(async () => {
Expand Down Expand Up @@ -194,7 +198,41 @@ test('#run', { concurrency: true }, async (suite) => {
});

// Add gcloudignore
await fs.writeFile(path.join(githubWorkspace, '.gcloudignore'), '*.txt');
await fs.writeFile(path.join(githubWorkspace, '.gcloudignore'), 'testdata/**/*.txt');

await run();

// Check call sites
const uploadedFiles = uploadMock.mock.calls.map((call) => call?.arguments?.at(0));
assert.deepStrictEqual(uploadedFiles, [
path.join(githubWorkspace, 'testdata', 'test.css'),
path.join(githubWorkspace, 'testdata', 'test.js'),
path.join(githubWorkspace, 'testdata', 'test.json'),
path.join(githubWorkspace, 'testdata', 'testfile'),
]);

// Check arguments
const call = uploadMock.mock.calls.at(0)?.arguments?.at(1) as UploadOptions;
assert.deepStrictEqual(call?.destination, 'sub/path/testdata/test.css');
});

await suite.test('processes a custom gcloudignore path', async (t) => {
const uploadMock = t.mock.method(Bucket.prototype, 'upload', mockUpload);
const gcloudIgnorePath = path.join(githubWorkspace, '.gcloudignore-other');

setInputs({
path: './testdata',
destination: 'my-bucket/sub/path',
gzip: 'true',
resumable: 'true',
parent: 'true',
concurrency: '10',
process_gcloudignore: 'true',
gcloudignore_path: '.gcloudignore-other',
});

// Add gcloudignore
await fs.writeFile(gcloudIgnorePath, 'testdata/**/*.txt');

await run();

Expand Down

0 comments on commit aa65d0a

Please sign in to comment.