Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Download and extract go-sqlcmd before main action #108

Merged
merged 6 commits into from
Aug 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/workflows/pr-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ jobs:
tenant-id: '${{ secrets.AAD_TENANT_ID }}'
sql-file: ./__testdata__/testsql.sql

# Test that go-sqlcmd has been added to runner, this step will be removed in future PR when go-sqlcmd is hooked up
- name: Test go-sqlcmd is setup
run: sqlcmd -?

- name: Set database name for cleanup
if: always()
run: sed 's/$(DbName)/${{ env.TEST_DB }}/' ./__testdata__/cleanup.sql > ${{ runner.temp }}/cleanup.sql
Expand Down
33 changes: 33 additions & 0 deletions __tests__/Setup.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import Setup from "../src/Setup";

jest.mock('@actions/core');

describe('Setup.ts tests', () => {
afterEach(() => {
jest.restoreAllMocks();
})

it('sets up sqlcmd correctly', async() => {
const cacheLookupSpy = jest.spyOn(tc, 'find').mockReturnValue('');
const downloadToolSpy = jest.spyOn(tc, 'downloadTool').mockResolvedValue('');
const extractTarSpy = jest.spyOn(tc, 'extractTar').mockResolvedValue('');
const extractZipSpy = jest.spyOn(tc, 'extractZip').mockResolvedValue('');
const addPathSpy = jest.spyOn(core, 'addPath');
const cacheDirSpy = jest.spyOn(tc, 'cacheDir').mockResolvedValue('');

await Setup.setupSqlcmd();

expect(cacheLookupSpy).toHaveBeenCalled();
expect(downloadToolSpy).toHaveBeenCalled();
if (process.platform === 'win32') {
expect(extractZipSpy).toHaveBeenCalled();
}
else if (process.platform === 'linux') {
expect(extractTarSpy).toHaveBeenCalled();
}
expect(addPathSpy).toHaveBeenCalled();
expect(cacheDirSpy).toHaveBeenCalled();
});
})
4 changes: 3 additions & 1 deletion __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as core from "@actions/core";
import * as tc from "@actions/tool-cache";
import { AuthorizerFactory } from 'azure-actions-webclient/AuthorizerFactory';

import run from "../src/main";
Expand All @@ -12,6 +13,7 @@ jest.mock('azure-actions-webclient/AuthorizerFactory');
jest.mock('../src/AzureSqlAction');
jest.mock('../src/FirewallManager');
jest.mock('../src/AzureSqlResourceManager');
jest.mock('../src/Setup');

describe('main.ts tests', () => {
afterEach(() => {
Expand Down Expand Up @@ -43,7 +45,7 @@ describe('main.ts tests', () => {
expect(AzureSqlAction).toHaveBeenCalled();
expect(detectIPAddressSpy).toHaveBeenCalled();
expect(getAuthorizerSpy).not.toHaveBeenCalled();
expect(getInputSpy).toHaveBeenCalledTimes(9);
expect(getInputSpy).toHaveBeenCalledTimes(10);
expect(resolveFilePathSpy).toHaveBeenCalled();
expect(addFirewallRuleSpy).not.toHaveBeenCalled();
expect(actionExecuteSpy).toHaveBeenCalled();
Expand Down
2 changes: 1 addition & 1 deletion lib/main.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions lib/main.js.LICENSE.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ PERFORMANCE OF THIS SOFTWARE.

/*! @azure/msal-common v6.3.0 2022-05-02 */

/*! safe-buffer. MIT License. Feross Aboukhadijeh <https://feross.org/opensource> */

/**
* @copyright (c) 2016, Philipp Thürwächter & Pattrick Hüper
* @copyright (c) 2007-present, Stephen Colebourne & Michael Nascimento Santos
Expand Down
115 changes: 100 additions & 15 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
"dependencies": {
"@actions/core": "^1.2.6",
"@actions/exec": "^1.0.1",
"@actions/tool-cache": "^2.0.1",
"azure-actions-webclient": "^1.0.3",
"es-aggregate-error": "^1.0.8",
"glob": "^7.1.4",
Expand Down
46 changes: 46 additions & 0 deletions src/Setup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// This file is run before main.js to setup the tools that the action depends on
// https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runspre

import * as core from '@actions/core';
import * as tc from '@actions/tool-cache';

export const sqlcmdToolName = 'go-sqlcmd';
export const sqlcmdVersion = '0.8.1';
zijchen marked this conversation as resolved.
Show resolved Hide resolved

export default class Setup {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: extra line

/**
* Ensures go-sqlcmd is in the runner's tool cache and PATH environment variable.
*/
public static async setupSqlcmd(): Promise<void> {
// Get sqlcmd from tool cache; if not found, download it and add to tool cache
let sqlcmdPath = tc.find(sqlcmdToolName, sqlcmdVersion);
if (!sqlcmdPath) {
const extractedPath = await this.downloadAndExtractSqlcmd();
sqlcmdPath = await tc.cacheDir(extractedPath, sqlcmdToolName, sqlcmdVersion);
}

// Add sqlcmd to PATH
core.addPath(sqlcmdPath);
zijchen marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* Downloads go-sqlcmd release from GitHub and extracts from the compressed file.
* @returns The path to the extracted file.
*/
private static async downloadAndExtractSqlcmd(): Promise<string> {
let downloadPath: string;
switch (process.platform) {
case 'linux':
downloadPath = await tc.downloadTool(`https://github.com/microsoft/go-sqlcmd/releases/download/v${sqlcmdVersion}/sqlcmd-v${sqlcmdVersion}-linux-x64.tar.bz2`);
return await tc.extractTar(downloadPath, undefined, 'xj');

case 'win32':
zijchen marked this conversation as resolved.
Show resolved Hide resolved
downloadPath = await tc.downloadTool(`https://github.com/microsoft/go-sqlcmd/releases/download/v${sqlcmdVersion}/sqlcmd-v${sqlcmdVersion}-windows-x64.zip`);
return await tc.extractZip(downloadPath);

default:
zijchen marked this conversation as resolved.
Show resolved Hide resolved
throw new Error(`Runner OS is not supported: ${process.platform}`);
}
}
}
3 changes: 3 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@ import AzureSqlActionHelper from "./AzureSqlActionHelper";
import SqlConnectionConfig from "./SqlConnectionConfig";
import SqlUtils from "./SqlUtils";
import Constants from "./Constants";
import Setup from "./Setup";

const userAgentPrefix = !!process.env.AZURE_HTTP_USER_AGENT ? `${process.env.AZURE_HTTP_USER_AGENT}` : "";

export default async function run() {
await Setup.setupSqlcmd();

let firewallManager;
try {
setUserAgentVariable();
Expand Down