Skip to content

Instantly share code, notes, and snippets.

@jue58

jue58/assign_pr.rb

Last active Apr 18, 2016
Embed
What would you like to do?
This is the code that a pull request is automatically assigned to a member at random by the bot.
# メンバーをランダムに選んで、プルリクエストの担当者に割りあてる
#
# backlogに下記の条件のWebhookを追加する
# - WebHook URL: このコードに割り当てられたURL
# - 通知するイベント: 「プルリクエストの追加」
#
require 'json'
require 'faraday'
require 'faraday_middleware'
# webhookのデータを取得
pr = JSON.parse(payload)
project_key = pr['project']['projectKey']
repo_id = pr['content']['repository']['id']
number = pr['content']['number']
connection =
Faraday.new(url: 'https://<SpaceID>.backlog.jp/') do |builder|
builder.request :url_encoded
builder.response :json
builder.adapter :net_http
end
path = "api/v2/projects/#{project_key}/git/repositories/#{repo_id}/pullRequests/#{number}"
# assigneeをAPIから取得
response =
connection.get do |request|
request.url path
request.params = { apiKey: ENV['BACKLOG_API_KEY'] }
end
assignee = response.body['assignee']
assignor = response.body['createdUser']
if assignee
puts 'assignee already exists'
exit
end
members = [1, 2, 3, 4] # プルリクエストを受けられるアカウントのID
members.delete(assignor['id'])
reviewer_id = members.sample
# assigneeを設定
response =
connection.patch do |request|
request.url path
request.params = { apiKey: ENV['BACKLOG_API_KEY'], assigneeId: reviewer_id }
request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
end
puts response.status
# プルリクエストに「reassign」とコメントすると担当者を割り当てしなおす
#
# backlogに下記の条件のWebhookを追加する
# - WebHook URL: このコードに割り当てられたURL
# - 通知するイベント: 「プルリクエストにコメント」
#
require 'json'
require 'faraday'
require 'faraday_middleware'
# webhookのデータを取得
pr = JSON.parse(payload)
project_key = pr['project']['projectKey']
repo_id = pr['content']['repository']['id']
number = pr['content']['number']
comment = pr['content']['comment']['content']
exit if comment != 'reassign'
connection =
Faraday.new(url: 'https://<SpaceID>.backlog.jp/') do |builder|
builder.request :url_encoded
builder.response :json
builder.adapter :net_http
end
path = "api/v2/projects/#{project_key}/git/repositories/#{repo_id}/pullRequests/#{number}"
# assigneeをAPIから取得
response =
connection.get do |request|
request.url path
request.params = { apiKey: ENV['BACKLOG_API_KEY'] }
end
assignee = response.body['assignee']
assignor = response.body['createdUser']
members = [1, 2, 3, 4] # プルリクエストを受けられるアカウントのID
members.delete(assignor['id'])
members.delete(assignee['id']) if assignee
reviewer_id = members.sample
# assigneeを設定
response =
connection.patch do |request|
request.url path
request.params = { apiKey: ENV['BACKLOG_API_KEY'], assigneeId: reviewer_id }
request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
end
puts response.status
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment