Skip to content

Commit fbc893f

Browse files
Simplify update-project-json script (#463)
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
1 parent 23993b1 commit fbc893f

File tree

3 files changed

+41
-86
lines changed

3 files changed

+41
-86
lines changed

.github/workflows/autofix.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: autofix.ci # needed to securely identify the workflow
2+
3+
on:
4+
pull_request:
5+
6+
concurrency:
7+
group: ${{ github.workflow }}-${{ github.ref }}
8+
cancel-in-progress: true
9+
10+
jobs:
11+
fix:
12+
name: Run automated fix
13+
uses: prettier/shared-workflows/.github/workflows/automated-fix.yml@main
14+
permissions: {}
15+
with:
16+
repository: prettier/prettier-regression-testing
17+
script: npm ci && npm run fix

.github/workflows/update-project-json.yml

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,10 @@ jobs:
2525

2626
- name: Run update-projects-json.mjs
2727
run: node ./scripts/update-projects-json.mjs
28-
env:
29-
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28+
29+
- uses: peter-evans/create-pull-request@v7
30+
with:
31+
commit-message: "Update projects.json"
32+
branch: automated-update-projects
33+
branch-suffix: timestamp
34+
title: "Update projects.json"

scripts/update-projects-json.mjs

Lines changed: 17 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -1,97 +1,30 @@
11
import * as prettier from "prettier";
22
import fs from "fs/promises";
3-
import path from "path";
4-
import url from "url";
53
import execa from "execa";
6-
import * as github from "@actions/github";
7-
8-
const __filename = url.fileURLToPath(import.meta.url);
9-
const __dirname = path.dirname(__filename);
10-
11-
let octokit;
12-
function getOctokit() {
13-
if (!octokit) {
14-
octokit = github.getOctokit(process.env.GITHUB_TOKEN);
15-
}
16-
return octokit;
17-
}
184

195
async function updateProjectsJsonFile() {
20-
const projectsJsonPath = path.join(__dirname, "..", "projects.json");
21-
const projects = JSON.parse(await fs.readFile(projectsJsonPath, "utf-8"));
22-
const octokit = getOctokit();
23-
const latestCommits = new Map();
6+
const projectsJsonFile = new URL("../projects.json", import.meta.url);
7+
const projects = JSON.parse(await fs.readFile(projectsJsonFile, "utf-8"));
8+
249
await Promise.all(
25-
Object.entries(projects).map(async ([projectName, { url }]) => {
26-
const splitted = url.split("/");
27-
const owner = splitted[3];
28-
const repo = splitted[4].slice(0, -4);
29-
const repository = await octokit.repos.get({ owner, repo });
30-
const defaultBranch = repository.data.default_branch;
31-
const latestCommit = await octokit.repos.getCommit({
32-
owner,
33-
repo,
34-
ref: defaultBranch,
35-
});
36-
const sha = latestCommit.data.sha;
37-
latestCommits.set(projectName, sha);
10+
Object.values(projects).map(async (project) => {
11+
const { stdout } = await execa("git", [
12+
"ls-remote",
13+
"--exit-code",
14+
project.url,
15+
"HEAD",
16+
]);
17+
const [sha] = stdout.trim().split(/\s/);
18+
project.commit = sha;
3819
}),
3920
);
40-
const newProjects = { ...projects };
41-
for (const [projectName, sha] of latestCommits) {
42-
newProjects[projectName].commit = sha;
43-
}
21+
4422
await fs.writeFile(
45-
projectsJsonPath,
46-
await prettier.format(JSON.stringify(newProjects)),
23+
projectsJsonFile,
24+
await prettier.format(JSON.stringify(projects), { parser: "json" }),
4725
);
4826
}
4927

50-
function getFormattedDate() {
51-
const date = new Date();
52-
const dateStr =
53-
date.getFullYear() +
54-
"-" +
55-
("0" + (date.getMonth() + 1)).slice(-2) +
56-
"-" +
57-
("0" + date.getDate()).slice(-2) +
58-
"-" +
59-
("0" + date.getHours()).slice(-2) +
60-
"-" +
61-
("0" + date.getMinutes()).slice(-2);
62-
return dateStr;
63-
}
64-
65-
async function createPullRequest() {
66-
const formattedDate = getFormattedDate();
67-
const branchName = `update-projects-json-${formattedDate}`;
68-
await execa("git", ["checkout", "-b", branchName]);
69-
await updateProjectsJsonFile();
70-
71-
const { stdout: diff } = await execa("git", ["diff", "--name-only"]);
72-
if (diff.includes("projects.json")) {
73-
await execa("git", ["add", "."]);
74-
await execa("git", ["commit", "-m", `"Update projects.json"`]);
75-
await execa("git", ["push", "origin", branchName]);
76-
const octokit = getOctokit();
77-
await octokit.pulls.create({
78-
...github.context.repo,
79-
title: `Update projects.json (${formattedDate})`,
80-
head: branchName,
81-
base: "main",
82-
maintainer_can_modify: true,
83-
});
84-
}
85-
}
86-
87-
process.on("unhandledRejection", function (reason) {
88-
throw reason;
89-
});
28+
await updateProjectsJsonFile();
9029

91-
createPullRequest()
92-
.then(() => {
93-
console.log("done");
94-
})
95-
.catch((e) => {
96-
throw e;
97-
});
30+
console.log("done");

0 commit comments

Comments
 (0)