Go Phabricator API 代碼/程序創(chuàng)建task/提交文件到Phabricator

Go Phabricator API 代碼/程序創(chuàng)建task/提交文件到Phabricator

Creat Task or upload file to phabricator with code in go

之前寫一個服務(wù)往phabricator 上提交task嘁信,上傳文件;當時查了好久,發(fā)現(xiàn)官方文檔很不明確噪奄,而網(wǎng)上又幾乎沒有資料,所以把最終寫的整理一下給需要的做一個參考。
Several days ago题篷,I have writing a service to submit tasks or upload files to the phabricator.At that time , search for a long time, found that the official document is not very clear, and almost no information on the Internet, so share something about this.

upload file/image or other info to phabricator by program in Go
Go語言,通過程序往phabricator上傳文件/圖片厅目,新建task.

最終是要在phabricator上提交一個task番枚,func3法严,通過調(diào)用調(diào)用func1,上傳一個file葫笼,通過func2深啤,獲取這個file的id。在func3中創(chuàng)建task的時候路星,可以通過“F【 + id】”的格式引用這個文件或者圖片溯街。
The final target is to submit a task to the phabricator. Func3, by calling func1 upload a file, through func2, get the id of this file. In the func3 create a task, you can use "F [+ id]" format to quoted the file(or picture).

上傳文件

upload file

注意 phabricator接收base64數(shù)據(jù),而且不要開頭的格式聲明部分洋丐,即只要“呈昔,”后邊的。
Pay attention友绝,phabricator receive a data of base64 format,and deny the chars befor ",".


//1,文件上傳:file.upload 堤尾,拿回來phid;在task里可以通過phid引用。
//1,upload file. api:file.upload. Get back the phid of this file which can be quoted in a task page迁客。
func UploadFile(imageBase64List [imageMaxNum]string) ([]string, error) {
    phidList := make([]string, 0)
    urlStr := "https://ph.com/api/file.upload"

    for _, imageBase64 := range imageBase64List {
        if imageBase64 == "" {
            continue
        }
        //截取“郭宝,”后邊的字符串,
        //Get chars after ","
        imageData := strings.Split(imageBase64, ",")
        if len(imageData) != 2 || !utils.JudgeBase64(imageData[1]) {
            return phidList, errors.New("圖片數(shù)據(jù)格式錯誤")
        }
        form := url.Values{}
        form.Add("data_base64", imageData[1])

        req, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(form.Encode()))
        q := req.URL.Query()
        q.Set("api.token", common.Config.ApiToken)
        req.URL.RawQuery = q.Encode()
        req.Header.Add("content-type", "application/x-www-form-urlencoded")

        resp, err := http.DefaultClient.Do(req)
        if err != nil {
            return phidList, err
        }
        defer resp.Body.Close()

        body, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            return phidList, err
        }
        //解析body,并且添加到切片
        //Unmarshal body and add to slice
        var uploadResp UploadFileResp
        if err := json.Unmarshal(body, &uploadResp); err != nil {
            err := errors.New("文件上傳時發(fā)生未知錯誤:" + err.Error())
            return phidList, err
        }
        phidList = append(phidList, uploadResp.Result)
    }
    return phidList, nil
}

根據(jù)文件的phid獲取id

get the id of file by phid


//  2,獲取文件/圖片id:file.info 拿回來一個file id
//  2,get the id of file/image though file.info .
func getImageId(phidList []string) (imageIdlist []string, err error) {
    imageIdList := make([]string, 0)
    urlStr := "https://ph.com/api/file.info"

    for _, phid := range phidList {
        if phid == "" {
            continue
        }

        form := url.Values{}
        form.Add("phid", phid)
        req, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(form.Encode()))
        req.Header.Add("content-type", "application/x-www-form-urlencoded")
        q := req.URL.Query()
        q.Set("api.token", common.Config.ApiToken)
        req.URL.RawQuery = q.Encode()

        resp, err := http.DefaultClient.Do(req)
        if err != nil {
            return imageIdList, err
        }
        defer resp.Body.Close()

        body, err := ioutil.ReadAll(resp.Body)
        if err != nil {
            return imageIdList, err
        }
        var findImageResp FindImageIdResp
        if err := json.Unmarshal(body, &findImageResp); err != nil {
            err := errors.New("獲取文件信息時發(fā)生未知錯誤" + err.Error())
            return imageIdList, err
        }
        imageIdList = append(imageIdList, findImageResp.Result.ObjectName)
    }
    return imageIdList, nil
}


入口函數(shù)

entrance func

