Skip to content

Commit d6c42aa

Browse files
authored
chore: fix eslint errors [IN-807] (#3632)
1 parent 95bd360 commit d6c42aa

File tree

9 files changed

+276
-263
lines changed

9 files changed

+276
-263
lines changed

services/cronjobs/archived_repositories/.env.example

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE=10000
2+
13
ARCHIVED_REPOSITORIES_CHECKER_GITHUB_TOKEN=
24
#ARCHIVED_REPOSITORIES_CHECKER_GITHUB_BATCH_SIZE=5000
35
#ARCHIVED_REPOSITORIES_CHECKER_GITHUB_DELAY_MS=500000
@@ -6,11 +8,6 @@ ARCHIVED_REPOSITORIES_CHECKER_GITLAB_TOKEN=
68
#ARCHIVED_REPOSITORIES_CHECKER_GITLAB_BATCH_SIZE=100
79
#ARCHIVED_REPOSITORIES_CHECKER_GITLAB_DELAY_MS=500000
810

9-
# How many repositories to fetch from the main database and enqueue in each batch
10-
ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE=500
11-
# How long to wait between batches in milliseconds
12-
ARCHIVED_REPOSITORIES_CHECKER_BATCH_DELAY_MS=5000
13-
1411

1512
# PostgreSQL connection details
1613
CROWD_DB_WRITE_HOST=

services/cronjobs/archived_repositories/README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ It gets all the repositories we have in our database, calls the GitHub and GitLa
88
repositories are archived, taking care to not go over the APIs rate limits, and updates each repository in the database
99
accordingly.
1010

11-
It gets repositories from the database in batches, to avoid memory issues with our large dataset.
12-
1311
It uses the [BullMQ](https://bullmq.io/) library to manage the job queue and concurrency, including the rate limiting.
1412
BullMQ requires a Redis instance to be running, which it uses as storage.
1513

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,41 @@
1-
import { ofetch } from 'ofetch';
2-
import { Config } from "../config";
3-
import { RepositoryStatus } from "../types";
1+
import { ofetch } from 'ofetch'
2+
3+
import { Config } from '../config'
4+
import { RepositoryStatus } from '../types'
45

56
export async function getGithubRepoStatus(url: string, config: Config): Promise<RepositoryStatus> {
6-
const parsed = new URL(url);
7-
const parts = parsed.pathname.split('/').filter(Boolean);
7+
const parsed = new URL(url)
8+
const parts = parsed.pathname.split('/').filter(Boolean)
89

910
if (parts.length < 2) {
10-
throw new Error(`Invalid GitHub repository URL: ${url}`);
11+
throw new Error(`Invalid GitHub repository URL: ${url}`)
1112
}
1213

13-
const [owner, repo] = parts;
14+
const [owner, repo] = parts
1415

1516
try {
1617
const data = await ofetch(`https://api.github.com/repos/${owner}/${repo}`, {
1718
headers: {
1819
Authorization: `Bearer ${config.GithubToken}`,
1920
Accept: 'application/vnd.github+json',
2021
},
21-
});
22+
})
2223

2324
return {
2425
archived: data.archived,
25-
excluded: false
26-
};
26+
excluded: false,
27+
}
2728
} catch (error: any) {
2829
// Handle 404 (not found) and 403 (forbidden) as excluded repositories
2930
if (error?.status === 404 || error?.status === 403) {
30-
console.log(`GitHub repo not accessible (${error.status}): ${url} - marking as excluded`);
31+
console.log(`GitHub repo not accessible (${error.status}): ${url} - marking as excluded`)
3132
return {
3233
archived: false,
33-
excluded: true
34-
};
34+
excluded: true,
35+
}
3536
}
3637

3738
// Re-throw other errors to maintain existing error handling
38-
throw error;
39+
throw error
3940
}
4041
}
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,38 @@
1-
import { ofetch } from 'ofetch';
2-
import { Config } from "../config";
3-
import { RepositoryStatus } from "../types";
1+
import { ofetch } from 'ofetch'
2+
3+
import { Config } from '../config'
4+
import { RepositoryStatus } from '../types'
45

56
export async function getGitlabRepoStatus(url: string, config: Config): Promise<RepositoryStatus> {
6-
const parsed = new URL(url);
7-
const projectPath = parsed.pathname.split('/').filter(Boolean).join('/');
7+
const parsed = new URL(url)
8+
const projectPath = parsed.pathname.split('/').filter(Boolean).join('/')
89

910
if (!projectPath) {
10-
throw new Error(`Invalid GitLab repository URL: ${url}`);
11+
throw new Error(`Invalid GitLab repository URL: ${url}`)
1112
}
1213

13-
const encodedProjectPath = encodeURIComponent(projectPath);
14+
const encodedProjectPath = encodeURIComponent(projectPath)
1415

1516
try {
1617
const data = await ofetch(`https://gitlab.com/api/v4/projects/${encodedProjectPath}`, {
1718
headers: { 'PRIVATE-TOKEN': config.GitlabToken },
18-
});
19+
})
1920

