forked from Automattic/vip-go-compatibility-scanner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zendesk-db.php
177 lines (150 loc) · 3.18 KB
/
zendesk-db.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
/*
* Open or create ZendeskDB file.
* Uses SQLite.
*/
function vipgocs_zendesk_db_open(
$path
) {
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'zendesk_db_open',
1
);
try {
$db_conn = new SQLite3(
$path,
SQLITE3_OPEN_READWRITE | SQLITE3_OPEN_CREATE
);
}
catch ( Exception $e ) {
$error_msg = $e->getMessage();
$db_conn = null;
}
if ( null === $db_conn ) {
vipgoci_sysexit(
'Unable to open ZendeskDB',
array(
'file' => $path,
'error' => $error_msg,
)
);
}
$res = $db_conn->query(
'CREATE TABLE IF NOT EXISTS vipgocs_github_issues ( ' .
'id integer PRIMARY KEY, ' .
'repo_name TEXT NOT NULL, ' .
'repo_owner TEXT NOT NULL, ' .
'github_url TEXT NOT NULL ) '
);
if ( false === $res ) {
vipgoci_sysexit(
'Unable to write to ZendeskDB',
array(
'file' => $path,
)
);
}
return $db_conn;
}
/*
* Close database handle.
*/
function vipgocs_zendesk_db_close(
$db_conn
) {
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'zendesk_db_close',
1
);
return $db_conn->close();
}
/*
* Write GitHub issue URL to DB along
* with repo-owner and repo-name.
*/
function vipgocs_zendesk_db_write_github_issue(
$db_conn,
$repo_owner,
$repo_name,
$github_url
) {
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'zendesk_db_write_github_issue',
1
);
$db_stmt = $db_conn->prepare(
'INSERT INTO vipgocs_github_issues ' .
'(repo_name, repo_owner, github_url) ' .
'VALUES ' .
'( :repo_name, :repo_owner, :github_url )'
);
$db_stmt->bindValue(':repo_name', $repo_name );
$db_stmt->bindValue(':repo_owner', $repo_owner );
$db_stmt->bindValue(':github_url', $github_url );
return $db_stmt->execute();
}
/*
* Fetch all GitHub issues from DB,
* merge all that belong to the same
* repo-owner/repo-name combination into
* one group.
*/
function vipgocs_zendesk_db_get_github_issues(
$db_conn
) {
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'zendesk_db_get_github_issues',
1
);
$res = $db_conn->query(
'SELECT id, repo_name, repo_owner, github_url FROM vipgocs_github_issues ORDER BY id'
);
$gh_issues_groups = array();
while ( $row = $res->fetchArray() ) {
$key = $row['repo_owner'] . '/' . $row['repo_name'];
if ( ! isset(
$gh_issues_groups[ $key ]
) ) {
$gh_issues_groups[ $key ] = array(
'repo_owner' => $row['repo_owner'],
'repo_name' => $row['repo_name'],
'github_issues_urls' => array(),
);
}
$gh_issues_groups[ $key ]['github_issues_urls'][] =
$row['github_url'];
}
return $gh_issues_groups;
}
/*
* Delete a particular URL from
* Zendesk DB, matched also by repo-owner
* and repo-name.
*/
function vipgocs_zendesk_db_delete_github_issue(
$db_conn,
$repo_owner,
$repo_name,
$github_url
) {
vipgoci_counter_report(
VIPGOCI_COUNTERS_DO,
'zendesk_db_delete_github_issue',
1
);
$db_stmt = $db_conn->prepare(
'DELETE FROM vipgocs_github_issues ' .
'WHERE ' .
'repo_owner = :repo_owner AND ' .
'repo_name = :repo_name AND ' .
'github_url = :github_url'
);
$db_stmt->bindValue(':repo_name', $repo_name );
$db_stmt->bindValue(':repo_owner', $repo_owner );
$db_stmt->bindValue(':github_url', $github_url );
return $db_stmt->execute();
}