Created
August 11, 2019 06:12
-
-
Save kemchenj/5a666cd81af2bc643af7d21149ee044c to your computer and use it in GitHub Desktop.
Gitment 迁移到 Gitalk 的脚本,处理 label 的重命名
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'openssl' | |
require 'json' | |
require 'net/http' | |
require 'cgi' | |
require 'digest' | |
repo = "kemchenj.github.io" | |
owner = "kemchenj" | |
account = "account" | |
password = "password" | |
base_uri = URI("https://api.github.com/") | |
# 获取所有 Label | |
repo_params = { "per_page": 1000 } | |
repo_uri = URI.join(base_uri.to_s, "repos/#{owner}/#{repo}/labels") | |
repo_uri.query = URI.encode_www_form(repo_params) | |
labels = JSON.parse(Net::HTTP.get(repo_uri)) | |
# Label 逐个 MD5 重命名 | |
labels | |
.map { |l| l["name"] } | |
.filter { |n| n.include?("/") } | |
.each { |l| | |
old = l.to_s | |
new = Digest::MD5.hexdigest old | |
p JSON.generate({ "old": old, "new": new }) | |
label_uri = URI.join(base_uri.to_s, "s/#{owner}/#{repo}/labels/#{CGI.escape(old)}") | |
req = Net::HTTP::Patch.new(label_uri) | |
req.body = JSON.generate({ "name": new }) | |
# 这里偷懒直接用了 basic auth | |
req.basic_auth account, password | |
req["Content-Type"] = "application/json" | |
res = Net::HTTP.start(label_uri.host, label_uri.port, :use_ssl => true) do |http| | |
http.request(req) | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment