media-fonts/sarasa-gothic: add 1.0.11, drop 1.0.10
[gentoo-zh.git] / .github / workflows / issues-bumper.js
blob606fc269b94c6214f8e335c78d04a6ca40c7e95c
1 async function getSearchIssuesResult(github, repo_name, titleSearchKeyword, page_number) {
2   // repo_name = "microcai/gentoo-zh";
3   searchQuery = `repo:${repo_name} is:issue label:nvchecker in:title ${titleSearchKeyword}`;
4   const searchIssues = await github.rest.search.issuesAndPullRequests({
5     q: searchQuery,
6     per_page: 100,
7     page: page_number,
8   });
9   return searchIssues.data.items;
12 function getGithubAccount(package) {
13   var toml = require("toml");
14   var fs = require("fs");
15   var tomlFileContent = fs.readFileSync(
16     ".github/workflows/overlay.toml",
17     "utf8"
18   );
20   github_account = toml.parse(tomlFileContent)[package]["github_account"];
22   return github_account;
25 module.exports = async ({ github, context, core }) => {
26   // hardcode gentoo-zh official repo name
27   var gentoo_zh_official_repo_name = "microcai/gentoo-zh";
28   var repo_name = process.env.GITHUB_REPOSITORY;
29   var repo_is_gentoo_zh_official = repo_name == gentoo_zh_official_repo_name;
31   let pkgs = JSON.parse(process.env.pkgs);
32   for (let pkg of pkgs) {
33     // // limit "x11-misc/9menu" and "dev-libs/libthai"
34     // if (pkg.name != "x11-misc/9menu" && pkg.name != "dev-libs/libthai") {
35     //   continue;
36     // }
38     titlePrefix = "[nvchecker] " + pkg.name + " can be bump to ";
39     title = titlePrefix + pkg.newver;
40     var body = "";
41     if (pkg.oldver != null) {
42       body += "oldver: " + pkg.oldver;
43     }
45     // append @github_account to body
46     // only mention user on official gentoo-zh repo
47     github_accounts = getGithubAccount(pkg.name);
48     if (typeof github_accounts == "object") {
49       // multiple accounts example:
50       // github_account = ["st0nie", "peeweep"]
51       body += "\nCC:";
52       for (let github_account of github_accounts) {
53         body += repo_is_gentoo_zh_official
54           ? " @" + github_account
55           : " " + github_account
56       }
57     } else if (typeof github_accounts == "string") {
58       // single account example:
59       // github_account = "peeweep"
60       body += repo_is_gentoo_zh_official
61         ? "\nCC: @" + github_accounts
62         : "\nCC: " + github_accounts
63     }
65     // if body still empty, convert to null
66     // because github rest api response's issue body is null, they should be same
67     if (body == "") {
68       body = null;
69     }
71     // search issues by titlePrefix
72     let issuesData = [];
73     let page_number = 0;
74     while (true) {
75       page_number++;
76       try {
77         const response = await getSearchIssuesResult(
78           github,
79           repo_name,
80           titleSearchKeyword = titlePrefix,
81           page_number
82         );
83         issuesData = issuesData.concat(response);
84         if (response.length < 100) {
85           break;
86         }
87       } catch (error) {
88         core.warning(`Waring ${error}, action may still succeed though`);
89       }
90     }
92     (async function () {
93       // search existed in issues
94       for (let issueData of issuesData) {
95         // if titlePrefix matched
96         if (
97           issueData.title.length >= titlePrefix.length
98             ? issueData.title.substring(0, titlePrefix.length) == titlePrefix
99             : false
100         ) {
101           // titlePrefix matched;
102           if (issueData.body == body && issueData.title == title) {
103             // if body and title all matched, goto next loop
104             return;
105           } else {
106             // if body or title not matched
107             if (issueData.state == "open") {
108               // if state is open, edit it, then goto next loop
109               // if (!repo_is_gentoo_zh_official) {
110               //   // only update on official gentoo-zh repo
111               //   return;
112               // }
113               const issueUpdate = await github.rest.issues.update({
114                 owner: context.repo.owner,
115                 repo: context.repo.repo,
116                 issue_number: issueData.number,
117                 title: title,
118                 body: body,
119               });
120               console.log("Edit issue on %s", issueUpdate.data.html_url);
121               return;
122             } else {
123               // if state is clsoe,create new
124               break;
125             }
126           }
127         }
128       }
129       try {
130         // create new issue
131         const issuesCreate = await github.rest.issues.create({
132           owner: context.repo.owner,
133           repo: context.repo.repo,
134           title: title,
135           body: body,
136           labels: ["nvchecker"],
137         });
138         console.log("Created issue on %s", issuesCreate.data.html_url);
139       } catch (error) {
140         core.warning(`Waring ${error}, action may still succeed though`);
141       }
142     })();
143   }