decide-note.php
7.74 KB
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
<?php
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/AbstractApi.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/SimpleXMLElement.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/User.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/Issue.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/IssueCategory.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/Attachment.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Api/Membership.php";
require_once "lib/3rdparty/php-redmine-api/lib/Redmine/Client.php";
require_once "lib/3rdparty/Inflect/Inflect.php";
require_once "config.php";
function redmine_proj_identifier2id ($ident) {
error_log("redmine_proj_identifier($ident)");
static $ident2id = array();
if (empty($ident2id)) {
$lines = split("\n", file_get_contents('/opt/redmine_projects.csv'));
foreach ($lines as &$line) {
$words = split("\t", $line);
$ident2id[strtolower($words[1])] = $words[0];
}
}
error_log ('RI:'.$ident2id[strtolower($ident)]);
return $ident2id[strtolower($ident)];
}
foreach (array('issueId', 'action', 'signature') as $k)
if (empty($k))
die('field "'.$k.'" is empty');
$isGoodSignature = false;
foreach ($SIGNKEYS as $key) {
if (sha1($_GET['issueId'].$key) == $_GET['signature']) {
$isGoodSignature = true;
break;
}
}
if ($isGoodSignature === false) {
die('bad signature');
}
mail('admin@ut.mephi.ru', 'decide-note.php action '.$_GET['action'], print_r($_GET, 1));
$redmine_admin = new Redmine\Client('https://redmine.ut.mephi.ru', REDMINE_LOGIN, REDMINE_PASSWORD);
$issue = $redmine_admin->api('issue')->show($_GET['issueId'])['issue'];
$assignedTo = $redmine_admin->api('user' )->show($issue['assigned_to']['id'])['user'];
$api_key = $assignedTo['api_key'];
$redmine = new Redmine\Client('https://redmine.ut.mephi.ru', $api_key);
function addUserToProject(&$redmine, $user_id, $project_id, $role_id) {
error_log("addUserToProject(redmine, $user_id, $project_id, $role_id)");
$current_membership = $redmine->api('membership')->all($project_id, array('limit' => '1000'));
//mail('dyokunev@ut.mephi.ru', 'memberships', print_r($current_membership['memberships'], 1));
foreach ($current_membership['memberships'] as $global_key => $member){
if ($member['user']['id'] == $user_id){
$cur_roles=array();
$membership_id = $current_membership['memberships'][$global_key]['id'];
foreach($current_membership['memberships'][$global_key]['roles'] as $role) {
if ($role['id'] == $role_id) {
error_log("already there!");
return true;
}
$cur_roles[] = $role['id'];
}
$role_ids = array('role_ids' => array_merge($cur_roles, array($role_id)));
error_log("updating");
//print_r($role_ids);
return $redmine->api('membership')->update($membership_id, $role_ids);
}
}
error_log("creating");
return $redmine->api('membership')->create($project_id, array('user_id' => $user_id, 'role_ids' => array($role_id)));
}
function removeUserFromProject(&$redmine, $user_id, $project_id, $role_id) {
error_log("removeUserFromProject(redmine, $user_id, $project_id, $role_id)");
$current_membership = $redmine->api('membership')->all($project_id, array('limit' => '1000'));
foreach ($current_membership['memberships'] as $global_key => $member){
if ($member['user']['id'] == $user_id){
$role_ids=array();
$membership_id = $current_membership['memberships'][$global_key]['id'];
foreach($current_membership['memberships'][$global_key]['roles'] as $role) {
if ($role['id'] == $role_id) {
continue;
}
$role_ids[] = $role['id'];
}
return $redmine->api('membership')->update($membership_id, array('role_ids' => $role_ids));
}
}
return true;
}
switch ($_GET['action']) {
case 'agree':
case 'move':
$recipientId = NULL;
foreach ( $issue['custom_fields'] as $cf) {
if ($cf['id'] == 95) {
$recipientId = $cf['value'];
break;
}
}
if (is_null($recipientId)) {
header('Content-Type: text/html');
mail('admin@ut.mephi.ru', 'Already confirmed in decide-note.php', print_r($_GET, 1));
//die('Произошла ошибка. Пожалуйста, повторите попытку позже. Cannot find recipient custom field in issue: '.$_GET['issueId']);
?><html><head><title>Служебная записка уже подтверждена</title><meta charset="UTF-8"><meta http-equiv="refresh" content="2; https://tasks.mephi.ru/issues/<?=$_GET['issueId']?>" /></head><body><h1>Служебная записка <a href='https://tasks.mephi.ru/issues/<?=$_GET['issueId']?>'>#<?=$_GET['issueId']?></a> уже подтверждена.</h1></body></html><?php
exit(0);
}
header('Content-Type: text/plain');
$recipient = $redmine_admin->api('user')->show($recipientId)['user'];
if (empty($recipient['mail'])) {
mail('admin@ut.mephi.ru', 'Got error on decide-note.php', print_r($_GET, 1));
die('Произошла ошибка. Пожалуйста, повторите попытку позже. Cannot find login of user with ID: '.$recipientId);
}
$project_id = redmine_proj_identifier2id(strtolower(explode('@', $recipient['mail'])[0]));
if (empty($project_id)) {
mail('admin@ut.mephi.ru', 'Got error on decide-note.php', 'empty $project_id, issue '.$issue['id']);
}
addUserToProject($redmine_admin, $assignedTo['id'], $project_id, ROLE_CUSTOMER);
addUserToProject($redmine_admin, $assignedTo['id'], 15971, ROLE_MOVER);
addUserToProject($redmine_admin, $assignedTo['id'], $project_id, ROLE_MOVER);
/*$result =*/
/*if ($result !== TRUE) {
mail('admin@ut.mephi.ru', 'Got error on decide-note.php', print_r($_GET, 1));
die('Произошла ошибка. Пожалуйста, повторите попытку позже. Cannot add user '.$recipientId.' to project '.$issue['project']['id'].': '.print_r($result, 1));
}*/
//print @json_encode($redmine->api('issue')->update($issue['id'], array('status_id' => STATUSID_NEW, 'project_id' => redmine_proj_identifier2id(strtolower(explode('@', $recipient['mail'])[0])), 'assigned_to_id' => $recipient['id'])));
$issue_props = array(
//'project_id' => $project_id,
'assigned_to_id' => $recipient['id'],
'tracker_id' => TRACKERID_TASK,
);
if ($_GET['action'] == 'agree')
$issue_props['status_id'] = STATUSID_NEW;
//print_r($issue_props);die();
$result = $redmine->api('issue')->update($issue['id'], $issue_props);
$issue_props = array(
'project_id' => $project_id,
'notes' => "h1. Подтверждаю служебную записку.\n@ @\n@ @",
);
$result = $redmine->api('issue')->update($issue['id'], $issue_props);
removeUserFromProject($redmine_admin, $assignedTo['id'], $project_id, ROLE_MOVER);
if ($result !== TRUE) {
mail('admin@ut.mephi.ru', 'Got error on decide-note.php', print_r($_GET, 1));
die('Произошла ошибка. Пожалуйста, повторите попытку позже. Cannot update the issue '.$issue['project']['id'].': '.print_r($result, 1));
}
header('Location: https://tasks.mephi.ru/issues/'.$_GET['issueId']);
break;
case 'decline':
header('Content-Type: text/plain');
//print @json_encode($redmine->api('issue')->update($_GET['issueId'], array('status_id' => STATUSID_DECLINED)));
$result = $redmine->api('issue')->update($_GET['issueId'], array('status_id' => STATUSID_DECLINED));
if ($result !== TRUE) {
mail('admin@ut.mephi.ru', 'Got error on decide-note.php', print_r($_GET, 1));
die('Произошла ошибка. Пожалуйста, повторите попытку позже. Cannot decline the issue '.$issue['project']['id'].': '.print_r($result, 1));
}
header('Location: https://tasks.mephi.ru/issues/'.$_GET['issueId']);
break;
default:
die('Unknown action: '.$_GET['action']);
}
?>