summaryrefslogtreecommitdiffstats
path: root/extensions/PhabBugz/lib/Project.pm
blob: 8af01f74e54ba61d30cb9cb76480fa6acc73a363 (plain)
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
# This Source Code Form is "Incompatible With Secondary Licenses", as
# defined by the Mozilla Public License, v. 2.0.

package Bugzilla::Extension::PhabBugz::Project;

use 5.10.1;
use Moo;
use Scalar::Util qw(blessed);
use Types::Standard -all;
use Type::Utils;
use Type::Params qw( compile );

use Bugzilla::Error;
use Bugzilla::Util qw(trim);
use Bugzilla::Extension::PhabBugz::User;
use Bugzilla::Extension::PhabBugz::Types qw(:types);
use Bugzilla::Extension::PhabBugz::Util qw(request);

#########################
#    Initialization     #
#########################

has id              => (is => 'ro',   isa => Int);
has phid            => (is => 'ro',   isa => Str);
has type            => (is => 'ro',   isa => Str);
has name            => (is => 'ro',   isa => Str);
has description     => (is => 'ro',   isa => Maybe [Str]);
has creation_ts     => (is => 'ro',   isa => Str);
has modification_ts => (is => 'ro',   isa => Str);
has view_policy     => (is => 'ro',   isa => Str);
has edit_policy     => (is => 'ro',   isa => Str);
has join_policy     => (is => 'ro',   isa => Str);
has members_raw     => (is => 'ro',   isa => ArrayRef [Dict [phid => Str]]);
has members         => (is => 'lazy', isa => ArrayRef [PhabUser]);

my $Invocant = class_type {class => __PACKAGE__};

sub new_from_query {
  my ($class, $params) = @_;

  my $data
    = {queryKey => 'all', attachments => {members => 1}, constraints => $params};

  my $result = request('project.search', $data);
  if (exists $result->{result}{data} && @{$result->{result}{data}}) {

    # If name is used as a query param, we need to loop through and look
    # for exact match as Conduit will tokenize the name instead of doing
    # exact string match :( If name is not used, then return first one.
    if (exists $params->{name}) {
      foreach my $item (@{$result->{result}{data}}) {
        next if $item->{fields}{name} ne $params->{name};
        return $class->new($item);
      }
    }
    else {
      return $class->new($result->{result}{data}[0]);
    }
  }
}

sub BUILDARGS {
  my ($class, $params) = @_;

  $params->{name}            = $params->{fields}->{name};
  $params->{description}     = $params->{fields}->{description};
  $params->{creation_ts}     = $params->{fields}->{dateCreated};
  $params->{modification_ts} = $params->{fields}->{dateModified};
  $params->{view_policy}     = $params->{fields}->{policy}->{view};
  $params->{edit_policy}     = $params->{fields}->{policy}->{edit};
  $params->{join_policy}     = $params->{fields}->{policy}->{join};
  $params->{members_raw}     = $params->{attachments}->{members}->{members};

  delete $params->{fields};
  delete $params->{attachments};

  return $params;
}

# {
#   "data": [
#     {
#       "id": 1,
#       "type": "PROJ",
#       "phid": "PHID-PROJ-pfssn7lndryddv7hbx4i",
#       "fields": {
#         "name": "bmo-core-security",
#         "slug": "bmo-core-security",
#         "milestone": null,
#         "depth": 0,
#         "parent": null,
#         "icon": {
#           "key": "group",
#           "name": "Group",
#           "icon": "fa-users"
#         },
#         "color": {
#           "key": "red",
#           "name": "Red"
#         },
#         "dateCreated": 1500403964,
#         "dateModified": 1505248862,
#         "policy": {
#           "view": "secure-revision",
#           "edit": "secure-revision",
#           "join": "secure-revision"
#         },
#         "description": "BMO Security Group for core-security"
#       },
#       "attachments": {
#         "members": {
#           "members": [
#             {
#               "phid": "PHID-USER-23ia7vewbjgcqahewncu"
#             },
#             {
#               "phid": "PHID-USER-uif2miph2poiehjeqn5q"
#             }
#           ]
#         }
#       }
#     }
#   ],
#   "maps": {
#     "slugMap": {}
#   },
#   "query": {
#     "queryKey": null
#   },
#   "cursor": {
#     "limit": 100,
#     "after": null,
#     "before": null,
#     "order": null
#   }
# }

