diff --git a/BuildServer/lib/builder.ts b/BuildServer/lib/builder.ts index 2df82aa..ab4b13f 100644 --- a/BuildServer/lib/builder.ts +++ b/BuildServer/lib/builder.ts @@ -8,6 +8,7 @@ import { join } from "path"; import settings from "../settings"; import { ReportResult } from "../types"; +import { createGithub } from "./github-wrapper"; import { gitLoader } from "./git/loader"; import { send as sendMail } from "./mail-sender"; import { writeReport } from "./report-processor"; @@ -45,7 +46,7 @@ const notifyStatus = (options, notifyStatusCallback) => { target_url: `${settings.siteRoot}status/${options.owner}/${options.reponame}/${options.hash}`, }; - settings.createGithub(options.owner).repos.createStatus(status, (createStatusErr) => { + createGithub(options.owner).repos.createStatus(status, (createStatusErr) => { if (createStatusErr) { console.log(`Error while creating status: ${createStatusErr}`); console.log(status); diff --git a/BuildServer/lib/commenter.ts b/BuildServer/lib/commenter.ts index 1d56b63..f25a1fd 100644 --- a/BuildServer/lib/commenter.ts +++ b/BuildServer/lib/commenter.ts @@ -3,8 +3,21 @@ import * as _ from "underscore"; import settings from "../settings"; +import { createGithub, IGithub } from "./github-wrapper"; import { getStatusMessageFromRelease } from "./report-processor"; +interface ICommentOnPullRequestOptions { + readonly action: string; + readonly app: any; + readonly baseRepoOptions: any; + readonly headRepoOptions: any; +} + +interface ICheckPullRequestOptions extends ICommentOnPullRequestOptions { + readonly github: IGithub; + readonly onTenthAttempt: () => void; +} + const featureNamePattern = /^feature-(\d+)(?:-[a-zA-Z0-9]+)+$/; const versionNamePattern = /^v\d+(\.\d+)*$/; const masterNamePattern = /^master$/; @@ -32,35 +45,35 @@ const closePullRequest = (options, message, callback) => writeComment(options, m }, callback); }); -const checkHasIssue = (options, issueNumber, callback) => options.github.issues.get({ +const checkHasIssue = (options: ICheckPullRequestOptions, issueNumber, callback) => options.github.issues.get({ number: issueNumber, owner: options.baseRepoOptions.owner, repo: options.baseRepoOptions.reponame, }, (getIssueErr, result) => { if (getIssueErr) { - if (getIssueErr.code !== httpNotFound) { + if (getIssueErr.code && getIssueErr.code !== httpNotFound) { return callback(getIssueErr.message); } return callback(null, false); } - if (!result.number) { - return callback(`Unable to get issue info for ${options.baseRepoOptions.owner}/${options.baseRepoOptions.reponame}/#${issueNumber}: ${JSON.stringify(result)}`); + if (!result) { + return callback("Result is empty"); } - if (result.number.toString() !== issueNumber) { + if (result.data.number.toString() !== issueNumber) { return callback(null, false); } - if (result.pull_request && result.pull_request.url) { + if (result.data.pull_request && result.data.pull_request.url) { return callback(null, false); } - return callback(null, true, result.title); + return callback(null, true, result.data.title); }); -const checkHasReleases = (options, callback) => options.github.repos.getReleases({ +const checkHasReleases = (options: ICheckPullRequestOptions, callback) => options.github.repos.getReleases({ owner: options.baseRepoOptions.owner, per_page: 1, repo: options.baseRepoOptions.reponame, @@ -69,10 +82,10 @@ const checkHasReleases = (options, callback) => options.github.repos.getReleases return callback(getReleasesErr); } - return callback(null, result && result.length); + return callback(null, result && result.data && result.data.length); }); -const checkPullRequest = (options, callback) => { +const checkPullRequest = (options: ICheckPullRequestOptions, callback) => { const head = options.headRepoOptions; const base = options.baseRepoOptions; @@ -146,10 +159,10 @@ const checkPullRequest = (options, callback) => { }); }; -export const commentOnPullRequest = (originalOptions, callback) => { +export const commentOnPullRequest = (originalOptions: ICommentOnPullRequestOptions, callback) => { const optionsGithub = { ...originalOptions, - github: settings.createGithub(originalOptions.baseRepoOptions.owner), + github: createGithub(originalOptions.baseRepoOptions.owner), }; const options = { ...optionsGithub, diff --git a/BuildServer/lib/github-wrapper.ts b/BuildServer/lib/github-wrapper.ts new file mode 100644 index 0000000..4ad949e --- /dev/null +++ b/BuildServer/lib/github-wrapper.ts @@ -0,0 +1,48 @@ +"use strict"; + +import * as RawGithub from "github"; +import settings from "../settings"; + +interface IHttpError extends Error { + readonly message: string; + readonly code: number; + readonly status: any; + readonly headers: any; +} + +type ICallback = (error: IHttpError, result?: { data: T }) => void; + +interface IIssueData { + readonly id: number; + readonly number: number; + readonly state: "open" | "closed"; + readonly title: string; + readonly pull_request?: { + readonly url: string; + }; +} + +interface IReleaseData { + readonly id: number; +} + +interface IStatusData { + readonly id: number; +} + +interface IGithub { + readonly issues: { + get(params: RawGithub.IssuesGetParams, callback: ICallback): void; + }; + readonly repos: { + createStatus(params: RawGithub.ReposCreateStatusParams, callback: ICallback); + getReleases(params: RawGithub.ReposGetReleasesParams, callback: ICallback); + }; +} + +const createGithub = (repoOwner) => settings.createGithub(repoOwner) as any as IGithub; + +export { + IGithub, + createGithub, +}; diff --git a/BuildServer/types.d.ts b/BuildServer/types.d.ts index 3a4ae52..291f170 100644 --- a/BuildServer/types.d.ts +++ b/BuildServer/types.d.ts @@ -73,5 +73,5 @@ export { TaskProcessor, TaskProcessorCore, TaskProcessorCallback, - Tasks + Tasks, }