-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathpre_commit_check.php
More file actions
231 lines (178 loc) · 8.61 KB
/
Copy pathpre_commit_check.php
File metadata and controls
231 lines (178 loc) · 8.61 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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
# Copyright (c) 2014 John Bailey
# Licensed under the MIT license
#
# This file is intended to be called from a pre-commit hook in order to verify
# mantis ticket references in the commit comment. The level of checking
# is configured on a per-repository basis
header("content-type: text/html; charset=utf-8");
include_once 'si_common.php';
if ( !si_is_key_ok() ) {
die( plugin_lang_get( 'invalid_key' ) );
}
# If you're worried about information "leaking" out via error messages, set this to false to prevent error messages
# containing any information from the ticket(s)
$t_informational_errors = true;
# Get a list of the bug IDs which were referenced in the commit comment
$t_bug_list = Source_Parse_Buglinks( gpc_get_string( 'commit_comment', '' ));
$t_resolved_threshold = config_get( 'bug_resolved_status_threshold' );
$f_committer_name = gpc_get_string( 'committer', '' );
$f_repo_name = gpc_get_string( 'repo_name', '' );
$t_repo = SourceRepo::load_by_name( $f_repo_name );
# Repo not found
if ( is_null( $t_repo ) ) {
die( plugin_lang_get( 'invalid_repo' ) );
}
$t_repo_commit_needs_issue = isset( $t_repo->info['repo_commit_needs_issue'] ) ? $t_repo->info['repo_commit_needs_issue'] : false;
$t_repo_commit_issues_must_exist = isset( $t_repo->info['repo_commit_issues_must_exist'] ) ? $t_repo->info['repo_commit_issues_must_exist'] : false;
$t_repo_commit_ownership_must_match = isset( $t_repo->info['repo_commit_ownership_must_match'] ) ? $t_repo->info['repo_commit_ownership_must_match'] : false;
$t_repo_commit_status_restricted = isset( $t_repo->info['repo_commit_status_restricted'] ) ? $t_repo->info['repo_commit_status_restricted'] : false;
$t_repo_commit_status_allowed = isset( $t_repo->info['repo_commit_status_allowed'] ) ? $t_repo->info['repo_commit_status_allowed'] : '';
$t_repo_commit_project_restricted = isset( $t_repo->info['repo_commit_project_restricted'] ) ? $t_repo->info['repo_commit_project_restricted'] : '';
$t_repo_commit_project_allowed = isset( $t_repo->info['repo_commit_project_allowed'] ) ? $t_repo->info['repo_commit_project_allowed'] : '';
$t_repo_commit_committer_must_be_member = isset( $t_repo->info['repo_commit_committer_must_be_member'] ) ? $t_repo->info['repo_commit_committer_must_be_member'] : '';
$t_repo_commit_committer_must_be_level = isset( $t_repo->info['repo_commit_committer_must_be_level'] ) ? $t_repo->info['repo_commit_committer_must_be_level'] : MantisEnum::getValues( config_get( 'access_levels_enum_string' ) ) ;
$t_all_ok = true;
# Check number of bugs referenced in the commit comment
if(( sizeof( $t_bug_list ) == 0 ) && $t_repo_commit_needs_issue ) {
# It was expected that the commit comment would reference one of more bug
# IDs but this was not the case
printf( "Check-Message: '%s'\r\n",plugin_lang_get( 'error_commit_needs_issue' ) );
$t_all_ok = false;
} else {
# Loop all the bug IDs referenced in the commit comment
foreach( $t_bug_list as $t_bug_id ) {
# Check existence first to prevent API throwing an error
if( bug_exists( $t_bug_id ) ) {
$t_bug = bug_get( $t_bug_id );
# Ownership of ticket must match committer?
if( $t_repo_commit_ownership_must_match ) {
if( 0 == $t_bug->handler_id ) {
$t_user_name = 'none';
$t_user_email = 'none';
} else {
$t_user_name = user_get_name( $t_bug->handler_id );
$t_user_email = user_get_email( $t_bug->handler_id );
}
# Check that the username of the committer matches the user name
# or e-mail address of the owner of the ticket
if( !( strlen( $f_committer_name ) &&
(( $t_user_name == $f_committer_name ) ||
( $t_user_email == $f_committer_name )))) {
printf( "Check-Message: '%s : %s %d",
plugin_lang_get( 'error_commit_issue_ownership' ),
plugin_lang_get( 'issue' ),
$t_bug_id );
if( $t_informational_errors ) {
# Informative errors turned on so display the user to whom
# the ticket is assigned
printf( " (%s/%s vs %s)",
$t_user_name, $t_user_email, $f_committer_name );
}
printf( "'\r\n" );
$t_all_ok = false;
}
} # End ownership must match ticket
# Only allowed to commit against tickets with a specific status?
if( $t_repo_commit_status_restricted ) {
# Check that the bug's status is at a level for which a commit
# is allowed
if( !in_array( $t_bug->status, $t_repo_commit_status_allowed )) {
printf( "Check-Message: '%s : %s %d",
plugin_lang_get( 'error_commit_issue_wrong_status' ),
plugin_lang_get( 'issue' ),
$t_bug_id );
if( $t_informational_errors ) {
# Informative errors turned on so display a list of statuses for which
# a commit would be accepted
# Get an array of the names of the statuses for which commit is allowed
$t_statuses = array_map( function( $p_status ) { return get_enum_element( 'status', $p_status ); }, $t_repo_commit_status_allowed );
printf( " (%s vs %s)",
get_enum_element( 'status', $t_bug->status ),
implode( ", ", $t_statuses ));
}
printf( "'\r\n" );
$t_all_ok = false;
}
} # End only allowed to commit against tickets with a specific status
# Only allowed to commit against Mantis tickets within specific project(s)
if( $t_repo_commit_project_restricted ) {
if( !in_array( 0, $t_repo_commit_project_allowed ) &&
!in_array( $t_bug->project_id, $t_repo_commit_project_allowed )) {
printf( "Check-Message: '%s : %s %d",
plugin_lang_get( 'error_commit_issue_wrong_project' ),
plugin_lang_get( 'issue' ),
$t_bug_id );
if( $t_informational_errors ) {
# Informative errors turned on so display a list of Mantis projects to
# which referenced tickets must belong
# Get an array of the names of all the projects
$t_projects = array_map( function( $p_proj ) { return project_get_field( $p_proj, 'name' ); }, $t_repo_commit_project_allowed );
printf( " (%s vs %s)",
project_get_field( $t_bug->project_id, 'name' ),
implode( $t_projects, ", " ));
}
printf( "'\r\n" );
$t_all_ok = false;
}
} # End only allowed to commit against tickets within specific projects
# Committer must belong to the Mantis project?
if( $t_repo_commit_committer_must_be_member ) {
$t_user_id = user_get_id_by_name( $f_committer_name );
# Didn't find the username? Try the e-mail address
if( $t_user_id == false ) {
$t_user_id = user_get_id_by_email( $f_committer_name );
}
/* Check that the user exists in Mantis */
if( $t_user_id == false ) {
printf( "Check-Message: '%s : %s %d (%s)'\r\n",
plugin_lang_get( 'error_commit_committer_not_found' ),
plugin_lang_get( 'issue' ),
$t_bug_id, $f_committer_name );
$t_all_ok = false;
/* Check that the user is assigned to the project */
} elseif( ! project_includes_user( $t_bug->project_id, $t_user_id )) {
printf( "Check-Message: '%s : %s %d (%s)'\r\n",
plugin_lang_get( 'error_commit_committer_not_member' ),
plugin_lang_get( 'issue' ),
$t_bug_id, $f_committer_name );
$t_all_ok = false;
} else {
$t_user_access_level = access_get_local_level( $t_user_id, $t_bug->project_id );
if( in_array( $t_user_access_level, $t_repo_commit_committer_must_be_level )) {
printf( "Check-Message: '%s : %s %d",
plugin_lang_get( 'error_commit_committer_wrong_level' ),
plugin_lang_get( 'issue' ),
$t_bug_id );
if( $t_informational_errors ) {
# Informative errors turned on so display a list of access levels
# for which commit is allowed
$t_levels = MantisEnum::getAssocArrayIndexedByValues( config_get( 'access_levels_enum_string' ) );
$t_allowed_levels = array_intersect_key( $t_levels, array_flip( $t_repo_commit_committer_must_be_level ));
printf( " (%s vs %s)",
$t_levels[ $t_user_access_level ],
implode( ", ", array_values( $t_allowed_levels ) ) );
}
printf( "'\r\n" );
$t_all_ok = false;
}
}
} # End committer must belong to mantis project
} else {
/* If the issue doesn't exist, then can't perform the checks */
if( $t_repo_commit_issues_must_exist ||
$t_repo_commit_ownership_must_match ||
$t_repo_commit_status_restricted ||
$t_repo_commit_project_restricted ||
$t_repo_commit_committer_must_be_member ) {
printf( "Check-Message: '%s : %s %d'\r\n",
plugin_lang_get( 'error_commit_nonexistent_issue' ),
plugin_lang_get( 'issue' ),
$t_bug_id );
$t_all_ok = false;
}
}
}
}
printf( "Check-OK: %d\r\n",$t_all_ok );
?>