summaryrefslogtreecommitdiffstats
path: root/extensions/PhabBugz/lib/Policy.pm
blob: 658d0dddefa8266bcdb2ef99f9c8594c0fb09b0e (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
# 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::Policy;

use 5.10.1;
use Moo;

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

use List::Util qw(first);

use Types::Standard -all;
use Type::Utils;
use Type::Params qw( compile );

has 'phid'      => (is => 'ro', isa => Str);
has 'type'      => (is => 'ro', isa => Str);
has 'name'      => (is => 'ro', isa => Str);
has 'shortName' => (is => 'ro', isa => Str);
has 'fullName'  => (is => 'ro', isa => Str);
has 'href'      => (is => 'ro', isa => Maybe [Str]);
has 'workflow'  => (is => 'ro', isa => Maybe [Str]);
has 'icon'      => (is => 'ro', isa => Str);
has 'default'   => (is => 'ro', isa => Str);
has 'rules' => (
  is => 'ro',
  isa =>
    ArrayRef [Dict [action => Str, rule => Str, value => Maybe [ArrayRef [Str]]]]
);

has 'rule_projects' => (is => 'lazy', isa => ArrayRef [Project],);

# {
#   "data": [
#     {
#       "phid": "PHID-PLCY-l2mt4yeq4byqgcot7x4j",
#       "type": "custom",
#       "name": "Custom Policy",
#       "shortName": "Custom Policy",
#       "fullName": "Custom Policy",
#       "href": null,
#       "workflow": null,
#       "icon": "fa-certificate",
#       "default": "deny",
#       "rules": [
#         {
#           "action": "allow",
#           "rule": "PhabricatorSubscriptionsSubscribersPolicyRule",
#           "value": null
#         },
#         {
#           "action": "allow",
#           "rule": "PhabricatorProjectsPolicyRule",
#           "value": [
#             "PHID-PROJ-cvurjiwfvh756mv2vhvi"
#           ]
#         }
#       ]
#     }
#   ],
#   "cursor": {
#     "limit": 100,
#     "after": null,
#     "before": null
#   }
# }

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

sub new_from_query {
  state $check = compile($Invocant | ClassName, Dict [phids => ArrayRef [Str]]);
  my ($class, $params) = $check->(@_);
  my $result = request('policy.query', $params);
  if (exists $result->{result}{data} && @{$result->{result}{data}}) {
    return $class->new($result->{result}->{data}->[0]);
  }
}

sub create {
  state $check = compile($Invocant | ClassName, ArrayRef [Project]);
  my ($class, $projects) = $check->(@_);

  my $data = {
    objectType => 'DREV',
    default    => 'deny',
    policy     => [
      {action => 'allow', rule => 'PhabricatorSubscriptionsSubscribersPolicyRule',},
      {action => 'allow', rule => 'PhabricatorDifferentialReviewersPolicyRule'}
    ]
  };

  if (@$projects) {
    push @{$data->{policy}},
      {
      action => 'allow',
      rule   => 'PhabricatorProjectsAllPolicyRule',
      value  => [map { $_->phid } @$projects],
      };
  }
  else {
    my $secure_revision
      = Bugzilla::Extension::PhabBugz::Project->new_from_query({
      name => 'secure-revision'
      });
    push @{$data->{policy}}, {action => 'allow', value => $secure_revision->phid};
  }

  my $result = request('policy.create', $data);
  return $class->new_from_query({phids => [$result->{result}{phid}]});
}

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

  return [] unless $self->rules;
  my $rule = first { $_->{rule} =~ /PhabricatorProjects(?:All)?PolicyRule/ }
  @{$self->rules};
  return [] unless $rule;
  return [
    map { Bugzilla::Extension::PhabBugz::Project->new_from_query({phids => [$_]}) }
      @{$rule->{value}}
  ];
}

1;