Page MenuHomeDevCentral

No OneTemporary

diff --git a/app/Analyzers/GitHub/Events/DefaultBranchEvent.php b/app/Analyzers/GitHub/Events/DefaultBranchEvent.php
new file mode 100644
index 0000000..d6b5bca
--- /dev/null
+++ b/app/Analyzers/GitHub/Events/DefaultBranchEvent.php
@@ -0,0 +1,41 @@
+<?php
+
+namespace Nasqueron\Notifications\Analyzers\GitHub\Events;
+
+/**
+ * DefaultBranchEvent payload analyzer
+ *
+ * @link https://docs.github.com/en/developers/webhooks-and-events/webhooks/webhook-events-and-payloads#repository
+ */
+class DefaultBranchEvent extends Event {
+
+ /**
+ * Gets description for the payload
+ *
+ * @return string
+ */
+ public function getDescription () : string {
+ $repository = $this->payload->repository->full_name;
+ $old = $this->payload->changes->default_branch->from;
+ $new = $this->payload->repository->default_branch;
+
+ return trans(
+ 'GitHub.EventsDescriptions.DefaultBranchEvent',
+ [
+ 'old' => $old,
+ 'new' => $new,
+ 'repository' => $repository,
+ ]
+ );
+ }
+
+ /**
+ * Gets link for the payload
+ *
+ * @return string
+ */
+ public function getLink () : string {
+ return $this->payload->repository->html_url;
+ }
+
+}
diff --git a/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php b/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
index 30ef854..58a18dd 100644
--- a/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
+++ b/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
@@ -1,116 +1,128 @@
<?php
namespace Nasqueron\Notifications\Analyzers\GitHub;
use Nasqueron\Notifications\Analyzers\BasePayloadAnalyzer;
use Nasqueron\Notifications\Analyzers\GitHub\Events\Event;
use Nasqueron\Notifications\Analyzers\GitHub\Events\UnknownEvent;
class GitHubPayloadAnalyzer extends BasePayloadAnalyzer {
/**
* The name of the service, used to get specific classes and config
*/
const SERVICE_NAME = "GitHub";
///
/// Private members
///
/**
* The GitHub event triggering this request
* @var string
*/
private $event;
/**
* The payload analyzer event
*
* @var \Nasqueron\Notifications\Analyzers\GitHub\Events\Event
*/
private $analyzerEvent;
///
/// Constructor
///
/**
* Creates a new GitHubPayloadAnalyzer instance.
*
* @param string $project
* @param string $event
* @param \stdClass $payload
*/
public function __construct(
string $project,
string $event,
\stdClass $payload
) {
parent::__construct($project, $payload);
- $this->event = $event;
+ $this->event = self::getEvent($event, $payload);
try {
$this->analyzerEvent = Event::forPayload($event, $payload);
} catch (\InvalidArgumentException $ex) {
$this->analyzerEvent = new UnknownEvent($event);
}
}
+ private static function getEvent (string $event, \stdClass $payload) : string {
+ // Some payload uses a specialized event class.
+ if (
+ $event == "repository" && $payload->action == "edited" &&
+ property_exists($payload->changes, "default_branch")
+ ) {
+ return "default_branch";
+ }
+
+ return $event;
+ }
+
///
/// Properties
///
/**
* Gets the name of the item, ie here of the name of the repository.
*
* @var string
*/
public function getItemName () : string {
if ($this->isAdministrativeEvent()) {
return '';
}
return $this->payload->repository->name;
}
///
/// Qualification of the payload
///
/**
* @return bool
*/
public function isAdministrativeEvent () : bool {
$administrativeEvents = [
'membership', // Member added to team
'ping', // Special ping pong event, fired on new hook
'repository', // Repository created
];
return in_array($this->event, $administrativeEvents);
}
///
/// Description of the payload
///
/**
* Gets a short textual description of the event.
*
* @return string
*/
public function getDescription () : string {
return $this->analyzerEvent->getDescription();
}
/**
* Gets a link to view the event on GitHub.
*
* @return string The most relevant URL
*/
public function getLink () : string {
return $this->analyzerEvent->getLink();
}
}
diff --git a/resources/lang/en/GitHub.php b/resources/lang/en/GitHub.php
index 76bcd17..dfb5d29 100644
--- a/resources/lang/en/GitHub.php
+++ b/resources/lang/en/GitHub.php
@@ -1,82 +1,84 @@
<?php
return [
/*
|--------------------------------------------------------------------------
| GitHub notifications messages
|--------------------------------------------------------------------------
|
| The following language lines are used to localize notifications for events
| fired by GitHub
|
*/
'Separator' => ' — ',
'Commits' => [
'Message' => ':committer committed :title',
'Authored' => ' (authored by :author)', // appended to Message
],
'RepoAndBranch' => ':repo (branch :branch)',
'EventsDescriptions' => [
'CommitCommentEvent' => ':author added a comment to :commit: :excerpt',
'CreateEvent' => 'New :type on :repository: :ref',
'CreateEventUnknown' => 'Unknown create reference: :type :ref',
+ 'DefaultBranchEvent' => ":repository default branch is now ':new' instead of ':old'.",
+
'DeleteEvent' => 'Removed :type on :repository: :ref',
'DeleteEventUnknown' => 'Unknown delete reference: :type :ref',
'ForkEvent' => ':repo_base has been forked to :repo_fork',
'IssueCommentEventPerAction' => [
'created' => ":author added a comment to issue #:issueNumber — :issueTitle: :excerpt",
'edited' => ":author edited a comment to issue #:issueNumber — :issueTitle: :excerpt",
'deleted' => ":author deleted a comment to issue #:issueNumber — :issueTitle",
],
'IssueCommentEventUnknown' => 'Unknown issue comment action: :action',
'PingEvent' => '« :zen » — GitHub Webhooks ping zen aphorism.',
'PullRequestEventPerAction' => [
'assigned' => ':author has assigned the pull request #:number — :title to :assignee',
'unassigned' => ':author has edited the assignees from the pull request #:number — :title',
'labeled' => ':author has labeled the pull request #:number — :title',
'unlabeled' => ':author has removed a label from the pull request #:number — :title',
'opened' => ':author has opened a pull request: #:number — :title',
'edited' => ':author has edited the pull request #:number — :title',
'closed' => ':author has closed the pull request #:number — :title',
'reopened' => ':author has reopened the pull request #:number — :title',
],
'PullRequestEventUnknown' => 'Unknown pull request action: :action',
'PushEvent' => [
'0' => ':user forcely updated :repoAndBranch',
'n' => ':user pushed :count commits to :repoAndBranch', // n > 1
],
'RepositoryEventPerAction' => [
'created' => 'New repository :repository',
'deleted' => "Repository :repository deleted (danger zone)",
'publicized' => "Repository :repository is now public",
'privatized' => "Repository :repository is now private",
],
'RepositoryEventFork' => ' (fork)',
'RepositoryEventUnknown' => 'Unknown repository action: :action',
'StatusEvent' => 'Status of :commit: :status',
'WatchEvent' => ':user starred :repository',
],
'StatusEventState' => [
'pending' => 'pending',
'success' => 'success',
'failure' => 'failure',
'error' => 'error',
],
];
diff --git a/tests/Analyzers/GitHub/Events/DefaultBranchEventTest.php b/tests/Analyzers/GitHub/Events/DefaultBranchEventTest.php
new file mode 100644
index 0000000..827553c
--- /dev/null
+++ b/tests/Analyzers/GitHub/Events/DefaultBranchEventTest.php
@@ -0,0 +1,45 @@
+<?php
+
+namespace Nasqueron\Notifications\Tests\Analyzers\GitHub\Events;
+
+use Nasqueron\Notifications\Analyzers\GitHub\Events\DefaultBranchEvent;
+use Nasqueron\Notifications\Tests\TestCase;
+
+class DefaultBranchEventTest extends TestCase {
+
+ /**
+ * @var DefaultBranchEvent
+ */
+ private $event;
+
+ public function setUp () : void {
+ $payload = new \stdClass;
+ $payload->repository = new \stdClass;
+ $payload->repository->full_name = 'baxterthehacker/public-repo';
+ $payload->repository->html_url = 'https://github.com/baxterthehacker/public-repo';
+ $payload->repository->default_branch = 'main';
+ $payload->action = 'edited';
+ $payload->changes = new \stdClass;
+ $payload->changes->default_branch = new \stdClass;
+ $payload->changes->default_branch->from = 'master';
+
+ $this->event = new DefaultBranchEvent($payload);
+
+ parent::setUp();
+ }
+
+ public function testGetDescription () {
+ $this->assertSame(
+ "baxterthehacker/public-repo default branch is now 'main' instead of 'master'.",
+ $this->event->getDescription()
+ );
+ }
+
+ public function testGetLink () {
+ $this->assertSame(
+ "https://github.com/baxterthehacker/public-repo",
+ $this->event->getLink()
+ );
+ }
+
+}

File Metadata

Mime Type
text/x-diff
Expires
Sun, Nov 24, 21:48 (7 h, 41 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2258975
Default Alt Text
(9 KB)

Event Timeline