Skip to content

Commit

Permalink
Merge pull request #5 from datatheorem/support-file-path-glob
Browse files Browse the repository at this point in the history
Support file path glob pattern
  • Loading branch information
marc-tranzer committed Oct 20, 2022
2 parents b53b274 + 680f494 commit dd941e0
Show file tree
Hide file tree
Showing 127 changed files with 8,404 additions and 147 deletions.
12 changes: 11 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ Create an encrypted variable named `DT_UPLOAD_API_KEY` in your Github repository

For more information, see [Github Encrypted secrets](https://docs.github.com/en/actions/reference/encrypted-secrets)

## Set the path to the binary files to upload
Configure the Action by indicating path to the file that will be uploaded in the `UPLOAD_BINARY_PATH` input.

You can use a glob pattern to indicate variable parts of the build's file name (for example, if the app's version number or build date is in the file name).
Examples of glob patterns:
- `app-*.apk` : search for any apk starting with `app-` in workspace root directory
- `**/app-*.ipa` : search for any ipa starting with `app-` in any subdirectory of the workspace
- `{,**/}app-debug*.*` : search for any file containing `app-debug` in root the directory or in any subdirectory of the workspace
If multiple files match the provided pattern all matching files will be uploaded.

## Sample usage

```yaml
Expand All @@ -36,7 +46,7 @@ jobs:
- name: Build debug APK
run: bash ./gradlew assembleDebug
- name: Upload to Data Theorem
uses: datatheorem/datatheorem-mobile-secure-action@v2.0.1
uses: datatheorem/datatheorem-mobile-secure-action@v2.1.0
with:
UPLOAD_BINARY_PATH: "./app/build/outputs/apk/debug/app-debug.apk"
DT_UPLOAD_API_KEY: ${{ secrets.DT_UPLOAD_API_KEY }}
Expand Down
11 changes: 10 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,16 @@ inputs:
description: 'Data Theorem upload API key'
required: true
UPLOAD_BINARY_PATH:
description: 'Path to the app to upload'
description: >
Path to the app to upload.
You can use a glob pattern to indicate variable parts of the build's file name (for example, if the app's version number or build date is in the file name).
Examples of glob patterns:
- `app-*.apk` : search for any apk starting with `app-` in workspace root directory
- `**/app-*.ipa` : search for any ipa starting with `app-` in any subdirectory of the workspace
- `{,**/}app-debug*.*` : search for any file containing `app-debug` in root the directory or in any subdirectory of the workspace
If multiple files match the provided pattern all matching files will be uploaded.
required: true
runs:
using: 'node16'
Expand Down
119 changes: 71 additions & 48 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
};
Object.defineProperty(exports, "__esModule", { value: true });
const core = require("@actions/core");
const fetch = require('node-fetch');
const fetch = require("node-fetch");
const glob = require("glob");
const FormData = require("form-data");
const fs = require("fs");
function run() {
Expand All @@ -19,58 +20,80 @@ function run() {
// Get inputs
const dt_upload_api_key = core.getInput("DT_UPLOAD_API_KEY");
const input_binary_path = core.getInput("UPLOAD_BINARY_PATH");
// Mask the API key
core.setSecret(dt_upload_api_key);
if (!fs.existsSync(input_binary_path)) {
throw new Error("Input file does not exist at " + input_binary_path);
// Check that the inputs are set
if (!dt_upload_api_key) {
throw new Error("DT_UPLOAD_API_KEY must be set!");
}
// retry upload 3 times
for (let loop_idx = 0; loop_idx < 3; loop_idx++) {
// Send the auth request to get the upload URL
const auth_response = yield fetch("https://api.securetheorem.com/uploadapi/v1/upload_init", {
method: 'POST',
headers: {
Authorization: "APIKey " + dt_upload_api_key,
Accept: "application/json",
"Content-Type": "application/json",
},
});
let auth_json;
try {
auth_json = yield auth_response.json();
}
catch (err) {
core.setFailed(err);
}
if (auth_response.status !== 200) {
// handles auth failure
core.setFailed(auth_json);
break;
}
const form = new FormData();
form.append('file', fs.createReadStream(input_binary_path));
// Send the scan request with file
const response = yield fetch(auth_json.upload_url, {
method: 'POST',
body: form,
});
let jsonformat;
try {
jsonformat = yield response.json();
}
catch (err) {
core.setFailed(err);
}
// Check the response
console.log(jsonformat);
if (response.status === 200) {
core.setOutput('response', jsonformat);
;
break;
if (!input_binary_path) {
throw new Error("UPLOAD_BINARY_PATH must be set!");
}
const files = glob.sync(input_binary_path);
if (!files.length) {
throw new Error("Did not find any files that match path:" + input_binary_path);
}
if (files.length > 3) {
throw new Error("Too many files match the provided glob pattern, please write a more restrictive pattern");
}
// Upload all the files that matched the file path
let output = [];
for (const file_path of files) {
if (!fs.existsSync(file_path)) {
throw new Error("Could not find file:" + file_path);
}
else if (loop_idx == 2) {
core.setFailed(jsonformat);
// retry upload 3 times
for (let loop_idx = 0; loop_idx < 3; loop_idx++) {
// Send the auth request to get the upload URL
const auth_response = yield fetch("https://api.securetheorem.com/uploadapi/v1/upload_init", {
method: "POST",
headers: {
Authorization: "APIKey " + dt_upload_api_key,
Accept: "application/json",
"Content-Type": "application/json",
},
});
let auth_json;
try {
auth_json = yield auth_response.json();
}
catch (err) {
core.setFailed(err);
}
if (auth_response.status !== 200) {
// handles auth failure
core.setFailed(auth_json);
break;
}
const form = new FormData();
form.append("file", fs.createReadStream(file_path));
// Send the scan request with file
console.log("Starting upload of:" + file_path);
const response = yield fetch(auth_json.upload_url, {
method: "POST",
body: form,
});
console.log("Finished upload of:" + file_path);
let jsonformat;
try {
jsonformat = yield response.json();
}
catch (err) {
core.setFailed(err);
}
output.push(jsonformat);
// Check the response
if (response.status === 200) {
console.log(jsonformat);
break;
}
else if (loop_idx == 2) {
core.setFailed(jsonformat);
}
}
}
core.setOutput("responses", output);
core.setOutput("response", output[0]); // keep the `response` output as the response of the first file upload to maintain compatibility
}
catch (err) {
core.setFailed(err.message);
Expand Down
136 changes: 82 additions & 54 deletions main.ts
Original file line number Diff line number Diff line change
@@ -1,79 +1,107 @@
import core = require('@actions/core');
const fetch = require('node-fetch');
import FormData = require('form-data');
import fs = require('fs');

import core = require("@actions/core");
const fetch = require("node-fetch");
const glob = require("glob");
import FormData = require("form-data");
import fs = require("fs");

async function run() {
try {
// Get inputs
const dt_upload_api_key: string = core.getInput("DT_UPLOAD_API_KEY");
const input_binary_path: string = core.getInput("UPLOAD_BINARY_PATH");
core.setSecret(dt_upload_api_key)

if (!fs.existsSync(input_binary_path)) {
throw new Error("Input file does not exist at :" + input_binary_path);
// Mask the API key
core.setSecret(dt_upload_api_key);
// Check that the inputs are set
if (!dt_upload_api_key){
throw new Error(
"DT_UPLOAD_API_KEY must be set!"
);
}

// retry upload 3 times
for (let loop_idx = 0; loop_idx < 3; loop_idx++) {

// Send the auth request to get the upload URL
const auth_response = await fetch(
"https://api.securetheorem.com/uploadapi/v1/upload_init",
{
method: 'POST',
headers: {
Authorization: "APIKey " + dt_upload_api_key,
Accept: "application/json",
"Content-Type": "application/json",
},
}
if (!input_binary_path){
throw new Error(
"UPLOAD_BINARY_PATH must be set!"
);
}

let auth_json
try {
auth_json = await auth_response.json()
} catch (err) {core.setFailed(err);}
const files = glob.sync(input_binary_path);
if (!files.length) {
throw new Error(
"Did not find any files that match path:" + input_binary_path
);
}
if (files.length > 3) {
throw new Error(
"Too many files match the provided glob pattern, please write a more restrictive pattern"
);
}

if (auth_response.status !== 200) {
// handles auth failure
core.setFailed(auth_json);
break;
// Upload all the files that matched the file path
let output: Array<any> = []
for (const file_path of files) {
if (!fs.existsSync(file_path)) {
throw new Error("Could not find file:" + file_path);
}
// retry upload 3 times
for (let loop_idx = 0; loop_idx < 3; loop_idx++) {
// Send the auth request to get the upload URL
const auth_response = await fetch(
"https://api.securetheorem.com/uploadapi/v1/upload_init",
{
method: "POST",
headers: {
Authorization: "APIKey " + dt_upload_api_key,
Accept: "application/json",
"Content-Type": "application/json",
},
}
);

const form = new FormData();
form.append('file', fs.createReadStream(input_binary_path));
let auth_json;
try {
auth_json = await auth_response.json();
} catch (err) {
core.setFailed(err);
}

// Send the scan request with file
const response = await fetch(
auth_json.upload_url,
{
method: 'POST',
body: form,
if (auth_response.status !== 200) {
// handles auth failure
core.setFailed(auth_json);
break;
}
);

let jsonformat
const form = new FormData();
form.append("file", fs.createReadStream(file_path));

// Send the scan request with file
console.log("Starting upload of:" + file_path);
const response = await fetch(auth_json.upload_url, {
method: "POST",
body: form,
});
console.log("Finished upload of:" + file_path);
let jsonformat;

try{
jsonformat = await response.json();
} catch (err) {core.setFailed(err)}
try {
jsonformat = await response.json();
} catch (err) {
core.setFailed(err);
}
output.push(jsonformat)

// Check the response
console.log(jsonformat);
if (response.status === 200) {
core.setOutput('response', jsonformat);;
break;
} else if (loop_idx == 2) {
core.setFailed(jsonformat);
// Check the response
if (response.status === 200) {
console.log(jsonformat);
break;
} else if (loop_idx == 2) {
core.setFailed(jsonformat);
}
}
}

core.setOutput("responses", output);
core.setOutput("response", output[0]); // keep the `response` output as the response of the first file upload to maintain compatibility
} catch (err) {
core.setFailed(err.message);
}
}

run();

1 change: 1 addition & 0 deletions node_modules/.bin/uuid

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

25 changes: 24 additions & 1 deletion node_modules/@actions/core/README.md

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

4 changes: 4 additions & 0 deletions node_modules/@actions/core/lib/core.d.ts

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

0 comments on commit dd941e0

Please sign in to comment.