Skip to content

Commit

Permalink
Ch59228/remove non supported features in actions (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pedro Pichatelli authored Sep 1, 2021
1 parent a5ac722 commit 45ab294
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 48 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
55 changes: 26 additions & 29 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand All @@ -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}`);
}
}

Expand All @@ -2179,51 +2179,51 @@ 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 }
}
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")

Expand Down Expand Up @@ -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!");
Expand All @@ -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);
}
}

Expand Down
34 changes: 17 additions & 17 deletions src/databricks.js
Original file line number Diff line number Diff line change
@@ -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;
}

/**
Expand All @@ -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}`);
}
Expand All @@ -40,31 +40,31 @@ 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}`;
}
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"];
Expand All @@ -73,18 +73,18 @@ class ReposAPI {
throw `Repo path ${repoPath} not found`
}

#buildSyncBody(branch, tag) {
buildSyncBody(branch, tag) {
var data = { "branch": branch }
if (tag != null) {
data = { "tag": tag }
}
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")

Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit 45ab294

Please sign in to comment.