diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 0000000..2758f90 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,20 @@ +name: Node.js CI + +on: + push: + branches: [ master ] + pull_request: + branches: [ master ] + +jobs: + build: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Use Node.js + uses: actions/setup-node@v2 + with: + node-version: '12.x' + cache: 'npm' + - run: npm install --production=false + - run: npm test diff --git a/README.md b/README.md index 71b58f0..794ba9e 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,8 @@ # databricks-repo-sync-action -Repo created to store the action in charge to sync Github Repos with Databricks + +[![Node.js CI](https://github.com/neofinancial/databricks-repo-sync-action/actions/workflows/main.yml/badge.svg)](https://github.com/neofinancial/databricks-repo-sync-action/actions/workflows/main.yml) + +Github action that sync Github with Databricks repos. ## Inputs diff --git a/dist/index.js b/dist/index.js index 603abb7..816de8f 100644 --- a/dist/index.js +++ b/dist/index.js @@ -2139,11 +2139,11 @@ exports.FetchError = FetchError; const fetch = __nccwpck_require__(467); class ReposAPI { - #accessToken; - #baseUrl; + accessToken; + baseUrl; constructor(instancePrefix, accessToken) { - this.#baseUrl = `https://${instancePrefix}.cloud.databricks.com/api/2.0/repos`; - this.#accessToken = accessToken; + this.baseUrl = `https://${instancePrefix}.cloud.databricks.com/api/2.0/repos`; + this.accessToken = accessToken; } /** @@ -2152,18 +2152,18 @@ class ReposAPI { * @return {[String]} [Databricks repo ID] */ async getDatabricksRepoId(repoPath) { - const getReposRequest = new fetch.Request(this.#baseUrl.concat(`?path_prefix=${repoPath}`), { + const getReposRequest = new fetch.Request(this.baseUrl.concat(`?path_prefix=${repoPath}`), { method: "GET", - headers: this.#buildHeaders(), + headers: this.buildHeaders(), }); const response = await fetch(getReposRequest); try { - const payload = await this.#handleResponse(response); - return this.#parseRepoIdFromResponse(payload['repos'], repoPath); + const payload = await this.handleResponse(response); + return this.parseRepoIdFromResponse(payload.repos || [], repoPath); } catch (err) { - throw `Failed to get repo id: ${err}`; + throw new Error(`Failed to get repo id: ${err}`); } } @@ -2179,40 +2179,40 @@ class ReposAPI { throw new Error(`Must supply a branch or tag! Got branch ${branch}, tag ${tag}`); }; - const syncRequest = new fetch.Request(this.#baseUrl.concat(`/${repoId}`), { + const syncRequest = new fetch.Request(this.baseUrl.concat(`/${repoId}`), { method: "PATCH", - body: this.#buildSyncBody(branch, tag), - headers: this.#buildHeaders(), + body: this.buildSyncBody(branch, tag), + headers: this.buildHeaders(), }); const response = await fetch(syncRequest); try { - const payload = await this.#handleResponse(response); + const payload = await this.handleResponse(response); return payload["head_commit_id"].substring(0, 7); } catch (err) { - throw `Failed to sync databricks repo: ${err}`; + throw new Error(`Failed to sync databricks repo: ${err}`); } } -// TODO - use response.data message to enrich error - async #handleResponse(response) { + async handleResponse(response) { + var data = await response.json(); if (!response.ok) { - throw `${response.status} ${response.statusText}`; + throw `${response.status} ${data.message}`; } - return response.json(); + return data; } - #parseRepoIdFromResponse(repos, repoPath) { + parseRepoIdFromResponse(repos, repoPath) { for (var x = 0; x < repos.length; x++) { if (repos[x]["path"] === repoPath) { - return repos[x]["id"] + return repos[x]["id"]; } } - throw new Error(`Repo path ${repoPath} not found`) + throw `Repo path ${repoPath} not found` } - #buildSyncBody(branch, tag) { + buildSyncBody(branch, tag) { var data = { "branch": branch } if (tag != null) { data = { "tag": tag } @@ -2220,10 +2220,10 @@ class ReposAPI { return JSON.stringify(data) } - #buildHeaders() { + buildHeaders() { const headers = new fetch.Headers(); - headers.append("Authorization", `Bearer ${this.#accessToken}`) + headers.append("Authorization", `Bearer ${this.accessToken}`) headers.append("Accept", "application/json") headers.append("Content-Type", "application/json") @@ -2402,9 +2402,6 @@ async function run() { repos = new ReposAPI(account, accessToken); - core.info("::warning file=app.js,line=1,col=5::Missing semicolon") - throw "toma" - if (repoId == '') { if (repoPath == '') { core.setFailed("Must supply a repo-id or repo-path!"); @@ -2416,13 +2413,13 @@ async function run() { core.info("repo-id found. Using it to sync repo"); } - const branchOrTag = branch ?? tag; + const branchOrTag = branch != null ? branch : tag; core.info(`Syncing Databricks repo with ${branchOrTag}`); const commit = await repos.syncDatabricksRepo(repoId, branch, tag); core.info(`Repo is now synced at commit ${commit}`); } catch (error) { - core.setFailed(error); + core.setFailed(error.message); } } diff --git a/src/databricks.js b/src/databricks.js index 01338a2..b52a139 100644 --- a/src/databricks.js +++ b/src/databricks.js @@ -1,10 +1,10 @@ const fetch = require("node-fetch"); class ReposAPI { - #accessToken; - #baseUrl; + accessToken; + baseUrl; constructor(instancePrefix, accessToken) { - this.#baseUrl = `https://${instancePrefix}.cloud.databricks.com/api/2.0/repos`; - this.#accessToken = accessToken; + this.baseUrl = `https://${instancePrefix}.cloud.databricks.com/api/2.0/repos`; + this.accessToken = accessToken; } /** @@ -13,16 +13,16 @@ class ReposAPI { * @return {[String]} [Databricks repo ID] */ async getDatabricksRepoId(repoPath) { - const getReposRequest = new fetch.Request(this.#baseUrl.concat(`?path_prefix=${repoPath}`), { + const getReposRequest = new fetch.Request(this.baseUrl.concat(`?path_prefix=${repoPath}`), { method: "GET", - headers: this.#buildHeaders(), + headers: this.buildHeaders(), }); const response = await fetch(getReposRequest); try { - const payload = await this.#handleResponse(response); - return this.#parseRepoIdFromResponse(payload.repos || [], repoPath); + const payload = await this.handleResponse(response); + return this.parseRepoIdFromResponse(payload.repos || [], repoPath); } catch (err) { throw new Error(`Failed to get repo id: ${err}`); } @@ -40,23 +40,23 @@ class ReposAPI { throw new Error(`Must supply a branch or tag! Got branch ${branch}, tag ${tag}`); }; - const syncRequest = new fetch.Request(this.#baseUrl.concat(`/${repoId}`), { + const syncRequest = new fetch.Request(this.baseUrl.concat(`/${repoId}`), { method: "PATCH", - body: this.#buildSyncBody(branch, tag), - headers: this.#buildHeaders(), + body: this.buildSyncBody(branch, tag), + headers: this.buildHeaders(), }); const response = await fetch(syncRequest); try { - const payload = await this.#handleResponse(response); + const payload = await this.handleResponse(response); return payload["head_commit_id"].substring(0, 7); } catch (err) { throw new Error(`Failed to sync databricks repo: ${err}`); } } - async #handleResponse(response) { + async handleResponse(response) { var data = await response.json(); if (!response.ok) { throw `${response.status} ${data.message}`; @@ -64,7 +64,7 @@ class ReposAPI { return data; } - #parseRepoIdFromResponse(repos, repoPath) { + parseRepoIdFromResponse(repos, repoPath) { for (var x = 0; x < repos.length; x++) { if (repos[x]["path"] === repoPath) { return repos[x]["id"]; @@ -73,7 +73,7 @@ class ReposAPI { throw `Repo path ${repoPath} not found` } - #buildSyncBody(branch, tag) { + buildSyncBody(branch, tag) { var data = { "branch": branch } if (tag != null) { data = { "tag": tag } @@ -81,10 +81,10 @@ class ReposAPI { return JSON.stringify(data) } - #buildHeaders() { + buildHeaders() { const headers = new fetch.Headers(); - headers.append("Authorization", `Bearer ${this.#accessToken}`) + headers.append("Authorization", `Bearer ${this.accessToken}`) headers.append("Accept", "application/json") headers.append("Content-Type", "application/json") diff --git a/src/index.js b/src/index.js index b984dc5..927d80b 100644 --- a/src/index.js +++ b/src/index.js @@ -24,7 +24,7 @@ async function run() { core.info("repo-id found. Using it to sync repo"); } - const branchOrTag = branch ?? tag; + const branchOrTag = branch != null ? branch : tag; core.info(`Syncing Databricks repo with ${branchOrTag}`); const commit = await repos.syncDatabricksRepo(repoId, branch, tag);