Slide 1

Slide 1 text

R で Github Repository を綺麗にする by khiroyuki1993

Slide 2

Slide 2 text

自己紹介 黒宮寛之(くろみー) 今日が人生初LTです ~2023.3 京都大学情報学研究科 博士課程学生 2023.4- GMOペパボ株式会社 Webアプリケーションエンジニア Website: https://kuromiyahiroyuki.tech/

Slide 3

Slide 3 text

Rとのかかわり 主に論文を書くときに使っていた(データ分析、図の作成) 特に統計の専門家というわけではなく、標準的な?Rユーザ 割合は Python : R = 1:1 くらい アプリに組み込むときは Python だけど、単発の分析ならRみたいな感じ 博士号をとれたのもRのおかげと言っても過言ではない

Slide 4

Slide 4 text

最近気づいたこと Github Repository の数が多い!

Slide 5

Slide 5 text

Github CLI を使って数えてみる % gh repo list --limit 100 | wc -l 72 --limit option をつけないとデフォルトで30個までしかとってこないので注意

Slide 6

Slide 6 text

全部有益なコードだったらいいのだが、(もちろん)大半は全く使い道のないレポジトリ フォークして放置 レポジトリ(とREADME)だけ作って放置 大昔のレポート

Slide 7

Slide 7 text

最近のGithub Private Repository の数無制限だからそのままでいいん じゃない?

Slide 8

Slide 8 text

ミニマリストへの憧れ 要らないものを自分の部屋に置いておきたくない 例えどれだけ部屋が広いとしても

Slide 9

Slide 9 text

試したこと

Slide 10

Slide 10 text

1. 手動でレポジトリを一つ一つ削除する → だるすぎる

Slide 11

Slide 11 text

2. Github CLIでレポジトリの削除 gh コマンドでレポジトリの削除ができる gh alias set repo-delete 'api -X DELETE "repos/$1"' gh repo-delete vilmibm/deleteme How to delete a repo using gh cli · Issue #2461 · cli/cli → でもこれだけ多いと手動でレポジトリをリストアップするのも大変 使っていないレポジトリを更新日時順とかでソート/フィルタリングしてほしい

Slide 12

Slide 12 text

3. Open Interpreter 最近流行りの自動コーディングツール % interpreter --fast Prompt: **Sort the github repository by last update date using gh command.** To sort the GitHub repositories by the last update date, we can use the gh command with the --sort option. The updated field can be used to sort the repositories by the date of the last update. Let's execute this command. gh repo list --limit 100 --sort updated

Slide 13

Slide 13 text

結果 I apologize for the confusion. It seems that the gh repo list command does not upport the --sort option. However, we can achieve the same result by using the gh api command to interact with the GitHub API directly. This will allow us to sort the repositories by the last update date. gh api コマンドを使えばできるっぽいがあまり複雑なシェルコマンドを打ちたくない(情 弱) → じゃあどうするか → 僕たちには R があるじゃないか

Slide 14

Slide 14 text

やったこと ほぼこの記事のパクリ Cleaning up forked GitHub repositories with {gh}

Slide 15

Slide 15 text

1. gh library をインストール install.packages("gh") 2. PAT を設定 credentials::set_github_pat()

Slide 16

Slide 16 text

3. 元記事の方法だと private repository が取れないので これを使う my_repos = gh("GET /user/repos", visibility = "all", affiliation = "owner", before = "2022-12-01T", sort = "updated", page = 1, per_page = 100) これでレポジトリの更新日時でフィルタリングしたりソートしたりできる (dplyr::filter とか使う必要もなかった) 参考 Repositories - GitHub Docs

Slide 17

Slide 17 text

4. レポジトリを更新日時でフィルタリングして削除 # list of list から dataframe に変換 targeted_repos = purrr::map_dfr(my_repos, ~unlist(.x["name"])) repos_to_delete = targeted_repos$name # 削除するレポジトリを手動で調整 excluded_repos = c("important repo") to_delete = setdiff(repos_to_delete, excluded_repos) # レポジトリを削除 purrr::map(to_delete, ~gh("DELETE /repos/:owner/:repo", owner = my_username, repo = .x))

Slide 18

Slide 18 text

レポジトリが消えた(-40個)

Slide 19

Slide 19 text

まとめ gh パッケージを使えばR から Github API を叩くことができる レポジトリ一覧が dataframe 形式で手に入るのでそこから R の様々な関数が適用可能

Slide 20

Slide 20 text

Rを使って快適な Github Life by khiroyuki1993