generated from pabio/github-actions-starter
-
Notifications
You must be signed in to change notification settings - Fork 149
/
Copy pathautomations.ts
59 lines (58 loc) · 2.18 KB
/
automations.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
import { readFile } from "fs-extra";
import { join } from "path";
import { Octokit } from "@octokit/rest";
import { config } from "dotenv";
import axios from "axios";
import { getSecret } from "./helpers/secrets";
config();
const createAutomatedIssue = async () => {
const octokit = new Octokit({
auth: getSecret("AUTOMATION_TOKEN"),
});
const searchResults = await octokit.search.repos({
q: "topic:upptime",
per_page: 100,
});
const numberOfPages = Math.floor(searchResults.data.total_count / 100);
const body = await readFile(join(".", "src", "issue-template.md"), "utf8");
for await (const page of Array.from(Array(numberOfPages)).map((_, i) => i + 1)) {
const results = await octokit.search.repos({
q: "topic:upptime",
per_page: 100,
page,
});
for await (const repository of results.data.items) {
const owner = repository.owner!.login; // TODO: Better handling for "error TS18047: 'repository.owner' is possibly 'null'."
const repo = repository.name;
if (`${owner}/${repo}` !== "wakatime/statuspage") {
let hasDisabledAutomatedIssues = false;
try {
const { data } = await axios.get(
`https://raw.githubusercontent.com/${owner}/${repo}/master/.upptimerc.yml`
);
if (data.includes("hasDisabledAutomatedIssues")) hasDisabledAutomatedIssues = true;
} catch (error) {}
try {
if (!hasDisabledAutomatedIssues) {
const { data } = await axios.get(
`https://raw.githubusercontent.com/${owner}/${repo}/HEAD/.upptimerc.yml`
);
if (data.includes("hasDisabledAutomatedIssues")) hasDisabledAutomatedIssues = true;
}
} catch (error) {}
try {
await octokit.issues.create({
owner,
repo,
title: "⚠️ Add `workflow` scope to your personal access token",
body: body.replace("{{TEAM}}", owner),
labels: ["bug", "upptime-automated"],
});
console.log("Created an issue in", owner, repo);
} catch (error) {
console.log("Got an error in creating this issue", String(error));
}
}
}
}
};