31 Commits

Author SHA1 Message Date
Kevin Siegler
b85f5330ee
Feat/admin command (#2022)
* WIP: cli admin command

* configure admin command to load ledger admin view

an express service was implemented, which serves the default site from
the instances

test plan: run `sourcecred admin` and browse to the ledger admin
instance to ensure it loads

* implement GET and POST handlers for ledger.json

The GET localhost:6006/data/ledger.json request will return
the ledger.json file if it exists

The POST request will write the submitted json to disk at
./data/ledger.json

test plan: run `$ sourcecred admin` or `$ yarn admin` then
post some json to the path by doing something like
```
$ curl --header "Content-Type: application/json" \
  --request POST \
  --data '{"username":"derp","password":"slerp"}' \
http://localhost:6006/data/ledger.json
```
and ensure there are no errors.

then
```
$ curl http://localhost:6006/data/ledger.json
```
and ensure the submitted json is returned

* remove unused variables

* Add Ledger to the UI load result

Note: This will fail if there is no ledger file. An empty JSON array is
technically a valid ledger, so if you want to update the example
instance just use:

`echo [] > data/ledger.json`

Test plan: Not yet tested, integrate into frontend

* implement button to save ledger to disk

test-plan: put and empty array ("[]") in the instance's ledger.json
file, then start the admin service "yarn admin" or "sourcecred admin"
then generate some ledger data using the identity interface and ensure
it saves back to disk

* Load ledger from disk for real

* add text middleware to express libdefs

* move ledger disk saving from application/json to text/plain

ledger.json files are encoded in such a way that they are indented and
escaped for some readability from the CLI. using json content-typing
will discard that escaping. By treating the payload as raw text, that
encoding is preserved

test-plan: modify and save a canonically serialized ledger.json file
from the browser frontend and observe that the spacing is preserved in
the file saved to disk

* remove unused deps and improve server console prompt

test-plan: start the admin server and ensure the frontend can create and
modify identities, then save the ledger.json file to disk successfully

* comments and capitalization

test-plan: `yarn flow` should still pass after this change

* remove redundant static middleware

we don't need to configure a static service for a subdirectory of a
statically-served directory

test-plan: ensure ledger.json can still be fetched either via curl or
the web frontend when the admin service is running

* remove address from fetch post request

if we're fetching to/from the parent address that served the javascript
we're calling from, an address is unnecessary

test-plan: make sure updated ledger.json files still POST back to disk

* add instance check

attempt to load the instance configurtion. If the config cannot be
loaded, an error will be throw indicating that the sourcecred.json file
cannot be found

test-plan: run `sourcecred admin` outside of an instance directory and
ensure it fails while looking for sourcecred.json

run `sourcecred admin` in an instance directory and ensure the service
starts up

Co-authored-by: Dandelion Mané <decentralion@dandelion.io>
2020-07-23 11:00:21 -05:00
Kevin Siegler
842313e1c0
Chore: combine frontends within React Admin (#1989)
* unite frontends behind the react admin menu

The cred explorer and other apps are now accessible from
within react admin.

test plan: manual UAT: ensure the cred explorer can recalculate weights
Also ensure both the explorer and dummy resource are visible in the
left menu bar.

* remove path filter and note

because we're uniting everything under react admin, we don't need this
hack anymore

* implement hash routing with react admin

Hash routing allows us to utilize deeplinks and path modifications
where the path is only adjusted after the `#` symbol, so no page
loading is needed

test plan: ensure the example instance website is navigable when
served via its root and its parent folder

* Give route and menu priority to cred explorer

cred explorer is the default route and now appears on top of the
left-hand menu in the ui

* provide some padding to the cred explorer

cred explorer is super close to the margins. adding padding to the sides
and bottom of the div containing app will make it look less cramped

* load bundledPlugins into frontend

bundled plugins are needed to see what plugins should be activated in
the frontend, namely initiatives

test plan: ensure this load function doesn't fail or error on render

* conditionally render react admin resources

resources should only accessible in the frontend when
`sourcecred/inititatives` are present in the instance's
sourcecred.json `bundledPlugins` array

test-plan: first build and browse the dev site while
`sourcecred/inititatives` is absent form the `bundledPlugins` array.
Ensure only the explorer is visible in the menu.

second add `sourcecred/initiatives` to the bundled plugins array and
rebuild the dev site. The dummy resource should now be visible

* migrate load logic to its own file

test plan: ensure no load errors on render in initiatives or cred
explorer when building and browsing the dev site

* utilize the Explorer component directly

initially, the entire explorer app was utilized. This caused redundant
loading requests. and bloat. The ExplorerApp component will be retained
if the explorer is desire to be rendered as a standalone app.

test plan: ensure the explorer renders with the cred data when browsing
the dev site

* add react-redux flow libdefs

* make explorer width responsive

thanks to @hammadj for suggesting this. The only wrinkle is the
cred-over-time charts don't render well after a certain point, so these
should only render at or above a specific aspect

test plan: resize the window to see how effectively the explorer renders
at various aspects

* remove extraneous logout and refresh buttons

In order to customize the top bar, the topbar must be implemented and
modified in our codebase. The conditionally-rendered logout icon has
also been removed from the left side menu

Because we haven't made any decisions about styling, I've left the
unused styling props in, but commented out so they comply with the
linter. This way, it will be straightforward to propagate any themes if
we decide to utilize the material ui API and class system

test plan: run the dev web environment and ensure the cred explorer
renders with some data from the example instance
2020-07-20 22:59:48 -05:00
Hammad Jutt
8140738da3
Setup React Admin frontend (#1888)
* Setup React Admin frontend

This configures ReactAdmin to render as the frontend with a simple "fake data provider".
SSR issues need to be addressed before the UI will be visible

TestPlan: Ensure the frontend builds when running yarn start

* Fix failing react-admin build

This mocks out the testing-library modules which were causing issues in the frontend build and adds temporary
patches on postinstall for the other problematic react-admin libraries. We can remove the patches once they merge
the PR that fixes this upstream: https://github.com/marmelab/react-admin/pull/4970

Test Plan: Run yarn start --instance <instance location> to ensure the frontend builds without errors

* Update history to compatible version

Updates the history package to a version compatible with the React Router v4

Test Plan: Run yarn start --instance <instance location> and ensure the
 React Admin UI renders and navigation works

* Fix flow errors importing from history package

v4 of the history package updated the import format, updated our usage to match.
More info: 845d690c55/CHANGES.md (v400-0)

TestPlan: Ensure flow check passes

* Add flow types for History and React-Router

Better type safety is always nice to have :)

TestPlan: Ensure versions for typings match installed versions of history and react-router

* remove withAssets and createRelativeHistory

withAssets is no longer needed since we have the Docusaurus site to fulfill those purposes and
our homepage is no longer being generated by this UI.

RelativeHistroy is no longer needed with Router V5 because this version
of the router accepts a `basename` parameter

TestPlan: Ensure that nothing is still using functionality from these two modules

* Update Link component to react-router v5

React router v5 has moved its Link component into a separate package: react-router-dom. This PR
just updates the place we import the Link component for our own internal wrapper around it.

TestPlan: Ensure that our Link component is still functional

* Remove react-admin patches

React admin merged my PR that fixes the issues with window (https://github.com/marmelab/react-admin/pull/4970),
so we no longer need these patches since the issue is addressed upstream now.

TestPlan: Ensure that the site builds and runs without "window is not defined" errors

Co-authored-by: Kevin Siegler <kevinsiegler54@gmail.com>
2020-07-14 17:36:19 -06:00
William Chargin
abbc024cbf
util: add uuid module (#1914)
Summary:
A new utility module defines an opaque UUID type. Clients can generate
random UUIDs and convert them to and from strings.

We roll our own module here rather than using the `uuid` NPM module
because that module only exposes the 32-character hyphen-separated
string form of UUID, which takes up nearly 50% more space. The usual
approach these days is to use fixed-length base64 strings without
padding. For instance, [YouTube video IDs are of this form][yt].

This module takes care not to generate IDs that might confuse
command-line argument parsers, and tries not to generate IDs that might
contain English curse words. The implementation uses isomorphic
libraries for RNG and base64 conversion because the relevant Node and
browser APIs are not aligned.

[yt]: https://webapps.stackexchange.com/a/101153

Test Plan:
Unit tests included, with full coverage.

wchargin-branch: util-uuid
2020-07-02 12:29:27 -07:00
William Chargin
deb257ff6a
flow: update libdefs for express, removing hacks (#1879)
Summary:
This removes a SourceCred-specific hack because the upstream issue has
been fixed. Removing the hack is important because it uses a suppression
comment and thus hampers upgrading to latest Flow.

The libdef portion of this change was generated by removing the old file
and then running `flow-typed install express@4.17.1`.

Test Plan:
That `yarn flow` passes suffices.

wchargin-branch: libdefs-express
2020-06-21 21:25:24 -07:00
William Chargin
1f8925fe77
flow: update better-sqlite3 types for v7.0.0 (#1770)
Summary:
I first wrote these type definitions for v4.x.x. The library API has
changed since then. This patch updates the type definitions to match the
current API as of the v7.0.0 release:
<https://github.com/JoshuaWise/better-sqlite3/blob/v7.0.0/docs/api.md>

There are breaking changes, but none among functions that we use. On the
other hand, there are new features that will be useful to us.

Test Plan:
Running `yarn flow` still passes. There may be some errors among typing
of functions that we don’t actually use (particularly `aggregate`, which
is more complicated than the others). If so, we can cross those bridges
when we come to them.

wchargin-branch: flow-better-sqlite3-v7-api
2020-04-26 13:08:51 -07:00
William Chargin
c18244bf19
deps: upgrade better-sqlite3@^7.0.0 (#1769)
Summary:
Notably, this includes a Jest compatibility improvement:
<https://github.com/JoshuaWise/better-sqlite3/issues/162#issuecomment-619401380>

Test Plan:
Existing unit tests pass.

wchargin-branch: better-sqlite3-v7.0.0
2020-04-26 13:04:31 -07:00
William Chargin
c162813a5e
Fix Prettier deprecations and typings post upgrade (#1307)
Summary:
In #1194, we upgraded Prettier from 1.13.4 to 1.18.2, but this upgrades
past <https://github.com/prettier/prettier/pull/5647>, which was first
released in Prettier 1.16.0. This commit fixes the uses of deprecated
code introduced as a result. It also upgrades the type definitions to
match, via `flow-typed install prettier@1.18.2`.

Addresses part of #1308.

Test Plan:
Prior to this commit, running `yarn unit` would print

```
    console.warn node_modules/prettier/index.js:7934
      { parser: "babylon" } is deprecated; we now treat it as { parser: "babel" }.
```

in two test cases; it no longer prints any such warnings. Furthermore,
running `git grep 'parser.*babylon'` no longer finds any matches.

wchargin-branch: prettier-deprecations
2019-08-22 09:00:25 -07:00
William Chargin
141a0a23d2
flow: add libdefs for deep-freeze (#1310)
Summary:
This dependency was added in #1249 without typedefs, and so is
implicitly `any`-typed.

Depends on #1309 to fix a bug that would otherwise be a true positive
type error.

Addresses part of #1308.

Generated with `flow-typed install deep-freeze@0.0.1`.

Test Plan:
Running `yarn flow` passes, but fails if you remove the `nodePrefix` or
`edgePrefix` attributes of the Discourse plugin declaration.

wchargin-branch: libdefs-deep-freeze
2019-08-22 08:57:16 -07:00
William Chargin
ecd15ed3c4
flow: add libdefs that have clean updates (#1311)
Summary:
Generated by running `flow-typed install --skip --overwrite` and
reverting a minimal set of libdefs such that the change does not
introduce any Flow errors (except Prettier, which is covered by #1307).

Addresses parts of #1308.

Changes:

  - `chalk`: upgraded v1.x.x to v2.x.x
  - `flow-bin`: no-op; explicit Flow window widening
  - `isomorphic-fetch`: no-op; formatting change
  - `jest`: updates for Flow v0.104.x (explicit inexact objects), and
    also some functional additions
  - `object-assign`: no-op; explicit Flow window widening
  - `rimraf`: added new at v2.x.x

Test Plan:
Flow passes, by construction.

wchargin-branch: libdefs-clean
2019-08-22 08:49:40 -07:00
William Chargin
158dc0b831
flow: update libdefs for express (#1312)
Summary:
These can be updated cleanly after applying the SourceCred-specific
patch. I’ve modified the comment on that patch to be clear that it *is*
SourceCred-specific—after updating, I spent a while trying to find why
it was deleted from upstream, before eventually realizing that it never
existed upstream anyway.

Generated by running `flow-typed install express@4.16.3 --overwrite` and
then manually inserting the three “SourceCred-specific hack” comment
blocks.

Addresses part of #1308.

Test Plan:
Running `yarn flow` still passes (but warns if the hacks are removed).

wchargin-branch: libdefs-express
2019-08-22 08:45:52 -07:00
William Chargin
c3f242d3af
libdefs: update libdefs for enzyme (#1314)
Summary:
These can be updated cleanly now that an upstream pull request has been
merged: <https://github.com/flow-typed/flow-typed/pull/3522>

Generated by running `flow-typed install enzyme@3.3.0 --overwrite`.

Addresses part of #1308.

Test Plan:
Running `yarn flow` still passes.

wchargin-branch: libdefs-enzyme
2019-08-22 08:36:04 -07:00
Dandelion Mané
eadcca8999 Upgrade flow to to 0.102.0
This necessitated a number of type fixes:
- Upgraded the express flow-typed file to latest
- Added manual flow error suppression to where the express flow-typed
file is still using a deprecated utility type
- Removed type polymorphism support on map.merge (see context here[1]).
We weren't using the polymorphism anywhere so I figured it was simplest
to just remove it.
- Improve typing around jest mocks throughout the codebase.

Test plan: `yarn test --full` passes.

[1]: https://github.com/flow-typed/flow-typed/issues/2991
2019-07-05 17:21:56 +01:00
Dandelion Mané
230756ffec Upgrade jest from v23 to v24
Just some general housekeeping. `yarn test --full` passes without issue.
2019-07-04 18:57:50 +01:00
Dandelion Mané
29c9229c28 Update better-sqlite3 to v5
When we took a dep on better-sqlite3 in #836, we used a fork, because
better-sqlite3 did not yet support private in-memory databases via the
`:memory:` filepath. As of better-sqlite3 v5, this has been added to
mainline, so we no longer need the fork.

The v4->v5 transition involves some breaking changes. The only ones that
affected us were two field renames, from `lastUpdateROWID` to
`lastUpdateRowid`, and `returnsData` to `reader`.

Test plan:
After updating the field accesses, `yarn test --full` passes. For added
safety, I also blew away cache, loaded a nontrivial repository, and
verified that the full cred workflow still works.

cc @wchargin
2019-07-04 20:31:32 +03:00
William Chargin
24242eccfe
chore: update flow-typed libdefs (#932)
Summary:
Generated with `flow-typed install --skip --overwrite`.

Test Plan:
`yarn flow` passes.

wchargin-branch: flow-typed-update
2018-10-22 10:05:11 -07:00
William Chargin
2687bc5115
flow-typed: update prettier libdef metadata (#931)
Summary:
Generated with `flow-typed install prettier@1.13.4 --overwrite`. The
changes in #925 have been merged upstream; this pulls in the updated
signature and version.

Test Plan:
`yarn flow` passes.

wchargin-branch: flow-typed-update-prettier
2018-10-22 10:02:51 -07:00
William Chargin
4558d775a5
flow-typed: fix Prettier libdefs (#925)
Summary:
This patches in the following two pull requests from upstream:

  - https://github.com/flow-typed/flow-typed/pull/2856
  - https://github.com/flow-typed/flow-typed/pull/2860

The first exports types so that they can be used in client code.
The second fixes a broken type definition.

Test Plan:
See the test plans in those two PRs.

wchargin-branch: flow-typed-fix-prettier
2018-10-19 08:54:00 -07:00
Dandelion Mané
42cdfa4332
Upgrade jest and typings (#893)
Motivated by my desire for `.toMatchInlineSnapshot()`. Really we just
need and updated typing file for this, but I upgraded `jest` too to just
get us in a clean state.

Commit generated via:
```
yarn add --dev jest
flow-typed install jest@23.6.0
```

Test plan: `yarn test`
2018-09-25 18:48:54 -07:00
William Chargin
252400d5e7
flow: fix Statement properties (#886)
Summary:
These were completely wrong; my bad.

[The docs][1] list `source` and `returnsData` but not `database`.
I include the latter only for consistency with `Transaction`, and will
consider removing them both as they are technically undocumented (though
not underscore-named, so it’s not clear-cut).

[1]: https://wchargin.github.io/better-sqlite3/api.html#properties-1

Test Plan:
At a Node console:

```js
> require("better-sqlite3")(":memory:").prepare("BEGIN")
Statement {
  returnsData: false,
  source: 'BEGIN',
  database:
   Database {
     inTransaction: false,
     open: true,
     memory: false,
     readonly: false,
     name: ':memory:' } }
```

wchargin-branch: flow-better-sqlite3-statement-properties
2018-09-21 17:48:28 -07:00
William Chargin
7702bb2e1d
flow: simplify better-sqlite3 bound parameters (#869)
Summary:
The actual constraints for bound parameters are too complicated to
express within Flow. This commit changes the type definitions from one
approximation to another, simpler one. Neither approximation is likely
to cause many problems in practice, either in terms of spurious errors
or spurious lacks of error. (The failure mode for the new formulation is
having multiple dictionaries of binding values, which would pass Flow
but quickly raise a `TypeError` at runtime.)

The reason for the change is that it makes the method definitions
considerably simpler, in a way that is likely to avoid other problems
with Flow. In particular, this removes method overloads and the need for
parameter disambiguation.

I fix a typo while in the area.

Test Plan:
Note that the following file typechecks:

```js
// @flow
import Database from "better-sqlite3";
const db = new Database(":memory:");
const stmt = db.prepare("BEGIN"); // SQL text doesn't matter

stmt.run();
stmt.run(null, 2, "three", new Buffer(Array(4)));
stmt.run(+false);
stmt.run(1, {dos: 2}, 3); // the binding dictionary can go anywhere
stmt.run({a: 1}, {b: 2}); // this will fail at runtime (TypeError)

// $ExpectFlowError
stmt.run(false); // booleans cannot be bound
// $ExpectFlowError
stmt.run({x: {y: "z"}}); // named parameters are not recursive
```

All but the last two success cases (lines 9 and 10) would also have
passed before this change.

wchargin-branch: flow-better-sqlite3-bound-parameters-simplify
2018-09-20 11:31:26 -07:00
William Chargin
8b22c1edd5
flow: change better-sqlite3 each to iterator (#863)
Summary:
This was changed in the v4.0.0 release of `better-sqlite3`:
<https://github.com/JoshuaWise/better-sqlite3/pull/61>

Test Plan:
Create the following test file, and note that it fails to typecheck
before this change (on two counts) but passes after:

```js
// @flow
import Database from "better-sqlite3";
const db = new Database(":memory:");
db.prepare("CREATE TABLE foo (id)").run();
const insert = db.prepare("INSERT INTO foo (id) VALUES (?)");
insert.run(1);
insert.run(2);
const retrieve = db.prepare("SELECT id FROM foo").pluck();

console.log("Old way:");
try {
  // $ExpectFlowError
  retrieve.each((x) => void console.log(x));
} catch (e) {
  console.log("Failed (good):", String(e));
}
console.log();

console.log("New way:");
for (const value of retrieve.iterate()) {
  console.log(value);
}
```

Also, run the test with `NODE_ENV=development babel-node src/test.js`,
and note that it outputs:

```
$ NODE_ENV=development babel-node src/test.js
Old way:
Failed (good): TypeError: retrieve.each is not a function

New way:
1
2
```

wchargin-branch: flow-better-sqlite3-iterator
2018-09-19 15:39:01 -07:00
William Chargin
51461f4842
flow: mark bound parameters as covariant (#842)
Summary:
Before this patch, an object whose type has read-only attributes cannot
be passed to `stmt.run`/etc., because the libdef does not promise not to
mutate its argument. This patch fixes that oversight.

Test Plan:
Create the following test file, and note that it fails to typecheck
before this change but passes after:

```js
// @flow
import Database from "better-sqlite3";
const db = new Database("/tmp/foo");
const args: {|+id: number|} = {id: 1};
db.prepare("INSERT INTO foo (id) VALUES (?)").run(args);
```

wchargin-branch: flow-better-sqlite3-bound-parameters-covariant
2018-09-17 11:15:04 -07:00
William Chargin
c616ec82fb
flow: add type definitions for better-sqlite3 (#837)
Summary:
The `flow-typed` repository doesn’t have type definitions for
`better-sqlite3`, so I wrote some. I figure that we can use them for a
while, fix any problems that arise, and then PR them upstream.

I started from [the TypeScript definitions][1], but made some
improvements (like stronger typing for bound parameters), and also made
some necessary changes due to differences between Flow and TypeScript.

[1]: https://github.com/DefinitelyTyped/DefinitelyTyped/commits/master/types/better-sqlite3/index.d.ts

Prettier does not format this file (it is in `flow-typed`), so I
manually ran it through Prettier with the settings used by `flow-typed`
itself.

Test Plan:
None.

wchargin-branch: flow-better-sqlite3
2018-09-13 18:32:00 -07:00
William Chargin
24a950629a
Add all available flow-typed libdefs (#548)
Summary:
Generated with `flow-typed install --skip --overwrite`.

Test Plan:
Amazingly, `yarn flow` passes. Go team.

wchargin-branch: add-libdefs
2018-07-27 17:23:24 -07:00
William Chargin
873eca6350
Upgrade Flow to v0.76.0 (#546)
Summary:
In addition to a routine libdef update, we also need to work around a
particularly nasty new bug in Flow, which requires `any`-casts that are
even more unsafe than usual. That said, I think that it’s worth that
cost to remain up to date with Flow, so that we can amortize future such
issues.

Test Plan:
Running `yarn travis --full` passes.

wchargin-branch: upgrade-flow-v0.76.0
2018-07-27 15:54:59 -07:00
Dandelion Mané
797fb6331d
Upgrade jest to 23.4.1 (#537)
Flow types regenerated via flow-typed install jest@23.4.1

Test plan: Travis
2018-07-27 12:28:04 -07:00
William Chargin
04acce4a4c Remove dependency on react-router-dom
Test Plan:
Note that nothing breaks, because we don’t actually have any routing
right now.

wchargin-branch: remove-rrv4
2018-07-23 13:29:48 -07:00
Dandelion Mané
0149d74971 Add react-router-dom
This commit adds a npm and flow-typed dependency, with no functional
change.

Test plan: `yarn travis` passes.
2018-05-08 12:55:38 -07:00
William Chargin
18ddbfff3e
Add dependency on express (#233)
wchargin-branch: express
2018-05-07 20:05:52 -07:00
Dandelion Mané
bc2377448f
Move package json to root (#37)
Reorganize the code so that we have a single package.json file, which is at the root.
All source code now lives under `src`, separated into `src/backend` and `src/explorer`.

Test plan:

- run `yarn start` - it works
- run `yarn test` - it finds the tests (all in src/explorer) and they pass
- run `yarn flow` - it works. (tested with an error, that works too)
- run `yarn prettify` - it finds all the js files and writes to them
2018-02-26 22:32:23 -08:00