-
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BitBucket webhook parser and statuses (#210)
* Add BitBucket webhook recordings * Create bitbucket webhook URL * Add Cypress test for bitbucket * Add BitBucket push webhook parser * Setup BitBucket build parser stump * Process BitBucket pipeline builds * Set proper process name and clean up readthedocs status determining * Test succesfull pipeline too * Create bitbucket pull request parser stub * Add BitBucket pull request parser and add documentation * Bump CIMonitor version to 4.14.0
- Loading branch information
Showing
29 changed files
with
2,202 additions
and
83 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { isOldProcess } from 'backend/status/helper'; | ||
import StatusManager from 'backend/status/manager'; | ||
import { BitBucketBuildState, BitBucketCommitStatusWebhook } from 'types/bitbucket'; | ||
import Status, { Process, State, StepAndStageState } from 'types/status'; | ||
|
||
class BitBucketBuildParser { | ||
parse(id: string, build: BitBucketCommitStatusWebhook): Status { | ||
let status = StatusManager.getStatus(id); | ||
|
||
if (!status) { | ||
status = { | ||
id, | ||
project: `${build.repository.workspace.name} / ${build.repository.name}`, | ||
state: 'info', | ||
source: 'bitbucket', | ||
time: new Date().toUTCString(), | ||
processes: [], | ||
}; | ||
} | ||
|
||
let processes: Process[] = status.processes || []; | ||
|
||
const processId = parseInt(build.commit_status.key); | ||
|
||
if (!processes.find((process) => process.id === processId)) { | ||
if (isOldProcess(status, processId)) { | ||
return null; | ||
} | ||
|
||
processes.push({ | ||
id: processId, | ||
title: build.commit_status.commit.message, | ||
state: 'warning', | ||
stages: [], | ||
time: new Date().toUTCString(), | ||
}); | ||
} | ||
|
||
processes = processes.map((process) => { | ||
if (process.id === processId) { | ||
return this.patchProcess(process, build); | ||
} | ||
|
||
return process; | ||
}); | ||
|
||
const commitUser = build.commit_status.commit.author.user; | ||
return { | ||
...status, | ||
processes, | ||
username: commitUser.display_name, | ||
userUrl: commitUser.links.html.href, | ||
userImage: commitUser.links.avatar.href, | ||
projectImage: build.repository.links.avatar.href, | ||
sourceUrl: build.repository.links.html.href, | ||
time: new Date().toUTCString(), | ||
}; | ||
} | ||
|
||
patchProcess(process: Process, build: BitBucketCommitStatusWebhook): Process { | ||
return { | ||
...process, | ||
stages: [ | ||
{ | ||
id: 'build', | ||
steps: [], | ||
time: new Date().toUTCString(), | ||
state: this.getStageState(build.commit_status.state), | ||
title: build.commit_status.name, | ||
}, | ||
], | ||
state: this.getProcessState(build.commit_status.state), | ||
}; | ||
} | ||
|
||
getProcessState(state: BitBucketBuildState): State { | ||
if (state === 'SUCCESSFUL') { | ||
return 'success'; | ||
} | ||
|
||
if (state === 'FAILED') { | ||
return 'error'; | ||
} | ||
|
||
return 'warning'; | ||
} | ||
|
||
getStageState(state: BitBucketBuildState): StepAndStageState { | ||
if (state === 'SUCCESSFUL') { | ||
return 'success'; | ||
} | ||
|
||
if (state === 'FAILED') { | ||
return 'failed'; | ||
} | ||
|
||
return 'running'; | ||
} | ||
} | ||
|
||
export default new BitBucketBuildParser(); |
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,68 @@ | ||
import Slugify from 'backend/parser/slug'; | ||
import { | ||
BitBucketChangeWrapper, | ||
BitBucketCommitStatusWebhook, | ||
BitBucketPullRequestWebhook, | ||
BitBucketPushWebhook, | ||
BitBucketRepository, | ||
} from 'types/bitbucket'; | ||
import Status from 'types/status'; | ||
|
||
import BitBucketBuildParser from './build'; | ||
import BitBucketPullRequestParser from './pull-request'; | ||
import BitBucketPushParser from './push'; | ||
|
||
class BitBucketParser { | ||
getInternalId(repository: BitBucketRepository, branch: string): string { | ||
return `bitbucket-${Slugify(repository.full_name)}-${Slugify(branch)}`; | ||
} | ||
|
||
parsePush(push: BitBucketPushWebhook): Status { | ||
console.log('[parser/bitbucket] Parsing push...'); | ||
|
||
const relevantChange = push.push.changes.find((changes: BitBucketChangeWrapper) => { | ||
if (changes.new) { | ||
return changes.new.type === 'branch' || changes.new.type === 'tag'; | ||
} | ||
|
||
return false; | ||
}); | ||
|
||
if (!relevantChange) { | ||
console.log('[parser/bitbucket] No relevant change of type branch was found. Stopping.'); | ||
return null; | ||
} | ||
|
||
const id = this.getInternalId(push.repository, relevantChange.new.name); | ||
|
||
return BitBucketPushParser.parse(id, push, relevantChange.new); | ||
} | ||
|
||
parseBuild(build: BitBucketCommitStatusWebhook): Status { | ||
console.log('[parser/bitbucket] Parsing build...'); | ||
|
||
if (build.commit_status.refname === null) { | ||
console.log('[parser/bitbucket] Build could not be linked to a branch. Stopping.'); | ||
return null; | ||
} | ||
|
||
if (parseInt(build.commit_status.key) === 0 || isNaN(parseInt(build.commit_status.key))) { | ||
console.log('[parser/bitbucket] Build has an invalid key, should be a build number. Stopping.'); | ||
return null; | ||
} | ||
|
||
const id = this.getInternalId(build.repository, build.commit_status.refname); | ||
|
||
return BitBucketBuildParser.parse(id, build); | ||
} | ||
|
||
parsePullRequest(pr: BitBucketPullRequestWebhook): Status { | ||
console.log('[parser/bitbucket] Parsing pull request...'); | ||
|
||
const id = this.getInternalId(pr.repository, pr.pullrequest.source.branch.name); | ||
|
||
return BitBucketPullRequestParser.parse(id, pr); | ||
} | ||
} | ||
|
||
export default new BitBucketParser(); |
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,36 @@ | ||
import StatusManager from 'backend/status/manager'; | ||
import { BitBucketPullRequestWebhook } from 'types/bitbucket'; | ||
import Status from 'types/status'; | ||
|
||
class BitBucketPullRequestParser { | ||
parse(id: string, pr: BitBucketPullRequestWebhook): Status { | ||
let status = StatusManager.getStatus(id); | ||
|
||
if (!status) { | ||
status = { | ||
id, | ||
project: `${pr.repository.workspace.name} / ${pr.repository.name}`, | ||
state: 'info', | ||
source: 'bitbucket', | ||
time: new Date().toUTCString(), | ||
processes: [], | ||
}; | ||
} | ||
|
||
const commitUser = pr.actor; | ||
return { | ||
...status, | ||
branch: pr.pullrequest.source.branch.name, | ||
username: commitUser.display_name, | ||
userUrl: commitUser.links.html.href, | ||
userImage: commitUser.links.avatar.href, | ||
mergeTitle: pr.pullrequest.title, | ||
mergeUrl: pr.pullrequest.links.html.href, | ||
projectImage: pr.repository.links.avatar.href, | ||
sourceUrl: pr.repository.links.html.href, | ||
time: new Date().toUTCString(), | ||
}; | ||
} | ||
} | ||
|
||
export default new BitBucketPullRequestParser(); |
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,41 @@ | ||
import StatusManager from 'backend/status/manager'; | ||
import { BitBucketChange, BitBucketPushWebhook } from 'types/bitbucket'; | ||
import Status from 'types/status'; | ||
|
||
class BitBucketPushParser { | ||
parse(id: string, push: BitBucketPushWebhook, change: BitBucketChange): Status { | ||
let status = StatusManager.getStatus(id); | ||
|
||
if (!status) { | ||
status = { | ||
id, | ||
project: `${push.repository.workspace.name} / ${push.repository.name}`, | ||
state: 'info', | ||
source: 'bitbucket', | ||
time: new Date().toUTCString(), | ||
processes: [], | ||
}; | ||
|
||
if (change.type === 'branch') { | ||
status.branch = change.name; | ||
} | ||
|
||
if (change.type === 'tag') { | ||
status.tag = change.name; | ||
} | ||
} | ||
|
||
const commitUser = change.target.author.user; | ||
return { | ||
...status, | ||
username: commitUser.display_name, | ||
userUrl: commitUser.links.html.href, | ||
userImage: commitUser.links.avatar.href, | ||
projectImage: push.repository.links.avatar.href, | ||
sourceUrl: push.repository.links.html.href, | ||
time: new Date().toUTCString(), | ||
}; | ||
} | ||
} | ||
|
||
export default new BitBucketPushParser(); |
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
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
Oops, something went wrong.