Sync Files #349
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Sync Files | |
on: | |
push: | |
branches: | |
- main | |
schedule: | |
- cron: '19 10 * * 5' # Run every Friday at 10:19 UTC | |
workflow_dispatch: | |
jobs: | |
sync: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Repository | |
uses: actions/checkout@v4 | |
- name: Generate token | |
id: generate_token | |
uses: tibdex/github-app-token@v2.1.0 | |
with: | |
app_id: ${{ secrets.APP_ID }} | |
private_key: ${{ secrets.APP_PRIVATE_KEY }} | |
- name: Set dependency labels | |
if: startsWith( github.event.head_commit.message, 'Bump' ) | |
run: | | |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) | |
echo "labels<<$EOF" >> "$GITHUB_ENV" | |
echo "sync" >> "$GITHUB_ENV" | |
echo "dependencies" >> "$GITHUB_ENV" | |
echo "$EOF" >> "$GITHUB_ENV" | |
- name: Set non-dependency labels | |
if: ${{ !startsWith( github.event.head_commit.message, 'Bump' ) }} | |
run: | | |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) | |
echo "labels<<$EOF" >> "$GITHUB_ENV" | |
echo "sync" >> "$GITHUB_ENV" | |
echo "maintenance" >> "$GITHUB_ENV" | |
echo "$EOF" >> "$GITHUB_ENV" | |
- name: Run GitHub File Sync | |
id: file_sync | |
uses: BetaHuhn/repo-file-sync-action@v1 | |
with: | |
GH_INSTALLATION_TOKEN: ${{ steps.generate_token.outputs.token }} | |
GIT_EMAIL: 89714990+file-sync-app[bot]@users.noreply.github.com | |
GIT_USERNAME: "file-sync-app[bot]" | |
ORIGINAL_MESSAGE: true | |
COMMIT_AS_PR_TITLE: true | |
COMMIT_EACH_FILE: false | |
ASSIGNEES: Nef10 | |
PR_LABELS: ${{ env.labels }} | |
- name: Enable automerge on PRs | |
uses: actions/github-script@v7 | |
env: | |
PRURLS: ${{ steps.file_sync.outputs.pull_request_urls }} | |
with: | |
github-token: ${{ steps.generate_token.outputs.token }} | |
script: | | |
let failed = false; | |
const { PRURLS } = process.env; | |
const prs = JSON.parse(PRURLS); | |
const idQuery = `query FindPullRequestId($owner: String!, $repoName: String!, $pullRequestNumber: Int!) { | |
repository(owner: $owner, name: $repoName) { | |
pullRequest(number: $pullRequestNumber) { | |
id | |
} | |
} | |
}`; | |
const autoMergeQuery = `mutation($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod!) { | |
enablePullRequestAutoMerge(input: { | |
pullRequestId: $pullRequestId, | |
mergeMethod: $mergeMethod | |
}) { | |
clientMutationId | |
pullRequest { | |
id | |
state | |
} | |
} | |
}`; | |
const mergeQuery = `mutation($pullRequestId: ID!, $mergeMethod: PullRequestMergeMethod!) { | |
mergePullRequest(input: { | |
pullRequestId: $pullRequestId, | |
mergeMethod: $mergeMethod | |
}) { | |
clientMutationId | |
pullRequest { | |
id | |
state | |
} | |
} | |
}`; | |
for (const pr of prs) { | |
try { | |
const urlParts = pr.replace('https://github.com/', '').split('/'); | |
const idVariables = { | |
owner: urlParts[0], | |
repoName: urlParts[1], | |
pullRequestNumber: parseInt(urlParts[3]) | |
}; | |
const idResult = await github.graphql(idQuery, idVariables); | |
const mergeVariables = { | |
pullRequestId: idResult.repository.pullRequest.id, | |
mergeMethod: 'REBASE' | |
}; | |
try { | |
const autoMergeResult = await github.graphql(autoMergeQuery, mergeVariables); | |
core.info(`Enabled automerge for ${pr}`); | |
} catch(err) { | |
const mergeResult = await github.graphql(mergeQuery, mergeVariables); | |
core.info(`Merged ${pr}`); | |
} | |
} catch(err) { | |
core.error(`Error enabling automerge / merging ${pr}: ${err}`); | |
failed = true; | |
} | |
} | |
if (failed) { | |
core.setFailed('Automerging could not be enabled on some PRs!'); | |
} |