I need to get datas from GitHub and create CSV file using all informations abut speciffic issue.
What I have tried:
<?php
require 'vendor/autoload.php';
use GuzzleHttp\Client;
const GITHUB_ACC = 'fox';
const GITHUB_REPO = 'szif-gt';
const GITHUB_TOKEN = '[REMOVED]';
const GITHUB_URL = 'https://api.github.com/repos/' . GITHUB_ACC .'/'. GITHUB_REPO . '/issues';
const MAX_ISSUES = 1000;
date_default_timezone_set('Europe/Prague');
header("Content-type: text/plain");
header("Content-Disposition: attachment");
$apiUrl = '"https://api.github.com/repos/{Pavel2Bambuch}/{szif-gt}/gtf-jira-git/jira-int//issues?q"';
$accessToken = '[REMOVED]';
$page = 1;
$data = array(array('"issueNumber","issueName","creationDate","creatorName","firstComment","lastComment","status(open/close)","ClosedDate/Assigned","IssueLabels"'));
$client = new Client();
$response = $client->get($apiUrl, [ 'headers'=>['Authorization' => 'Bearer' . $accessToken, 'Accept' => 'application/vnd.github.v3+json',],]);
$issues = json_decode ($response->getBody(), true);
foreach ($issues as $issue) {
$issueNumber = $issue['number'];
$issueName = $issue['title'];
$creationDate = $issue['created_at'];
$creatorName = $issue['user']['login'];
$commentsResponse = $client->get($issue['comments_url'], [
'headers' => [
'Authorization' => 'Bearer ' . $accessToken,
'Accept' => 'application/vnd.github.v3+json',
],
]);
$comments = json_decode($commentsResponse->getBody(), true);
$firstComment = $comments[0]['body'];
$lastComment = end($comments)['body'];
$status = ($issue['state'] == 'closed') ? 'closed' : 'open';
if ($status == 'closed') {$closedDate/Assigned = ($issue['closed_at']};
else {$closedData/Assigned = ($issue['assignee'] !== null) ? $issue['assignee']['login'] : 'none'};
$labels = implode(',', array_column($issue['labels'], 'name')});
$data[] = array(
$issueNumber,
$issueName,
$creationDate,
$creatorName,
$firstComment,
$lastComment,
$status,
$closedDate/Assigned,
$labels
);
}
$filename = 'csv_' . date('YmdHis') . '.csv';
$fp = fopen($filename, 'w');
foreach ($data as $row) {
fputcsv($fp, $row);
}
fclose($fp);
$to = 'o';
$subject = 'GitHub Issues CSV';
$message = 'V příloze by měl být CSV soubor.';
$headers = "From: u\r\n";
$headers .= "Reply-To: u\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: multipart/mixed; boundary=\"boundary\"\r\n";
$fileContent = file_get_contents($filename);
$fileContent = chunk_split(base64_encode($fileContent));
$attachment = "--boundary\r\n";
$attachment .= "Content-Type: application/octet-stream; name=\"" . basename($filename) . "\"\r\n";
$attachment .= "Content-Disposition: attachment\r\n";
$attachment
?>