Last active
January 24, 2024 13:11
-
-
Save tuchangwei/1ddc08c0caceaaec76bb9297343a19a2 to your computer and use it in GitHub Desktop.
Upload git log to Clickup via git hooks
This file contains 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
#!/usr/bin/env ruby | |
# how to use? | |
# 1, input your your_api_key | |
# 2, make the file executable first: chmod +x post-commit | |
# 3, put the file in your_project/.git/hooks/ folder | |
require "git" | |
require 'net/http' | |
require 'json' | |
class Task | |
attr_accessor :name, :text_content, :description, :id | |
def initialize(name, text_content, description, id) | |
@name = name | |
@text_content = text_content | |
@description = description | |
@id = id | |
end | |
end | |
#定义一个方法,将json转换成Task对象 | |
def json_to_task(json) | |
task = Task.new(json['name'], json['text_content'], json['description'], json['id']) | |
task | |
end | |
repo = Git.open(Dir.pwd) | |
repo_name = Dir.pwd[%r{[\.\-\w]+$}] # top directory | |
date = repo.log.first.date.strftime("%Y-%m-%d") # format date | |
message = repo.log.first.message.lines.first.strip # just first line | |
branch = repo.log.first.name | |
apiKey = 'your_api_key' | |
url = URI.parse("https://api.clickup.com/api/v2/list/164637054/task") | |
http = Net::HTTP.new(url.host, url.port) | |
http.use_ssl = true | |
request = Net::HTTP::Get.new(url.request_uri) | |
request['Authorization'] = apiKey | |
response = http.request(request) | |
#将 response.body 转换成Task对象数组 | |
tasks = JSON.parse(response.body)['tasks'].map { |json| json_to_task(json) } | |
#找出tasks中name为“2024-01-22”的task | |
task = tasks.find { |task| task.name == date } | |
# 如果这个task存在,就更新它, 否则就创建一个新的task | |
if task | |
# 得到text_content的行数 | |
lines = task.text_content.lines.count | |
lines = lines + 1 | |
# 在text_content的后面添加一行 | |
task.text_content += "\n#{lines}, #{message}" | |
task.description += "\n#{lines}, #{message}" | |
# 通过https://api.clickup.com/api/v2/task/task_id更新task | |
url = URI.parse("https://api.clickup.com/api/v2/task/#{task.id}") | |
http = Net::HTTP.new(url.host, url.port) | |
http.use_ssl = true | |
request = Net::HTTP::Put.new(url.request_uri) | |
request['Authorization'] = apiKey | |
request['Content-Type'] = 'application/json' | |
request.body = {"name": "#{task.name}", "description": "#{task.description}"}.to_json | |
response = http.request(request) | |
# 判断是否更新成功 | |
if response.code == '200' | |
puts "更新成功" | |
else | |
puts "更新失败" | |
end | |
else | |
# 通过https://api.clickup.com/api/v2/list/list_id/task创建task | |
url = URI.parse("https://api.clickup.com/api/v2/list/164637054/task") | |
http = Net::HTTP.new(url.host, url.port) | |
http.use_ssl = true | |
request = Net::HTTP::Post.new(url.request_uri) | |
request['Authorization'] = apiKey | |
request['Content-Type'] = 'application/json' | |
request.body = {"name": "#{date}", "description": "#{message}", "text_content": "#{message}", "status": "Completed"}.to_json | |
response = http.request(request) | |
# 判断是否创建成功 | |
if response.code == '200' | |
puts "创建成功" | |
else | |
puts "创建失败" | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment