1<?php
2
3// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the GNU Affero General Public License v3.0.
4// See the LICENCE file in the repository root for full licence text.
5
6namespace App\Libraries;
7
8use App\Models\Build;
9use App\Models\ChangelogEntry;
10
11class GithubImporter
12{
13 public $eventType;
14 public $data;
15 public $repository;
16
17 public function __construct($params)
18 {
19 $this->data = $params['data'];
20 $this->eventType = $params['eventType'];
21 $this->repository = $this->data['repository']['name'];
22 }
23
24 public function import()
25 {
26 if ($this->repository === OsuWiki::repository() && $this->isPushTo(OsuWiki::branch())) {
27 OsuWiki::updateFromGithub($this->data);
28 } elseif ($this->isMergedPullRequest()) {
29 return ChangelogEntry::importFromGithub($this->data);
30 } elseif ($this->isNewRelease()) {
31 return Build::importFromGithubNewRelease($this->data);
32 }
33 }
34
35 public function isMergedPullRequest()
36 {
37 return $this->eventType === 'pull_request' &&
38 $this->data['action'] === 'closed' &&
39 $this->data['pull_request']['merged'];
40 }
41
42 public function isNewRelease()
43 {
44 return $this->eventType === 'release' && $this->data['action'] === 'published';
45 }
46
47 public function isPushTo(string $branch): bool
48 {
49 return $this->eventType === 'push' &&
50 $this->data['ref'] === 'refs/heads/'.$branch;
51 }
52}