net-im/tencent-qq: bump version to 3.2.15_p241224
[gentoo-zh.git] / .github / workflows / issues-bumper.js
blob392c4311258ec5e45b5b54403810c6ac43f87538
1 function getGithubAccount(package) {
2 var toml = require("toml");
3 var fs = require("fs");
4 var tomlFileContent = fs.readFileSync(
5 ".github/workflows/overlay.toml",
6 "utf8"
7 );
9 github_account = toml.parse(tomlFileContent)[package]["github_account"];
11 return github_account;
14 function randomSort(a, b) {
15 return 0.5 - Math.random();
18 module.exports = async ({ github, context, core }) => {
19 // hardcode gentoo-zh official repo name
20 var gentoo_zh_official_repo_name = "microcai/gentoo-zh";
21 var repo_name = process.env.GITHUB_REPOSITORY;
22 var repo_is_gentoo_zh_official = repo_name == gentoo_zh_official_repo_name;
24 let pkgs = JSON.parse(process.env.pkgs);
25 const SEARCH_MAX_REMAINING = 30;
26 var pkgs_gt_search_limit = pkgs.length > SEARCH_MAX_REMAINING;
28 if (pkgs_gt_search_limit) {
29 pkgs.sort(randomSort);
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 // }
37 if (pkgs_gt_search_limit) {
38 const { data: rate_limit_data } = await github.request("/rate_limit");
39 if (rate_limit_data.resources.search.remaining == 0) {
40 core.warning(`cause rate limit, failed to search ${pkg.name}'s issues`);
41 break;
45 titlePrefix = "[nvchecker] " + pkg.name + " can be bump to ";
46 title = titlePrefix + pkg.newver;
47 var body = "";
48 if (pkg.oldver != null) {
49 body += "oldver: " + pkg.oldver;
52 // append @github_account to body
53 // only mention user on official gentoo-zh repo
54 github_accounts = getGithubAccount(pkg.name);
55 if (typeof github_accounts == "object") {
56 // multiple accounts example:
57 // github_account = ["st0nie", "peeweep"]
58 body += "\nCC:";
59 for (let github_account of github_accounts) {
60 body += repo_is_gentoo_zh_official
61 ? " @" + github_account
62 : " " + github_account
64 } else if (typeof github_accounts == "string") {
65 // single account example:
66 // github_account = "peeweep"
67 body += repo_is_gentoo_zh_official
68 ? "\nCC: @" + github_accounts
69 : "\nCC: " + github_accounts
72 // if body still empty, convert to null
73 // because github rest api response's issue body is null, they should be same
74 if (body == "") {
75 body = null;
78 // search issues by titlePrefix
79 let issuesData = [];
80 let page_number = 0;
81 // repo_name = "microcai/gentoo-zh";
82 searchQuery = `repo:${repo_name} is:issue label:nvchecker in:title ${titlePrefix}`;
83 while (true) {
84 page_number++;
85 try {
86 const searchIssues = await github.rest.search.issuesAndPullRequests({
87 q: searchQuery,
88 per_page: 100,
89 page: page_number,
90 });
91 response = searchIssues.data.items;
93 issuesData = issuesData.concat(response);
94 if (response.length < 100) {
95 break;
97 } catch (error) {
98 core.warning(`failed to search issues with title: ${searchQuery}`);
99 throw error;
103 (async function () {
104 // search existed in issues
105 for (let issueData of issuesData) {
106 // if titlePrefix matched
107 if (
108 issueData.title.length >= titlePrefix.length
109 ? issueData.title.substring(0, titlePrefix.length) == titlePrefix
110 : false
112 // titlePrefix matched;
113 if (issueData.body == body && issueData.title == title) {
114 // if body and title all matched, goto next loop
115 return;
116 } else {
117 // if body or title not matched
118 if (issueData.state == "open") {
119 // if state is open, edit it, then goto next loop
120 // if (!repo_is_gentoo_zh_official) {
121 // // only update on official gentoo-zh repo
122 // return;
123 // }
124 const issueUpdate = await github.rest.issues.update({
125 owner: context.repo.owner,
126 repo: context.repo.repo,
127 issue_number: issueData.number,
128 title: title,
129 body: body,
131 console.log("Edit issue on %s", issueUpdate.data.html_url);
132 return;
133 } else {
134 // if state is clsoe,create new
135 break;
140 try {
141 // create new issue
142 const issuesCreate = await github.rest.issues.create({
143 owner: context.repo.owner,
144 repo: context.repo.repo,
145 title: title,
146 body: body,
147 labels: ["nvchecker"],
149 console.log("Created issue on %s", issuesCreate.data.html_url);
150 } catch (error) {
151 core.warning(`failed to create issues with title: ${title}`);
152 throw error;
154 })();