Page Menu
Home
DevCentral
Search
Configure Global Search
Log In
Files
F3715604
D2680.diff
No One
Temporary
Actions
View File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Flag For Later
Size
4 KB
Referenced Files
None
Subscribers
None
D2680.diff
View Options
diff --git a/app/Analyzers/GitHub/Events/DefaultBranchEvent.php b/app/Analyzers/GitHub/Events/DefaultBranchEvent.php
new file mode 100644
--- /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
--- a/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
+++ b/app/Analyzers/GitHub/GitHubPayloadAnalyzer.php
@@ -48,7 +48,7 @@
) {
parent::__construct($project, $payload);
- $this->event = $event;
+ $this->event = self::getEvent($event, $payload);
try {
$this->analyzerEvent = Event::forPayload($event, $payload);
@@ -57,6 +57,18 @@
}
}
+ 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
///
diff --git a/resources/lang/en/GitHub.php b/resources/lang/en/GitHub.php
--- a/resources/lang/en/GitHub.php
+++ b/resources/lang/en/GitHub.php
@@ -27,6 +27,8 @@
'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',
diff --git a/tests/Analyzers/GitHub/Events/DefaultBranchEventTest.php b/tests/Analyzers/GitHub/Events/DefaultBranchEventTest.php
new file mode 100644
--- /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
Details
Attached
Mime Type
text/plain
Expires
Wed, Nov 6, 08:15 (11 h, 1 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
2231334
Default Alt Text
D2680.diff (4 KB)
Attached To
Mode
D2680: Detect default branch change in repository GitHub event
Attached
Detach File
Event Timeline
Log In to Comment