Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/phabricator
Path: blob/master/src/applications/doorkeeper/option/PhabricatorAsanaConfigOptions.php
12256 views
1
<?php
2
3
final class PhabricatorAsanaConfigOptions
4
extends PhabricatorApplicationConfigOptions {
5
6
public function getName() {
7
return pht('Integration with Asana');
8
}
9
10
public function getDescription() {
11
return pht('Asana integration options.');
12
}
13
14
public function getIcon() {
15
return 'fa-exchange';
16
}
17
18
public function getGroup() {
19
return 'core';
20
}
21
22
public function getOptions() {
23
return array(
24
$this->newOption('asana.workspace-id', 'string', null)
25
->setSummary(pht('Asana Workspace ID to publish into.'))
26
->setDescription(
27
pht(
28
'To enable synchronization into Asana, enter an Asana Workspace '.
29
'ID here.'.
30
"\n\n".
31
"NOTE: This feature is new and experimental.")),
32
$this->newOption('asana.project-ids', 'wild', null)
33
->setSummary(pht('Optional Asana projects to use as application tags.'))
34
->setDescription(
35
pht(
36
'When %s creates tasks in Asana, it can add the tasks '.
37
'to Asana projects based on which application the corresponding '.
38
'object in %s comes from. For example, you can add code '.
39
'reviews in Asana to a "Differential" project.'.
40
"\n\n".
41
'NOTE: This feature is new and experimental.',
42
PlatformSymbols::getPlatformServerName(),
43
PlatformSymbols::getPlatformServerName())),
44
);
45
}
46
47
public function renderContextualDescription(
48
PhabricatorConfigOption $option,
49
AphrontRequest $request) {
50
51
switch ($option->getKey()) {
52
case 'asana.workspace-id':
53
break;
54
case 'asana.project-ids':
55
return $this->renderContextualProjectDescription($option, $request);
56
default:
57
return parent::renderContextualDescription($option, $request);
58
}
59
60
$viewer = $request->getUser();
61
62
$provider = PhabricatorAsanaAuthProvider::getAsanaProvider();
63
if (!$provider) {
64
return null;
65
}
66
67
$account = id(new PhabricatorExternalAccountQuery())
68
->setViewer($viewer)
69
->withUserPHIDs(array($viewer->getPHID()))
70
->withProviderConfigPHIDs(
71
array(
72
$provider->getProviderConfigPHID(),
73
))
74
->requireCapabilities(
75
array(
76
PhabricatorPolicyCapability::CAN_VIEW,
77
PhabricatorPolicyCapability::CAN_EDIT,
78
))
79
->executeOne();
80
if (!$account) {
81
return null;
82
}
83
84
$token = $provider->getOAuthAccessToken($account);
85
if (!$token) {
86
return null;
87
}
88
89
try {
90
$workspaces = id(new PhutilAsanaFuture())
91
->setAccessToken($token)
92
->setRawAsanaQuery('workspaces')
93
->resolve();
94
} catch (Exception $ex) {
95
return null;
96
}
97
98
if (!$workspaces) {
99
return null;
100
}
101
102
$out = array();
103
$out[] = sprintf(
104
'| %s | %s |',
105
pht('Workspace ID'),
106
pht('Workspace Name'));
107
$out[] = '| ------------ | -------------- |';
108
foreach ($workspaces as $workspace) {
109
$out[] = sprintf(
110
'| `%s` | `%s` |',
111
$workspace['gid'],
112
$workspace['name']);
113
}
114
115
$out = implode("\n", $out);
116
117
$out = pht(
118
"The Asana Workspaces your linked account has access to are:\n\n%s",
119
$out);
120
121
return new PHUIRemarkupView($viewer, $out);
122
}
123
124
private function renderContextualProjectDescription(
125
PhabricatorConfigOption $option,
126
AphrontRequest $request) {
127
128
$viewer = $request->getUser();
129
130
$publishers = id(new PhutilClassMapQuery())
131
->setAncestorClass('DoorkeeperFeedStoryPublisher')
132
->execute();
133
134
$out = array();
135
$out[] = pht(
136
'To specify projects to add tasks to, enter a JSON map with publisher '.
137
'class names as keys and a list of project IDs as values. For example, '.
138
'to put Differential tasks into Asana projects with IDs `123` and '.
139
'`456`, enter:'.
140
"\n\n".
141
" lang=txt\n".
142
" {\n".
143
" \"DifferentialDoorkeeperRevisionFeedStoryPublisher\" : [123, 456]\n".
144
" }\n");
145
146
$out[] = pht('Available publishers class names are:');
147
foreach ($publishers as $publisher) {
148
$out[] = ' - `'.get_class($publisher).'`';
149
}
150
151
$out[] = pht(
152
'You can find an Asana project ID by clicking the project in Asana and '.
153
'then examining the URL:'.
154
"\n\n".
155
" lang=txt\n".
156
" https://app.asana.com/0/12345678901234567890/111111111111111111\n".
157
" ^^^^^^^^^^^^^^^^^^^^\n".
158
" This is the ID to use.\n");
159
160
$out = implode("\n", $out);
161
162
return new PHUIRemarkupView($viewer, $out);
163
}
164
165
}
166
167