Skip to content
This repository has been archived by the owner on Aug 5, 2024. It is now read-only.

Commit

Permalink
Simplifie la V0 d'intégration mattermost pour la transparence budgétaire
Browse files Browse the repository at this point in the history
  • Loading branch information
guillett committed Jul 12, 2022
1 parent 8ba177f commit 7dce332
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { buildMattermostConfig } from "./lib/mattermost/config"

const isSecure = (process.env.SECURE || 'true') === 'true';


const userStatusOptions = [
{ label: 'Indépendant', value: 'independent' },
{
Expand Down
47 changes: 25 additions & 22 deletions src/controllers/budgetController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ function buildBudgetURL(startup, budget) {
if (budget.startsWith('http') || parseInt(budget) === NaN) {
return budget
} else {
const startDts = startup?.phases?.map(p => p?.start).filter(p => p) || []
const startDts = startup.attributes?.phases?.map(p => p?.start).filter(p => p) || []
startDts.sort()

const url = new URL('https://beta-gouv-fr-budget.netlify.app')
Expand All @@ -73,7 +73,7 @@ export async function determineMetadata(req, res, next) {
})
}

const [subcommand, startupId, budget] = req.body?.text.split(" ")?.filter(e => e.length) || []
const [subcommand, startupId, details] = req.body?.text.split(" ")?.filter(e => e.length) || []
const startups = await betagouv.startupsInfos()
const startup = startups.find(s => s.id == startupId)
if (!startup) {
Expand All @@ -82,32 +82,35 @@ export async function determineMetadata(req, res, next) {
})
}
req.startup = startup
req.budget_url = buildBudgetURL(startup, budget)

return res.json({
text: `${req.body?.user_name} vient de demander la génération d'une page budget :smiley: (avec la commande \`${req.body?.command} ${req.body?.text}\` :tada:).
console.log(subcommand)
if (subcommand === "page") {
req.budget_url = buildBudgetURL(startup, details)
return res.json({
text: `${req.body?.user_name} vient de demander la génération d'une page budget :smiley: (avec la commande \`${req.body?.command} ${req.body?.text}\` :tada:).
La voilà :
${req.budget_url}
Si vous avez des questions ou des problèmes, n'hésitez pas à rejoindre le canal [~domaine-transparence-budget](https://mattermost.incubateur.net/betagouv/channels/domaine-transparence-budget) :smiley:
`,
response_type: 'in_channel',
attachments: [{
actions: [{
"id": `${req.body.trigger_id}-publish`,
"name": "Publier cette première version",
"integration": {
"url": "/notifications/budget/mattermost",
"context": {
"token": req.body.token,
"startup": startupId,
"budget_url": req.budget_url,
response_type: 'in_channel',
attachments: [{
actions: [{
"id": `${req.body.trigger_id}-publish`,
"name": "Publier cette première version",
"integration": {
"url": "/notifications/budget/mattermost",
"context": {
"token": req.body.token,
"startup": startupId,
"budget_url": req.budget_url,
}
}
}
}]
}]
}]
})

})
}
req.budget_url = details
req.channel_url = `${mattermost.servers[req.mattermostServerId]}/${req.body?.team_domain}/channels/${req.body?.channel_name}`
next()
}

async function addStartupProps(startup, props, res) {
Expand Down Expand Up @@ -139,7 +142,7 @@ async function addStartupProps(startup, props, res) {
export async function createPullRequest(req, res, next) {
const data = {
budget_url: req.budget_url,
channel_url: req.channel_url,
//channel_url: req.channel_url,
}
const result = await addStartupProps(req.startup, data, res)
return res.json({text: "DONE", response_type: "in_channel", ...result})
Expand Down
14 changes: 4 additions & 10 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ app.use(session({ cookie: { maxAge: 300000, sameSite: 'lax' } })); // Only used
app.use(flash());
app.use(expressSanitizer());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const getJwtTokenForUser = (id) =>
jwt.sign({ id }, config.secret, { expiresIn: '7 days' });
Expand All @@ -78,7 +77,7 @@ app.use(
'/signin',
'/marrainage/accept',
'/marrainage/decline',
/notifications\/budget*/,
'/notifications/budget',
'/notifications/github',
'/onboarding',
/onboardingSuccess\/*/,
Expand Down Expand Up @@ -140,14 +139,9 @@ app.post(
'/notifications/github',
githubNotificationController.processNotification
);
app.post('/notifications/budget/mattermost',
budgetController.determineMattermostServer,
budgetController.checkToken,
budgetController.determineMetadata,
budgetController.createPullRequest,
);

app.post('/notifications/budget/site',
/*app.use('/notifications/budget',
bodyParser.json())*/
app.post('/notifications/budget',
budgetController.determineMattermostServer,
budgetController.checkToken,
budgetController.determineMetadata,
Expand Down
3 changes: 1 addition & 2 deletions src/lib/frontmatter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import YAML from "yaml";


export function applyChanges(text, changes) {
const changeKeys = Object.keys(changes)
const updates = {}
Expand Down Expand Up @@ -47,4 +46,4 @@ export function applyChanges(text, changes) {
content,
updates
}
}
}

0 comments on commit 7dce332

Please sign in to comment.