Skip to content

Commit 712adc4

Browse files
committed
- added lock command processing
1 parent 6afce1c commit 712adc4

File tree

1 file changed

+85
-26
lines changed

1 file changed

+85
-26
lines changed

index.js

Lines changed: 85 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,33 @@ const getDeletePayloads = async (context, { owner, repo, pullNumber, sha }) => {
264264
}, Promise.resolve([]));
265265
}
266266

267+
const getLockPayloads = async (context, { owner, repo, pullNumber, sha }) => {
268+
const config = getConfig(owner);
269+
const domain = config.domain;
270+
// find deploy by repo
271+
const deploy = config.deploy.find((d) => d.name === repo);
272+
if (!deploy) {
273+
return [];
274+
}
275+
276+
const charts = deploy.components
277+
.map(({ name }) => ({ name }));
278+
279+
return charts.reduce(async (acc, { name }) => {
280+
const val = await acc;
281+
const description = `Lock ${name} for ${repo}/pull/${pullNumber}`;
282+
const environment = `${repo.replace('.', '-')}-pull-${pullNumber}`;
283+
return [...val, {
284+
repo,
285+
component: name.replace('.', '-'),
286+
description,
287+
environment,
288+
domain: `${environment}.${domain}`,
289+
action: 'lock',
290+
}];
291+
}, Promise.resolve([]));
292+
}
293+
267294
const createDeployments = async (app, context, owner, payloads) => {
268295
await bluebird.mapSeries(payloads, async ({ repo, component, chart, version, gitVersion, environment, description, domain, action, values, addon = false }) => {
269296
app.log.info({ repo, component, chart, version, gitVersion, environment, description, domain, action, values });
@@ -330,6 +357,28 @@ const deleteDeployments = async (app, context, owner, payloads) => {
330357
// );
331358
};
332359

360+
const lockDeployments = async (app, context, owner, payloads) => {
361+
await bluebird.mapSeries(payloads, async ({ repo, component, environment, description, domain, action }) => {
362+
app.log.info({ repo, component, environment, description, domain, action });
363+
const res = await context.octokit.repos.createDeployment({
364+
owner: owner,
365+
repo: 'charts',
366+
ref: 'master', // The ref to deploy. This can be a branch, tag, or SHA.
367+
task: 'lock', // Specifies a task to execute (e.g., deploy or deploy:migrations).
368+
auto_merge: false, // Attempts to automatically merge the default branch into the requested ref, if it is behind the default branch.
369+
required_contexts: [], // The status contexts to verify against commit status checks. If this parameter is omitted, then all unique contexts will be verified before a deployment is created. To bypass checking entirely pass an empty array. Defaults to all unique contexts.
370+
payload: {
371+
repo, component, domain, action, environment,
372+
}, // JSON payload with extra information about the deployment. Default: ""
373+
environment, // Name for the target deployment environment (e.g., production, staging, qa)
374+
description, // Short description of the deployment
375+
transient_environment: true, // Specifies if the given environment is specific to the deployment and will no longer exist at some point in the future.
376+
production_environment: false, // Specifies if the given environment is one that end-users directly interact with.
377+
});
378+
app.log.info(`Created lock deployment #${res.data.id} for pull request ${environment}`);
379+
});
380+
};
381+
333382
/**
334383
* This is the main entrypoint to your Probot app
335384
* @param {import('probot').Probot} app
@@ -339,49 +388,59 @@ module.exports = (app) => {
339388
app.on(
340389
"issue_comment.created",
341390
async (context) => {
342-
const commandRegex = /^[\\\|\/\#]deploy([^$]*)$/;
343391
const {
344392
comment: { body: comment },
345393
repository: { owner: { login: owner }, name: repo },
346394
} = context.payload;
347-
let matched;
348-
if (
349-
!comment ||
350-
!(matched = comment.toLowerCase().match(commandRegex))
351-
) {
352-
app.log.info(`Missing comment body or comment doesn't start with /deploy message, body: ${comment}`);
395+
if (!comment) {
396+
app.log.info(`Missing comment body`);
353397
return;
354398
}
399+
355400
app.log.info('issue_comment.created');
356401
app.log.info(context.payload);
357402

358403
const pullNumber = context.payload.issue.html_url.indexOf('/pull/')
359404
? context.payload.issue.number : null;
360405

361-
await sync(app, context, owner);
362-
363406
if (!pullNumber) {
364-
app.log.info('Cannot find pull request. Deploy dismissed.');
407+
app.log.info('Cannot find pull request. Command dismissed.');
365408
return;
366409
}
367410

368-
const components = matched[1]
369-
.split(/\s|\n/)
370-
.filter(Boolean)
371-
.map((component) => {
372-
const parts = component.split(':');
373-
return {
374-
component: parts[0],
375-
version: parts[1],
376-
};
377-
});
411+
// lock flow
412+
const commandLockRegex = /^[\\\|\/\#]lock([^$]*)$/;
413+
if ((comment.toLowerCase().match(commandLockRegex))) {
414+
const payloads = await getLockPayloads(
415+
context, { owner, repo, pullNumber }
416+
);
417+
await lockDeployments(app, context, owner, payloads);
418+
return;
419+
}
378420

379-
app.log.info('extracted comment components');
380-
app.log.info(JSON.stringify(components));
381-
const payloads = await getDeployPayloads(
382-
context, { owner, repo, pullNumber }, components, 'comment', app.log,
383-
);
384-
await createDeployments(app, context, owner, payloads);
421+
// deploy flow
422+
let matched;
423+
const commandDeployRegex = /^[\\\|\/\#]deploy([^$]*)$/;
424+
if ((matched = comment.toLowerCase().match(commandDeployRegex))) {
425+
await sync(app, context, owner);
426+
const components = matched[1]
427+
.split(/\s|\n/)
428+
.filter(Boolean)
429+
.map((component) => {
430+
const parts = component.split(':');
431+
return {
432+
component: parts[0],
433+
version: parts[1],
434+
};
435+
});
436+
437+
app.log.info('extracted comment components');
438+
app.log.info(JSON.stringify(components));
439+
const payloads = await getDeployPayloads(
440+
context, { owner, repo, pullNumber }, components, 'comment', app.log,
441+
);
442+
await createDeployments(app, context, owner, payloads);
443+
}
385444
},
386445
);
387446
app.on(

0 commit comments

Comments
 (0)