Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
バックログのタスクをローカルにダウンロードする使い捨てのクソスクリプト
#!/usr/bin/env ruby
# coding: utf-8
#
# バックログのタスクをローカルにダウンロードする使い捨てのクソスクリプト
#
# カレントディレクトリ以下に issues/ って名前のディレクトリを作り、
# issues/ST-1.txt みたいな名前のファイルに課題の内容(タイトル、本文、コメントetc)を記録、
# issues/ST-1/ みたいな名前のディレクトリ以下に課題の添付ファイルをダウンロード
# レジューム機能付き。途中で Ctrl-C で停止した場合、前回ダウンロード完了分移行からダウンロード開始する
require "uri"
require "net/http"
require "json"
require "pry"
# vvv ここを書き換えてね vvv
BASE_URL = "https://skimatalk.backlog.jp/api/v2/"
API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
# ^^^ ここを書き換えてね ^^^
def get(uri)
response = Net::HTTP.get_response(uri)
unless response.code == "200"
fail "failed to download #{uri}"
end
response.body
end
def get_api(path, params = {})
params[:apiKey] = API_KEY
url = URI.join(BASE_URL, path)
url += "?" + params.map{|key, value| "#{key}=#{value}" }.join("&")
json = get(url)
JSON.parse(json)
end
def download_api(path, params = {})
params[:apiKey] = API_KEY
url = URI.join(BASE_URL, path)
url += "?" + params.map{|key, value| "#{key}=#{value}" }.join("&")
get(url)
end
def save(dirname, filename, content)
filename = filename.tr("/", "_").delete(":")
path = File.join(dirname, filename)
open(path, "wb") do |output|
output.write(content)
end
end
def save_issue(issue, dirname = "issues")
# 課題
puts ">> #{issue["issueKey"]} #{issue["summary"]}"
filename = "#{issue["issueKey"]}.txt"
unless File.exist? File.join(dirname, filename)
issueContent = <<HERE
[#{issue["status"]["name"]}] #{issue["issueKey"]} #{issue["summary"]}
担当者: #{issue["assignee"]["name"] rescue nil}
作成者: #{issue["createdUser"]["name"]}
作成日: #{issue["created"]}
開始日: #{issue["startDate"]}
期限日: #{issue["dueDate"]}
#{issue["description"]}
HERE
# 課題コメント
minId = nil
while true do
puts ">> DOWNLOADING COMMENTS FOR #{issue["issueKey"]}"
comments = get_api("issues/#{issue["id"]}/comments", count: 100, order: "asc", minId: minId);
break if comments.empty?
comments.each do |comment|
next if comment["content"].nil? || comment["content"].empty?
issueContent +=
"--------------------------------------------------\n" +
"#{comment["content"]}\n" +
"by #{comment["createdUser"]["name"]}, #{comment["created"]}\n"
end
break if comments.size < 100
minId = comments.last["id"]
end
# 課題内容を保存
save(dirname, filename, issueContent)
end
# 添付ファイル
if issue["attachments"].any?
# 添付ファイル用ディレクトリ作成
atmt_dirname = File.join(dirname, issue["issueKey"])
Dir.mkdir(atmt_dirname) unless Dir.exist?(atmt_dirname)
# 添付ファイルをダウンロード
issue["attachments"].each do |attachment|
puts ">> DOWNLOADING ATTAACHMENT #{attachment["name"]}"
filename = "#{attachment["id"]}-#{attachment["name"]}"
next if File.exist? File.join(atmt_dirname, filename)
content = download_api("issues/#{issue["id"]}/attachments/#{attachment["id"]}")
save(atmt_dirname, filename, content)
end
end
end
# DIRNAME = "wikis"
# Dir.mkdir(DIRNAME) unless Dir.exist?(DIRNAME)
# items = get_api("wikis", projectIdOrKey: "ST")
# items.each do |wiki|
# puts "wiki##{wiki["id"]}"
# wikipage = get_api("wikis/#{wiki["id"]}")
# puts wikipage["name"]
# filename = "#{wikipage["name"]}.txt"
# save(DIRNAME, filename, wikipage["content"])
# end
dirname = "issues"
Dir.mkdir(dirname) unless Dir.exist?(dirname)
offset = Dir.glob("#{dirname}/*.txt").size - 1
while true do
# 課題一覧を取得
puts ">>>> DOWNLOADING NEXT 100 ISSUES..."
issues = get_api("issues", count: 100, offset: offset)
puts ">>>> FOUND #{issues.size} ISSUES"
break if issues.empty?
issues.each do |issue|
save_issue(issue, dirname)
end
offset += issues.size
end
@monmonmon
Copy link
Author

monmonmon commented Feb 3, 2016

例。こんな感じでファイルに保存するよ

$ cat issues/ST-1460.txt
[未対応] ST-1460 rails3.2 -> rails4.2
担当者: monmon
作成者: monmon
作成日: 2015-12-23T01:08:54Z
開始日: 2015-12-23T00:00:00Z
期限日: 2015-12-25T00:00:00Z

http://railsguides.jp/upgrading_ruby_on_rails.html#rails-4-1からrails-4-2へのアップグレード

--------------------------------------------------
ST-1460 update rails to 4.0
by monmon, 2015-12-26T09:20:41Z
--------------------------------------------------
ST-1460 replace command files
by monmon, 2015-12-26T09:20:41Z
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment