-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.mjs
127 lines (109 loc) · 3.82 KB
/
index.mjs
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
import * as core from "@actions/core";
import fs from "fs/promises";
import github from "@actions/github";
import { i18nextGoogleSheet } from "i18next-google-sheet";
import * as gitUtils from "./gitUtils.js";
const STATS_KEYS = {
added: "새로 추가된 항목",
updated: "업데이트된 항목",
reused: "사용으로 변경된 항목",
pruned: "미사용으로 변경된 항목",
};
const SPREADSHEET_MUTABLE_STATS = ["added", "reused", "pruned"];
function getStatsSummary(stats) {
return stats
.map(
([key, { count, namespaces }]) =>
`- ${STATS_KEYS[key]}: ${count} ${
count !== 0 ? `(${Array.from(namespaces).join(", ")})` : ""
}`
)
.join("\n");
}
function hasSpreadsheetChanged(stats) {
return stats.some(
([key, { count }]) => SPREADSHEET_MUTABLE_STATS.includes(key) && count > 0
);
}
function getPrBody(stats) {
return [
"i18next 다국어 구글 스프레드시트 싱크 PR입니다.\n",
getStatsSummary(stats),
].join("\n");
}
(async () => {
try {
const { GITHUB_TOKEN, GOOGLE_SPREADSHEET_CREDENTIALS } = process.env;
if (!GITHUB_TOKEN) {
core.setFailed("Please add the GITHUB_TOKEN to the action.");
return;
}
if (!GOOGLE_SPREADSHEET_CREDENTIALS) {
core.setFailed(
"Please add the GOOGLE_SPREADSHEET_CREDENTIALS to the action."
);
return;
}
console.log("setting git user");
await gitUtils.setupUser();
console.log("setting GitHub credentials");
await fs.writeFile(
`${process.env.HOME}/.netrc`,
`machine github.com\nlogin github-actions[bot]\npassword ${GITHUB_TOKEN}`
);
console.log("syncing google spreadsheet");
const path = core.getInput("path");
const range = core.getInput("range");
const spreadsheetId = core.getInput("spreadsheet-id");
const escapeNonPrintableUnicodeCharacters = core.getBooleanInput('escape-non-printable-unicode-characters');
const stats = await i18nextGoogleSheet({
path,
range,
spreadsheet_id: spreadsheetId,
credentials_json: GOOGLE_SPREADSHEET_CREDENTIALS,
escape_non_printable_unicode_characters: escapeNonPrintableUnicodeCharacters,
});
const stats_list = Object.entries(stats);
console.log("checking diff");
if ((await gitUtils.diff(path)) === 0) {
console.log("no changes found");
return;
}
const repo = `${github.context.repo.owner}/${github.context.repo.repo}`;
const branch = github.context.ref.replace("refs/heads/", "");
const newBranch = `sync-i18next/${branch}`;
const octokit = github.getOctokit(GITHUB_TOKEN);
const searchQuery = `repo:${repo}+state:open+head:${newBranch}+base:${branch}`;
const searchResult = await octokit.rest.search.issuesAndPullRequests({
q: searchQuery,
});
await gitUtils.checkout(newBranch);
const commitMessage = "chore: sync i18next google spreadsheet";
await gitUtils.commit(commitMessage, path);
await gitUtils.push(newBranch);
const prBody = getPrBody(stats_list);
if (searchResult.data.items.length === 0) {
console.log("creating pull request");
await octokit.rest.pulls.create({
base: branch,
head: newBranch,
title: "chore: sync i18next google spreadsheet",
body: prBody,
...github.context.repo,
});
} else {
const [pullRequest] = searchResult.data.items;
console.log(`updating found pull request ${pullRequest.number}`);
await octokit.rest.pulls.update({
pull_number: pullRequest.number,
title: "chore: sync i18next google spreadsheet",
body: prBody,
...github.context.repo,
});
}
core.setOutput("stats", getStatsSummary(stats_list));
core.setOutput("changed", hasSpreadsheetChanged(stats_list));
} catch (error) {
core.setFailed(error.message);
}
})();