Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions .github/workflows/autofix.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: autofix.ci # needed to securely identify the workflow

on:
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
fix:
name: Run automated fix
uses: prettier/shared-workflows/.github/workflows/automated-fix.yml@main
permissions: {}
with:
repository: prettier/prettier-regression-testing
script: npm ci && npm run fix
9 changes: 7 additions & 2 deletions .github/workflows/update-project-json.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ jobs:

- name: Run update-projects-json.mjs
run: node ./scripts/update-projects-json.mjs
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- uses: peter-evans/create-pull-request@v7
with:
commit-message: "Update projects.json"
branch: automated-update-projects
branch-suffix: timestamp
title: "Update projects.json"
101 changes: 17 additions & 84 deletions scripts/update-projects-json.mjs
Original file line number Diff line number Diff line change
@@ -1,97 +1,30 @@
import * as prettier from "prettier";
import fs from "fs/promises";
import path from "path";
import url from "url";
import execa from "execa";
import * as github from "@actions/github";

const __filename = url.fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

let octokit;
function getOctokit() {
if (!octokit) {
octokit = github.getOctokit(process.env.GITHUB_TOKEN);
}
return octokit;
}

async function updateProjectsJsonFile() {
const projectsJsonPath = path.join(__dirname, "..", "projects.json");
const projects = JSON.parse(await fs.readFile(projectsJsonPath, "utf-8"));
const octokit = getOctokit();
const latestCommits = new Map();
const projectsJsonFile = new URL("../projects.json", import.meta.url);
const projects = JSON.parse(await fs.readFile(projectsJsonFile, "utf-8"));

await Promise.all(
Object.entries(projects).map(async ([projectName, { url }]) => {
const splitted = url.split("/");
const owner = splitted[3];
const repo = splitted[4].slice(0, -4);
const repository = await octokit.repos.get({ owner, repo });
const defaultBranch = repository.data.default_branch;
const latestCommit = await octokit.repos.getCommit({
owner,
repo,
ref: defaultBranch,
});
const sha = latestCommit.data.sha;
latestCommits.set(projectName, sha);
Object.values(projects).map(async (project) => {
const { stdout } = await execa("git", [
"ls-remote",
"--exit-code",
project.url,
"HEAD",
]);
const [sha] = stdout.trim().split(/\s/);
project.commit = sha;
}),
);
const newProjects = { ...projects };
for (const [projectName, sha] of latestCommits) {
newProjects[projectName].commit = sha;
}

await fs.writeFile(
projectsJsonPath,
await prettier.format(JSON.stringify(newProjects)),
projectsJsonFile,
await prettier.format(JSON.stringify(projects), { parser: "json" }),
);
}

function getFormattedDate() {
const date = new Date();
const dateStr =
date.getFullYear() +
"-" +
("0" + (date.getMonth() + 1)).slice(-2) +
"-" +
("0" + date.getDate()).slice(-2) +
"-" +
("0" + date.getHours()).slice(-2) +
"-" +
("0" + date.getMinutes()).slice(-2);
return dateStr;
}

async function createPullRequest() {
const formattedDate = getFormattedDate();
const branchName = `update-projects-json-${formattedDate}`;
await execa("git", ["checkout", "-b", branchName]);
await updateProjectsJsonFile();

const { stdout: diff } = await execa("git", ["diff", "--name-only"]);
if (diff.includes("projects.json")) {
await execa("git", ["add", "."]);
await execa("git", ["commit", "-m", `"Update projects.json"`]);
await execa("git", ["push", "origin", branchName]);
const octokit = getOctokit();
await octokit.pulls.create({
...github.context.repo,
title: `Update projects.json (${formattedDate})`,
head: branchName,
base: "main",
maintainer_can_modify: true,
});
}
}

process.on("unhandledRejection", function (reason) {
throw reason;
});
await updateProjectsJsonFile();

createPullRequest()
.then(() => {
console.log("done");
})
.catch((e) => {
throw e;
});
console.log("done");
Loading