forked from JimMackin/RepoBackup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepoSources.php
105 lines (86 loc) · 3.11 KB
/
RepoSources.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
<?php
interface RepoSource {
function setCredentials( $params );
function getRepositories();
}
class BitbucketSource implements RepoSource {
function setCredentials( $params ) {
$this->user = strtolower( $params['user'] );
$this->pass = $params['pass'];
$this->client = new Bitbucket\API\Repositories();
$this->client->setCredentials( new Bitbucket\API\Authentication\Basic( $params['user'], $params['pass'] ) );
}
function getRepositories() {
$res = $this->client->all( $this->user );
$list = json_decode( $res->getContent(), true );
$repos = $list['values'];
while ( ! empty( $list['next'] ) ) {
$list = $this->client->getClient()->setApiVersion( '2.0' )->request( $list['next'] );
$list = $list = json_decode( $list->getContent(), true );
$repos = array_merge( $repos, $list['values'] );
}
$ret = array();
foreach ( $repos as $repo ) {
$url = $repo['links']['clone'][1]['href'];
$ret[] = array( 'name' => $repo['name'], 'clone_url' => $url, 'type' => $repo['scm'] );
}
return $ret;
}
}
class BitbucketSourceV2 implements RepoSource {
public $repositories;
public $client;
public $team;
function setCredentials( $params ) {
$this->client_id = $params['client_id'];
$this->client_secret = $params['client_secret'];
$this->team = $params['team'];
$oauth_params = array(
'client_id' => $params['client_id'],
'client_secret' => $params['client_secret'],
);
$this->client = new \Bitbucket\API\Api();
$this->client->getClient()->addListener(
new \Bitbucket\API\Http\Listener\OAuth2Listener($oauth_params)
);
/* @var \Bitbucket\API\Repositories $repositories */
$this->repositories = $this->client->api('Repositories');
}
function getRepositories() {
$res = $this->repositories->all( $this->team );
$list = json_decode( $res->getContent(), true );
$repos = $list['values'];
while ( ! empty( $list['next'] ) ) {
$list = $this->client->getClient()->setApiVersion( '2.0' )->request( $list['next'] );
$list = $list = json_decode( $list->getContent(), true );
$repos = array_merge( $repos, $list['values'] );
}
$ret = array();
foreach ( $repos as $repo ) {
$url = $repo['links']['clone'][1]['href'];
$ret[] = array( 'name' => $repo['name'], 'clone_url' => $url, 'type' => $repo['scm'] );
}
return $ret;
}
}
class GitHubSource implements RepoSource {
function setCredentials( $params ) {
$this->user = $params['user'];
$this->client = new \Github\Client(
new \Github\HttpClient\CachedHttpClient( array( 'cache_dir' => '/tmp/github-api-cache' ) )
);
$this->client->authenticate( $params['token'], Github\Client::AUTH_HTTP_TOKEN );
}
function getRepositories() {
$repos = $this->client->api( 'user' )->repositories( $this->user );
$ret = array();
foreach ( $repos as $repo ) {
$ret[] = array( 'clone_url' => $repo['clone_url'], 'name' => $repo['name'], 'type' => 'git' );
}
$gists = $this->client->api( 'gists' )->all();
foreach ( $gists as $gist ) {
$ret[] = array( 'clone_url' => $gist['git_pull_url'], 'name' => 'gists/' . $gist['id'], 'type' => 'git' );
}
return $ret;
}
}