Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Move github API into it's own class

  • Loading branch information...
commit c4c40181f1032d08f9093c965dfd8c46383a68b9 1 parent 56cc727
Daniel Beardsley authored October 22, 2011
5  credentials.php
... ...
@@ -0,0 +1,5 @@
  1
+<?php
  2
+
  3
+$username = 'Put your github username here';
  4
+$password = 'Put your github password here';
  5
+
53  github.php
... ...
@@ -0,0 +1,53 @@
  1
+<?php
  2
+class Github {
  3
+   public $repo;
  4
+
  5
+   private $username;
  6
+   private $password;
  7
+
  8
+   public function __construct($username, $password) {
  9
+      $this->username = $username;
  10
+      $this->password = $password;
  11
+   }
  12
+
  13
+   public function post($url, $json, $patch = false) {
  14
+      $ch = curl_init();
  15
+      curl_setopt($ch, CURLOPT_USERPWD, "$this->username:$this->password");
  16
+      curl_setopt($ch, CURLOPT_URL, "https://api.github.com$url");
  17
+      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  18
+      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  19
+      curl_setopt($ch, CURLOPT_HEADER, false);
  20
+      curl_setopt($ch, CURLOPT_POST, true);
  21
+      curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
  22
+      if ($patch) {
  23
+         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
  24
+      }
  25
+      $ret = curl_exec($ch);
  26
+      if(!$ret) { 
  27
+           trigger_error(curl_error($ch)); 
  28
+       } 
  29
+      curl_close($ch);
  30
+      return $ret;
  31
+   }
  32
+
  33
+   public function add_milestone($data) {
  34
+      return json_decode(github_post("/repos/$this->repo/milestones",
  35
+         json_encode($data)), true);
  36
+   }
  37
+
  38
+   public function add_issue($data) {
  39
+      return json_decode(github_post("/repos/$this->repo/issues",
  40
+         json_encode($data)), true);
  41
+   }
  42
+
  43
+   public function add_comment($issue, $body) {
  44
+      return json_decode(github_post("/repos/$this->repo/issues/$issue/comments",
  45
+         json_encode(array('body' => $body))), true);
  46
+   }
  47
+
  48
+   public function update_issue($issue, $data) {
  49
+      return json_decode(github_post("/repos/$this->repo/issues/$issue",
  50
+         json_encode($data), true), true);
  51
+   }
  52
+}
  53
+
55  trac2github.php
@@ -7,10 +7,11 @@
7 7
  * @license BSD
8 8
  */
9 9
 
  10
+require("github.php");
  11
+
10 12
 // Edit configuration below
11 13
 
12  
-$username = 'Put your github username here';
13  
-$password = 'Put your github password here';
  14
+require("credentials.php");
14 15
 $project = 'Organization or User name';
15 16
 $repo = 'Repository name';
16 17
 
@@ -54,6 +55,7 @@
54 55
 set_time_limit(0);
55 56
 
56 57
 $trac_db = new PDO('mysql:host='.$mysqlhost_trac.';dbname='.$mysqldb_trac, $mysqluser_trac, $mysqlpassword_trac);
  58
+$github = new Github($username, $password);
57 59
 
58 60
 echo 'Connected to Trac';
59 61
 
@@ -68,7 +70,7 @@
68 70
 	$mnum = 1;
69 71
 	foreach ($res->fetchAll() as $row) {
70 72
 		//$milestones[$row['name']] = ++$mnum;
71  
-		$resp = github_add_milestone(array(
  73
+		$resp = $github->add_milestone(array(
72 74
 			'title' => $row['name'],
73 75
 			'state' => $row['completed'] == 0 ? 'open' : 'closed',
74 76
 			'description' => empty($row['description']) ? 'None' : $row['description'],
@@ -105,7 +107,7 @@
105 107
 		if (empty($row['owner']) || !isset($users_list[$row['owner']])) {
106 108
 			$row['owner'] = $username;
107 109
 		}
108  
-		$resp = github_add_issue(array(
  110
+		$resp = $github->add_issue(array(
109 111
 			'title' => $row['summary'],
110 112
 			'body' => empty($row['description']) ? 'None' : $row['description'],
111 113
 			'assignee' => isset($users_list[$row['owner']]) ? $users_list[$row['owner']] : $row['owner'],
@@ -117,7 +119,7 @@
117 119
 			echo "Ticket #{$row['id']} converted to issue #{$resp['number']}\n";
118 120
 			if ($row['status'] == 'closed') {
119 121
 				// Close the issue
120  
-				$resp = github_update_issue($resp['number'], array(
  122
+				$resp = $github->update_issue($resp['number'], array(
121 123
 					'title' => $row['summary'],
122 124
 					'body' => empty($row['description']) ? 'None' : $row['description'],
123 125
 					'assignee' => isset($users_list[$row['owner']]) ? $users_list[$row['owner']] : $row['owner'],
@@ -145,7 +147,7 @@
145 147
 	$res = $trac_db->query("SELECT * FROM `ticket_change` where `field` = 'comment' AND `newvalue` != '' ORDER BY `ticket`, `time` $limit");
146 148
 	foreach ($res->fetchAll() as $row) {
147 149
 		$text = strtolower($row['author']) == strtolower($username) ? $row['newvalue'] : '**Author: ' . $row['author'] . "**\n" . $row['newvalue'];
148  
-		$resp = github_add_comment($tickets[$row['ticket']], $text);
  150
+		$resp = $github->add_comment($tickets[$row['ticket']], $text);
149 151
 		if (isset($resp['url'])) {
150 152
 			// OK
151 153
 			echo "Added comment {$resp['url']}\n";
@@ -159,45 +161,4 @@
159 161
 
160 162
 echo "Done whatever possible, sorry if not.\n";
161 163
 
162  
-function github_post($url, $json, $patch = false) {
163  
-	global $username, $password;
164  
-	$ch = curl_init();
165  
-	curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
166  
-	curl_setopt($ch, CURLOPT_URL, "https://api.github.com$url");
167  
-	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
168  
-	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
169  
-	curl_setopt($ch, CURLOPT_HEADER, false);
170  
-	curl_setopt($ch, CURLOPT_POST, true);
171  
-	curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
172  
-	if ($patch) {
173  
-		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PATCH');
174  
-	}
175  
-	$ret = curl_exec($ch);
176  
-	if(!$ret) { 
177  
-        trigger_error(curl_error($ch)); 
178  
-    } 
179  
-	curl_close($ch);
180  
-	return $ret;
181  
-}
182  
-
183  
-function github_add_milestone($data) {
184  
-	global $project, $repo;
185  
-	return json_decode(github_post("/repos/$project/$repo/milestones", json_encode($data)), true);
186  
-}
187  
-
188  
-function github_add_issue($data) {
189  
-	global $project, $repo;
190  
-	return json_decode(github_post("/repos/$project/$repo/issues", json_encode($data)), true);
191  
-}
192  
-
193  
-function github_add_comment($issue, $body) {
194  
-	global $project, $repo;
195  
-	return json_decode(github_post("/repos/$project/$repo/issues/$issue/comments", json_encode(array('body' => $body))), true);
196  
-}
197  
-
198  
-function github_update_issue($issue, $data) {
199  
-	global $project, $repo;
200  
-	return json_decode(github_post("/repos/$project/$repo/issues/$issue", json_encode($data), true), true);
201  
-}
202 164
 
203  
-?>

0 notes on commit c4c4018

Please sign in to comment.
Something went wrong with that request. Please try again.