2021
return {
2122
archived: data.archived,
22-
excluded: false
23-
};
23+
excluded: false,
24+
}
2425
} catch (error: any) {
2526
// Handle 404 (not found) and 403 (forbidden) as excluded repositories
2627
if (error?.status === 404 || error?.status === 403) {
27-
console.log(`GitLab repo not accessible (${error.status}): ${url} - marking as excluded`);
28+
console.log(`GitLab repo not accessible (${error.status}): ${url} - marking as excluded`)
2829
return {
2930
archived: false,
30-
excluded: true
31-
};
31+
excluded: true,
32+
}
3233
}
3334

3435
// Re-throw other errors to maintain existing error handling
35-
throw error;
36+
throw error
3637
}
3738
}
Lines changed: 73 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,77 @@
1-
import dotenv from 'dotenv';
1+
import dotenv from 'dotenv'
22

33
export interface Config {
4-
GithubToken: string;
5-
GitlabToken: string;
6-
7-
PostgresHost: string;
8-
PostgresPort: number;
9-
PostgresDBName: string;
10-
PostgresUsername: string;
11-
PostgresPassword: string;
12-
PostgresUrl: string;
13-
14-
RedisHost: string;
15-
RedisPort: number;
16-
RedisUsername: string;
17-
RedisPassword: string;
18-
RedisDB: number; // This is the Redis database index, not a URL.
19-
RedisUrl: string;
20-
21-
BatchSize: number;
22-
BatchDelayMs: number;
4+
GithubToken: string
5+
GitlabToken: string
6+
7+
PostgresHost: string
8+
PostgresPort: number
9+
PostgresDBName: string
10+
PostgresUsername: string
11+
PostgresPassword: string
12+
PostgresUrl: string
13+
14+
RedisHost: string
15+
RedisPort: number
16+
RedisUsername: string
17+
RedisPassword: string
18+
RedisDB: number // This is the Redis database index, not a URL.
19+
RedisUrl: string
20+
21+
BatchSize: number
2322
}
2423

25-
// These environment variables are required for the application to function correctly. The remaining ones should have
26-
// sensible defaults.
24+
// These environment variables are required for the application to function correctly.
25+
// The remaining ones should have sensible defaults.
2726
const requiredEnvVars = [
2827
'ARCHIVED_REPOSITORIES_CHECKER_GITHUB_TOKEN',
2928
'ARCHIVED_REPOSITORIES_CHECKER_GITLAB_TOKEN',
30-
29+
3130
'CROWD_DB_WRITE_HOST',
3231
'CROWD_DB_READ_HOST',
3332
'CROWD_DB_PORT',
3433
'CROWD_DB_USERNAME',
3534
'CROWD_DB_PASSWORD',
3635
'CROWD_DB_DATABASE',
37-
36+
3837
'CROWD_REDIS_USERNAME',
3938
'CROWD_REDIS_PASSWORD',
4039
'CROWD_REDIS_HOST',
4140
'CROWD_REDIS_PORT',
4241
'ARCHIVED_REPOSITORIES_CHECKER_REDIS_DB',
43-
];
42+
]
4443

4544
const defaults = {
46-
ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE: 100,
47-
ARCHIVED_REPOSITORIES_CHECKER_BATCH_DELAY_MS: 5000,
45+
ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE: 10000,
4846
}
4947

48+
/**
49+
* Helper that returns a validated environment variable as a string.
50+
* Throws an error if the variable is required and missing, or if an optional variable is missing
51+
* and has no default.
52+
*/
53+
const getEnv = (name: string): string => {
54+
const val = process.env[name]
55+
56+
// If we have the value, just return it.
57+
if (val !== undefined) {
58+
return val
59+
}
60+
61+
// If it's a required env var, throw.
62+
if (requiredEnvVars.includes(name)) {
63+
throw new Error(`Missing required environment variable: ${name}`)
64+
}
65+
66+
// Optional env var: fall back to defaults if present, otherwise undefined.
67+
const defaultVal = (defaults as Record<string, unknown>)[name]
68+
if (defaultVal !== undefined) {
69+
return String(defaultVal)
70+
}
71+
72+
// If we reach this point, an optional env var is missing and has no default set.
73+
throw new Error(`Missing default value for optional environment variable: ${name}`)
74+
}
5075

