{"id":438,"date":"2018-02-23T15:00:00","date_gmt":"2018-02-23T06:00:00","guid":{"rendered":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/02\/23\/slash_cmd_for_prs"},"modified":"2022-10-29T22:41:19","modified_gmt":"2022-10-29T13:41:19","slug":"slash_cmd_for_prs","status":"publish","type":"post","link":"https:\/\/p-corporate-blog-cms.mmmcorp.co.jp\/blog\/2018\/02\/23\/slash_cmd_for_prs\/","title":{"rendered":"PullRequest\u3092Slack\u304b\u3089\u53d6\u5f97\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u3066\u307f\u305f"},"content":{"rendered":"
MMM\u30b5\u30fc\u30d0\u30b5\u30a4\u30c9\u30a8\u30f3\u30b8\u30cb\u30a2\u306e\u67f3\u6cbc\u3067\u3059\u3002\u3053\u306e\u9593\u96ea\u307e\u3064\u308a\u306b\u884c\u304d\u307e\u3057\u305f\u3002<\/p>\n
MMM\u306f\u30e1\u30f3\u30d0\u30fc\u6570\u304c\u591a\u304f\u306a\u3044\u3053\u3068\u3082\u3042\u308a\u3001\u3072\u3068\u308a\u304c\u8907\u6570\u306e\u30d7\u30ed\u30b8\u30a7\u30af\u30c8\u306b\u95a2\u308f\u308b\u3053\u3068\u304c\u591a\u304f\u3042\u308a\u307e\u3059\u3002
\n\u305d\u306e\u3088\u3046\u306a\u72b6\u6cc1\u3067\u306f\u3001\u81ea\u5206\u306b\u6765\u3066\u3044\u308b\u30d7\u30eb\u30ea\u30af\u30a8\u30b9\u30c8\u3092\u898b\u843d\u3068\u3057\u304c\u3061\u3067\u3001\u305f\u307e\u306b\u50ac\u4fc3\u3055\u308c\u308b\u3053\u3068\u304c\u3042\u308a\u307e\u3059\u3002
\n\u3042\u307e\u308a\u826f\u304f\u306a\u3044\u72b6\u6cc1\u306a\u306e\u3067\u3001\u6539\u5584\u65b9\u6cd5\u3092\u8003\u3048\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n
\u3053\u3061\u3089\u306e\u30ea\u30dd\u30b8\u30c8\u30ea<\/a>\u306b\u7f6e\u3044\u3068\u304d\u307e\u3057\u305f\u3002 Go\u88fd\u3067\u3059\u3002 API\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u306e\u5b9f\u88c5\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u884c\u3044\u307e\u3059\u3002<\/p>\n client\u69cb\u9020\u4f53\u3092\u5b9a\u7fa9\u3057\u3001<\/p>\n client\u69cb\u9020\u4f53\u3092\u751f\u6210\u3059\u308b\u95a2\u6570\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002 \u305d\u3057\u3066\u3001 \u6700\u5f8c\u306b\u3001 \u5f8c\u306f\u3001\u5b9f\u969b\u306eAPI\u306b\u5bfe\u5fdc\u3059\u308b\u30e1\u30bd\u30c3\u30c9\u3092\u4f5c\u308a\u307e\u3059\u3002 \u3053\u308c\u3089\u3092\u5229\u7528\u3059\u308b\u3068\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306bAPI\u304c\u53e9\u3051\u307e\u3059\u3002<\/p>\n \u3053\u306e\u3084\u308a\u65b9\u3067\u3042\u308c\u3070\u3001\u5225\u306eAPI\u3092\u4f7f\u3046\u3068\u304d\u3082\u3001\u30e1\u30bd\u30c3\u30c9\u3092\u5897\u3084\u305b\u3070\u5bfe\u5fdc\u3067\u304d\u308b\u3057\u3001main\u306e\u8a18\u8ff0\u304c\u6e1b\u308a\u307e\u3059\u3002 <\/p>\n
\nMMM\u306b\u30d5\u30a3\u30c3\u30c8\u3059\u308b\u3088\u3046\u306b\u4f5c\u3063\u3066\u3042\u308b\u306e\u3067\u3001\u305d\u306e\u307e\u307e\u306f\u4f7f\u3048\u307e\u305b\u3093\u3002<\/p>\n\u30b9\u30e9\u30c3\u30b7\u30e5\u30b3\u30de\u30f3\u30c9\u306e\u4f5c\u6210<\/h1>\n
\n\u30b9\u30e9\u30c3\u30b7\u30e5\u30b3\u30de\u30f3\u30c9\u3092\u3064\u304f\u308b\u306e\u306f\u59cb\u3081\u3066\u3067\u3057\u305f\u304c\u3001\u516c\u5f0f<\/a>\u3092
\n\u53c2\u8003\u306b\u3001\u7c21\u5358\u306b\u4f5c\u308c\u307e\u3057\u305f\u3002
\n\u30a8\u30f3\u30c9\u30dd\u30a4\u30f3\u30c8\u3068\u30dd\u30fc\u30c8\u3092\u6307\u5b9a\u3059\u308c\u3070Post\u30ea\u30af\u30a8\u30b9\u30c8\u304c\u98db\u3093\u3067\u304f\u308c\u308b\u306e\u3067\u3001EC2\u306a\u3069\u3067\u4f5c\u308b\u5834\u5408\u306f\u30bb\u30ad\u30e5\u30ea\u30c6\u30a3\u30b0\u30eb\u30fc\u30d7\u306e\u8a2d\u5b9a\u3092\u3044\u3044\u611f\u3058\u306b\u3084\u3063\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\nGo\u3067API\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u3092\u5b9f\u88c5\u3059\u308b\u3053\u3068<\/h1>\n
type client struct {\n URL *url.URL\n Org string\n HTTPClient *http.Client\n Authorization string\n Logger *log.Logger\n}<\/code><\/pre>\n
func newClient(org string, auth string) *client {\n client := new(client)\n u, _ := url.Parse("https:\/\/api.github.com")\n client.URL = u\n client.Org = org\n client.HTTPClient = &http.Client{}\n client.Authorization = auth\n f, _ := os.OpenFile("access.log", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0755)\n client.Logger = log.New(f, "logger: ", log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)\n return client\n}<\/code><\/pre>\n
\n\u3053\u3061\u3089\u306e\u5b9f\u88c5\u3067\u306f\u30ed\u30b0\u51fa\u529b\u5148\u3092\u30d5\u30a1\u30a4\u30eb\u306b\u3057\u3066\u3044\u307e\u3059\u304c\u3001\u5f15\u6570\u3067\u6e21\u3059\u3088\u3046\u306b\u3059\u308c\u3070\u6a19\u6e96\u51fa\u529b\u306b\u3082\u3067\u304d\u307e\u3059\u3002
\nAPI\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306a\u3069\u304c\u7570\u306a\u308b\u5834\u5408\u306f\u3001\u4e2d\u306eURL\u3092\u5909\u3048\u3066\u3001 clientV1<\/code>
clientV2<\/code> \u306a\u3069\u306b\u69cb\u9020\u4f53\u3092\u5206\u3051\u308b\u306e\u304c\u304a\u3059\u3059\u3081\u3067\u3059\u3002<\/p>\n
newRequest<\/code> \u30e1\u30bd\u30c3\u30c9\u3092\u5b9a\u7fa9\u3057\u307e\u3059\u3002<\/p>\n
func (c *client) newRequest(method string, spath string, body io.Reader) (*http.Request, error) {\n u := *c.URL\n u.Path = path.Join(c.URL.Path, spath)\n req, err := http.NewRequest(method, u.String(), body)\n if err != nil {\n return nil, err\n }\n req.Header.Set("Authorization", c.Authorization)\n return req, nil\n}<\/code><\/pre>\n
decodeBody<\/code> \u95a2\u6570\u3092\u4f5c\u3063\u3066\u304a\u304d\u307e\u3059\u3002<\/p>\n
func decodeBody(resp *http.Response, out interface{}) error {\n defer resp.Body.Close()\n decoder := json.NewDecoder(resp.Body)\n return decoder.Decode(out)\n}<\/code><\/pre>\n
\n\u4f8b\u3048\u3070\u3001Github\u306e\u30d7\u30eb\u30ea\u30af\u30a8\u30b9\u30c8\u306eAPI<\/a>\u3092\u4f7f\u3046\u306b\u306f\u3001\u3053\u3093\u306a\u611f\u3058\u306b\u3067\u304d\u307e\u3059\u3002
\npullRequests<\/code> \u69cb\u9020\u4f53\u306f\u4e8b\u524d\u306b\u4f5c\u3063\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002\u7b46\u8005\u306f\u3053\u3061\u3089<\/a>\u3092\u3088\u304f\u5229\u7528\u3057\u3066\u307e\u3059\u3002<\/p>\n
func (c *client) getPRs(repo string) (*pullRequests, error) {\n spath := fmt.Sprintf("\/repos\/%v\/%v\/pulls", c.Org, repo)\n req, _ := c.newRequest("GET", spath, nil)\n res, err := c.HTTPClient.Do(req)\n if err != nil {\n return nil, err\n }\n defer res.Body.Close()\n var prs pullRequests\n if err := decodeBody(res, &prs); err != nil {\n return nil, err\n }\n return &prs, err\n}<\/code><\/pre>\n
func main() {\n var c *client\n c = newClient(os.Getenv("PR_GITHUB_ORG"), os.Getenv("PR_GITHUB_TOKEN"))\n prs, err := c.getPRs("example_repository_name")\n c.log(err)\n}<\/code><\/pre>\n
\n*http.Client<\/code> \u30aa\u30d6\u30b8\u30a7\u30af\u30c8\u3092\u4f7f\u3044\u307e\u308f\u305b\u308b\u306e\u3082\u30e1\u30ea\u30c3\u30c8\u3067\u3059\u3002<\/p>\n
\u4f7f\u3063\u3066\u307f\u305f\u69d8\u5b50<\/h1>\n
\u6240\u611f<\/h1>\n