2021-01-21 03:28:19 +07:00
|
|
|
name: UncivBot
|
|
|
|
|
|
|
|
on: [issue_comment]
|
|
|
|
|
|
|
|
jobs:
|
2021-01-22 18:03:26 +07:00
|
|
|
summary:
|
2021-01-21 03:42:47 +07:00
|
|
|
if: github.event.comment.body == 'summary'
|
2021-01-21 03:28:19 +07:00
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
|
|
- uses: actions/github-script@v3
|
|
|
|
with:
|
|
|
|
github-token: ${{secrets.GITHUB_TOKEN}}
|
|
|
|
script: |
|
2021-01-21 03:56:45 +07:00
|
|
|
var result = await github.repos.listCommits({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
per_page: 50 });
|
2021-01-21 03:58:45 +07:00
|
|
|
var commitSummary = "";
|
|
|
|
var ownerToCommits = {}
|
|
|
|
var reachedPreviousVersion = false
|
|
|
|
result.data.forEach(commit => {
|
2021-01-21 04:05:16 +07:00
|
|
|
if (reachedPreviousVersion) return
|
2021-01-21 04:03:36 +07:00
|
|
|
var author = commit.author.login
|
2021-01-21 04:05:16 +07:00
|
|
|
if (author=="uncivbot[bot]") return
|
2021-01-21 04:03:36 +07:00
|
|
|
var commitMessage = commit.commit.message.split("\n")[0];
|
2021-01-21 04:16:46 +07:00
|
|
|
|
|
|
|
if (commitMessage.match(/^\d+\.\d+\.\d+$/)){ // match EXACT version, like 3.4.55 ^ is for start-of-line, $ for end-of-line
|
2021-01-21 04:05:16 +07:00
|
|
|
reachedPreviousVersion=true
|
|
|
|
console.log(commitMessage)
|
|
|
|
return
|
|
|
|
}
|
2021-01-24 00:28:46 +07:00
|
|
|
if (commitMessage.startsWith("Merge ") || commitMessage.startsWith("Update ")) return
|
2021-01-21 04:06:54 +07:00
|
|
|
commitMessage = commitMessage.replace(/\(\#\d+\)/,"") // match PR auto-text, like (#2345)
|
2021-01-21 04:11:21 +07:00
|
|
|
if (author != context.repo.owner){
|
2021-01-21 04:31:01 +07:00
|
|
|
if (ownerToCommits[author] == undefined) ownerToCommits[author]=[]
|
2021-01-21 04:12:41 +07:00
|
|
|
ownerToCommits[author].push(commitMessage)
|
2021-01-21 04:06:54 +07:00
|
|
|
}
|
|
|
|
else commitSummary += "\n\n" + commitMessage
|
|
|
|
});
|
2021-01-21 04:21:38 +07:00
|
|
|
Object.entries(ownerToCommits).forEach(entry => {
|
2021-01-21 04:27:22 +07:00
|
|
|
const [author, commits] = entry;
|
2021-01-21 04:31:01 +07:00
|
|
|
if (commits.length==1) commitSummary += "\n\n" + commits[0] + " - By "+author
|
2021-01-21 04:16:46 +07:00
|
|
|
else {
|
|
|
|
commitSummary += "\n\nBy "+author+":"
|
2021-01-21 04:28:48 +07:00
|
|
|
commits.forEach(commitMessage => { commitSummary += "\n- "+commitMessage })
|
2021-01-21 04:16:46 +07:00
|
|
|
}
|
|
|
|
})
|
2021-01-21 03:47:27 +07:00
|
|
|
github.issues.createComment({
|
|
|
|
issue_number: context.issue.number,
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
2021-01-21 03:58:45 +07:00
|
|
|
body: commitSummary
|
2021-01-21 03:47:27 +07:00
|
|
|
})
|
2021-01-22 18:03:26 +07:00
|
|
|
|
|
|
|
merge_translations:
|
|
|
|
if: github.event.comment.body == 'merge translations'
|
|
|
|
runs-on: ubuntu-latest
|
|
|
|
steps:
|
|
|
|
- uses: actions/github-script@v3
|
|
|
|
with:
|
2021-01-24 04:43:49 +07:00
|
|
|
github-token: ${{secrets.ACTIONS_ACCESS_TOKEN}}
|
2021-01-22 18:03:26 +07:00
|
|
|
script: |
|
|
|
|
async function branchExists(branchName) {
|
|
|
|
try {
|
|
|
|
await github.git.getRef({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
ref: 'heads/' + branchName })
|
|
|
|
return true
|
|
|
|
} catch (err) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getDefaultBranch() {
|
|
|
|
var repo = await github.repos.get({owner: context.repo.owner, repo: context.repo.repo})
|
|
|
|
return repo.data.default_branch
|
|
|
|
}
|
|
|
|
|
2021-01-24 00:28:46 +07:00
|
|
|
var translations = "translations"
|
2021-01-22 18:03:26 +07:00
|
|
|
|
2021-01-24 00:28:46 +07:00
|
|
|
async function createTranslationBranchIfNeeded() {
|
|
|
|
if (await branchExists(translations)) return
|
|
|
|
var defaultBranch = await getDefaultBranch()
|
2021-01-24 01:09:56 +07:00
|
|
|
|
2021-01-24 00:35:04 +07:00
|
|
|
var currentHead = await github.git.getRef({
|
2021-01-24 00:28:46 +07:00
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
ref: 'heads/' + defaultBranch })
|
2021-01-24 01:09:56 +07:00
|
|
|
|
2021-01-24 00:28:46 +07:00
|
|
|
var currentSha = currentHead.data.object.sha
|
2021-01-24 00:35:04 +07:00
|
|
|
console.log("Current sha: " + currentSha)
|
2021-01-24 01:09:56 +07:00
|
|
|
|
2021-01-24 00:35:04 +07:00
|
|
|
await github.git.createRef({
|
2021-01-24 00:28:46 +07:00
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
ref: `refs/heads/`+translations,
|
2021-01-24 00:32:05 +07:00
|
|
|
sha: currentSha })
|
2021-01-24 01:09:56 +07:00
|
|
|
|
2021-01-24 00:35:04 +07:00
|
|
|
await github.issues.createComment({
|
2021-01-24 00:32:05 +07:00
|
|
|
issue_number: context.issue.number,
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
body: 'Translations branch created' })
|
2021-01-24 00:28:46 +07:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:52:58 +07:00
|
|
|
async function mergeExistingTranslationsIntoBranch(){
|
2021-01-24 01:11:10 +07:00
|
|
|
var translationPrs = await github.pulls.list({
|
2021-01-24 00:52:58 +07:00
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
2021-01-24 02:48:32 +07:00
|
|
|
state: "open"
|
2021-01-24 00:52:58 +07:00
|
|
|
})
|
2021-01-24 01:09:56 +07:00
|
|
|
|
2021-01-24 01:57:00 +07:00
|
|
|
translationPrs.data.forEach(async pr => {
|
2021-01-24 01:31:25 +07:00
|
|
|
if (pr.labels.some(label => label.name == "mergeable translation"))
|
2021-01-24 01:48:50 +07:00
|
|
|
await tryMergePr(pr)
|
2021-01-24 01:14:44 +07:00
|
|
|
})
|
2021-01-24 00:52:58 +07:00
|
|
|
}
|
|
|
|
|
2021-01-24 01:48:50 +07:00
|
|
|
async function tryMergePr(pr){
|
|
|
|
if (pr.base.ref != translations)
|
|
|
|
await github.pulls.update({
|
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
pull_number: pr.number,
|
|
|
|
base: translations })
|
|
|
|
|
2021-01-24 04:03:18 +07:00
|
|
|
try {
|
2021-01-24 01:54:00 +07:00
|
|
|
await github.pulls.merge({
|
2021-01-24 01:48:50 +07:00
|
|
|
owner: context.repo.owner,
|
|
|
|
repo: context.repo.repo,
|
|
|
|
pull_number: pr.number,
|
|
|
|
merge_method: "squash" })
|
2021-01-24 04:43:49 +07:00
|
|
|
console.log("Merged #"+pr.number+", "+pr.title)
|
2021-01-24 04:03:18 +07:00
|
|
|
} catch (err) {
|
|
|
|
console.log(err)
|
|
|
|
}
|
|
|
|
|
2021-01-24 01:48:50 +07:00
|
|
|
}
|
|
|
|
|
2021-01-24 00:28:46 +07:00
|
|
|
await createTranslationBranchIfNeeded()
|
2021-01-24 01:16:10 +07:00
|
|
|
await mergeExistingTranslationsIntoBranch()
|
2021-01-24 00:28:46 +07:00
|
|
|
|
2021-01-22 18:03:26 +07:00
|
|
|
//async function createTranslationPrIfNeeded(context: Context<Webhooks.WebhookPayloadIssueComment>,
|
|
|
|
// owner: string, translations: string) {
|
|
|
|
// var translationPulls = await context.github.pulls.list(context.repo({ state: "open", head: owner + ":" + translations }));
|
|
|
|
// if (translationPulls.data.length == 0) {
|
|
|
|
// var defaultBranch = await getDefaultBranch(context);
|
|
|
|
// await context.github.pulls.create(context.repo({ title: "Translations update", head: translations, base: defaultBranch }));
|
|
|
|
// await context.github.issues.createComment(context.issue({ body: 'Translations PR created' }));
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
|
|
|
|