Implemented github-wrapper with return type declarations

dependabot/npm_and_yarn/BuildServer/eslint-7.2.0
Inga 🏳‍🌈 7 years ago
parent 4346148207
commit 89f8b61713
  1. 3
      BuildServer/lib/builder.ts
  2. 37
      BuildServer/lib/commenter.ts
  3. 48
      BuildServer/lib/github-wrapper.ts
  4. 2
      BuildServer/types.d.ts

@ -8,6 +8,7 @@ import { join } from "path";
import settings from "../settings"; import settings from "../settings";
import { ReportResult } from "../types"; import { ReportResult } from "../types";
import { createGithub } from "./github-wrapper";
import { gitLoader } from "./git/loader"; import { gitLoader } from "./git/loader";
import { send as sendMail } from "./mail-sender"; import { send as sendMail } from "./mail-sender";
import { writeReport } from "./report-processor"; import { writeReport } from "./report-processor";
@ -45,7 +46,7 @@ const notifyStatus = (options, notifyStatusCallback) => {
target_url: `${settings.siteRoot}status/${options.owner}/${options.reponame}/${options.hash}`, 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) { if (createStatusErr) {
console.log(`Error while creating status: ${createStatusErr}`); console.log(`Error while creating status: ${createStatusErr}`);
console.log(status); console.log(status);

@ -3,8 +3,21 @@
import * as _ from "underscore"; import * as _ from "underscore";
import settings from "../settings"; import settings from "../settings";
import { createGithub, IGithub } from "./github-wrapper";
import { getStatusMessageFromRelease } from "./report-processor"; 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 featureNamePattern = /^feature-(\d+)(?:-[a-zA-Z0-9]+)+$/;
const versionNamePattern = /^v\d+(\.\d+)*$/; const versionNamePattern = /^v\d+(\.\d+)*$/;
const masterNamePattern = /^master$/; const masterNamePattern = /^master$/;
@ -32,35 +45,35 @@ const closePullRequest = (options, message, callback) => writeComment(options, m
}, callback); }, callback);
}); });
const checkHasIssue = (options, issueNumber, callback) => options.github.issues.get({ const checkHasIssue = (options: ICheckPullRequestOptions, issueNumber, callback) => options.github.issues.get({
number: issueNumber, number: issueNumber,
owner: options.baseRepoOptions.owner, owner: options.baseRepoOptions.owner,
repo: options.baseRepoOptions.reponame, repo: options.baseRepoOptions.reponame,
}, (getIssueErr, result) => { }, (getIssueErr, result) => {
if (getIssueErr) { if (getIssueErr) {
if (getIssueErr.code !== httpNotFound) { if (getIssueErr.code && getIssueErr.code !== httpNotFound) {
return callback(getIssueErr.message); return callback(getIssueErr.message);
} }
return callback(null, false); return callback(null, false);
} }
if (!result.number) { if (!result) {
return callback(`Unable to get issue info for ${options.baseRepoOptions.owner}/${options.baseRepoOptions.reponame}/#${issueNumber}: ${JSON.stringify(result)}`); return callback("Result is empty");
} }
if (result.number.toString() !== issueNumber) { if (result.data.number.toString() !== issueNumber) {
return callback(null, false); 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, 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, owner: options.baseRepoOptions.owner,
per_page: 1, per_page: 1,
repo: options.baseRepoOptions.reponame, repo: options.baseRepoOptions.reponame,
@ -69,10 +82,10 @@ const checkHasReleases = (options, callback) => options.github.repos.getReleases
return callback(getReleasesErr); 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 head = options.headRepoOptions;
const base = options.baseRepoOptions; 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 = { const optionsGithub = {
...originalOptions, ...originalOptions,
github: settings.createGithub(originalOptions.baseRepoOptions.owner), github: createGithub(originalOptions.baseRepoOptions.owner),
}; };
const options = { const options = {
...optionsGithub, ...optionsGithub,

@ -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<T> = (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<IIssueData>): void;
};
readonly repos: {
createStatus(params: RawGithub.ReposCreateStatusParams, callback: ICallback<IStatusData>);
getReleases(params: RawGithub.ReposGetReleasesParams, callback: ICallback<IReleaseData[]>);
};
}
const createGithub = (repoOwner) => settings.createGithub(repoOwner) as any as IGithub;
export {
IGithub,
createGithub,
};

@ -73,5 +73,5 @@ export {
TaskProcessor, TaskProcessor,
TaskProcessorCore, TaskProcessorCore,
TaskProcessorCallback, TaskProcessorCallback,
Tasks Tasks,
} }

Loading…
Cancel
Save