conduit api 測試頁面參數(shù)示例
conduit pages api test parameters example

    [   {       "type":"title",         "value":"Test API in conduit"   }, 
    {       "type":"view",          "value":"users" },
    {       "type":"projects.add",          "value":["bug_管理","生 產(chǎn)環(huán)境"]      },  
    {       "type":"description",       "value":"Nothing , need not to be concerned. "  },
    {       "type":"priority",      "value":"low"},
    {       "type":"custom.mq.customer_id_and_name","value":"coustomId and name"}
 ]

接收參數(shù)掷漱,并調(diào)用fun1粘室、fun2,最終提交task卜范。
Get parameters 衔统,call func1、func2海雪,submit a task at last缰冤。


// 3,接收bug信息,上傳文件/圖片,獲取文件id喳魏,提交bug信息,返回提示信息怀薛。
// 3,receive the infomation from pages,get the id of file/image ,submit info and sendback tips.
func ReceiptBugInfo(w http.ResponseWriter, r *http.Request) {
    var reqInfo ReceiptBugInfoRequest
    if err := utils.BindJSON(r, &reqInfo); err != nil {
        render.BindError(w, r, err)
        return
    }

    desc := reqInfo.Desc + "\n\n終端信息:" + reqInfo.EndPointInfo + "\nbug提交位置參考路徑:" + reqInfo.BugPageUrl
    customIdAndName := "用戶名:" + reqInfo.CustomerName + "    用戶ID:" + reqInfo.CustomerId + "    租戶ID" + reqInfo.TenantId
    desc += "\n" + customIdAndName + "\n"
    //檢查刺彩,不為空則調(diào)用getImgId,并拼入desc
    if reqInfo.ImagesData[0] != "" {
        phidList, err := UploadFile(reqInfo.ImagesData)
        if err != nil {
            render.InternalError(w, r, err)
        }
        imageIdSlice, err := getImageId(phidList)
        if err != nil {
            render.InternalError(w, r, err)
        }
        for _, images := range imageIdSlice {
            if images == "" {
                break
            }
            desc += "\nbug截圖:\n{" + images + "}"
        }
    }

    urlStr := "https://ph.com/api/maniphest.edit"

    form := url.Values{}
    form.Add("transactions[0][type]", "title")
    form.Add("transactions[0][value]", reqInfo.Title)
    form.Add("transactions[1][type]", "description")
    form.Add("transactions[1][value]", desc)
    form.Add("transactions[2][type]", "priority")
    form.Add("transactions[2][value]", reqInfo.Priority)
    form.Add("transactions[3][type]", "projects.add")
    form.Add("transactions[3][value][0]", "bug_管理")
    form.Add("transactions[3][value][1]", "生產(chǎn)環(huán)境")
    form.Add("transactions[4][type]", "space")
    form.Add("transactions[4][value]", "PHID-SPCE-cnulhg6i3r4m5evgb4wc")

    req, _ := http.NewRequest("POST", urlStr, bytes.NewBufferString(form.Encode()))
    req.Header.Add("content-type", "application/x-www-form-urlencoded")
    q := req.URL.Query()
    q.Set("api.token", common.Config.ApiToken)
    req.URL.RawQuery = q.Encode()

    resp, err := http.DefaultClient.Do(req)
    if err != nil {
        render.InternalNetError(w, r, err)
    }
    defer resp.Body.Close()

    //解析這個body枝恋,error不為空則報錯,圖片上傳.
    //Unmarshal body,if error is not nil,panic.else upload file/image.
    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        render.InternalError(w, r, err)
        return
    }
    var bugInfoResp BugInfoUploadResp
    if err := json.Unmarshal(body, &bugInfoResp); err != nil {
        error := errors.New("解析反饋信息時發(fā)生未知錯誤" + err.Error())
        render.InternalError(w, r, error)
        return
    }

    if bugInfoResp.ErrorCode != "" {
        err := errors.New("提交信息時發(fā)生未知錯誤" + bugInfoResp.ErrorInfo)
        render.InternalError(w, r, err)
        return
    }

    render.Success(w, r)
}

結(jié)構(gòu)體定義如下

definition struct as below


type BugInfoUploadResp struct {
    Result    BugRespResult `json:"result" binding:"omitempty`
    ErrorCode string        `json:"error_code" binding:"omitempty`
    ErrorInfo string        `json:"error_info" binding:"omitempty`
}

type BugRespResult struct {
    Object       BugRespObject `json:"object" binding:"omitempty`
    Transactions Phids         `json:"transactions" binding:"omitempty`
}

type BugRespObject struct {
    Id   int    `json:"id" binding:"omitempty`
    Phid string `json:"phid" binding:"omitempty`
}

type Phids []Phid

type Phid struct {
    Phid string `json:"phid" binding:"omitempty`
}

