Skip to content

Commit

Permalink
Merge pull request #870 from amazeeio/601-skip-deploy-pr-body-changes
Browse files Browse the repository at this point in the history
Skip PR deployments if only the body has changed
  • Loading branch information
Schnitzel committed Feb 26, 2019
2 parents b347760 + 48713ba commit 8abde13
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,14 @@ async function bitbucketPullRequestUpdated(webhook: WebhookRequestData, project:
body,
} = webhook;

console.log(body);
const meta = {
projectName: project.name,
pullrequestTitle: body.pullrequest.title,
pullrequestNumber: body.pullrequest.id,
pullrequestUrl: body.pullrequest.destination.repository.links.html.href,
repoName: body.repository.full_name,
repoUrl: body.repository.links.html.href,
}

const headBranchName = body.pullrequest.source.branch.name
const headSha = body.pullrequest.source.commit.hash
Expand Down Expand Up @@ -51,7 +58,7 @@ async function bitbucketPullRequestUpdated(webhook: WebhookRequestData, project:
case "UnknownActiveSystem":
// These are not real errors and also they will happen many times. We just log them locally but not throw an error
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:handledButNoTask`, meta,
`*[${project.name}]* PR ${body.object_attributes.id} opened. No remove task created, reason: ${error}`
`*[${project.name}]* PR ${body.object_attributes.id} updated. No deploy task created, reason: ${error}`
)
return;

Expand Down
10 changes: 9 additions & 1 deletion services/webhooks2tasks/src/handlers/githubPullRequestOpened.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ async function githubPullRequestOpened(webhook: WebhookRequestData, project: Pro
const baseBranchName = body.pull_request.base.ref
const baseSha = body.pull_request.base.sha

const meta = {
projectName: project.name,
pullrequestTitle: body.pull_request.title,
pullrequestNumber: body.number,
pullrequestUrl: body.pull_request.html_url,
repoName: body.repository.full_name,
repoUrl: body.repository.html_url,
}

const data: deployData = {
repoUrl: body.repository.html_url,
Expand Down Expand Up @@ -50,7 +58,7 @@ async function githubPullRequestOpened(webhook: WebhookRequestData, project: Pro
case "UnknownActiveSystem":
// These are not real errors and also they will happen many times. We just log them locally but not throw an error
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:handledButNoTask`, meta,
`*[${project.name}]* PR ${body.number} opened. No remove task created, reason: ${error}`
`*[${project.name}]* PR ${body.number} opened. No deploy task created, reason: ${error}`
)
return;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
// @flow

const R = require('ramda');
const { logger } = require('@lagoon/commons/src/local-logging');
const { sendToLagoonLogs } = require('@lagoon/commons/src/logs');
const { createDeployTask } = require('@lagoon/commons/src/tasks');

import type { WebhookRequestData, removeData, ChannelWrapper, Project } from '../types';

const isEditAction = R.propEq('action', 'edited');

const onlyBodyChanges = R.pipe(
R.propOr({}, 'changes'),
R.keys,
R.equals(['body']),
);

const skipRedeploy = R.and(isEditAction, onlyBodyChanges);

async function githubPullRequestSynchronize(webhook: WebhookRequestData, project: Project) {

const {
Expand All @@ -16,9 +27,21 @@ async function githubPullRequestSynchronize(webhook: WebhookRequestData, project
body,
} = webhook;

if (body.action == 'updated' && !('base' in body.changes)) {
// we are only interested in this webhook if the base had been updated, ofter updates like title or description don't need a redeploy
return
const meta = {
projectName: project.name,
pullrequestTitle: body.pull_request.title,
pullrequestNumber: body.number,
pullrequestUrl: body.pull_request.html_url,
repoName: body.repository.full_name,
repoUrl: body.repository.html_url,
}

// Don't trigger deploy if only the PR body was edited.
if (skipRedeploy(body)) {
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:handledButNoTask`, meta,
`*[${project.name}]* PR ${body.number} updated. No deploy task created, reason: Only body changed`
)
return;
}

const headBranchName = body.pull_request.head.ref
Expand Down Expand Up @@ -54,7 +77,7 @@ async function githubPullRequestSynchronize(webhook: WebhookRequestData, project
case "UnknownActiveSystem":
// These are not real errors and also they will happen many times. We just log them locally but not throw an error
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:handledButNoTask`, meta,
`*[${project.name}]* PR ${body.number} opened. No remove task created, reason: ${error}`
`*[${project.name}]* PR ${body.number} opened. No deploy task created, reason: ${error}`
)
return;

Expand Down
11 changes: 10 additions & 1 deletion services/webhooks2tasks/src/handlers/gitlabPullRequestOpened.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ async function gitlabPullRequestOpened(webhook: WebhookRequestData, project: Pro
body,
} = webhook;

const meta = {
projectName: project.name,
pullrequestNumber: body.object_attributes.id,
pullrequestTitle: body.object_attributes.title,
pullrequestUrl: body.object_attributes.url,
repoName: body.object_attributes.target.name,
repoUrl: body.object_attributes.target.web_url,
}

const headBranchName = body.object_attributes.source_branch
const headSha = body.object_attributes.last_commit.id
const baseBranchName = body.object_attributes.target_branch
Expand Down Expand Up @@ -50,7 +59,7 @@ async function gitlabPullRequestOpened(webhook: WebhookRequestData, project: Pro
case "UnknownActiveSystem":
// These are not real errors and also they will happen many times. We just log them locally but not throw an error
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:handledButNoTask`, meta,
`*[${project.name}]* PR ${body.object_attributes.id} opened. No remove task created, reason: ${error}`
`*[${project.name}]* PR ${body.object_attributes.id} opened. No deploy task created, reason: ${error}`
)
return;

Expand Down
11 changes: 10 additions & 1 deletion services/webhooks2tasks/src/handlers/gitlabPullRequestUpdated.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ async function gitlabPullRequestUpdated(webhook: WebhookRequestData, project: Pr
body,
} = webhook;

const meta = {
projectName: project.name,
pullrequestNumber: body.object_attributes.id,
pullrequestTitle: body.object_attributes.title,
pullrequestUrl: body.object_attributes.url,
repoName: body.object_attributes.target.name,
repoUrl: body.object_attributes.target.web_url,
}

const headBranchName = body.object_attributes.source_branch
const headSha = body.object_attributes.last_commit.id
const baseBranchName = body.object_attributes.target_branch
Expand Down Expand Up @@ -51,7 +60,7 @@ async function gitlabPullRequestUpdated(webhook: WebhookRequestData, project: Pr
case "UnknownActiveSystem":
// These are not real errors and also they will happen many times. We just log them locally but not throw an error
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:handledButNoTask`, meta,
`*[${project.name}]* PR ${body.object_attributes.id} opened. No remove task created, reason: ${error}`
`*[${project.name}]* PR ${body.object_attributes.id} updated. No deploy task created, reason: ${error}`
)
return;

Expand Down

0 comments on commit 8abde13

Please sign in to comment.