name: User-agent updater. on: repository_dispatch: types: ["Restart user_agent_updater workflow."] schedule: # At 00:00 on day-of-month 1. - cron: '0 0 1 * *' pull_request_target: types: [closed] jobs: User-agent: runs-on: ubuntu-latest env: codeFile: "Telegram/SourceFiles/mtproto/details/mtproto_domain_resolver.cpp" headBranchPrefix: "chrome_" baseBranch: "dev" isPull: "0" steps: - name: Set env. if: startsWith(github.event_name, 'pull_request') run: | echo "isPull=1" >> $GITHUB_ENV - name: Clone. uses: actions/checkout@v2 - name: Set up git. run: | token=${{ secrets.TOKEN_FOR_MASTER_UPDATER }} if [ -z "${token}" ]; then echo "Token is unset. Nothing to do." exit 1 fi url=https://x-access-token:$token@github.com/$GITHUB_REPOSITORY git config --global user.email "action@github.com" git config --global user.name "GitHub Action" git remote set-url origin $url - name: Delete branch. if: | env.isPull == '1' && github.event.action == 'closed' && startsWith(github.head_ref, env.headBranchPrefix) run: | git push origin --delete ${{ github.head_ref }} - name: Write a new version of Google Chrome to the user-agent for DNS. if: env.isPull == '0' shell: python run: | import subprocess, os, re; regExpVersion = "[0-9]+.[0-9]+.[0-9]+.[0-9]+"; chrome = "Chrome/"; def newVersion(): output = subprocess.check_output(["google-chrome", "--version"]); version = re.search(regExpVersion, output); if not version: print("Can't find a Chrome version."); exit(); return version.group(0); newChromeVersion = newVersion(); print(newChromeVersion); def setEnv(value): open(os.environ['GITHUB_ENV'], "a").write(value); def writeUserAgent(): p = os.environ['codeFile']; w = open(p, "r"); content = w.read(); w.close(); regExpChrome = chrome + regExpVersion; version = re.search(regExpChrome, content); if not version: print("Can't find an user-agent in the code."); exit(); content = re.sub(regExpChrome, chrome + newChromeVersion, content); w = open(p, "w"); w.write(content); setEnv("ChromeVersion=" + newChromeVersion); writeUserAgent(); - name: Push to a new branch. if: env.isPull == '0' && env.ChromeVersion != '' run: | git diff > git_diff.txt if [[ ! -s git_diff.txt ]]; then echo "Nothing to commit." exit 0 fi git checkout -b $headBranchPrefix$ChromeVersion git add $codeFile git commit -m "Update User-Agent for DNS to Chrome $ChromeVersion." git push origin $headBranchPrefix$ChromeVersion echo "Done!" - name: Close previous pull requests. if: env.isPull == '0' && env.ChromeVersion != '' uses: actions/github-script@0.4.0 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const common = { owner: context.repo.owner, repo: context.repo.repo, }; github.pulls.list(common).then(response => { response.data.forEach((item, _) => { if (item.head.ref.startsWith(process.env.headBranchPrefix)) { console.log(`Close ${item.title} #${item.number}.`); github.pulls.update({ pull_number: item.number, state: "closed", ...common }); } }); }); - name: Create a new pull request. if: env.isPull == '0' && env.ChromeVersion != '' uses: actions/github-script@0.4.0 with: github-token: ${{ secrets.GITHUB_TOKEN }} script: | const version = process.env.ChromeVersion; const title = `Update User-Agent for DNS to Chrome ${version}.`; github.pulls.create({ title: title, body: "", head: `${process.env.headBranchPrefix}${version}`, base: process.env.baseBranch, owner: context.repo.owner, repo: context.repo.repo, });