Skip to content

Instantly share code, notes, and snippets.

@dhmlau
Created June 12, 2021 02:21
Show Gist options
  • Save dhmlau/28b295c486e4d5f4773b6d1362091b24 to your computer and use it in GitHub Desktop.
Save dhmlau/28b295c486e4d5f4773b6d1362091b24 to your computer and use it in GitHub Desktop.
Snippet of GHQueryController
/**
* Get issues from URL
* @param nextLinkURL
* @param queryService
* @param queryResult
* @returns
*/
async getIssueByURL(nextLinkURL: string, queryService: GhQueryService, queryResult:QueryResult) {
let result = await queryService.getIssuesByURL(nextLinkURL);
result.body.items.forEach(issue => {
this.addToResult(issue, queryResult);
});
const nextLink2 = this.getNextLink(result.headers.link);
if (nextLink2 == null) return;
await this.getIssueByURL(nextLink2, queryService, queryResult);
}
/**
* Get the URL for the "next" page.
* The Link header is in the format of:
* Link: <https://api.github.com/search/code?q=addClass+user%3Amozilla&page=2>; rel="next",
<https://api.github.com/search/code?q=addClass+user%3Amozilla&page=34>; rel="last"
* @param link
* @returns
*/
getNextLink(link: string): string|null {
if (link == undefined) return null;
let tokens: string[] = link.split(',');
let url: string|null = null;
tokens.forEach(token => {
if (token.indexOf('rel="next"')!=-1) {
url = token.substring(token.indexOf('<')+1, token.indexOf(';')-1);
}
});
return url;
}
/**
* Add the issue to the QueryResult object
* @param issue
* @param queryResult
*/
addToResult(issue: IssueInfo, queryResult: QueryResult) {
let issueInfo:ResultIssueInfo = new ResultIssueInfo();
issueInfo.html_url = issue.html_url;
issueInfo.title = issue.title;
issueInfo.state = issue.state;
issueInfo.age = this.getIssueAge(issue.created_at);
queryResult.items.push(issueInfo);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment