Skip to content

Commit f35aac9

Browse files
committed
chore: adding pagination
1 parent f011dc6 commit f35aac9

6 files changed

Lines changed: 71 additions & 30 deletions

File tree

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"camelcase": "off",
2525
"import/extensions": "off",
2626
"import/no-unresolved": "off",
27-
"import/no-extraneous-dependencies": "off"
27+
"import/no-extraneous-dependencies": "off",
28+
"import/prefer-default-export": "off"
2829
}
2930
}

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ Para utilizar a action em seu workflow, adicione o seguinte trecho ao seu arquiv
1919
repo-token: "${{ secrets.GITHUB_TOKEN }}"
2020
```
2121
22+
**Observação importante:** Não esquecer de adicionar a permission ***actions:read*** no job onde o step do **eteg/list-github-environments** atuará.
23+
2224
### Parâmetros
2325
2426
Aqui estão os parâmetros disponíveis para personalizar a action:

scr/index.ts

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import 'dotenv/config';
22
import { getInput, setOutput } from '@actions/core';
3-
import { context as contextGit } from '@actions/github';
4-
import api from './providers/api';
3+
import { context } from '@actions/github';
4+
import { Github } from './services/github';
55

66
const hasProtectionRuleFilter = (
77
value: ProtectionRule[],
@@ -26,20 +26,13 @@ const hasProtectionRuleFilter = (
2626

2727
const repoToken = getInput('repo-token', { required: true });
2828

29-
const {
30-
repo: { owner, repo },
31-
} = contextGit;
29+
const { repo } = context;
3230

33-
const fetchEnvs = await api.get<unknown, IEnvironmentApi>(
34-
`/repos/${owner}/${repo}/environments`,
35-
{
36-
headers: {
37-
Authorization: `Bearer ${repoToken}`,
38-
},
39-
},
40-
);
31+
const git = new Github({ apiToken: repoToken });
4132

42-
const envList = fetchEnvs.environments
33+
const fetchEnvs = await git.listAllEnvironments(repo);
34+
35+
const envList = fetchEnvs
4336
.filter(
4437
({ name, protection_rules }) =>
4538
!excludeEnvs.includes(name) &&

scr/providers/api.ts

Lines changed: 0 additions & 14 deletions
This file was deleted.

scr/services/github.ts

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import axios, { AxiosInstance } from 'axios';
2+
3+
type Pagination = {
4+
owner: string;
5+
repo: string;
6+
per_page?: number;
7+
page?: number;
8+
};
9+
10+
export class Github {
11+
private githubRequest: AxiosInstance;
12+
13+
constructor({ apiToken }: { apiToken: string }) {
14+
this.githubRequest = Github.createGithubConnection(apiToken);
15+
}
16+
17+
public async listAllEnvironments({
18+
owner,
19+
repo,
20+
per_page = 2,
21+
page = 1,
22+
}: Pagination) {
23+
let envs: Environment[] = [];
24+
const { data } = await this.githubRequest.get<IEnvironmentApi>(
25+
`/repos/${owner}/${repo}/environments`,
26+
{
27+
params: {
28+
per_page,
29+
page,
30+
},
31+
},
32+
);
33+
34+
envs = data.environments;
35+
36+
if (Math.ceil(data.total_count / per_page) >= page + 1) {
37+
envs = [
38+
...envs,
39+
...(await this.listAllEnvironments({
40+
owner,
41+
repo,
42+
per_page,
43+
page: page + 1,
44+
})),
45+
];
46+
}
47+
48+
return envs;
49+
}
50+
51+
private static createGithubConnection(apiToken: string) {
52+
return axios.create({
53+
baseURL: 'https://api.github.com',
54+
headers: {
55+
Authorization: `Bearer ${apiToken}`,
56+
},
57+
});
58+
}
59+
}

tsconfig.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"skipLibCheck": true,
99
"strict": true,
1010
"esModuleInterop": true,
11-
"forceConsistentCasingInFileNames": true,
11+
"forceConsistentCasingInFileNames": true
1212
}
1313
}

0 commit comments

Comments
 (0)