#########################
#     Modification      #
#########################

sub create {
  state $check = compile(
    $Invocant | ClassName,
    Dict [
      name        => Str,
      description => Str,
      view_policy => Str,
      edit_policy => Str,
      join_policy => Str,
    ]
  );
  my ($class, $params) = $check->(@_);

  my $name        = trim($params->{name});
  my $description = $params->{description};
  my $view_policy = $params->{view_policy};
  my $edit_policy = $params->{edit_policy};
  my $join_policy = $params->{join_policy};

  my $data = {
    transactions => [
      {type => 'name',        value => $name},
      {type => 'description', value => $description},
      {type => 'edit',        value => $edit_policy},
      {type => 'join',        value => $join_policy},
      {type => 'view',        value => $view_policy},
      {type => 'icon',        value => 'group'},
      {type => 'color',       value => 'red'}
    ]
  };

  my $result = request('project.edit', $data);

  return $class->new_from_query({phids => [$result->{result}{object}{phid}]});
}

sub update {
  my ($self) = @_;

  my $data = {objectIdentifier => $self->phid, transactions => []};

  if ($self->{set_name}) {
    push(@{$data->{transactions}}, {type => 'name', value => $self->{set_name}});
  }

  if ($self->{set_description}) {
    push(
      @{$data->{transactions}},
      {type => 'description', value => $self->{set_description}}
    );
  }

  if ($self->{set_members}) {
    push(
      @{$data->{transactions}},
      {type => 'members.set', value => $self->{set_members}}
    );
  }
  else {
    if ($self->{add_members}) {
      push(
        @{$data->{transactions}},
        {type => 'members.add', value => $self->{add_members}}
      );
    }

    if ($self->{remove_members}) {
      push(
        @{$data->{transactions}},
        {type => 'members.remove', value => $self->{remove_members}}
      );
    }
  }

  if ($self->{set_policy}) {
    foreach my $name ("view", "edit") {
      next unless $self->{set_policy}->{$name};
      push(
        @{$data->{transactions}},
        {type => $name, value => $self->{set_policy}->{$name}}
      );
    }
  }

  if ($self->{add_projects}) {
    push(
      @{$data->{transactions}},
      {type => 'projects.add', value => $self->{add_projects}}
    );
  }

  if ($self->{remove_projects}) {
    push(
      @{$data->{transactions}},
      {type => 'projects.remove', value => $self->{remove_projects}}
    );
  }

  my $result = request('project.edit', $data);

  return $result;
}

#########################
#       Mutators        #
#########################

sub set_name {
  my ($self, $name) = @_;
  $name = trim($name);
  $self->{set_name} = $name;
}

sub set_description {
  my ($self, $description) = @_;
  $description = trim($description);
  $self->{set_description} = $description;
}

sub add_member {
  my ($self, $member) = @_;
  $self->{add_members} ||= [];
  my $member_phid = blessed $member ? $member->phid : $member;
  push(@{$self->{add_members}}, $member_phid);
}

sub remove_member {
  my ($self, $member) = @_;
  $self->{remove_members} ||= [];
  my $member_phid = blessed $member ? $member->phid : $member;
  push(@{$self->{remove_members}}, $member_phid);
}

sub set_members {
  my ($self, $members) = @_;
  $self->{set_members} = [map { blessed $_ ? $_->phid : $_ } @$members];
}

sub set_policy {
  my ($self, $name, $policy) = @_;
  $self->{set_policy} ||= {};
  $self->{set_policy}->{$name} = $policy;
}

############
# Builders #
############

sub _build_members {
  my ($self) = @_;
  return [] unless $self->members_raw;

  my @phids;
  foreach my $member (@{$self->members_raw}) {
    push(@phids, $member->{phid});
  }

  return [] if !@phids;

  return Bugzilla::Extension::PhabBugz::User->match({phids => \@phids});
}

1;