import fetch from 'node-fetch' ; import queryString from 'query-string' ; import { encodeURL } from '../util' ; import PromiseHandler from '../../ph-manager/promise-handler' ; export class Issues { token : string ; promiseHandler : PromiseHandler ; constructor ( token , promiseHandler = new PromiseHandler ( ) ) { this . token = token ; this . promiseHandler = promiseHandler ; } async all ( param ) { const result = await this . promiseHandler . add ( async ( ) => await fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/${ param . repo }/issues` , { headers : { Authorization : `bearer ${ this . token }` } } ) ) ; return await result . json ( ) ; } async allComments ( param ) { const result1 = await this . promiseHandler . add ( async ( ) => await fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/${ param . repo }/issues/comments?${ queryString . stringify ( { page : 1 , per_page : 100 , } ) }` , { method : 'GET' , headers : { Authorization : `bearer ${ this . token }` } , } ) ) ; let comments = await result1 . json ( ) ; const pageCount = parseInt ( result1 . headers . get ( 'total_page' ) as any ) ; if ( pageCount > 1 ) { const requestList = [ ] ; for ( let index = 2 ; index <= pageCount ; index ++ ) { requestList . push ( this . promiseHandler . add ( async ( ) => fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/${ param . repo }/issues/comments?${ queryString . stringify ( { page : index , per_page : 100 , } ) }` , { method : 'GET' , headers : { Authorization : `bearer ${ this . token }` } , } ) ) ) ; } const responseList = await Promise . all ( requestList ) ; const commentsList = await Promise . all ( responseList . map ( r => r . json ( ) ) ) ; commentsList . map ( r => { comments = comments . concat ( r ) ; } ) ; } return comments ; } async create ( param ) { const result = await this . promiseHandler . add ( async ( ) => await fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/issues` , { method : 'POST' , headers : { Authorization : `bearer ${ this . token }` , 'Content-Type' : 'application/x-www-form-urlencoded' , } , body : queryString . stringify ( { ... param , labels : param . labels ?. join ( ',' ) , } ) , } ) ) ; return await result . json ( ) ; } async select ( param ) { const result = await this . promiseHandler . add ( async ( ) => await fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/${ param . repo }/issues/${ param . number }` , { headers : { Authorization : `bearer ${ this . token }` } } ) ) ; return await result . json ( ) ; } async update ( param ) { /**
         * Because some unknown magical reasons, gitee will clear all labels
         * if you do not provide them again when update issue. While other
         * attributes will be keep completely. Thus we have to select the issue
         * before update it.
         */ const issue = await this . select ( param ) ; const labelsName = issue . labels . map ( l => { return l . name ; } ) ; const labels = labelsName . join ( ',' ) ; const result = await this . promiseHandler . add ( async ( ) => await fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/issues/${ param . number }` , { method : 'PATCH' , headers : { Authorization : `bearer ${ this . token }` , 'Content-Type' : 'application/x-www-form-urlencoded' , } , body : queryString . stringify ( { labels , ... param , } ) , } ) ) ; return await result . json ( ) ; } async createComment ( param ) { const result = await this . promiseHandler . add ( async ( ) => await fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/${ param . repo }/issues/${ param . number }/comments` , { method : 'POST' , headers : { Authorization : `bearer ${ this . token }` , 'Content-Type' : 'application/x-www-form-urlencoded' , } , body : queryString . stringify ( { ... param , } ) , } ) ) ; return await result . json ( ) ; } async listLabelsForRepo ( param ) { const result = await this . promiseHandler . add ( async ( ) => await fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/${ param . repo }/labels` , { method : 'GET' , headers : { Authorization : `bearer ${ this . token }` } , } ) ) ; return await result . json ( ) ; } async addLabel ( param ) { const result = await this . promiseHandler . add ( async ( ) => await fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/${ param . repo }/issues/${ param . number }/labels` , { method : 'POST' , headers : { Authorization : `bearer ${ this . token }` } , body : JSON . stringify ( param . labels ) , } ) ) ; return await result . json ( ) ; } async createLabel ( param ) { const result = await this . promiseHandler . add ( async ( ) => await fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/${ param . repo }/labels` , { method : 'POST' , headers : { Authorization : `bearer ${ this . token }` , 'Content-Type' : 'application/x-www-form-urlencoded' , } , body : queryString . stringify ( { ... param , } ) , } ) ) ; return result . json ( ) ; } async updateLabel ( param ) { const result = await this . promiseHandler . add ( async ( ) => await fetch ( `https://gitee.com/api/v5/repos/${ param . owner }/${ param . repo }/labels/${ encodeURL ( param . current_name ) }` , { method : 'PATCH' , headers : { Authorization : `bearer ${ this . token }` , 'Content-Type' : 'application/x-www-form-urlencoded' , } , body : queryString . stringify ( { name : param . name , color : param . color , } ) , } ) ) ; return result . json ( ) ; } }