DEV Community

Rin
Rin

Posted on • Updated on

Removes the specified Collaborators from all repositories of the target Gituhub account.

Removes the specified Collaborators from all repositories of the target Gituhub account.

Please check this Github for details.

Overall picture


 /**
     * GLOBAL
     * gitアカウントのメールアドレス
     * ログインパスワード
     * githubのアカウント名
     * 削除したいユーザー
     * を入力してください。
     */
    $Email = '';
    $PASS = '';
    $USER = '';
    $DELTE_USER = '';


    /**
     * 下記のAPIを実行し、api.jsonを作成してください。別途アクセストークンが必要です。
     * curl  "https://api.github.com/user/repos?access_token={token}&per_page=100&page=1&sort=created" > api.json
     */

    $url = './api.json';
    $json = file_get_contents($url);
    $json = mb_convert_encoding($json, 'UTF8', 'ASCII,JIS,UTF-8,EUC-JP,SJIS-WIN');
    $arr = json_decode($json,true);
    echo $count = count($arr);


    for ($i = 0; $i < $count; ++$i) {
        $repoName = $arr[$i]['name'];

        $cmd = "curl -X DELETE -u $Email:$PASS 'https://api.github.com/repos/$USER/$repoName/collaborators/$DELTE_USER'";
        echo $repoName;
        echo exec($cmd);
    }

Enter fullscreen mode Exit fullscreen mode

First, please get APIKey from setting of Github management screen.
In that case, please check the repo of Select scopes.

After obtaining the access token, execute the following command to create api.json.

curl  "https://api.github.com/user/repos?access_token={token}&per_page=100&page=1&sort=created" > api.json
Enter fullscreen mode Exit fullscreen mode

Specify a variable of remove.php.

$Email = 'hoge@gmail.com';
$PASS = 'password';
$USER = 'MyName';
$DELTE_USER = 'user_id';
Enter fullscreen mode Exit fullscreen mode

Run remove.php

php remove.php
Enter fullscreen mode Exit fullscreen mode

Supplement

A maximum of 100 repositories can be acquired.
If you want to get more than 100 repositories, create additional json file separately as below,
Run PHP again specifying the file.

curl  "https://api.github.com/user/repos?access_token={token}&per_page=100&page=2&sort=created" > api_2.json
Enter fullscreen mode Exit fullscreen mode

remove.php

$url = './api_2.json';
Enter fullscreen mode Exit fullscreen mode

Top comments (0)