Skip to content

Commit 8d311c1

Browse files
style(docs-infra): add explicit return types to methods
1 parent 77a37e6 commit 8d311c1

14 files changed

+59
-46
lines changed

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/clean-up/build-cleaner.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class BuildCleaner {
2424
}
2525

2626
// Methods - Public
27-
public async cleanUp() {
27+
public async cleanUp(): Promise<void> {
2828
try {
2929
this.logger.log('Cleaning up builds and downloads');
3030
const openPrs = await this.getOpenPrNumbers();
@@ -38,17 +38,17 @@ export class BuildCleaner {
3838
}
3939
}
4040

41-
public async cleanBuilds(openPrs: number[]) {
41+
public async cleanBuilds(openPrs: number[]): Promise<void> {
4242
const existingBuilds = await this.getExistingBuildNumbers();
4343
await this.removeUnnecessaryBuilds(existingBuilds, openPrs);
4444
}
4545

46-
public async cleanDownloads(openPrs: number[]) {
46+
public async cleanDownloads(openPrs: number[]): Promise<void> {
4747
const existingDownloads = await this.getExistingDownloads();
4848
await this.removeUnnecessaryDownloads(existingDownloads, openPrs);
4949
}
5050

51-
public getExistingBuildNumbers() {
51+
public getExistingBuildNumbers(): Promise<number[]> {
5252
return new Promise<number[]>((resolve, reject) => {
5353
fs.readdir(this.buildsDir, (err, files) => {
5454
if (err) {
@@ -65,14 +65,14 @@ export class BuildCleaner {
6565
});
6666
}
6767

68-
public async getOpenPrNumbers() {
68+
public async getOpenPrNumbers(): Promise<number[]> {
6969
const api = new GithubApi(this.githubToken);
7070
const githubPullRequests = new GithubPullRequests(api, this.githubOrg, this.githubRepo);
7171
const prs = await githubPullRequests.fetchAll('open');
7272
return prs.map(pr => pr.number);
7373
}
7474

75-
public removeDir(dir: string) {
75+
public removeDir(dir: string): void {
7676
try {
7777
if (shell.test('-d', dir)) {
7878
shell.chmod('-R', 'a+w', dir);
@@ -83,7 +83,7 @@ export class BuildCleaner {
8383
}
8484
}
8585

86-
public removeUnnecessaryBuilds(existingBuildNumbers: number[], openPrNumbers: number[]) {
86+
public removeUnnecessaryBuilds(existingBuildNumbers: number[], openPrNumbers: number[]): void {
8787
const toRemove = existingBuildNumbers.filter(num => !openPrNumbers.includes(num));
8888

8989
this.logger.log(`Existing builds: ${existingBuildNumbers.length}`);
@@ -100,7 +100,7 @@ export class BuildCleaner {
100100
forEach(dir => this.removeDir(dir));
101101
}
102102

103-
public getExistingDownloads() {
103+
public getExistingDownloads(): Promise<string[]> {
104104
const artifactFile = path.basename(this.artifactPath);
105105
return new Promise<string[]>((resolve, reject) => {
106106
fs.readdir(this.downloadsDir, (err, files) => {
@@ -113,7 +113,7 @@ export class BuildCleaner {
113113
});
114114
}
115115

116-
public removeUnnecessaryDownloads(existingDownloads: string[], openPrNumbers: number[]) {
116+
public removeUnnecessaryDownloads(existingDownloads: string[], openPrNumbers: number[]): void {
117117
const toRemove = existingDownloads.filter(filePath => {
118118
const {pr} = getPrInfoFromDownloadPath(filePath);
119119
return !openPrNumbers.includes(pr);

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/clean-up/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {BuildCleaner} from './build-cleaner';
1313
_main();
1414

1515
// Functions
16-
function _main() {
16+
function _main(): void {
1717
const buildCleaner = new BuildCleaner(
1818
AIO_BUILDS_DIR,
1919
AIO_GITHUB_ORGANIZATION,

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/common/circle-ci-api.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export class CircleCiApi {
5555
* @param buildNumber The CircleCI build number that generated the artifact.
5656
* @returns A promise to the info about the build
5757
*/
58-
public async getBuildInfo(buildNumber: number) {
58+
public async getBuildInfo(buildNumber: number): Promise<BuildInfo> {
5959
try {
6060
const baseUrl = `${CIRCLE_CI_API_URL}/${this.githubOrg}/${this.githubRepo}/${buildNumber}`;
6161
const response = await fetch(`${baseUrl}?${this.tokenParam}`);
@@ -73,7 +73,7 @@ export class CircleCiApi {
7373
* @param artifactPath The path, within the build to the artifact.
7474
* @returns A promise to the URL that can be requested to download the actual build artifact file.
7575
*/
76-
public async getBuildArtifactUrl(buildNumber: number, artifactPath: string) {
76+
public async getBuildArtifactUrl(buildNumber: number, artifactPath: string): Promise<string> {
7777
const baseUrl = `${CIRCLE_CI_API_URL}/${this.githubOrg}/${this.githubRepo}/${buildNumber}`;
7878
try {
7979
const response = await fetch(`${baseUrl}/artifacts?${this.tokenParam}`);

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/common/github-api.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export class GithubApi {
5656
}
5757

5858
// Methods - Protected
59-
protected buildPath(pathname: string, params?: RequestParamsOrNull) {
59+
protected buildPath(pathname: string, params?: RequestParamsOrNull): string {
6060
if (params == null) {
6161
return pathname;
6262
}
@@ -67,7 +67,7 @@ export class GithubApi {
6767
return `${pathname}${joiner}${search}`;
6868
}
6969

70-
protected request<T>(method: string, path: string, data: any = null) {
70+
protected request<T>(method: string, path: string, data: any = null): Promise<T> {
7171
return new Promise<T>((resolve, reject) => {
7272
const options = {
7373
headers: {...this.requestHeaders},
@@ -101,7 +101,7 @@ export class GithubApi {
101101
});
102102
}
103103

104-
protected serializeSearchParams(params: RequestParams) {
104+
protected serializeSearchParams(params: RequestParams): string {
105105
return Object.keys(params).
106106
filter(key => params[key] != null).
107107
map(key => `${key}=${encodeURIComponent(String(params[key]))}`).

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/common/github-pull-requests.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class GithubPullRequests {
3838
* @param body The body of the comment to post.
3939
* @returns A promise that resolves when the comment has been posted.
4040
*/
41-
public addComment(pr: number, body: string) {
41+
public addComment(pr: number, body: string): Promise<any> {
4242
assert(pr > 0, `Invalid PR number: ${pr}`);
4343
assert(!!body, `Invalid or empty comment body: ${body}`);
4444
return this.api.post<any>(`/repos/${this.repoSlug}/issues/${pr}/comments`, null, {body});
@@ -49,7 +49,7 @@ export class GithubPullRequests {
4949
* @param pr The number of the PR for which to request info.
5050
* @returns A promise that is resolves with information about the specified PR.
5151
*/
52-
public fetch(pr: number) {
52+
public fetch(pr: number): Promise<PullRequest> {
5353
assert(pr > 0, `Invalid PR number: ${pr}`);
5454
// Using the `/issues/` URL, because the `/pulls/` one does not provide labels.
5555
return this.api.get<PullRequest>(`/repos/${this.repoSlug}/issues/${pr}`);
@@ -60,7 +60,7 @@ export class GithubPullRequests {
6060
* @param state Only retrieve PRs that have this state.
6161
* @returns A promise that is resolved with information about the requested PRs.
6262
*/
63-
public fetchAll(state: PullRequestState = 'all') {
63+
public fetchAll(state: PullRequestState = 'all'): Promise<PullRequest[]> {
6464
const pathname = `/repos/${this.repoSlug}/pulls`;
6565
const params = {state};
6666

@@ -72,7 +72,7 @@ export class GithubPullRequests {
7272
* @param pr The number of the PR for which to request files.
7373
* @returns A promise that resolves to an array of file information
7474
*/
75-
public fetchFiles(pr: number) {
75+
public fetchFiles(pr: number): Promise<FileInfo[]> {
7676
assert(pr > 0, `Invalid PR number: ${pr}`);
7777
return this.api.get<FileInfo[]>(`/repos/${this.repoSlug}/pulls/${pr}/files`);
7878
}

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/common/github-teams.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class GithubTeams {
2424
* Request information about all the organisation's teams in GitHub.
2525
* @returns A promise that is resolved with information about the teams.
2626
*/
27-
public fetchAll() {
27+
public fetchAll(): Promise<Team[]> {
2828
return this.api.getPaginated<Team>(`/orgs/${this.githubOrg}/teams`);
2929
}
3030

@@ -34,7 +34,7 @@ export class GithubTeams {
3434
* @param teamIds The team to check for the username.
3535
* @returns a Promise that resolves to `true` if the username is a member of the team.
3636
*/
37-
public async isMemberById(username: string, teamIds: number[]) {
37+
public async isMemberById(username: string, teamIds: number[]): Promise<boolean> {
3838

3939
const getMembership = async (teamId: number) => {
4040
try {
@@ -60,7 +60,7 @@ export class GithubTeams {
6060
* @param teamSlugs A collection of slugs that represent the teams to check for the the username.
6161
* @returns a Promise that resolves to `true` if the usernane is a member of at least one of the specified teams.
6262
*/
63-
public async isMemberBySlug(username: string, teamSlugs: string[]) {
63+
public async isMemberBySlug(username: string, teamSlugs: string[]): Promise<boolean> {
6464
try {
6565
const teams = await this.fetchAll();
6666
const teamIds = teams.filter(team => teamSlugs.includes(team.slug)).map(team => team.id);

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/upload-server/build-creator.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export class BuildCreator extends EventEmitter {
119119
});
120120
}
121121

122-
protected getCandidatePrDirs(pr: number, isPublic: boolean) {
122+
protected getCandidatePrDirs(pr: number, isPublic: boolean): {oldPrDir: string, newPrDir: string} {
123123
const hiddenPrDir = path.join(this.buildsDir, HIDDEN_DIR_PREFIX + pr);
124124
const publicPrDir = path.join(this.buildsDir, `${pr}`);
125125

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/upload-server/build-retriever.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class BuildRetriever {
3030
* @param buildNum The number of the build for which to retrieve the info.
3131
* @returns The Github org, repo, PR and latest SHA for the specified build.
3232
*/
33-
public async getGithubInfo(buildNum: number) {
33+
public async getGithubInfo(buildNum: number): Promise<GithubInfo> {
3434
const buildInfo = await this.api.getBuildInfo(buildNum);
3535
const githubInfo: GithubInfo = {
3636
org: buildInfo.username,
@@ -48,7 +48,7 @@ export class BuildRetriever {
4848
* @param downloadSizeLimit The maximum size we are willing to download from the URL
4949
* @returns A promise to the buffer containing the contents of the downloaded file.
5050
*/
51-
public async downloadBuildArtifact(buildNum: number, pr: number, sha: string, artifactPath: string) {
51+
public async downloadBuildArtifact(buildNum: number, pr: number, sha: string, artifactPath: string): Promise<string> {
5252
try {
5353
const outPath = computeArtifactDownloadPath(this.downloadDir, pr, sha, artifactPath);
5454
const downloadExists = await new Promise(resolve => fs.exists(outPath, exists => resolve(exists)));
@@ -71,7 +71,7 @@ export class BuildRetriever {
7171
}
7272
}
7373

74-
function getPrfromBranch(branch: string) {
74+
function getPrfromBranch(branch: string): number {
7575
// CircleCI only exposes PR numbers via the `branch` field :-(
7676
const match = /^pull\/(\d+)$/.exec(branch);
7777
if (!match) {

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/upload-server/build-verifier.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class BuildVerifier {
2424
* @param pr The number of the PR to check
2525
* @param significantFilePattern A regex that selects files that are significant.
2626
*/
27-
public async getSignificantFilesChanged(pr: number, significantFilePattern: RegExp) {
27+
public async getSignificantFilesChanged(pr: number, significantFilePattern: RegExp): Promise<boolean> {
2828
const files = await this.prs.fetchFiles(pr);
2929
return files.some(file => significantFilePattern.test(file.filename));
3030
}
@@ -40,7 +40,7 @@ export class BuildVerifier {
4040
(await this.teams.isMemberBySlug(prInfo.user.login, this.allowedTeamSlugs));
4141
}
4242

43-
protected hasLabel(prInfo: PullRequest, label: string) {
43+
protected hasLabel(prInfo: PullRequest, label: string): boolean {
4444
return prInfo.labels.some(labelObj => labelObj.name === label);
4545
}
4646
}

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/upload-server/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import {UploadServerFactory} from './upload-server-factory';
2121
_main();
2222

2323
// Functions
24-
function _main() {
24+
function _main(): void {
2525
UploadServerFactory
2626
.create({
2727
buildArtifactPath: AIO_ARTIFACT_PATH,

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/upload-server/upload-server-factory.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export class UploadServerFactory {
156156
return middleware;
157157
}
158158

159-
public static createBuildCreator(prs: GithubPullRequests, buildsDir: string, domainName: string) {
159+
public static createBuildCreator(prs: GithubPullRequests, buildsDir: string, domainName: string): BuildCreator {
160160
const buildCreator = new BuildCreator(buildsDir);
161161
const postPreviewsComment = (pr: number, shas: string[]) => {
162162
const body = shas.

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/upload-server/utils.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import {UploadError} from './upload-error';
77
* @param res The response to configure as an error.
88
* @param err The error that needs to be reported.
99
*/
10-
export async function respondWithError(res: express.Response, err: any) {
10+
export async function respondWithError(res: express.Response, err: any): Promise<void> {
1111
if (!(err instanceof UploadError)) {
1212
err = new UploadError(500, String((err && err.message) || err));
1313
}

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/verify-setup/helper.ts

+17-6
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ class Helper {
4242
}
4343

4444
// Methods - Public
45-
public cleanUp() {
45+
public cleanUp(): void {
4646
while (this.cleanUpFns.length) {
4747
// Clean-up fns remove themselves from the list.
4848
this.cleanUpFns[0]();
@@ -66,7 +66,7 @@ class Helper {
6666
}
6767
}
6868

69-
public createDummyBuild(pr: number, sha: string, isPublic = true, force = false, legacy = false) {
69+
public createDummyBuild(pr: number, sha: string, isPublic = true, force = false, legacy = false): CleanUpFn {
7070
const prDir = this.getPrDir(pr, isPublic);
7171
const shaDir = this.getShaDir(prDir, sha, legacy);
7272
const idxPath = path.join(shaDir, 'index.html');
@@ -101,7 +101,7 @@ class Helper {
101101
});
102102
}
103103

104-
public runForAllSupportedSchemes(suiteFactory: TestSuiteFactory) {
104+
public runForAllSupportedSchemes(suiteFactory: TestSuiteFactory): void {
105105
Object.keys(this.portPerScheme).forEach(scheme => suiteFactory(scheme, this.portPerScheme[scheme]));
106106
}
107107

@@ -137,13 +137,13 @@ class Helper {
137137
}
138138

139139
public writeBuildFile(pr: number, sha: string, relFilePath: string, content: string, isPublic = true,
140-
legacy = false) {
140+
legacy = false): void {
141141
const shaDir = this.getShaDir(this.getPrDir(pr, isPublic), sha, legacy);
142142
const absFilePath = path.join(shaDir, relFilePath);
143143
this.writeFile(absFilePath, {content}, true);
144144
}
145145

146-
public writeFile(filePath: string, {content, size}: FileSpecs, force = false) {
146+
public writeFile(filePath: string, {content, size}: FileSpecs, force = false): void {
147147
if (!force && fs.existsSync(filePath)) {
148148
throw new Error(`Refusing to overwrite existing file '${filePath}'.`);
149149
}
@@ -206,7 +206,18 @@ export function makeCurl(baseUrl: string) {
206206
};
207207
}
208208

209-
export function payload(buildNum: number) {
209+
export interface PayloadData {
210+
data: {
211+
payload: {
212+
build_num: number,
213+
build_parameters: {
214+
CIRCLE_JOB: string;
215+
};
216+
};
217+
};
218+
}
219+
220+
export function payload(buildNum: number): PayloadData {
210221
return {
211222
data: {
212223
payload: {

Diff for: aio/aio-builds-setup/dockerbuild/scripts-js/lib/verify-setup/jasmine-custom-matchers.ts

+11-9
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {computeShortSha} from '../common/utils';
66
import {SHA} from './constants';
77
import {helper} from './helper';
88

9-
function checkFile(filePath: string, remove: boolean) {
9+
function checkFile(filePath: string, remove: boolean): boolean {
1010
const exists = existsSync(filePath);
1111
if (exists && remove) {
1212
// if we expected the file to exist then we remove it to prevent leftover file errors
@@ -15,7 +15,7 @@ function checkFile(filePath: string, remove: boolean) {
1515
return exists;
1616
}
1717

18-
function getArtifactPath(prNum: number, sha: string = SHA) {
18+
function getArtifactPath(prNum: number, sha: string = SHA): string {
1919
return `${AIO_DOWNLOADS_DIR}/${prNum}-${computeShortSha(sha)}-aio-snapshot.tgz`;
2020
}
2121

@@ -35,8 +35,8 @@ function checkFiles(prNum: number, isPublic: boolean, sha: string, isLegacy: boo
3535
return { existingFiles, missingFiles };
3636
}
3737

38-
class ToExistAsAFile {
39-
public compare(actual: string, remove = true) {
38+
class ToExistAsAFile implements jasmine.CustomMatcher {
39+
public compare(actual: string, remove = true): jasmine.CustomMatcherResult {
4040
const pass = checkFile(actual, remove);
4141
return {
4242
message: `Expected file at "${actual}" ${pass ? 'not' : ''} to exist`,
@@ -45,8 +45,8 @@ class ToExistAsAFile {
4545
}
4646
}
4747

48-
class ToExistAsAnArtifact {
49-
public compare(actual: {prNum: number, sha?: string}, remove = true) {
48+
class ToExistAsAnArtifact implements jasmine.CustomMatcher {
49+
public compare(actual: {prNum: number, sha?: string}, remove = true): jasmine.CustomMatcherResult {
5050
const { prNum, sha = SHA } = actual;
5151
const filePath = getArtifactPath(prNum, sha);
5252
const pass = checkFile(filePath, remove);
@@ -57,8 +57,9 @@ class ToExistAsAnArtifact {
5757
}
5858
}
5959

60-
class ToExistAsABuild {
61-
public compare(actual: {prNum: number, isPublic?: boolean, sha?: string, isLegacy?: boolean}, remove = true) {
60+
class ToExistAsABuild implements jasmine.CustomMatcher {
61+
public compare(actual: {prNum: number, isPublic?: boolean, sha?: string, isLegacy?: boolean}, remove = true):
62+
jasmine.CustomMatcherResult {
6263
const {prNum, isPublic = true, sha = SHA, isLegacy = false} = actual;
6364
const {missingFiles} = checkFiles(prNum, isPublic, sha, isLegacy, remove);
6465
return {
@@ -67,7 +68,8 @@ class ToExistAsABuild {
6768
pass: missingFiles.length === 0,
6869
};
6970
}
70-
public negativeCompare(actual: {prNum: number, isPublic?: boolean, sha?: string, isLegacy?: boolean}) {
71+
public negativeCompare(actual: {prNum: number, isPublic?: boolean, sha?: string, isLegacy?: boolean}):
72+
jasmine.CustomMatcherResult {
7173
const {prNum, isPublic = true, sha = SHA, isLegacy = false} = actual;
7274
const { existingFiles } = checkFiles(prNum, isPublic, sha, isLegacy, false);
7375
return {

0 commit comments

Comments
 (0)