Help us understand the problem. What is going on with this article?

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

More than 3 years have passed since last update.

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に登録されているはず

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした