Verified Commit 6d95ee23 authored by JP's avatar JP
Browse files

apply opstooling-js & opstooling-js-style

remove tests
parent 0a0e1db4
dist
node_modules
const {
getConfiguration,
} = require("opstooling-js-style/src/eslint/configuration")
module.exports = getConfiguration({ typescript: { rootDir: __dirname } })
{
"extends": ["eslint:recommended", "prettier"],
"parserOptions": {
"ecmaVersion": "latest"
},
"env": {
"node": true,
"commonjs": true,
"es6": true
},
"overrides": [
{
"files": ["**/*.test.js"],
"env": {
"mocha": true
},
"plugins": ["mocha"],
"rules": {
"mocha/no-exclusive-tests": "error",
"mocha/no-pending-tests": "error"
}
},
{
"files": ["**/*.ts"],
"parser": "@typescript-eslint/parser",
"plugins": ["@typescript-eslint"]
}
],
"rules": {
"prefer-const": "warn"
}
}
'use strict';
module.exports = {
require: 'ts-node/register',
spec: ['test/**/*.test.ts'],
sort: true,
};
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- repo: local
hooks:
- id: eslint
name: eslint
entry: node_modules/.bin/eslint
language: node
files: \.[^\.]*(t|j)s$
args: ["--quiet"]
- id: prettier
name: prettier
entry: node_modules/.bin/prettier
language: node
files: \.json$
args: ["--check"]
- id: typecheck
name: typecheck
entry: node_modules/.bin/tsc-files
language: node
files: \.ts$
dist
node_modules
{
"trailingComma": "es5",
"tabWidth": 2,
"singleQuote": true,
"semi": true,
"bracketSpacing": true
}
module.exports = require("opstooling-js-style/src/prettier/configuration")
FROM node:14-alpine
FROM node:16-alpine
RUN apk -U upgrade --no-cache
......
......@@ -12,17 +12,17 @@
"probot-app"
],
"scripts": {
"typecheck": "tsc --noEmit",
"fix:eslint": "eslint --fix",
"fix:prettier": "prettier --write",
"fix": "yarn fix:eslint '{*,**/*}.{js,ts}' && yarn fix:prettier '{*,**/*}.json'",
"start": "nodemon",
"test": "mocha",
"lint": "eslint src/**/* --cache",
"lint:fix": "eslint src/**/* --cache --fix",
"build": "run-s build:*",
"build:clean": "rimraf dist/",
"build:js": "babel src/ --extensions '.ts,.js' --out-dir dist/"
"build": "rimraf dist; babel src/ --extensions '.ts,.js' --out-dir dist/"
},
"dependencies": {
"@polkadot/api": "^8.1.1",
"@polkadot/util-crypto": "^9.0.1",
"opstooling-js": "https://github.com/paritytech/opstooling-js#v0.0.3",
"probot": "^11.0.1"
},
"devDependencies": {
......@@ -30,20 +30,10 @@
"@babel/core": "^7.15.8",
"@babel/preset-env": "^7.15.8",
"@babel/preset-typescript": "^7.15.0",
"@types/chai": "^4.2.22",
"@types/mocha": "^9.0.0",
"@resolritter/tsc-files": "^1.1.4",
"@types/node": "^16.10.3",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"chai": "^4.3.4",
"eslint": "^7.32.0",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"mocha": "^9.1.2",
"nock": "^13.0.5",
"nodemon": "^2.0.13",
"npm-run-all": "^4.1.5",
"prettier": "^2.4.1",
"opstooling-js-style": "https://github.com/paritytech/opstooling-js-style#master",
"rimraf": "^3.0.2",
"smee-client": "^1.2.2",
"ts-node": "^10.3.0",
......
import { Probot, run } from 'probot';
import { ApiPromise, WsProvider, Keyring } from '@polkadot/api';
import { cryptoWaitReady } from '@polkadot/util-crypto';
import { HandlerFunction } from "@octokit/webhooks/dist-types/types"
import { ApiPromise, Keyring, WsProvider } from "@polkadot/api"
import { cryptoWaitReady } from "@polkadot/util-crypto"
import assert from "assert"
import { displayError, envVar } from "opstooling-js"
import { Probot, run } from "probot"
import { postComment } from './helpers/github';
import { State } from "./types"
// TODO add some kind of timeout then return an error
// TODO Unit tests
/* TODO add some kind of timeout then return an error
TODO Unit tests */
export async function tipUser(
address,
contributor,
network,
pullRequestNumber,
pullRequestRepo,
size
{ seedOfTipperAccount, bot }: State,
{
contributor,
pullRequestNumber,
pullRequestRepo,
tipSize,
}: {
contributor: {
githubUsername: string
account: {
address: string
network: "localtest" | "kusama" | "polkadot"
}
}
pullRequestNumber: number
pullRequestRepo: string
tipSize: string
},
) {
await cryptoWaitReady();
const keyring = new Keyring({ type: 'sr25519' });
// Connect to the appropriate network.
let provider, account;
if (network == 'localtest') {
provider = new WsProvider('ws://localhost:9944');
account = keyring.addFromUri('//Alice', { name: 'Alice default' });
} else if (network == 'polkadot') {
provider = new WsProvider('wss://rpc.polkadot.io/');
account = keyring.addFromUri(process.env.ACCOUNT_SEED);
} else if (network == 'kusama') {
provider = new WsProvider('wss://kusama-rpc.polkadot.io/');
account = keyring.addFromUri(process.env.ACCOUNT_SEED);
} else {
return;
}
await cryptoWaitReady()
const keyring = new Keyring({ type: "sr25519" })
const { provider, botTipAccount } = (() => {
switch (contributor.account.network) {
case "localtest": {
return {
provider: new WsProvider("ws://localhost:9944"),
botTipAccount: keyring.addFromUri("//Alice", {
name: "Alice default",
}),
}
}
case "polkadot": {
return {
provider: new WsProvider("wss://rpc.polkadot.io/"),
botTipAccount: keyring.addFromUri(seedOfTipperAccount),
}
}
case "kusama": {
return {
provider: new WsProvider("wss://kusama-rpc.polkadot.io/"),
botTipAccount: keyring.addFromUri(seedOfTipperAccount),
}
}
default: {
const exhaustivenessCheck: never = contributor.account.network
throw new Error(
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
`Network is not handled properly in tipUser: ${exhaustivenessCheck}`,
)
}
}
})()
const api = await ApiPromise.create({ provider });
const api = await ApiPromise.create({ provider })
// Get general information about the node we are connected to
const [chain, nodeName, nodeVersion] = await Promise.all([
api.rpc.system.chain(),
api.rpc.system.name(),
api.rpc.system.version(),
]);
console.log(
`You are connected to chain ${chain} using ${nodeName} v${nodeVersion}`
);
const reason = `TO: ${contributor} FOR: ${pullRequestRepo}#${pullRequestNumber} (${size})`;
// TODO before submitting, check tip does not already exist via a storage query.
// TODO potentially prevent duplicates by also checking for reasons with the other sizes.
])
bot.log(
`You are connected to chain ${chain.toString()} using ${nodeName.toString()} v${nodeVersion.toString()}`,
)
const reason = `TO: ${contributor.githubUsername} FOR: ${pullRequestRepo}#${pullRequestNumber} (${tipSize})`
/* TODO before submitting, check tip does not already exist via a storage query.
TODO potentially prevent duplicates by also checking for reasons with the other sizes. */
const unsub = await api.tx.tips
.reportAwesome(reason, address)
.signAndSend(account, (result) => {
console.log(`Current status is ${result.status}`);
.reportAwesome(reason, botTipAccount)
.signAndSend(botTipAccount, (result) => {
bot.log(`Current status is ${result.status.toString()}`)
if (result.status.isInBlock) {
console.log(`Tip included at blockHash ${result.status.asInBlock}`);
bot.log(
`Tip included at blockHash ${result.status.asInBlock.toString()}`,
)
} else if (result.status.isFinalized) {
console.log(`Tip finalized at blockHash ${result.status.asFinalized}`);
unsub();
bot.log(
`Tip finalized at blockHash ${result.status.asFinalized.toString()}`,
)
unsub()
}
});
})
return true;
return true
}
export default function bot(bot: Probot) {
bot.log.info('Tip bot was loaded!');
bot.on('issue_comment', async (context) => {
// Get all the relevant contextual information.
const commentText = context.payload.comment.body;
const pullRequestBody = context.payload.issue.body;
const pullRequestUrl = context.payload.issue.html_url;
const tipper = context.payload.comment.user.login;
const contributor = context.payload.issue.user.login;
const pullRequestNumber = context.payload.issue.number;
const pullRequestRepo = context.payload.repository.name;
// The bot only triggers on creation of a new comment on a pull request.
if (
!Object.prototype.hasOwnProperty.call(
context.payload.issue,
'pull_request'
) ||
context.payload.action !== 'created' ||
!commentText.startsWith('/tip')
) {
return;
}
const onIssueComment = async (
state: State,
context: Parameters<HandlerFunction<"issue_comment.created", unknown>>[0],
tipRequester: string,
) => {
const { allowedTipRequesters, bot } = state
const commentText = context.payload.comment.body
const pullRequestBody = context.payload.issue.body
const pullRequestUrl = context.payload.issue.html_url
const contributor = context.payload.issue.user.login
const pullRequestNumber = context.payload.issue.number
const pullRequestRepo = context.payload.repository.name
// Any problems along the way will be stored here, and used to return an error if needed.
const problemsText = [];
const [botMention, tipSizeInput] = commentText.split(" ") as (
| string
| undefined
)[]
// The bot only triggers on creation of a new comment on a pull request.
if (
!Object.prototype.hasOwnProperty.call(
context.payload.issue,
"pull_request",
) ||
context.payload.action !== "created" ||
!botMention?.startsWith("/tip")
) {
return
}
if (tipRequester === contributor) {
return "Contributor and tipper cannot be the same person!"
}
if (!allowedTipRequesters.includes(tipRequester)) {
return `You are not allowed to request a tip. Only ${allowedTipRequesters.join(
", ",
)} are allowed.`
}
if (tipper === contributor) {
problemsText.push(`Contributor and tipper cannot be the same person!`)
const contributorAccount = (() => {
const matches = pullRequestBody.match(
// match "polkadot address: <ADDRESS>"
/(polkadot|kusama|localtest)\s*address:\s*([a-z0-9]+)/i,
)
if (!matches || matches.length != 3) {
throw new Error(
`Contributor did not properly post their Polkadot or Kusama address. \n\n Make sure the pull request description has: "{network} address: {address}".`,
)
}
// TODO check contributor is NOT member of parity org (or better, not a member of the org where the repo lives)
// if (contributor is in github org) {
// problemsText.push(`Contributor can't be a member of Parity!`)
// }
const [matched, networkInput, address] = matches
assert(networkInput, `networkInput could not be parsed from "${matched}"`)
assert(address, `address could not be parsed from "${matched}"`)
if (!process.env.ALLOWED_USERS.includes(tipper)) {
problemsText.push(
`You are not allowed to access the tip bot. Only ${process.env.ALLOWED_USERS} are allowed.`
);
const validNetworks = {
polkadot: "polkadot",
kusama: "kusama",
localtest: "localtest",
} as const
const validNetwork =
networkInput in validNetworks
? validNetworks[networkInput as keyof typeof validNetworks]
: undefined
if (!validNetwork) {
throw new Error(
`Invalid network: "${networkInput}". Please select one of: ${Object.keys(
validNetworks,
).join(", ")}.`,
)
}
// We will populate this information by processing the pull request and tip comment.
let network, address, size;
// match "polkadot address: <ADDRESS>"
const addressRegex = /(polkadot|kusama|localtest) address:\s?([a-z0-9]+)/i;
const maybeMatch = pullRequestBody.match(addressRegex);
if (!maybeMatch || maybeMatch.length != 3) {
problemsText.push(
`Contributor did not properly post their Polkadot or Kusama address. \n\n Make sure the pull request description has: "{network} address: {address}".`
);
} else {
network = maybeMatch[1].toLowerCase();
if (!['polkadot', 'kusama', 'localtest'].includes(network)) {
problemsText.push(
`Invalid network: ${maybeMatch[1]}. Please select "polkadot" or "kusama".`
);
}
address = maybeMatch[2];
return { network: validNetwork, address }
})()
const tipSize = (() => {
const validTipSizes = {
small: "small",
medium: "medium",
large: "large",
} as const
const validTipSize =
tipSizeInput && tipSizeInput in validTipSizes
? validTipSizes[tipSizeInput as keyof typeof validTipSizes]
: undefined
if (!validTipSize) {
throw new Error(
`Invalid tip size. Please specify one of ${Object.keys(
validTipSizes,
).join(", ")}.`,
)
}
// Tip initiation comment should be: "/tip { small / medium / large }"
const textParts = commentText.split(' ');
if (textParts.length !== 2) {
problemsText.push(
`Invalid command! Payload should be: "/tip { small / medium / large }".`
);
} else {
// We already match `/tip` at the top of this program, so just check size.
size = textParts[1].toLowerCase();
if (size == 's') {
size = 'small';
} else if (size == 'm') {
size = 'medium';
} else if (size == 'l') {
size = 'large';
return validTipSize
})()
bot.log(
`Valid command!\n${tipRequester} wants to tip ${contributor} (${contributorAccount.address} on ${contributorAccount.network}) a ${tipSize} tip for pull request ${pullRequestUrl}.`,
)
const result = await tipUser(state, {
contributor: { githubUsername: contributor, account: contributorAccount },
pullRequestNumber,
pullRequestRepo,
tipSize,
})
const tipUrl = (() => {
switch (contributorAccount.network) {
case "polkadot": {
return "https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polkadot.io#/treasury/tips"
}
if (!['small', 'medium', 'large'].includes(size)) {
problemsText.push(
`Invalid tip size. Please specify one of small, medium, or large.`
);
case "kusama": {
return "https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fkusama-rpc.polkadot.io#/treasury/tips"
}
default: {
return "https://polkadot.js.org/apps/#/treasury/tips"
}
}
})()
if (problemsText.length > 0) {
// there was some error to get to this point, lets list them.
let comment =
'Please fix the following problems before calling the tip bot again:';
for (const problem of problemsText) {
comment += `\n * ${problem}`;
}
postComment(context, comment);
} else {
console.log(
`Valid command! \n ${tipper} wants to tip ${contributor} (${address} on ${network}) a ${size} tip for pull request ${pullRequestUrl}.`
);
// Send the transaction to the network.
const result = await tipUser(
address,
contributor,
network,
pullRequestNumber,
pullRequestRepo,
size
);
let tipUrl;
if (network == 'polkadot') {
tipUrl = "https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Frpc.polkadot.io#/treasury/tips";
} else if (network == 'kusama') {
tipUrl = "https://polkadot.js.org/apps/?rpc=wss%3A%2F%2Fkusama-rpc.polkadot.io#/treasury/tips";
} else {
tipUrl = "https://polkadot.js.org/apps/#/treasury/tips";
}
// TODO actually check for problems with submitting the tip. Maybe even query storage to ensure the tip is there.
return result
? `A ${tipSize} tip was successfully submitted for ${contributor} (${contributorAccount.address} on ${contributorAccount.network}). \n\n ${tipUrl}`
: "Could not submit tip :( Notify someone at Parity."
}
const main = (bot: Probot) => {
const allowedTipRequesters = JSON.parse(envVar("ALLOWED_USERS")) as unknown[]
if (!Array.isArray(allowedTipRequesters)) {
throw new Error("$ALLOWED_USERS needs to be an array")
}
const seedOfTipperAccount = envVar("ACCOUNT_SEED")
// TODO actually check for problems with submitting the tip. Maybe even query storage to ensure the tip is there.
if (result) {
postComment(
context,
`A ${size} tip was successfully submitted for ${contributor} (${address} on ${network}). \n\n ${tipUrl}`
);
} else {
postComment(
context,
`Could not submit tip :( Notify someone at Parity.`
);
const state = { allowedTipRequesters, seedOfTipperAccount, bot }
bot.log.info("State", state)
bot.log.info("Tip bot was loaded!")
bot.on("issue_comment", (context) => {
const tipRequester = context.payload.comment.user.login
const onIssueCommentResult = async (result: string | Error | undefined) => {
if (result === undefined) {
return
}
await context.octokit.issues.createComment({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
issue_number: context.payload.issue.number,
body: `@${tipRequester} ${
result instanceof Error ? `ERROR: ${displayError(result)}` : result
}`,
})
}
return;
});
void onIssueComment(state, context, tipRequester)
.then(onIssueCommentResult)
.catch(onIssueCommentResult)
})
}
run(bot);
void run(main)
export function postComment(context, body) {
const issueComment = context.issue({
body: body,
});
return context.octokit.issues.createComment(issueComment);
}
import { HandlerFunction } from "@octokit/webhooks/dist-types/types"
import { Probot } from "probot"
export type IssueCommentCreatedContext = Parameters<
HandlerFunction<"issue_comment.created", unknown>
>[0]
export type State = {
allowedTipRequesters: unknown[]
seedOfTipperAccount: string
bot: Probot
}