Skip to content

Commit

Permalink
feat(alert): forward contrib alert (#174)
Browse files Browse the repository at this point in the history
* feat(alert): post contribution alert to contribution-api

* fix: post data

* fix: send diff payload

* fix: test
  • Loading branch information
lionelB committed Nov 23, 2020
1 parent 1d98dfc commit 9c3aa88
Show file tree
Hide file tree
Showing 7 changed files with 126 additions and 24 deletions.
5 changes: 4 additions & 1 deletion targets/alert-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
"@shared/graphql-client": "1.0.0",
"@socialgouv/cdtn-slugify": "^4.35.0",
"@socialgouv/cdtn-sources": "^4.35.0",
"@types/node-fetch": "^2.5.7",
"node-fetch": "^2.6.1",
"nodegit": "0.27.0",
"semver": "^7.3.2",
"unist-util-parents": "^1.0.3",
Expand All @@ -15,11 +17,12 @@
"@babel/preset-env": "^7.12.1",
"@shared/types": "1.0.0",
"@socialgouv/contributions-data-types": "^3.0.0",
"@socialgouv/datafiller-data-types": "^2.6.0",
"@socialgouv/eslint-config-recommended": "^1.46.0",
"@socialgouv/fiches-travail-data-types": "^4.6.0",
"@socialgouv/datafiller-data-types": "^2.6.0",
"@types/jest": "^26.0.15",
"@types/node": "^14.14.6",
"@types/node-fetch": "^2.5.7",
"@types/nodegit": "^0.26.12",
"@types/semver": "^7.3.4",
"@types/unist": "^2.0.3",
Expand Down
36 changes: 20 additions & 16 deletions targets/alert-cli/src/__test__/relevantContent.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,16 @@ describe("getRelevantContent", () => {
source: "contributions",
title: "question1",
},
reference: {
category: "agreement",
dila_cid: "c123",
dila_container_id: "kalicont123",
dila_id: "125",
title: "accord c123",
url: "url/c123",
},
references: [
{
category: "agreement",
dila_cid: "c123",
dila_container_id: "kalicont123",
dila_id: "125",
title: "accord c123",
url: "url/c123",
},
],
},
];
expect(await getRelevantDocuments(changes)).toEqual(expected);
Expand All @@ -103,14 +105,16 @@ describe("getRelevantContent", () => {
source: "contributions",
title: "question1",
},
reference: {
category: "agreement",
dila_cid: "3",
dila_container_id: "kalicont42",
dila_id: "3",
title: "Accord du 3 novembre",
url: "legifrance.url/kalicont42",
},
references: [
{
category: "agreement",
dila_cid: "3",
dila_container_id: "kalicont42",
dila_id: "3",
title: "Accord du 3 novembre",
url: "legifrance.url/kalicont42",
},
],
},
];
expect(await getRelevantDocuments(changes)).toEqual(expected);
Expand Down
70 changes: 70 additions & 0 deletions targets/alert-cli/src/exportContributionAlerts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import fetch from "node-fetch";

const contribApi =
"https://contributions-api.codedutravail.fabrique.social.gouv.fr/alerts";

/**
*
* @param {alerts.AlertChanges[]} changes
*/
export async function exportContributionAlerts(changes) {
const dilaAlertChanges = /** @type {alerts.DilaAlertChanges[]} */ (changes.filter(
(change) => change.type === "dila"
));
const contributions = dilaAlertChanges.flatMap((alert) => {
const targetedContribs = alert.documents.filter(
(targetDoc) => targetDoc.document.source == "contributions"
);
if (targetedContribs.length === 0) {
return [];
}
const nodes = [...alert.modified, ...alert.removed, ...alert.added];
return targetedContribs.flatMap(({ references, document: contrib }) => {
return references.map((reference) => ({
answer_id: contrib.id,
cid: reference.dila_cid,
id: reference.dila_id,
value: computeDiff(
nodes.find((node) => node.data.cid === reference.dila_cid)
),
version: alert.ref,
}));
});
});
await fetch(contribApi, {
body: JSON.stringify(contributions),
headers: {
"Content-Type": "application/json",
},
method: "POST",
});
}

/**
*
* @param {alerts.DilaNodeForDiff} node
*/
function computeDiff(node) {
const textFieldname =
node.context.containerId === "LEGITEXT000006072050" ? "texte" : "content";
const content = node.data[textFieldname] || "";
const previousContent = node.previous?.data[textFieldname] || "";
const showDiff = content !== previousContent;
const showNotaDiff = node.previous.data.nota !== node.data.nota;
const texts = [];
if (showDiff) {
texts.push({ current: content, previous: previousContent });
}

if (showNotaDiff) {
texts.push({
current: node.data.nota,
previous: node.previous.data.nota,
});
}

return {
etat: { current: node.data.etat, previous: node.previous.data.etat },
texts,
};
}
6 changes: 5 additions & 1 deletion targets/alert-cli/src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ type AstChanges = {
}

