Work below
First, get your Github token
Get the necessary information with curl It seems that api has to add a strange header like a trial period First, you need the id of the project and the id of the columns created by the project, so save it to get it.
curl -H "Accept: application/vnd.github.inertia-preview+json" -u "username:token" https://api.github.com/repos/Team name/Repository name/projects
curl -H "Accept: application/vnd.github.inertia-preview+json" -u "username:token" https://api.github.com/repos/Team name/Repository name/projects/(The id of the project obtained above)/columns
Make a note of the id obtained here as it will be used in the following script. Also, it is easier to get a token of Authorization: Basic with wireshark or something. (I should encode username: token to base64, but I got bad because I got a communication.)
All you have to do is assign the required values to the script below.
# -*- 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 version 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)
I didn't want to put token in plain text in the script, so I recreated it with Go so that it can be binarized and operated. (I'm not saying it's safe) Error handling is texto, so please do it well
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 version 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)
}
./ (script or binary) issue_title An issue with issue_title should have been created and registered in any project.
Recommended Posts