WebhookEvent.php 2.68 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
<?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 = '';

  /**
35 36
   * @var \stdClass[]
   *   An array of commits in the event, each commit will have properties "ID",
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
   *   "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;
  }

  /**
117
   * @param \stdClass[] $commits
118 119 120 121 122 123
   */
  public function setCommits(array $commits) {
    $this->commits = $commits;
  }

  /**
124
   * @return \stdClass[]
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
   */
  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);
  }
}