gitlab apiいろいろ| プロサバメモ

WEBプログラミングやサーバ設定などのメモ場

gitlab apiいろいろ

2024.02.262024.02.27

1. ファイル作成

<?php


//for( $i=0; $i<1000; $i++) {
/*
$cmd = "curl --request POST --header 'PRIVATE-TOKEN: ' \
     --header \"Content-Type: application/json\" \
     --data '{\"branch\": \"main\",
        \"author_email\": \"author@example.com\",
        \"author_name\": \"Firstname Lastname\",
        \"content\": \"some content\",
        \"commit_message\": \"create a new file\"}' \
     \"http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/files/app%2Ftest{$i}%2Etxt\"";
*/

/*
$cmd = "curl --request POST \
     --form \"branch=main\" \
     --form \"commit_message=some commit message\" \
     --form \"start_branch=main\" \
--form \"actions[][action]=delete\" \
     --form \"actions[][file_path]=app/project.rb\" \
--header \"PRIVATE-TOKEN: \" \
     \"http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/commits\"";

$ret = shell_exec( $cmd );

echo "{$ret}\n";

exit;
*/



$head = "curl --request POST \
     --form \"branch=main\" \
     --form \"commit_message=some commit message\" \
     --form \"start_branch=main\" \
";

$del = "";
for( $i=1000; $i<=1500; $i++ ) {

     $cre .= "--form \"actions[][action]=create\" \
     --form \"actions[][file_path]=app2/test{$i}.txt\" \
     --form \"actions[][content]=test\" \
";
}

$cmd = $head . $cre . "--header \"PRIVATE-TOKEN: \" \
     \"http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/commits\"
";

echo "{$cmd}\n";


$ret = shell_exec( $cmd );

echo "[$ret]\n";

//}

2. ファイル削除

<?php

//for( $i=0; $i<1000; $i++) {
/*
$cmd = "curl --request POST --header 'PRIVATE-TOKEN: ' \
     --header \"Content-Type: application/json\" \
     --data '{\"branch\": \"main\",
        \"author_email\": \"author@example.com\",
        \"author_name\": \"Firstname Lastname\",
        \"content\": \"some content\",
        \"commit_message\": \"create a new file\"}' \
     \"http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/files/app%2Ftest{$i}%2Etxt\"";
*/

/*
$cmd = "curl --request POST \
     --form \"branch=main\" \
     --form \"commit_message=some commit message\" \
     --form \"start_branch=main\" \
--form \"actions[][action]=delete\" \
     --form \"actions[][file_path]=app/project.rb\" \
--header \"PRIVATE-TOKEN: \" \
     \"http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/commits\"";

$ret = shell_exec( $cmd );

echo "{$ret}\n";

exit;
*/



$head = "curl --request POST \
     --form \"branch=main\" \
     --form \"commit_message=some commit message\" \
     --form \"start_branch=main\" \
";

$del = "";
for( $i=0; $i<1001; $i++ ) {

     $del .= "--form \"actions[][action]=delete\" \
     --form \"actions[][file_path]=app/test{$i}.txt\" \
";
}

$cmd = $head . $del . "--header \"PRIVATE-TOKEN: \" \
     \"http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/commits\"
";

echo "{$cmd}\n";


$ret = shell_exec( $cmd );

echo "$ret\n";

//}

3. ファイルtree

<?php


$cmd = "curl --verbose --header \"PRIVATE-TOKEN: \" \"http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/tree?ref=main&pagination=keyset&per_page=100&recursive=true&order_by=id&sort=asc\"";


$opts = array(
        'http'=>array(
                'method' => 'GET',
                'header' => "PRIVATE-TOKEN: ",
        )
);
$context = stream_context_create($opts);
$response = file_get_contents('http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/tree?ref=main&pagination=keyset&per_page=100&recursive=true&order_by=id&sort=asc', false, $context);

print_r( $http_response_header );
print_r( $response );


exit;

4. ファイルツリーその2

<?php

$opts = array(
        'http'=>array(
                'method' => 'GET',
                'header' => "PRIVATE-TOKEN: ",
        )
);
$context = stream_context_create($opts);

$page_param = '';


$response = file_get_contents('http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/tree?ref=main&pagination=keyset&per_page=100&recursive=true&order_by=id&sort=asc', false, $context);

$res = json_decode( $response, true );

$total_page_num = get_total_page_num( $http_response_header );

for( $i=2; $i<=$total_page_num; $i++ ) {
        $url = "http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/tree?ref=main&pagination=keyset&per_page=100&page={$i}&recursive=true&order_by=id&sort=asc";
        $response = file_get_contents( $url, false, $context );

        $res = json_decode( $response, true );

        echo "url = {$url}\n";

        print_r( $res );

        print_r( $res[0]['path'] );
}


function get_next_page_num( $_http_response_header ) {
        foreach( $_http_response_header as $wk ) {
                if( preg_match( "/^X\-Next\-Page\: (\d+)$/", $wk, $args ) ) {
                        return $args[1];
                }
        }
        return null;
}

function get_total_page_num( $_http_response_header ) {
        foreach( $_http_response_header as $wk ) {
                if( preg_match( "/^X\-Total\-Pages\: (\d+)$/", $wk, $args ) ) {
                        return $args[1];
                }
        }
        return null;
}

function get_next_url( $_http_response_header ) {
        foreach( $_http_response_header as $wk ) {
                if( preg_match( "/^Link\: <(.+?)>.*$/", $wk, $args ) ) {
                        return $args[1];
                }
        }
        return null;
}

5. 消込

<?php


$data = [
        "branch" => "main",
        "commit_message" => "TESTTEST",
        "actions" => []
];


for( $i=0; $i<10000; $i++ ) {
        $data['actions'][] = [ "action" => "delete", "file_path" => "foo7000/x{$i}.txt", "content" => "{$i}" ];
}

//print_r( json_encode( $data ) );exit;


$url = "http://192.168.33.10/api/v4/projects/xxx%2Faaa/repository/commits";

$opts = array(
        'http'=>array(
                'method'  => 'POST',
                'header'  => [ "PRIVATE-TOKEN: ", "Content-Type: application/json" ],
                'content' => json_encode( $data ),
        )
);
$context = stream_context_create($opts);

var_dump( $context );

$response = file_get_contents( $url, false, $context );

print_r( $http_response_header );
print_r( $response );


exit;

関連記事

TOP