mirror of
https://github.com/status-im/sourcecred.git
synced 2025-01-09 12:15:56 +00:00
Initiatives: use common timestamp types and utils (#1749)
In PR #1746 we've added `src/util/timestamp.js`. This commit replaces the plugin's internal opaque string and number types with well tested types and conversion functions.
This commit is contained in:
parent
7caca360a0
commit
48a7659ff9
@ -8,6 +8,7 @@ import {
|
|||||||
} from "../../core/graph";
|
} from "../../core/graph";
|
||||||
import * as Weights from "../../core/weights";
|
import * as Weights from "../../core/weights";
|
||||||
import type {ReferenceDetector, URL} from "../../core/references";
|
import type {ReferenceDetector, URL} from "../../core/references";
|
||||||
|
import * as Timestamp from "../../util/timestamp";
|
||||||
import type {Initiative, InitiativeRepository} from "./initiative";
|
import type {Initiative, InitiativeRepository} from "./initiative";
|
||||||
import {createId, addressFromId} from "./initiative";
|
import {createId, addressFromId} from "./initiative";
|
||||||
import {createWeightedGraph, initiativeWeight} from "./createGraph";
|
import {createWeightedGraph, initiativeWeight} from "./createGraph";
|
||||||
@ -23,7 +24,7 @@ function _createInitiative(overrides?: $Shape<Initiative>): Initiative {
|
|||||||
return {
|
return {
|
||||||
id: createId("UNSET_SUBTYPE", "42"),
|
id: createId("UNSET_SUBTYPE", "42"),
|
||||||
title: "Unset test initiative",
|
title: "Unset test initiative",
|
||||||
timestampMs: 123,
|
timestampMs: Timestamp.fromNumber(123),
|
||||||
completed: false,
|
completed: false,
|
||||||
dependencies: [],
|
dependencies: [],
|
||||||
references: [],
|
references: [],
|
||||||
@ -49,7 +50,7 @@ class MockInitiativeRepository implements InitiativeRepository {
|
|||||||
const initiative = _createInitiative({
|
const initiative = _createInitiative({
|
||||||
id: createId("TEST_SUBTYPE", String(num)),
|
id: createId("TEST_SUBTYPE", String(num)),
|
||||||
title: `Example Initiative ${num}`,
|
title: `Example Initiative ${num}`,
|
||||||
timestampMs: 400 + num,
|
timestampMs: Timestamp.fromNumber(400 + num),
|
||||||
...shape,
|
...shape,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
import {type URL} from "../../core/references";
|
import {type URL} from "../../core/references";
|
||||||
import {type NodeAddressT, NodeAddress} from "../../core/graph";
|
import {type NodeAddressT, NodeAddress} from "../../core/graph";
|
||||||
import {type NodeWeight} from "../../core/weights";
|
import {type NodeWeight} from "../../core/weights";
|
||||||
|
import {type TimestampMs} from "../../util/timestamp";
|
||||||
import {initiativeNodeType} from "./declaration";
|
import {initiativeNodeType} from "./declaration";
|
||||||
|
|
||||||
// Composite ID, used as input for NodeAddressT.
|
// Composite ID, used as input for NodeAddressT.
|
||||||
@ -44,7 +45,7 @@ export type InitiativeWeight = {|
|
|||||||
export type Initiative = {|
|
export type Initiative = {|
|
||||||
+id: InitiativeId,
|
+id: InitiativeId,
|
||||||
+title: string,
|
+title: string,
|
||||||
+timestampMs: number,
|
+timestampMs: TimestampMs,
|
||||||
+weight?: InitiativeWeight,
|
+weight?: InitiativeWeight,
|
||||||
+completed: boolean,
|
+completed: boolean,
|
||||||
+dependencies: $ReadOnlyArray<URL>,
|
+dependencies: $ReadOnlyArray<URL>,
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
import {type URL} from "../../core/references";
|
import {type URL} from "../../core/references";
|
||||||
|
import {type TimestampISO} from "../../util/timestamp";
|
||||||
import {type NodeAddressT, NodeAddress} from "../../core/graph";
|
import {type NodeAddressT, NodeAddress} from "../../core/graph";
|
||||||
import {type Compatible, fromCompat, toCompat} from "../../util/compat";
|
import {type Compatible, fromCompat, toCompat} from "../../util/compat";
|
||||||
import {initiativeNodeType} from "./declaration";
|
import {initiativeNodeType} from "./declaration";
|
||||||
@ -17,7 +18,7 @@ export const INITIATIVE_FILE_SUBTYPE = "INITIATIVE_FILE";
|
|||||||
*/
|
*/
|
||||||
export type InitiativeFile = {|
|
export type InitiativeFile = {|
|
||||||
+title: string,
|
+title: string,
|
||||||
+timestampIso: ISOTimestamp,
|
+timestampIso: TimestampISO,
|
||||||
+weight: InitiativeWeight,
|
+weight: InitiativeWeight,
|
||||||
+completed: boolean,
|
+completed: boolean,
|
||||||
+dependencies: $ReadOnlyArray<URL>,
|
+dependencies: $ReadOnlyArray<URL>,
|
||||||
@ -26,9 +27,6 @@ export type InitiativeFile = {|
|
|||||||
+champions: $ReadOnlyArray<URL>,
|
+champions: $ReadOnlyArray<URL>,
|
||||||
|};
|
|};
|
||||||
|
|
||||||
// Note: setting this to opaque forces us to convert it to timestampMs.
|
|
||||||
opaque type ISOTimestamp: string = string;
|
|
||||||
|
|
||||||
const COMPAT_INFO = {type: "sourcecred/initiativeFile", version: "0.1.0"};
|
const COMPAT_INFO = {type: "sourcecred/initiativeFile", version: "0.1.0"};
|
||||||
|
|
||||||
export function fromJSON(j: Compatible<any>): InitiativeFile {
|
export function fromJSON(j: Compatible<any>): InitiativeFile {
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
// @flow
|
// @flow
|
||||||
|
|
||||||
import {NodeAddress} from "../../core/graph";
|
import {NodeAddress} from "../../core/graph";
|
||||||
|
import * as Timestamp from "../../util/timestamp";
|
||||||
import {createId, addressFromId} from "./initiative";
|
import {createId, addressFromId} from "./initiative";
|
||||||
import {type InitiativesDirectory} from "./initiativesDirectory";
|
import {type InitiativesDirectory} from "./initiativesDirectory";
|
||||||
import {
|
import {
|
||||||
@ -13,7 +14,7 @@ import {
|
|||||||
|
|
||||||
const exampleInitiativeFile = (): InitiativeFile => ({
|
const exampleInitiativeFile = (): InitiativeFile => ({
|
||||||
title: "Sample initiative",
|
title: "Sample initiative",
|
||||||
timestampIso: ("2020-01-08T22:01:57.766Z": any),
|
timestampIso: Timestamp.toISO(Date.parse("2020-01-08T22:01:57.766Z")),
|
||||||
weight: {incomplete: 360, complete: 420},
|
weight: {incomplete: 360, complete: 420},
|
||||||
completed: false,
|
completed: false,
|
||||||
champions: ["http://foo.bar/champ"],
|
champions: ["http://foo.bar/champ"],
|
||||||
|
@ -5,6 +5,7 @@ import fs from "fs-extra";
|
|||||||
import globby from "globby";
|
import globby from "globby";
|
||||||
import {type URL} from "../../core/references";
|
import {type URL} from "../../core/references";
|
||||||
import {type NodeAddressT} from "../../core/graph";
|
import {type NodeAddressT} from "../../core/graph";
|
||||||
|
import * as Timestamp from "../../util/timestamp";
|
||||||
import {compatReader} from "../../backend/compatIO";
|
import {compatReader} from "../../backend/compatIO";
|
||||||
import {
|
import {
|
||||||
type ReferenceDetector,
|
type ReferenceDetector,
|
||||||
@ -148,10 +149,11 @@ export function _convertToInitiatives(
|
|||||||
const initiatives = [];
|
const initiatives = [];
|
||||||
for (const [fileName, initiativeFile] of map.entries()) {
|
for (const [fileName, initiativeFile] of map.entries()) {
|
||||||
const {timestampIso, ...partialInitiativeFile} = initiativeFile;
|
const {timestampIso, ...partialInitiativeFile} = initiativeFile;
|
||||||
|
const timestampMs = Timestamp.fromISO(timestampIso);
|
||||||
const initiative: Initiative = {
|
const initiative: Initiative = {
|
||||||
...partialInitiativeFile,
|
...partialInitiativeFile,
|
||||||
id: initiativeFileId(directory, fileName),
|
id: initiativeFileId(directory, fileName),
|
||||||
timestampMs: Date.parse(timestampIso),
|
timestampMs,
|
||||||
};
|
};
|
||||||
initiatives.push(initiative);
|
initiatives.push(initiative);
|
||||||
}
|
}
|
||||||
|
@ -5,6 +5,7 @@ import path from "path";
|
|||||||
import fs from "fs-extra";
|
import fs from "fs-extra";
|
||||||
import stringify from "json-stable-stringify";
|
import stringify from "json-stable-stringify";
|
||||||
import {MappedReferenceDetector} from "../../core/references";
|
import {MappedReferenceDetector} from "../../core/references";
|
||||||
|
import * as Timestamp from "../../util/timestamp";
|
||||||
import {type Initiative, createId, addressFromId} from "./initiative";
|
import {type Initiative, createId, addressFromId} from "./initiative";
|
||||||
import {
|
import {
|
||||||
type InitiativesDirectory,
|
type InitiativesDirectory,
|
||||||
@ -20,7 +21,7 @@ import {type InitiativeFile} from "./initiativeFile";
|
|||||||
|
|
||||||
const exampleInitiativeFile = (): InitiativeFile => ({
|
const exampleInitiativeFile = (): InitiativeFile => ({
|
||||||
title: "Sample initiative",
|
title: "Sample initiative",
|
||||||
timestampIso: ("2020-01-08T22:01:57.766Z": any),
|
timestampIso: Timestamp.toISO(Date.parse("2020-01-08T22:01:57.766Z")),
|
||||||
weight: {incomplete: 360, complete: 420},
|
weight: {incomplete: 360, complete: 420},
|
||||||
completed: false,
|
completed: false,
|
||||||
champions: ["http://foo.bar/champ"],
|
champions: ["http://foo.bar/champ"],
|
||||||
@ -31,10 +32,11 @@ const exampleInitiativeFile = (): InitiativeFile => ({
|
|||||||
|
|
||||||
const exampleInitiative = (remoteUrl: string, fileName: string): Initiative => {
|
const exampleInitiative = (remoteUrl: string, fileName: string): Initiative => {
|
||||||
const {timestampIso, ...partialInitiativeFile} = exampleInitiativeFile();
|
const {timestampIso, ...partialInitiativeFile} = exampleInitiativeFile();
|
||||||
|
const timestampMs = Timestamp.fromISO(timestampIso);
|
||||||
return {
|
return {
|
||||||
...partialInitiativeFile,
|
...partialInitiativeFile,
|
||||||
id: createId("INITIATIVE_FILE", remoteUrl, fileName),
|
id: createId("INITIATIVE_FILE", remoteUrl, fileName),
|
||||||
timestampMs: Date.parse((timestampIso: any)),
|
timestampMs,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user