1 | <?php |
||
10 | class Writing_On_GitHub_Payload { |
||
11 | |||
12 | /** |
||
13 | * Application container. |
||
14 | * |
||
15 | * @var Writing_On_GitHub |
||
16 | */ |
||
17 | protected $app; |
||
18 | |||
19 | /** |
||
20 | * Payload data. |
||
21 | * |
||
22 | * @var stdClass |
||
23 | */ |
||
24 | protected $data; |
||
25 | |||
26 | /** |
||
27 | * Writing_On_GitHub_Payload constructor. |
||
28 | * |
||
29 | * @param Writing_On_GitHub $app Application container. |
||
30 | * @param string $raw_data Raw request data. |
||
31 | */ |
||
32 | public function __construct( Writing_On_GitHub $app, $raw_data ) { |
||
33 | $this->app = $app; |
||
34 | $this->data = json_decode( $raw_data ); |
||
35 | } |
||
36 | |||
37 | /** |
||
38 | * Returns whether payload should be imported. |
||
39 | * |
||
40 | * @return true|WP_Error |
||
41 | */ |
||
42 | public function should_import() { |
||
43 | // @todo how do we get this without importing the whole api object just for this? |
||
44 | if ( strtolower( $this->data->repository->full_name ) !== strtolower( $this->app->api()->fetch()->repository() ) ) { |
||
45 | return new WP_Error( |
||
46 | 'incorrect_repository', |
||
47 | sprintf( 'Incorrect repository, %s -> %s .', |
||
48 | $this->data->repository->full_name, |
||
49 | $this->app->api()->fetch()->repository() |
||
50 | ) |
||
51 | ); |
||
52 | } |
||
53 | |||
54 | // The last term in the ref is the payload_branch name. |
||
55 | $refs = explode( '/', $this->data->ref ); |
||
56 | $payload_branch = array_pop( $refs ); |
||
57 | |||
58 | $branch = $this->app->api()->fetch()->branch(); |
||
59 | |||
60 | if ( $branch !== $payload_branch ) { |
||
61 | return new WP_Error( |
||
62 | 'incorrect_branch', |
||
63 | sprintf( 'Incorrect branch, %s -> %s .', |
||
64 | $payload_branch, |
||
65 | $branch |
||
66 | ) |
||
67 | ); |
||
68 | } |
||
69 | |||
70 | // We add a tag to commits we push out, so we shouldn't pull them in again. |
||
71 | $tag = apply_filters( 'wogh_commit_msg_tag', 'wogh' ); |
||
72 | |||
73 | if ( ! $tag ) { |
||
74 | throw new Exception( __( 'Commit message tag not set. Filter `wogh_commit_msg_tag` misconfigured.', 'writing-on-github' ) ); |
||
75 | } |
||
76 | |||
77 | if ( $tag === substr( $this->message(), -1 * strlen( $tag ) ) ) { |
||
78 | return new WP_Error( |
||
79 | 'skip_import', |
||
80 | 'Skip import on auto export post.' |
||
81 | ); |
||
82 | } |
||
83 | |||
84 | if ( ! $this->get_commit_id() ) { |
||
85 | return new WP_Error( |
||
86 | 'invalid_payload', |
||
87 | "[Missing Commit ID] won't be imported." |
||
88 | ); |
||
89 | } |
||
90 | |||
91 | return true; |
||
92 | } |
||
93 | |||
94 | public function get_before_commit_id() { |
||
95 | return $this->data->before ? $this->data->before : null; |
||
96 | } |
||
97 | |||
98 | /** |
||
99 | * Returns the sha of the head commit. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | public function get_commit_id() { |
||
104 | return $this->data->head_commit ? $this->data->head_commit->id : null; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Returns the email address for the commit author. |
||
109 | * |
||
110 | * @return string |
||
111 | */ |
||
112 | public function get_author_email() { |
||
113 | return $this->data->head_commit->author->email; |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * Returns array commits for the payload. |
||
118 | * |
||
119 | * @return array |
||
120 | */ |
||
121 | public function get_commits() { |
||
122 | return $this->data->commits; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Returns the repository's full name. |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | public function get_repository_name() { |
||
132 | } |
||
133 | |||
134 | /** |
||
135 | * Returns the payload's commit message. |
||
136 | * |
||
137 | * @return string |
||
138 | */ |
||
139 | protected function message() { |
||
141 | } |
||
142 | } |
||
143 |