5176
/**
5277
* Loads configuration from environment variables or a .env file.
@@ -55,38 +80,39 @@ const defaults = {
5580
* we load the configuration in the future if needed (e.g. using a secrets store).
5681
*/
5782
export function getConfig(): Config {
58-
dotenv.config();
83+
dotenv.config()
5984

6085
// Check if all required environment variables are set
6186
for (const envVar of requiredEnvVars) {
6287
if (!process.env[envVar]) {
63-
throw new Error(`Missing required environment variable: ${envVar}`);
88+
throw new Error(`Missing required environment variable: ${envVar}`)
6489
}
6590
}
6691

6792
// Assemble database connection URL strings for convenience
68-
const postgresURL = `postgresql://${process.env.CROWD_DB_USERNAME}:${process.env.CROWD_DB_PASSWORD}@${process.env.CROWD_DB_WRITE_HOST}:${process.env.CROWD_DB_PORT}/${process.env.CROWD_DB_DATABASE}?ssl=true`;
69-
const redisURL = `redis://${process.env.CROWD_REDIS_USERNAME}:${process.env.CROWD_REDIS_PASSWORD}@${process.env.CROWD_REDIS_HOST}:${process.env.CROWD_REDIS_PORT}/${process.env.ARCHIVED_REPOSITORIES_CHECKER_REDIS_DB}`;
93+
const postgresURL = `postgresql://${getEnv('CROWD_DB_USERNAME')}:${getEnv('CROWD_DB_PASSWORD')}@${getEnv('CROWD_DB_WRITE_HOST')}:${getEnv('CROWD_DB_PORT')}/${getEnv('CROWD_DB_DATABASE')}?ssl=true`
94+
const redisURL = `redis://${getEnv('CROWD_REDIS_USERNAME')}:${getEnv('CROWD_REDIS_PASSWORD')}@${getEnv('CROWD_REDIS_HOST')}:${getEnv('CROWD_REDIS_PORT')}/${getEnv('ARCHIVED_REPOSITORIES_CHECKER_REDIS_DB')}`
7095

7196
return {
72-
GithubToken: process.env.ARCHIVED_REPOSITORIES_CHECKER_GITHUB_TOKEN!,
73-
GitlabToken: process.env.ARCHIVED_REPOSITORIES_CHECKER_GITLAB_TOKEN!,
74-
75-
PostgresHost: process.env.CROWD_DB_WRITE_HOST!,
76-
PostgresPort: parseInt(process.env.CROWD_DB_PORT!, 10),
77-
PostgresDBName: process.env.CROWD_DB_DATABASE!,
78-
PostgresUsername: process.env.CROWD_DB_USERNAME!,
79-
PostgresPassword: process.env.CROWD_DB_PASSWORD!,
97+
GithubToken: getEnv('ARCHIVED_REPOSITORIES_CHECKER_GITHUB_TOKEN'),
98+
GitlabToken: getEnv('ARCHIVED_REPOSITORIES_CHECKER_GITLAB_TOKEN'),
99+
100+
PostgresHost: getEnv('CROWD_DB_WRITE_HOST'),
101+
PostgresPort: parseInt(getEnv('CROWD_DB_PORT'), 10),
102+
PostgresDBName: getEnv('CROWD_DB_DATABASE'),
103+
PostgresUsername: getEnv('CROWD_DB_USERNAME'),
104+
PostgresPassword: getEnv('CROWD_DB_PASSWORD'),
80105
PostgresUrl: postgresURL,
81106

82-
RedisHost: process.env.CROWD_REDIS_HOST!,
83-
RedisPort: parseInt(process.env.CROWD_REDIS_PORT!, 10),
84-
RedisUsername: process.env.CROWD_REDIS_USERNAME!,
85-
RedisPassword: process.env.CROWD_REDIS_PASSWORD!,
86-
RedisDB: parseInt(process.env.REDIS_DB!, 10),
107+
RedisHost: getEnv('CROWD_REDIS_HOST'),
108+
RedisPort: parseInt(getEnv('CROWD_REDIS_PORT'), 10),
109+
RedisUsername: getEnv('CROWD_REDIS_USERNAME'),
110+
RedisPassword: getEnv('CROWD_REDIS_PASSWORD'),
111+
RedisDB: parseInt(getEnv('ARCHIVED_REPOSITORIES_CHECKER_REDIS_DB'), 10),
87112
RedisUrl: redisURL,
88113

89-
BatchSize: parseInt(process.env.ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE!, 10) || defaults.ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE,
90-
BatchDelayMs: parseInt(process.env.ARCHIVED_REPOSITORIES_CHECKER_BATCH_DELAY_MS!, 10) || defaults.ARCHIVED_REPOSITORIES_CHECKER_BATCH_DELAY_MS,
91-
};
114+
BatchSize:
115+
parseInt(getEnv('ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE'), 10) ||
116+
defaults.ARCHIVED_REPOSITORIES_CHECKER_BATCH_SIZE,
117+
}
92118
}

0 commit comments

Comments
 (0)