LoginSignup
5
5

More than 5 years have passed since last update.

Githubでissueを作ってProjectに登録するPOSTスクリプトを作ってみた

Last updated at Posted at 2016-10-05

Githubで任意のリポジトリでissueを作って、任意のprojectのcolumnに登録する

※ただの作業記録です.調べた限りあんま情報なかったので。
Projectを弄れるAPIライブラリを見つけることができなかった。あったら辛い。

以下作業

まず、自分のGithubのトークンをとってくる

curlで必要な情報を取得しておく
なんかapiが試用期間らしくて変なヘッダーをつけないといけないらしい
まず、projectのidとprojectで作ったcolumnsのidが必要なのでそれを取得するためにそれをとっておく

curl -H "Accept: application/vnd.github.inertia-preview+json" -u "username:token" https://api.github.com/repos/チーム名/リポジトリ名/projects
curl -H "Accept: application/vnd.github.inertia-preview+json" -u "username:token" https://api.github.com/repos/チーム名/リポジトリ名/projects/(上で取得したprojectのid)/columns

ここで取得したidは以下のスクリプトで使うのでメモっておいてください.
あとwiresharkかなんかでAuthorization : Basicのトークンを取得しておくと楽です.
(username:tokenをbase64にエンコードすればいいのだが、したのにでbad食らったので通信出来たのもらってきた)

あとは以下のスクリプトに必要な値を代入するだけ。

Pythonバージョン

# -*- coding: utf-8 -*-
import requests
import json
import sys

response = {}
ownr = ''
repo = ''
column_id = ''
issue_url = "https://api.github.com/repos/" + ownr + "/" + repo + "/issues"
project_url = "https://api.github.com/repos/" + ownr + "/" + repo + "/projects/columns/" + column_id + "/cards"
headers = {
    "Authorization": "Basic base64化したusername:token",
    "Accept": "application/vnd.github.inertia-preview+json",
    "Content-Type": "application/json",
}
argvs = sys.argv
"""---------------Create Issue---------------------"""
params = {"title": argvs[1]}
res = requests.post(issue_url, data=json.dumps(params), headers=headers)
"""---------Register created issue to Project------"""
params = {"content_id": int(res.json()['id']), "content_type": "Issue"}
res = requests.post(project_url, data=json.dumps(params), headers=headers)
print(res.status_code)

Goバージョン

スクリプトに平文でtokenのせておくのがいやだったのでバイナリ化して運用できるようにするためにGoで作り直しました。(安全とは言っていない)
エラー処理はテキトーなので上手くやってください


package main

import (
        "bytes"
        "encoding/json"
        "flag"
        "fmt"
        "io/ioutil"
        "net/http"
        "strconv"
        "time"
)

func HttpPost(url, param string) (interface{}, error) {
        req, err := http.NewRequest(
                "POST",
                url,
                bytes.NewBuffer([]byte(param)),
        )
        if err != nil {
                return nil, err
        }
        req.Header.Set("Content-Type", "application/json")
        req.Header.Set("Authorization", "Basic base64化したusername:token")
        req.Header.Set("Accept", "application/vnd.github.inertia-preview+json")

        client := &http.Client{Timeout: time.Duration(10 * time.Second)}
        resp, err := client.Do(req)
        defer resp.Body.Close()
        if err != nil {
                return nil, err
        }
        var response interface{}
        body, err := ioutil.ReadAll(resp.Body)
        if err != nil {
                return nil, err
        }
        err = json.Unmarshal(body, &response)
        return response, err
}

func main() {
        flag.Parse()
        if len(flag.Args()) < 1 {
                fmt.Println("no issue title")
                return
        }
        issue_title := flag.Args()[0]
        ownr := ""
        repo := ""
        column_id := ""
        issue_url := "https://api.github.com/repos/" + ownr + "/" + repo + "/issues"
        project_url := "https://api.github.com/repos/" + ownr + "/" + repo + "/projects/columns/" + column_id + "/cards"

        params := `{"title": "` + issue_title + `"}`
        res, err := HttpPost(issue_url, params)
        if err != nil {
                fmt.Println(err)
        }
        id := res.(map[string]interface{})["id"].(float64)
        params = `{"content_id": ` + strconv.Itoa(int(id)) + `, "content_type": "Issue"}`
        res, err = HttpPost(project_url, params)
        if err != nil {
                fmt.Println(err)
        }
        fmt.Println(200)
}

使い方

./(スクリプトorバイナリ) issue_title
でissue_titleのissueが出来て任意のprojectに登録されているはず

5
5
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
5