protected function execute(InputInterface $input, OutputInterface $output) { if ($input->hasOption('input-file') && $input->getOption('input-file')) { $commentFilePath = $input->getOption('input-file'); if (!file_exists($commentFilePath)) { throw new \Exception("{$commentFilePath} not found"); } $commentText = file_get_contents($commentFilePath); } else { if ($input->hasOption('string') && $input->getOption('string')) { $commentText = $input->getOption('string'); } } if (!$commentText) { throw new \Exception('No comment input, add a comment text with -s or -i options'); } $client = new Client(); $request = $client->createRequest('GET', 'https://www.pivotaltracker.com/services/v5/me', array('X-TrackerToken' => 'be17fcf368af9fa35cfe88b7460d2c67')); $response = $client->send($request); $this->projects = array_map(function ($item) { return $item['project_id']; }, json_decode($response->getBody(), true)['projects']); $labelsList = implode(',', $this->projects); $extractor = new StoriesExtractor(); $storyIds = $extractor->collect(); $storyCount = count($storyIds); $output->writeln("Adding comments on {$storyCount} stories on projects {$labelsList}"); foreach ($storyIds as $storyId) { foreach ($this->projects as $project) { $request = $client->createRequest('POST', "https://www.pivotaltracker.com/services/v5/projects/{$project}/stories/{$storyId}/comments"); $request->setHeader('X-TrackerToken', 'be17fcf368af9fa35cfe88b7460d2c67'); $request->setHeader('Content-type', 'application/json'); $request->setHeader('Accept', 'application/json'); $request->setBody(json_encode(['text' => $commentText])); try { $client->send($request); $output->write('.'); break; } catch (ClientErrorResponseException $ex) { $output->writeln('Could not comment on story ' . $storyId); $output->writeln($ex->getResponse()->getBody(true), true); } } } $output->writeln(''); }
protected function execute(InputInterface $input, OutputInterface $output) { $label = $input->getArgument('label'); $extractor = new StoriesExtractor(); $storyIds = $extractor->collect(); $client = new Client(); $request = $client->createRequest('GET', 'https://www.pivotaltracker.com/services/v5/me', array('X-TrackerToken' => 'be17fcf368af9fa35cfe88b7460d2c67')); $response = $client->send($request); $this->projects = array_map(function ($item) { return $item['project_id']; }, json_decode($response->getBody(), true)['projects']); $labelsList = implode(',', $this->projects); $storyCount = count($storyIds); $output->writeln("Labeling {$storyCount} stories on projects {$labelsList}"); foreach ($storyIds as $storyId) { foreach ($this->projects as $project) { $request = $client->createRequest('GET', "https://www.pivotaltracker.com/services/v5/projects/{$project}/stories/{$storyId}", ['X-TrackerToken' => 'be17fcf368af9fa35cfe88b7460d2c67']); // $request->setHeader§('X-TrackerToken', 'be17fcf368af9fa35cfe88b7460d2c67'); try { $storyData = $client->send($request); } catch (ClientErrorResponseException $ex) { continue; } $body = $storyData->getBody(true); $storyData = json_decode($body, true); $labels = $storyData['labels']; $labels[] = ['name' => $label]; $request = $client->createRequest('PUT', "https://www.pivotaltracker.com/services/v5/projects/{$project}/stories/{$storyId}"); $request->setHeader('X-TrackerToken', 'be17fcf368af9fa35cfe88b7460d2c67'); $request->setHeader('Content-type', 'application/json'); $request->setHeader('Accept', 'application/json'); $request->setBody(json_encode(['labels' => $labels])); try { $client->send($request); $output->write('.'); break; } catch (ClientErrorResponseException $ex) { $output->writeln('Could not label story ' . $storyId); $output->writeln($ex->getResponse()->getBody(true), true); } } } $output->writeln(''); }