This repository has been archived by the owner on Feb 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.ts
146 lines (139 loc) · 3.98 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import "dotenv/config";
import * as fs from "fs";
import fetch from "node-fetch";
const { API_TOKEN, CVS, OWNER_TYPE, OWNER_NAME } = process.env;
if (
API_TOKEN == undefined ||
CVS == undefined ||
OWNER_TYPE == undefined ||
OWNER_NAME == undefined
) {
throw new Error("Environment variables are not configured.");
}
const readConnectedProjects = () => {
const text = fs.readFileSync("projects.txt", "utf8");
const linesFromEnd = text.split("\n").reverse();
const projects: string[] = [];
let i = 0;
while (i < linesFromEnd.length) {
const line = linesFromEnd[i];
if (["", "Follow Project", "Unfollow Project"].includes(line)) {
i++;
} else if (line === "Set Up Project") {
i = i + 2;
} else {
projects.push(line);
i++;
}
}
return projects.reverse();
};
const getProjectEnvvars = async (
projectName: string,
pageToken?: string
): Promise<any[]> => {
const headers = {
authorization: `Basic ${Buffer.from(API_TOKEN).toString("base64")}`,
};
const response = await fetch(
`https://circleci.com/api/v2/project/${CVS}/${OWNER_NAME}/${projectName}/envvar${
pageToken ? `?page-token=${pageToken}` : ""
}`,
{ headers }
);
const data = await response.json();
if (data.items != undefined) {
const next: any[] = data.next_page_token
? await getProjectEnvvars(projectName, data.next_page_token)
: [];
return [...data.items, ...next];
} else if (data.message != undefined) {
return [];
} else {
throw new Error(data);
}
};
const getProjectSettings = async (
projectName: string,
pageToken?: string
): Promise<any[]> => {
const headers = {
authorization: `Basic ${Buffer.from(API_TOKEN).toString("base64")}`,
};
const response = await fetch(
`https://circleci.com/api/v1.1/project/${CVS}/${OWNER_NAME}/${projectName}/settings${
pageToken ? `?page-token=${pageToken}` : ""
}`,
{ headers }
);
const data = await response.json();
return data.aws;
};
const getContexts = async (pageToken?: string): Promise<any[]> => {
const headers = {
authorization: `Basic ${Buffer.from(API_TOKEN).toString("base64")}`,
};
const response = await fetch(
`https://circleci.com/api/v2/context?owner-slug=${CVS}/${OWNER_NAME}&owner-type=${OWNER_TYPE}${
pageToken ? `&page-token=${pageToken}` : ""
}`,
{
headers,
}
);
const data = await response.json();
if (data.items != undefined) {
const next: any[] = data.next_page_token
? await getContexts(data.next_page_token)
: [];
return [...data.items, ...next];
} else if (data.message != undefined) {
return [];
} else {
throw new Error(data);
}
};
const getContextEnvvars = async (
contextId: string,
pageToken?: string
): Promise<any[]> => {
const headers = {
authorization: `Basic ${Buffer.from(API_TOKEN).toString("base64")}`,
};
const response = await fetch(
`https://circleci.com/api/v2/context/${contextId}/environment-variable${
pageToken ? `?page-token=${pageToken}` : ""
}`,
{ headers }
);
const data = await response.json();
if (data.items != undefined) {
const next: any[] = data.next_page_token
? await getProjectEnvvars(contextId, data.next_page_token)
: [];
return [...data.items, ...next].map(({ variable, created_at }) => ({
variable,
created_at,
}));
} else if (data.message != undefined) {
return [];
} else {
throw new Error(data);
}
};
const app = async () => {
// Projects
const connectedProjects = readConnectedProjects();
connectedProjects.forEach(async (project) => {
const envVars = await getProjectEnvvars(project);
const awsConfig = await getProjectSettings(project);
console.log(JSON.stringify({ project, envVars, awsConfig }));
});
// Contexts
const contexts = await getContexts();
contexts.forEach(async (context) => {
const items = await getContextEnvvars(context.id);
console.log(JSON.stringify({ context: context.name, items }));
});
};
app();