Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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;

//Je potřeba nainstallova GuzzleHttp

//curl -X GET --url "https://api.github.com/repos/gtf-jira-git/jira-int/issues" --header "Authorization: Bearer [REMOVED]"
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]';

//obecné nastavení

$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'];

    // Get comments for the issue
    $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
    );
}

// Vytvoří CSV a pojmenuje podle data+času
$filename = 'csv_' . date('YmdHis') . '.csv';

// Vytvoří CSV a zapíše do něj, po zapsání se uloží
$fp = fopen($filename, 'w');
foreach ($data as $row) {
    fputcsv($fp, $row);
}
fclose($fp);

// Pošle e-mail s přiloženým CSV
$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
?>
Posted
Updated 20-Jun-23 3:09am
v2
Comments
Richard MacCutchan 20-Jun-23 4:13am    
OK, and what is the problem with the above code?
Pavel Bambuch 20-Jun-23 4:16am    
Somehow it doesn't seems to work and die after a few lines.... :/
Richard MacCutchan 20-Jun-23 4:38am    
That is not really sufficient information for anyone here to guess what is happening. You will need to add some debug code to gather more information.
Dave Kreskowiak 20-Jun-23 9:08am    
Define "die" and give the exact text of any error messages you're getting.

Oh, and DO NOT POST ANY ACCESS TOKENS TO PUBLIC FORUMS UNLESS YOU WANT YOUR TOKENS STOLEN!
Pavel Bambuch 20-Jun-23 9:14am    
This token is cutted. :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900