Skip to content
This repository was archived by the owner on Jul 12, 2019. It is now read-only.

Commit

Permalink
Gitlab add to project
Browse files Browse the repository at this point in the history
  • Loading branch information
shalvah committed Jan 14, 2017
1 parent 77c1598 commit 997667a
Show file tree
Hide file tree
Showing 2 changed files with 318 additions and 183 deletions.
93 changes: 57 additions & 36 deletions app/Jobs/HandleSlackEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
namespace App\Jobs;

use App\Models\Credential;
use App\Models\Link;
use App\Models\LinkTag;
use Gitlab\Api\Projects;
use GuzzleHttp\Client;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Support\Facades\Log;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class HandleSlackEvent implements ShouldQueue
{
Expand All @@ -36,15 +34,24 @@ public function __construct(array $request)
public function handle()
{
if ($this->request['event']['type'] == "message") {
$rawText=$this->request['event']['text'];
$parsedText=$this->parseText($rawText);
if($parsedText) {
$matchea=[];
preg_match("/[\w+]/i", $parsedText['username'], $matches);
$result=$this->addToGitlab($matches[0]);
if ($result) {}
$rawText = $this->request['event']['text'];
$parsedText = $this->parseText($rawText);
if (isset($parsedText["type"])) {
if ($parsedText["type"] == "gitlab-add") {
$result = $this->addToGitlab($parsedText["username"]);
if ($result) {
$user=$this->request['event']['user'];
$data = array(
"team_id" => $this->request['team_id'],
"channel" => $this->request['event']['channel'],
"text" => "Added to Gitlab! <@$user>"
);

$response = $this->respond($data);
}
}
}
}
}
/*if ($this->request['event']['type'] == "message") {
$userId = $this->request['event']['user'];
if ($this->request['event']['subtype'] == "channel_join") {
Expand All @@ -71,29 +78,25 @@ public function handle()

public function parseText($text)
{
$tokens = explode(' ', $text);
$botUserId=Credential::where('team_id', $this->request['team_id'])->get()->first()->bot_user_id;
if (in_array("<@$botUserId>", $tokens)) {
if (!empty(preg_grep("/add/i", $tokens)) || (!empty(preg_grep("/gitlab/i", $tokens))) || (!empty(preg_grep("/username/i", $tokens)))) {
$botUserId = Credential::where('team_id', $this->request['team_id'])->get()->first()->bot_user_id;
if (preg_match("/<@$botUserId>/", $text)) {
$matches = [];
if (preg_match("/@-(\w+)/i", $text, $matches)) {
return array(
'type' => 'add-gitlab',
'username' => preg_grep("/@-[\w+]/i", $tokens)[0]
}/* else if ($tokens[1] == "find" || $tokens[1] == "search") {
return array(
'type' => 'search',
'query_terms' => array_slice($tokens, 2));
}*/
}
return null;
'type' => 'gitlab-add',
'username' => $matches[1]
);
}
}
return null;
return [];
}


/**
* Posts responses to Slack
*/
public function respond(array $data)
public
function respond(array $data)
{
$client = new Client();
$response = $client->request('GET', 'https://slack.com/api/chat.postMessage',
Expand All @@ -107,14 +110,32 @@ public function respond(array $data)
return json_decode($response->getBody(), true);
}

public function addToGitlab ($username) {
$client = new \Gitlab\Client('http://git.yourdomain.com/api/v3/'); // change here $client->authenticate('your_gitlab_token_here', \Gitlab\Client::AUTH_URL_TOKEN); // change here

//get user's Gitlab user id
public function addToGitlab($username)
{
//authenticate
$client = new \Gitlab\Client('http://gitlab.com.com/api/v3/'); // change here $client->authenticate('your_gitlab_token_here', \Gitlab\Client::AUTH_URL_TOKEN); // change here
$client->authenticate(env('GITLAB_TOKEN'), \Gitlab\Client::AUTH_URL_TOKEN);

//get user's Gitlab user id

//get project
$api = new Projects($client);
$projects = $api->accessible();
$projId;
foreach ($projects as $project) {
if($project["weburl"] == "https://gitlab.com/hng-interns/getting-started"
|| $project["weburl"] == "http://gitlab.com/hng-interns/getting-started" ) {
$projId=$project["id"];
}
}
if(!$projId) {
throw new \ErrorException("couldnt get project");
}

//add user to project
$project = new \Gitlab\Model\Project(1, $client);
$user=project->addMember($userId, $accessLevel);
}
//add user to project
$project = new \Gitlab\Model\Project($projId, $client);
$user = $project->addMember($userId, 30);
return $user;
}

}
Loading

0 comments on commit 997667a

Please sign in to comment.