Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 01c1dac

Browse files
author
Ruslan Mamaev
authored
Some fixes
1 parent 73b8be6 commit 01c1dac

File tree

5 files changed

+73
-12
lines changed

5 files changed

+73
-12
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ docs/data-examples
2727

2828
# Results of executed requests
2929
.idea/httpRequests
30+
tf-commands.txt

get-terraform.php

+37-10
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@
3535
$org_user_admins_parameters = array($org, null, 'all', 'admin');
3636
$org_user_admins = $paginator->fetchAll($client->api('members'), 'all', $org_user_admins_parameters);
3737

38+
// I need these data to populate users list in users/vars.tf
39+
$github_user_logins = "";
40+
foreach ($org_user_members as $github_user) {
41+
$github_user_logins = $github_user_logins . "\"" . $github_user['login'] . "\", ";
42+
}
43+
44+
$github_admin_logins = "";
45+
foreach ($org_user_admins as $github_user) {
46+
$github_admin_logins = $github_admin_logins . "\"" . $github_user['login'] . "\", ";
47+
}
48+
3849
/**
3950
* github_team:
4051
* name
@@ -59,6 +70,15 @@
5970
$repo_topics[$repo['name']] = $topic;
6071
}
6172
}
73+
74+
$repo_topics_as_str = array();
75+
foreach ($repo_topics as $repo => $value) {
76+
$topics_as_a_string = "";
77+
foreach ($value as $topics) {
78+
$topics_as_a_string = $topics_as_a_string . "\"," . $topics;
79+
}
80+
$repo_topics_as_str[$repo] = $topics_as_a_string;
81+
}
6282
/*
6383
* To get protected branches I have modified Repo\brances() method
6484
* public function branches($username, $repository, $branch = null, array $params = [])
@@ -72,11 +92,11 @@
7292
}
7393
*/
7494
// TODO: add these data to repo template
75-
$protected_branches = array();
76-
foreach ($org_repositories as $repo) {
77-
$parameters = array($org, $repo['name'], null, array("protected" => "true"));
78-
$protected_branches[$repo['name']] = $paginator->fetchAll($client->api('repositories'), 'branches', $parameters);
79-
}
95+
//$protected_branches = array();
96+
//foreach ($org_repositories as $repo) {
97+
// $parameters = array($org, $repo['name'], null, array("protected" => "true"));
98+
// $protected_branches[$repo['name']] = $paginator->fetchAll($client->api('repositories'), 'branches', $parameters);
99+
//}
80100

81101
// TODO: add github branch protection. See https://www.terraform.io/docs/providers/github/r/branch_protection.html
82102
// TODO: add webhooks for a repo: https://www.terraform.io/docs/providers/github/r/repository_webhook.html
@@ -102,6 +122,7 @@
102122

103123
$team_repositories = array();
104124
$team_repositories_with_permission = array();
125+
$org_team_ids = array();
105126
foreach ($org_teams as $team) {
106127
$parameters = array($team['id']);
107128
foreach ($paginator->fetchAll($client->api('teams'), 'repositories', $parameters) as $rep) {
@@ -111,10 +132,13 @@
111132
}
112133
$team_repositories[$team['slug']] = $team_repositories_with_permission;
113134
$team_repositories_with_permission = array();
135+
$org_team_ids[$team['slug']] = $team['id'];
114136
}
115137

116138
$org_team_members = array();
117139
$org_team_maintainers = array();
140+
$org_team_members_ids = array();
141+
$org_team_maintainers_ids = array();
118142
$team_maintainers = array();
119143
$team_members = array();
120144
/**
@@ -151,7 +175,7 @@
151175
$team_maintainers = array();
152176
}
153177

154-
$file = '/home/rmamaev/workspace/github-terraform-exporter/tf-commands.txt';
178+
$file = '/home/ruslan.mamaev/workspace/github-terraform-exporter/tf-commands.txt';
155179

156180
foreach ($org_teams as $team) {
157181
$command = "terraform import github_team.team_" . $team['slug'] . " " .
@@ -185,16 +209,18 @@
185209
file_put_contents($file, $command, FILE_APPEND);
186210
}
187211

188-
foreach ($team_members as $team => $users) {
212+
foreach ($org_team_members as $team => $users) {
189213
foreach ($users as $user) {
190-
$command = "terraform import github_team_membership.member " . $team . ":" . $user . "\n";
214+
$command = "terraform import github_team_membership.team_" . $team . "_" . $user['login'] . "_membership" .
215+
" " . $org_team_ids[$team] . ":" . $user['login'] . "\n";
191216
file_put_contents($file, $command, FILE_APPEND);
192217
}
193218
}
194219

195-
foreach ($team_maintainers as $team => $users) {
220+
foreach ($org_team_maintainers as $team => $users) {
196221
foreach ($users as $user) {
197-
$command = "terraform import github_team_membership.member " . $team . ":" . $user . "\n";
222+
$command = "terraform import github_team_membership.team_" . $team . "_" . $user['login'] . "_membership" .
223+
" " . $org_team_ids[$team] . ":" . $user['login'] . "\n";
198224
file_put_contents($file, $command, FILE_APPEND);
199225
}
200226
}
@@ -204,3 +230,4 @@
204230
require_once 'templates/org-users.php';
205231
require_once 'templates/teams.php';
206232
require_once 'templates/team-members.php';
233+

templates/org-users.php

+13
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,19 @@
2121
}
2222
<?php } ?>
2323

24+
<?php echo "============== Users list short (for users/vars.tf) ============== " ?>
25+
variable "github_admins" {
26+
description = "List with admin users in GitHub Organization"
27+
type = "list"
28+
default = [<?php echo $github_admin_logins ?>]
29+
}
30+
31+
variable "github_users" {
32+
description = "List with limited users in GitHub Organization"
33+
type = "list"
34+
default = [<?php echo $github_user_logins ?>]
35+
}
36+
2437
<?php echo "============== Users outputs ============== " ?>
2538
<?php foreach ($org_user_members as $user) { ?>
2639
output "github_<?= $user['login'] ?>_username" {

templates/repos.php

+14-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
description = "<?= $repo['description'] ?>"
88

9-
homepage_url = <?= $repo['homepage'] ?>
9+
<?php if ($repo['homepage'] === "") { ?>
10+
homepage_url = ""
11+
<?php } else { ?>
12+
homepage_url = "<?= $repo['homepage'] ?>"
13+
<?php } ?>
1014

1115
has_projects = <?= json_encode($repo['has_projects']) ?>
1216

@@ -20,6 +24,14 @@
2024

2125
archived = <?= json_encode($repo['archived']) ?>
2226

23-
topics = [ <?= implode(",", $repo_topics[$repo['name']]) ?> ]
27+
topics = [ <?= $repo_topics_as_str[$repo['name']] ?> ]
28+
}
29+
<?php } ?>
30+
31+
<?php echo "============== repository outputs ============== " ?>
32+
<?php
33+
foreach ($org_repositories as $repo) { ?>
34+
output "repo_<?= $repo['name'] ?>_name" {
35+
value = "${github_repository.<?= $repo['name'] ?>.name}"
2436
}
2537
<?php } ?>

templates/teams.php

+8
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,11 @@
3434
<?php } ?>
3535
<?php } ?>
3636
<?php } ?>
37+
38+
<?php echo "============== Team outputs ============== " ?>
39+
<?php
40+
foreach ($org_teams as $team) { ?>
41+
output "team_<?= $team['slug'] ?>_id" {
42+
value = "${github_team.<?= $team['slug'] ?>.id}"
43+
}
44+
<?php } ?>

0 commit comments

Comments
 (0)