@@ -51,6 +51,7 @@ class GitalkComponent extends Component {
5151 super ( props )
5252 this . options = Object . assign ( { } , {
5353 id : location . href ,
54+ number : - 1 ,
5455 labels : [ 'Gitalk' ] ,
5556 title : document . title ,
5657 body : '' , // location.href + header.meta[description]
@@ -187,13 +188,36 @@ class GitalkComponent extends Component {
187188 this . logout ( )
188189 } )
189190 }
190- getIssue ( ) {
191- const { issue } = this . state
192- if ( issue ) {
193- this . setState ( { isNoInit : false } )
194- return Promise . resolve ( issue )
195- }
191+ getIssueById ( ) {
192+ const { owner, repo, number, clientID, clientSecret } = this . options
193+ const getUrl = `/repos/${ owner } /${ repo } /issues/${ number } `
194+
195+ return new Promise ( ( resolve , reject ) => {
196+ axiosGithub . get ( getUrl , {
197+ params : {
198+ client_id : clientID ,
199+ client_secret : clientSecret ,
200+ t : Date . now ( )
201+ }
202+ } )
203+ . then ( res => {
204+ let issue = null
205+
206+ if ( res && res . data && res . data . number === number ) {
207+ issue = res . data
196208
209+ this . setState ( { issue, isNoInit : false } )
210+ }
211+ resolve ( issue )
212+ } )
213+ . catch ( err => {
214+ // When the status code is 404, promise will be resolved with null
215+ if ( err . response . status === 404 ) resolve ( null )
216+ reject ( err )
217+ } )
218+ } )
219+ }
220+ getIssueByLabels ( ) {
197221 const { owner, repo, id, labels, clientID, clientSecret } = this . options
198222
199223 return axiosGithub . get ( `/repos/${ owner } /${ repo } /issues` , {
@@ -220,6 +244,21 @@ class GitalkComponent extends Component {
220244 return issue
221245 } )
222246 }
247+ getIssue ( ) {
248+ const { number, issue } = this . options
249+ if ( issue ) {
250+ this . setState ( { isNoInit : false } )
251+ return Promise . resolve ( issue )
252+ }
253+
254+ if ( typeof number === 'number' && number > 0 ) {
255+ return this . getIssueById ( ) . then ( resIssue => {
256+ if ( ! resIssue ) return this . getIssueByLabels ( )
257+ return resIssue
258+ } )
259+ }
260+ return this . getIssueByLabels ( )
261+ }
223262 createIssue ( ) {
224263 const { owner, repo, title, body, id, labels, url } = this . options
225264 return axiosGithub . post ( `/repos/${ owner } /${ repo } /issues` , {
0 commit comments