-
Notifications
You must be signed in to change notification settings - Fork 150
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Toby Bellwood
authored
Dec 22, 2020
1 parent
4cc622e
commit dbdfd07
Showing
9 changed files
with
490 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
services/webhooks2tasks/src/handlers/giteaBranchDeleted.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { sendToLagoonLogs } from '@lagoon/commons/dist/logs'; | ||
import { createRemoveTask } from '@lagoon/commons/dist/tasks'; | ||
|
||
import { WebhookRequestData, removeData, Project } from '../types'; | ||
|
||
export async function giteaBranchDeleted(webhook: WebhookRequestData, project: Project) { | ||
const { | ||
webhooktype, | ||
event, | ||
giturl, | ||
uuid, | ||
body, | ||
} = webhook; | ||
|
||
const meta: { [key: string]: any } = { | ||
projectName: project.name, | ||
branch: body.ref.replace('refs/heads/',''), | ||
branchName: body.ref.replace('refs/heads/','') | ||
} | ||
|
||
const data: removeData = { | ||
projectName: project.name, | ||
branch: meta.branch, | ||
branchName: meta.branchName, | ||
forceDeleteProductionEnvironment: false, | ||
type: 'branch' | ||
} | ||
|
||
try { | ||
await createRemoveTask(data); | ||
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:delete:handled`, meta, | ||
`*[${project.name}]* \`${meta.branch}\` deleted in <${body.repository.html_url}|${body.repository.full_name}>` | ||
) | ||
return; | ||
} catch (error) { | ||
meta.error = error | ||
switch (error.name) { | ||
case "ProjectNotFound": | ||
case "NoActiveSystemsDefined": | ||
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}]* \`${meta.branch}\` deleted. No remove task created, reason: ${error}` | ||
) | ||
return; | ||
|
||
case "CannotDeleteProductionEnvironment": | ||
// These are not real errors and also they will happen many times. We just log them locally but not throw an error | ||
sendToLagoonLogs('warning', project.name, uuid, `${webhooktype}:${event}:CannotDeleteProductionEnvironment`, meta, | ||
`*[${project.name}]* \`${meta.branch}\` not deleted. ${error}` | ||
) | ||
return; | ||
|
||
default: | ||
// Other messages are real errors and should reschedule the message in RabbitMQ in order to try again | ||
throw error | ||
} | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
services/webhooks2tasks/src/handlers/giteaPullRequestClosed.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { sendToLagoonLogs } from '@lagoon/commons/dist/logs'; | ||
import { createRemoveTask } from '@lagoon/commons/dist/tasks'; | ||
|
||
import { WebhookRequestData, removeData, Project } from '../types'; | ||
|
||
export async function giteaPullRequestClosed(webhook: WebhookRequestData, project: Project) { | ||
|
||
const { | ||
webhooktype, | ||
event, | ||
giturl, | ||
uuid, | ||
body, | ||
user, | ||
sender, | ||
} = webhook; | ||
|
||
const meta: { [key: string]: any } = { | ||
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: removeData = { | ||
projectName: project.name, | ||
pullrequestNumber: body.number, | ||
pullrequestTitle: body.pull_request.title, | ||
type: 'pullrequest' | ||
} | ||
|
||
try { | ||
await createRemoveTask(data); | ||
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:closed:handled`, meta, | ||
`*[${project.name}]* PR <${body.pull_request.html_url}|#${body.number} (${body.pull_request.title})> by <${body.pull_request.user.login}> changed by <${body.sender.login}> closed in <${body.repository.html_url}|${body.repository.full_name}>` | ||
) | ||
return; | ||
} catch (error) { | ||
meta.error = error | ||
switch (error.name) { | ||
case "ProjectNotFound": | ||
case "NoActiveSystemsDefined": | ||
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} closed. No remove task created, reason: ${error}` | ||
) | ||
return; | ||
|
||
case "CannotDeleteProductionEnvironment": | ||
// These are not real errors and also they will happen many times. We just log them locally but not throw an error | ||
sendToLagoonLogs('warning', project.name, uuid, `${webhooktype}:${event}:CannotDeleteProductionEnvironment`, meta, | ||
`*[${project.name}]* \`${meta.branch}\` not deleted. ${error}` | ||
) | ||
return; | ||
|
||
default: | ||
// Other messages are real errors and should reschedule the message in RabbitMQ in order to try again | ||
throw error | ||
} | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
services/webhooks2tasks/src/handlers/giteaPullRequestOpened.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import R from 'ramda'; | ||
import { sendToLagoonLogs } from '@lagoon/commons/dist/logs'; | ||
import { createDeployTask } from '@lagoon/commons/dist/tasks'; | ||
|
||
import { WebhookRequestData, deployData, Project } from '../types'; | ||
|
||
export async function giteaPullRequestOpened(webhook: WebhookRequestData, project: Project) { | ||
|
||
const { | ||
webhooktype, | ||
event, | ||
giturl, | ||
uuid, | ||
body, | ||
} = webhook; | ||
|
||
const headRepoId = body.pull_request.head.repo.id | ||
const headBranchName = body.pull_request.head.ref | ||
const headSha = body.pull_request.head.sha | ||
const baseRepoId = body.pull_request.base.repo.id | ||
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, | ||
} | ||
|
||
// Don't trigger deploy if the head and base repos are different | ||
if (!R.equals(headRepoId, baseRepoId)) { | ||
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:handledButNoTask`, meta, | ||
`*[${project.name}]* PR ${body.number} opened. No deploy task created, reason: Head/Base not same repo` | ||
) | ||
return; | ||
} | ||
|
||
const data: deployData = { | ||
repoUrl: body.repository.html_url, | ||
repoName: body.repository.full_name, | ||
pullrequestTitle: body.pull_request.title, | ||
pullrequestNumber: body.number, | ||
pullrequestUrl: body.pull_request.html_url, | ||
projectName: project.name, | ||
type: 'pullrequest', | ||
headBranchName: headBranchName, | ||
headSha: headSha, | ||
baseBranchName: baseBranchName, | ||
baseSha: baseSha, | ||
branchName: `pr-${body.number}`, | ||
} | ||
|
||
try { | ||
await createDeployTask(data); | ||
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:opened:handled`, data, | ||
`*[${project.name}]* PR <${body.pull_request.html_url}|#${body.number} (${body.pull_request.title})> opened in <${body.repository.html_url}|${body.repository.full_name}>` | ||
) | ||
return; | ||
} catch (error) { | ||
switch (error.name) { | ||
case "ProjectNotFound": | ||
case "NoActiveSystemsDefined": | ||
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 deploy task created, reason: ${error}` | ||
) | ||
return; | ||
|
||
default: | ||
// Other messages are real errors and should reschedule the message in RabbitMQ in order to try again | ||
throw error | ||
} | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
services/webhooks2tasks/src/handlers/giteaPullRequestSynchronize.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import R from 'ramda'; | ||
import { sendToLagoonLogs } from '@lagoon/commons/dist/logs'; | ||
import { createDeployTask } from '@lagoon/commons/dist/tasks'; | ||
|
||
import { WebhookRequestData, deployData, 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); | ||
|
||
export async function giteaPullRequestSynchronize(webhook: WebhookRequestData, project: Project) { | ||
|
||
const { | ||
webhooktype, | ||
event, | ||
giturl, | ||
uuid, | ||
body, | ||
} = webhook; | ||
|
||
const headRepoId = body.pull_request.head.repo.id | ||
const headBranchName = body.pull_request.head.ref | ||
const headSha = body.pull_request.head.sha | ||
const baseRepoId = body.pull_request.base.repo.id | ||
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, | ||
} | ||
|
||
// 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; | ||
} | ||
|
||
// Don't trigger deploy if the head and base repos are different | ||
if (!R.equals(headRepoId, baseRepoId)) { | ||
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:handledButNoTask`, meta, | ||
`*[${project.name}]* PR ${body.number} updated. No deploy task created, reason: Head/Base not same repo` | ||
) | ||
return; | ||
} | ||
|
||
const data: deployData = { | ||
repoName: body.repository.full_name, | ||
repoUrl: body.repository.html_url, | ||
pullrequestUrl: body.pull_request.html_url, | ||
pullrequestTitle: body.pull_request.title, | ||
pullrequestNumber: body.number, | ||
projectName: project.name, | ||
type: 'pullrequest', | ||
headBranchName: headBranchName, | ||
headSha: headSha, | ||
baseBranchName: baseBranchName, | ||
baseSha: baseSha, | ||
branchName: `pr-${body.number}`, | ||
} | ||
|
||
try { | ||
await createDeployTask(data); | ||
sendToLagoonLogs('info', project.name, uuid, `${webhooktype}:${event}:synchronize:handled`, data, | ||
`*[${project.name}]* PR <${body.pull_request.html_url}|#${body.number} (${body.pull_request.title})> updated in <${body.repository.html_url}|${body.repository.full_name}>` | ||
) | ||
return; | ||
} catch (error) { | ||
switch (error.name) { | ||
case "ProjectNotFound": | ||
case "NoActiveSystemsDefined": | ||
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 deploy task created, reason: ${error}` | ||
) | ||
return; | ||
|
||
default: | ||
// Other messages are real errors and should reschedule the message in RabbitMQ in order to try again | ||
throw error | ||
} | ||
} | ||
} |
Oops, something went wrong.