type UploadFileResp struct {
    Result    string `json:"result" binding:"omitempty`
    ErrorCode string `json:"error_code" binding:"omitempty`
    ErrorInfo string `json:"error_info" binding:"omitempty`
}

type FindImageIdResp struct {
    Result    ImgIdRespInfo `json:"result" binding:"omitempty`
    ErrorCode string        `json:"error_code" binding:"omitempty`
    ErrorInfo string        `json:"error_info" binding:"omitempty`
}

type ImgIdRespInfo struct {
    Id           string `json:"id" binding:"omitempty"`
    Phid         string `json:"phid" binding:"omitempty"`
    ObjectName   string `json:"objectName" binding:"omitempty"`
    Name         string `json:"name" binding:"omitempty"`
    MimeType     string `json:"mimeType" binding:"omitempty"`
    ByteSize     string `json:"byteSize" binding:"omitempty"`
    AuthorPHID   string `json:"authorPHID" binding:"omitempty"`
    DateCreated  string `json:"dateCreated" binding:"omitempty"`
    DateModified string `json:"dateModified" binding:"omitempty"`
    Uri          string `json:"uri" binding:"omitempty"`
}

type ReceiptBugInfoRequest struct {
    TenantId     string    `json:"tenantId" binding:"required"`
    CustomerName string    `json:"customerName" binding:"required"`
    CustomerId   string    `json:"customerId" binding:"required"`
    Desc         string    `json:"desc" binding:"required"`
    Title        string    `json:"title" binding:"required"`
    Priority     string    `json:"priority" binding:"required"`
    EndPointInfo string    `json:"endPointInfo" binding:"required"`
    BugPageUrl   string    `json:"bugPageUrl" binding:"omitempty`
    ImagesData   ImageList `json:"imageData" binding:"omitempty"`
}

type ImageList [imageMaxNum]string

//反饋创倔,圖片最大支持數(shù)目。
//feedback,the max number of image
const imageMaxNum = 10

轉(zhuǎn)載注明出處----名白
http://www.cnblogs.com/mingbai/p/GoPhabricator.html

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末焚碌,一起剝皮案震驚了整個濱河市畦攘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌十电,老刑警劉巖知押,帶你破解...
    沈念sama閱讀 217,907評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叹螟,死亡現(xiàn)場離奇詭異,居然都是意外死亡台盯,警方通過查閱死者的電腦和手機罢绽,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,987評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來静盅,“玉大人良价,你說我怎么就攤上這事≥锏” “怎么了明垢?”我有些...
    開封第一講書人閱讀 164,298評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長市咽。 經(jīng)常有香客問我痊银,道長,這世上最難降的妖魔是什么魂务? 我笑而不...
    開封第一講書人閱讀 58,586評論 1 293
  • 正文 為了忘掉前任曼验,我火速辦了婚禮,結(jié)果婚禮上粘姜,老公的妹妹穿的比我還像新娘鬓照。我一直安慰自己,他們只是感情好孤紧,可當我...
    茶點故事閱讀 67,633評論 6 392
  • 文/花漫 我一把揭開白布豺裆。 她就那樣靜靜地躺著,像睡著了一般号显。 火紅的嫁衣襯著肌膚如雪臭猜。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,488評論 1 302
  • 那天押蚤,我揣著相機與錄音蔑歌,去河邊找鬼。 笑死揽碘,一個胖子當著我的面吹牛次屠,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播雳刺,決...
    沈念sama閱讀 40,275評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼劫灶,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了掖桦?” 一聲冷哼從身側(cè)響起本昏,我...
    開封第一講書人閱讀 39,176評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎枪汪,沒想到半個月后涌穆,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體怔昨,經(jīng)...
    沈念sama閱讀 45,619評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,819評論 3 336
  • 正文 我和宋清朗相戀三年蒲犬,在試婚紗的時候發(fā)現(xiàn)自己被綠了朱监。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,932評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡原叮,死狀恐怖赫编,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情奋隶,我是刑警寧澤擂送,帶...
    沈念sama閱讀 35,655評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站唯欣,受9級特大地震影響嘹吨,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜境氢,卻給世界環(huán)境...
    茶點故事閱讀 41,265評論 3 329
  • 文/蒙蒙 一蟀拷、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧萍聊,春花似錦问芬、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,871評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至亭螟,卻和暖如春挡鞍,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背预烙。 一陣腳步聲響...
    開封第一講書人閱讀 32,994評論 1 269
  • 我被黑心中介騙來泰國打工墨微, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人扁掸。 一個月前我還...
    沈念sama閱讀 48,095評論 3 370
  • 正文 我出身青樓欢嘿,卻偏偏與公主長得像,于是被迫代替她去往敵國和親也糊。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 44,884評論 2 354

推薦閱讀更多精彩內(nèi)容