Commit d53825a1 authored by michael's avatar michael

60: Add webhook event class to represent event from any source

parent f2675cc2
<?php
namespace AppBundle\Model;
class WebhookEvent {
const SOURCE_GITHUB = 'Github';
const SOURCE_GITLAB = 'Gitlab';
/**
* @var string
* Where did the hook original, e.g. github
*/
protected $source = '';
/**
* @var string
* What sort of event is it, for example "push"
*/
protected $type = '';
/**
* @var string
* The repo this event was fired from
*/
protected $repo = '';
/**
* @var string
* The branch the event was fired from
*/
protected $branch = '';
/**
* @var array[]
* An array of commits in the event, each commit will have keys "ID",
* "author" and "message"
*/
protected $commits = [];
/**
* @return string
*/
public function getSource(): string {
return $this->source;
}
/**
* @param string $source
* @return $this
*/
public function setSource(string $source) {
$this->source = $source;
return $this;
}
/**
* @return string
*/
public function getType(): string {
return $this->type;
}
/**
* @param string $type
* @return $this
*/
public function setType(string $type) {
$this->type = $type;
return $this;
}
/**
* @return string
*/
public function getRepo(): string {
return $this->repo;
}
/**
* @param string $repo
* @return $this
*/
public function setRepo(string $repo) {
$this->repo = $repo;
return $this;
}
/**
* @return string
*/
public function getBranch(): string {
return $this->branch;
}
/**
* @param string $branch
* @return $this
*/
public function setBranch(string $branch) {
$this->branch = $branch;
return $this;
}
/**
* @param array $commit
*/
public function addCommit(array $commit) {
$this->commits[] = $commit;
}
/**
* @param array[] $commits
*/
public function setCommits(array $commits) {
$this->commits = $commits;
}
/**
* @return array[]
*/
public function getCommits() {
return $this->commits;
}
/**
* @return array
*/
public function getCommitMessages() {
return array_column($this->getCommits(), 'message');
}
/**
* @return array
* Email address of all those who should be notified about the event
*/
public function getNotificationRecipients() {
$recipients = [];
foreach ($this->getCommits() as $commit) {
$author = $commit->author ?? NULL;
if (property_exists($author, 'email')) {
$recipients[] = $author->email;
}
}
// filter out mails that start with "do not reply"
$recipients = preg_grep('/^(donot|no)reply@/', $recipients, PREG_GREP_INVERT);
return array_unique($recipients);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment