Skip to content

Commit

Permalink
webhook api
Browse files Browse the repository at this point in the history
  • Loading branch information
lxerxa committed Sep 19, 2018
1 parent b5eda42 commit e0163e2
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 6 deletions.
8 changes: 4 additions & 4 deletions app/Http/Controllers/WebhookController.php
Expand Up @@ -35,10 +35,9 @@ public function exec($type, $project_key)
if (!$token || $token !== $_SERVER['HTTP_X_GITLAB_TOKEN'])
{
Response()->json([ 'ecode' => -1, 'emsg' => 'token error.' ]);
return;
}

$payload = str_replace([ "\r\n", "\r", "\n", "\t" ], [ ' ', ' ', ' ', ' ' ], $payload);

$push = new GitLabPush(json_decode($payload, true));
$push->insCommits($project_key);
}
Expand All @@ -53,15 +52,16 @@ public function exec($type, $project_key)
if (!$token)
{
Response()->json([ 'ecode' => -2, 'emsg' => 'token can not empty.' ]);
return;
}

$signature = hash_hmac('sha1', $payload, $token);
$signature = 'sha1=' . hash_hmac('sha1', $payload, $token);
if ($signature !== $_SERVER['HTTP_X_HUB_SIGNATURE'])
{
Response()->json([ 'ecode' => -1, 'emsg' => 'token error.' ]);
return;
}

$payload = str_replace([ "\r\n", "\r", "\n", "\t" ], [ ' ', ' ', ' ', ' ' ], $payload);
$push = new GitHubPush(json_decode($payload, true));
$push->insCommits($project_key);
}
Expand Down
93 changes: 93 additions & 0 deletions app/WebHook/GitHubPush.php
@@ -0,0 +1,93 @@
<?php

namespace App\WebHook;

use App\WebHook\GitPush;
use Cartalyst\Sentinel\Users\EloquentUser;

class GitHubPush extends GitPush
{

/**
* parse the data.
*
* @param array $data
* @return array
*/
public function parse($data)
{
// set the repo
$repo = [];
$repo['name'] = $data['repository']['name'];
$repo['homepage'] = $data['repository']['html_url'];
$this->repo = $repo;
// set the branch
$this->branch = isset($data['ref']) ? substr($data['ref'], 11) : ''; // refs/heads/
// get the pusher
$user_name = isset($data['pusher']) && isset($data['pusher']['user_name']) ? $data['pusher']['user_name'] : '';
$user_email = isset($data['pusher']) && isset($data['pusher']['user_email']) ? $data['pusher']['user_email'] : '';
$user = [ 'name' => $user_name, 'email' => $user_email ];
if ($user_email)
{
$user2 = EloquentUser::where('email', $user_email)->first();
if ($user2)
{
$user['id'] = $user2->id;
$user['name'] = $user2->first_name;
$user['email'] = $user2->email;
}
}
$this->pusher = $user;
// set the commits
$this->commits = isset($data['commits']) ? $data['commits'] : [];
// get the after
$this->after = isset($data['after']) ? $data['after'] : '';
// get the before
$this->before = isset($data['before']) ? $data['before'] : '';
}

/**
* arrange the data format.
*
* @param array $commit
* @return array
*/
public function arrangeCommit($commit)
{
$new_commit = [];

$new_commit['repo'] = $this->getRepo();
$new_commit['branch'] = $this->getBranch();
$new_commit['pusher'] = $this->getPusher();
$new_commit['pushed_at'] = time();

$new_commit['sha'] = isset($commit['id']) ? $commit['id'] : '';
$new_commit['url'] = isset($commit['url']) ? $commit['url'] : '';
$new_commit['message'] = isset($commit['message']) ? trim($commit['message']) : '';
$new_commit['added'] = isset($commit['added']) ? $commit['added'] : [];
$new_commit['modified'] = isset($commit['modified']) ? $commit['modified'] : [];
$new_commit['removed'] = isset($commit['removed']) ? $commit['removed'] : [];
$new_commit['committed_at'] = isset($commit['timestamp']) ? strtotime($commit['timestamp']) : '';

if (isset($commit['author']))
{
$new_commit['author'] = $commit['author'];
if (isset($commit['author']['email']) && $commit['author']['email'])
{
$new_author = EloquentUser::where('email', $commit['author']['email'])->first();
if ($new_author)
{
$new_commit['author']['id'] = $new_author->id;
$new_commit['author']['name'] = $new_author->first_name;
$new_commit['author']['email'] = $new_author->email;
}
}
}
else
{
$new_commit['author'] = [];
}

return $new_commit;
}
}
3 changes: 1 addition & 2 deletions app/WebHook/GitPush.php
Expand Up @@ -141,8 +141,7 @@ public function insCommits($project_key)
// transimit the issue status
if ($action > 0 && isset($new_commit['author']['id']) && $new_commit['author']['id'])
{
echo 'aaa';
//$this->execWorkflow($project_key, $issue, $action, $new_commit['author']['id']);
$this->execWorkflow($project_key, $issue, $action, $new_commit['author']['id']);
}
}
return;
Expand Down

0 comments on commit e0163e2

Please sign in to comment.