type Changes = AstChanges & {
documents: { document: DocumentInfo, reference: ParseDilaReference }[]
documents: { document: DocumentInfo, references: ParseDilaReference[] }[]
}

type DilaAlertChanges = {
Expand Down Expand Up @@ -120,6 +120,10 @@ type DilaNodeWithContext = DilaNode & {
}
}

type DilaNodeForDiff = DilaNodeWithContext & {
previous: DilaNodeWithContext
}

type FicheVddIndex = {
id: string
date: string
Expand Down
8 changes: 6 additions & 2 deletions targets/alert-cli/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ccns } from "./ccn-list.js";
import { compareArticles } from "./compareTree.js";
import { processTravailDataDiff } from "./diff/fiches-travail-data";
import { processVddDiff } from "./diff/fiches-vdd";
import { exportContributionAlerts } from "./exportContributionAlerts";
import { getFicheServicePublicIds } from "./getFicheServicePublicIds";
import { createToJson, getFilename } from "./node-git.helpers";
import { openRepo } from "./openRepo";
Expand Down Expand Up @@ -356,6 +357,9 @@ async function main() {
if (result.changes.length === 0) {
console.log(`no update for ${result.repository}`);
} else {
// forward alert to contributions
exportContributionAlerts(result.changes);

const inserts = await batchPromises(
result.changes,
(diff) => insertAlert(result.repository, diff),
Expand All @@ -380,8 +384,8 @@ async function main() {
);
process.exit(-1);
}
const update = await updateSource(result.repository, result.newRef);
console.log(`update source ${update.repository} to ${update.tag}`);
// const update = await updateSource(result.repository, result.newRef);
// console.log(`update source ${update.repository} to ${update.tag}`);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions targets/alert-cli/src/relevantContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export async function getRelevantDocuments({ added, modified, removed }) {

const references = contribReferences.concat(travailEmploiReferences);
const documents = references.flatMap((item) => {
const reference = item.references.find(
const references = item.references.filter(
(ref) =>
modified.find(
(node) =>
Expand All @@ -25,8 +25,8 @@ export async function getRelevantDocuments({ added, modified, removed }) {
added.find((node) => node.data.cid === ref.dila_cid)
);

if (reference) {
return { document: item.document, reference };
if (references.length) {
return { document: item.document, references };
}
return [];
});
Expand Down
19 changes: 18 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3302,6 +3302,14 @@
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.0.tgz#69a23a3ad29caf0097f06eda59b361ee2f0639f6"
integrity sha1-aaI6OtKcrwCX8G7aWbNh7i8GOfY=

"@types/node-fetch@^2.5.7":
version "2.5.7"
resolved "https://registry.yarnpkg.com/@types/node-fetch/-/node-fetch-2.5.7.tgz#20a2afffa882ab04d44ca786449a276f9f6bbf3c"
integrity sha512-o2WVNf5UhWRkxlf6eq+jMZDu7kjgpgJfl4xVNlvryc95O/6F2ld8ztKX+qu+Rjyet93WAWm5LjeX9H5FGkODvw==
dependencies:
"@types/node" "*"
form-data "^3.0.0"

"@types/node@*", "@types/node@>= 8", "@types/node@^14.14.6":
version "14.14.6"
resolved "https://registry.yarnpkg.com/@types/node/-/node-14.14.6.tgz#146d3da57b3c636cc0d1769396ce1cfa8991147f"
Expand Down Expand Up @@ -5092,7 +5100,7 @@ columnify@^1.5.4:
strip-ansi "^3.0.0"
wcwidth "^1.0.0"

combined-stream@^1.0.6, combined-stream@~1.0.6:
combined-stream@^1.0.6, combined-stream@^1.0.8, combined-stream@~1.0.6:
version "1.0.8"
resolved "https://registry.yarnpkg.com/combined-stream/-/combined-stream-1.0.8.tgz#c3d45a8b34fd730631a110a8a2520682b31d5a7f"
integrity sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==
Expand Down Expand Up @@ -7314,6 +7322,15 @@ forever-agent@~0.6.1:
resolved "https://registry.yarnpkg.com/forever-agent/-/forever-agent-0.6.1.tgz#fbc71f0c41adeb37f96c577ad1ed42d8fdacca91"
integrity sha1-+8cfDEGt6zf5bFd60e1C2P2sypE=

form-data@^3.0.0:
version "3.0.0"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-3.0.0.tgz#31b7e39c85f1355b7139ee0c647cf0de7f83c682"
integrity sha512-CKMFDglpbMi6PyN+brwB9Q/GOw0eAnsrEZDgcsH5Krhz5Od/haKHAX0NmQfha2zPPz0JpWzA7GJHGSnvCRLWsg==
dependencies:
asynckit "^0.4.0"
combined-stream "^1.0.8"
mime-types "^2.1.12"

form-data@~2.3.2:
version "2.3.3"
resolved "https://registry.yarnpkg.com/form-data/-/form-data-2.3.3.tgz#dcce52c05f644f298c6a7ab936bd724ceffbf3a6"
Expand Down

0 comments on commit 9c3aa88

Please sign in to comment.