Skip to content
This repository has been archived by the owner on Nov 8, 2022. It is now read-only.

Commit

Permalink
Merge pull request #33 from hyahatiph-labs/release/v0.1.7
Browse files Browse the repository at this point in the history
Release/v0.1.7
  • Loading branch information
Nigel Christian authored Apr 7, 2021
2 parents c9471f4 + 88b260d commit bc1b884
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 38 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gitpayd",
"version": "0.1.6",
"version": "0.1.7",
"description": "Github Workflows + BTC / LND, gitpayd watches your repo for new commits and sends payments to contributors",
"scripts": {
"clean": "rm -rf dist/",
Expand Down
23 changes: 14 additions & 9 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import * as yargs from "yargs";
import os from "os";
import { LogLevel } from "../util/logging";

// api key size
export const API_KEY_SIZE: number = 32;
Expand Down Expand Up @@ -28,7 +27,7 @@ export enum GitpaydConfig {
DEFAULT_PAYMENT_THRESHOLD = 250000,
HTTP_OK = 200,
UNAUTHORIZED = 403,
SERVER_FAILURE = 500
SERVER_FAILURE = 500,
}

/**
Expand All @@ -37,6 +36,9 @@ export enum GitpaydConfig {
export const SSL_SCHEMA: any = {
properties: {
sslpassphrase: {
message:
"Enter SSL passphrase or press Enter for DEV mode " +
"\n\tHint: for DEV mode export GITPAYD_ENV=DEV\n",
hidden: true,
},
},
Expand Down Expand Up @@ -143,19 +145,21 @@ const DEFAULT_MAX_PAYMENT: number = 100000;
const DEFAULT_PAYMENT_THRESHOLD: number = 250000;
export const MAX_PAYMENT: number | string =
CUSTOM_MAX_PAYMENT === undefined
? DEFAULT_MAX_PAYMENT
: parseInt(CUSTOM_MAX_PAYMENT, 10);
? DEFAULT_MAX_PAYMENT
: parseInt(CUSTOM_MAX_PAYMENT, 10);
export const PAYMENT_THRESHOLD: number =
CUSTOM_PAYMENT_THRESHOLD === undefined
? DEFAULT_PAYMENT_THRESHOLD
: parseInt(CUSTOM_PAYMENT_THRESHOLD, 10);

// global log level
const LOG_LEVEL_ARG: string = ARGS["log-level"];
const IS_MULTI_LOG_LEVEL: boolean = LOG_LEVEL_ARG !== undefined
&& LOG_LEVEL_ARG.length > 0 && LOG_LEVEL_ARG.indexOf(",") > 0
const IS_MULTI_LOG_LEVEL: boolean =
LOG_LEVEL_ARG !== undefined &&
LOG_LEVEL_ARG.length > 0 &&
LOG_LEVEL_ARG.indexOf(",") > 0;
const singleLogLevel: string[] = [];
if(!IS_MULTI_LOG_LEVEL && LOG_LEVEL_ARG !== undefined) {
if (!IS_MULTI_LOG_LEVEL && LOG_LEVEL_ARG !== undefined) {
singleLogLevel.push(LOG_LEVEL_ARG);
} else {
// default log level
Expand All @@ -164,7 +168,8 @@ if(!IS_MULTI_LOG_LEVEL && LOG_LEVEL_ARG !== undefined) {
}
export const LOG_FILTERS: string[] | null = IS_MULTI_LOG_LEVEL
? LOG_LEVEL_ARG.split(",")
: !IS_MULTI_LOG_LEVEL ? singleLogLevel
: !IS_MULTI_LOG_LEVEL
? singleLogLevel
: null;

// some defaults for linux
Expand All @@ -186,4 +191,4 @@ export enum PaymentAction {
DECODE = "DECODE",
PAY = "PAY",
RETURN_BALANCE = "RETURN_BALANCE",
}
}
36 changes: 24 additions & 12 deletions src/noops.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,22 @@ let githubToken: string;
// set accept in axios header
axios.defaults.headers.get.Accept = "application/vnd.github.v3+json";

/**
* Run payment validity logic
* @param issueAmount - issue bounty
* @param balance - balance of the lightning node gitpayd connects to
* @returns
*/
const isValidPayment = (issueAmount: string, balance: number): boolean => {
const NUM_AMT = parseInt(issueAmount, 10);
return (
NUM_AMT > 0 &&
NUM_AMT < MAX_PAYMENT &&
balance >= NUM_AMT &&
NUM_AMT < PAYMENT_THRESHOLD
);
};

/**
* Make the API call to LND for processing payments
* @param {string} paymentRequest - lnd invoice
Expand All @@ -29,6 +45,12 @@ async function sendPayment(paymentRequest: string): Promise<void> {
log(`payment pre-image: ${preimage}`, LogLevel.INFO, false);
}

/**
* Process issue number and bounty amount from pull request
* @param issueNum - issue number
* @param paymentRequest - lightning invoice
* @param pullNum - pull request number
*/
async function processIssues(
issueNum: string,
paymentRequest: string,
Expand Down Expand Up @@ -66,13 +88,7 @@ async function processPayments(
pullNum: number,
paymentRequest: string
): Promise<void> {
const NUM_AMT = parseInt(issueAmount, 10);
const isValidPayment =
NUM_AMT > 0 &&
NUM_AMT < MAX_PAYMENT &&
balance >= NUM_AMT &&
NUM_AMT < PAYMENT_THRESHOLD;
if (isValidPayment) {
if (isValidPayment(issueAmount, balance)) {
const headers: object = { authorization: `token ${githubToken}` };
const MERGE = await axios.put(
`${API}/${GITPAYD_OWNER}/${GITPAYD_REPO}/pulls/${pullNum.toString()}/merge`,
Expand Down Expand Up @@ -144,11 +160,7 @@ export async function runNoOps(token: string): Promise<void> {
pull.author_association
);
if (!isCollaborator) {
log(
`unauthorized collaborator ${pull.user.login} access on gitpayd`,
LogLevel.DEBUG,
true
);
log(`unauthorized user: ${pull.user.login}`, LogLevel.DEBUG, true);
}
if (ISSUE_NUM && PAYMENT_REQUEST && isCollaborator) {
processIssues(ISSUE_NUM, PAYMENT_REQUEST, PULL_NUM);
Expand Down
29 changes: 13 additions & 16 deletions util/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,18 @@ export default async function log(
if (isFirstLog && write) {
await fs.writeFile(LOG_FILE, "");
}
// check if log filters got passed into the app
if (LOG_FILTERS.length > 0) {
LOG_FILTERS.forEach((filter: LogLevel) => {
if (filter === level) {
isFirstLog = false;
const DATE: string = new Date().toISOString();
const LOG_STRING: string = `[${level}]\t${DATE} => ${message}`;
if (write) {
fs.appendFile(LOG_FILE, `${LOG_STRING}\n`);
}
const CHILD_LOG: ChildProcessWithoutNullStreams = spawn("echo", [
`${LOG_STRING}`,
]);
CHILD_LOG.stdout.pipe(process.stdout);
isFirstLog = false;
LOG_FILTERS.forEach((filter: LogLevel) => {
if (filter === level) {
const DATE: string = new Date().toISOString();
const LOG_STRING: string = `[${level}]\t${DATE} => ${message}`;
if (write) {
fs.appendFile(LOG_FILE, `${LOG_STRING}\n`);
}
});
}
const CHILD_LOG: ChildProcessWithoutNullStreams = spawn("echo", [
`${LOG_STRING}`,
]);
CHILD_LOG.stdout.pipe(process.stdout);
}
});
}

0 comments on commit bc1b884

Please sign in to comment.