|
1 | 1 | import * as prettier from "prettier"; |
2 | 2 | import fs from "fs/promises"; |
3 | | -import path from "path"; |
4 | | -import url from "url"; |
5 | 3 | 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 | | -} |
18 | 4 |
|
19 | 5 | 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 | + |
24 | 9 | 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; |
38 | 19 | }), |
39 | 20 | ); |
40 | | - const newProjects = { ...projects }; |
41 | | - for (const [projectName, sha] of latestCommits) { |
42 | | - newProjects[projectName].commit = sha; |
43 | | - } |
| 21 | + |
44 | 22 | await fs.writeFile( |
45 | | - projectsJsonPath, |
46 | | - await prettier.format(JSON.stringify(newProjects)), |
| 23 | + projectsJsonFile, |
| 24 | + await prettier.format(JSON.stringify(projects), { parser: "json" }), |
47 | 25 | ); |
48 | 26 | } |
49 | 27 |
|
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(); |
90 | 29 |
|
91 | | -createPullRequest() |
92 | | - .then(() => { |
93 | | - console.log("done"); |
94 | | - }) |
95 | | - .catch((e) => { |
96 | | - throw e; |
97 | | - }); |
| 30 | +console.log("done"); |
0 commit comments