forked from tibdex/auto-update
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 3c56434
Showing
13 changed files
with
2,230 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"use strict"; | ||
|
||
module.exports = { | ||
env: { | ||
es6: true, | ||
}, | ||
extends: [ | ||
"plugin:unicorn/recommended", | ||
"xo", | ||
"xo-typescript", | ||
"prettier", | ||
"prettier/@typescript-eslint", | ||
], | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
ecmaVersion: 2018, | ||
project: "tsconfig.json", | ||
sourceType: "module", | ||
}, | ||
plugins: ["sort-destructure-keys", "typescript-sort-keys", "unicorn"], | ||
root: true, | ||
rules: { | ||
// @actions/github uses a lot of snake_case keys. | ||
"@typescript-eslint/camelcase": "off", | ||
// TypeScript is good at type inference and already requires types where they matter: exported symbols. | ||
"@typescript-eslint/explicit-function-return-type": "off", | ||
// We use sort-keys instead. | ||
"@typescript-eslint/member-ordering": "off", | ||
// Leads to false positives. | ||
"@typescript-eslint/restrict-template-expressions": "off", | ||
"arrow-body-style": "error", | ||
// Forbid function declarations | ||
"func-style": ["error", "expression", { allowArrowFunctions: true }], | ||
// It's fine to use await in for loops instead of Promise.all to execute promises sequentially. | ||
"no-await-in-loop": "off", | ||
"no-console": "error", | ||
// TypeScript already takes care of that. See https://github.com/bradzacher/eslint-plugin-typescript/issues/110. | ||
"no-undef": "off", | ||
"object-shorthand": [ | ||
"error", | ||
"always", | ||
{ avoidExplicitReturnArrows: true }, | ||
], | ||
"sort-destructure-keys/sort-destructure-keys": [ | ||
"error", | ||
{ caseSensitive: false }, | ||
], | ||
"sort-keys": [ | ||
"error", | ||
"asc", | ||
{ caseSensitive: false, minKeys: 2, natural: true }, | ||
], | ||
"typescript-sort-keys/interface": "error", | ||
"typescript-sort-keys/string-enum": "error", | ||
}, | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
name: Auto-update | ||
on: push | ||
# If pull requests are always based on the same branches, only triggering the workflow when these branches receive new commits will minimize the workflow execution. | ||
# on: | ||
# push: | ||
# branches: | ||
# - dev | ||
# - prod | ||
|
||
jobs: | ||
autosquash: | ||
name: Autosquash | ||
runs-on: ubuntu-18.04 | ||
steps: | ||
- uses: tibdex/auto-update@v1 | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Publish | ||
on: | ||
push: | ||
branches: | ||
- main | ||
|
||
jobs: | ||
publish: | ||
name: Publish | ||
runs-on: ubuntu-18.04 | ||
steps: | ||
- uses: actions/checkout@v1 | ||
- uses: dylanvann/[email protected] | ||
with: | ||
github_token: ${{ secrets.GITHUB_TOKEN }} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
name: Test | ||
on: | ||
push: | ||
branches-ignore: | ||
- main | ||
|
||
jobs: | ||
test: | ||
name: Test | ||
runs-on: ubuntu-18.04 | ||
steps: | ||
- uses: actions/checkout@v1 | ||
- name: Install | ||
run: yarn install --frozen-lockfile | ||
- name: Build | ||
run: yarn run build | ||
- name: ESLint | ||
run: yarn run eslint | ||
- name: Prettier | ||
run: yarn run check-prettier |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/dist | ||
/node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
The MIT License (MIT) | ||
Copyright (c) 2020 Thibault Derousseaux <[email protected]> | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Auto-update is a simple [JavaScript GitHub action](https://help.github.com/en/articles/about-actions#javascript-actions) to keep pull requests [up to date with their base branch](https://developer.github.com/changes/2019-05-29-update-branch-api/). | ||
|
||
It is the missing piece to really [automatically merge pull requests](https://docs.github.com/en/free-pro-team@latest/github/collaborating-with-issues-and-pull-requests/automatically-merging-a-pull-request) when [strict status checks](https://help.github.com/en/articles/types-of-required-status-checks) are set up to protect against [semantic conflicts](https://bors.tech/essay/2017/02/02/pitch/). | ||
|
||
Add [.github/workflows/auto-update.yml](.github/workflows/auto-update.yml) to your repository to use this action. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
name: Auto-update | ||
author: Thibault Derousseaux <[email protected]> | ||
description: Automatically keep pull requests up to date with their base branch. | ||
inputs: | ||
github_token: | ||
description: Token for the GitHub API. | ||
required: true | ||
label: | ||
description: If given, pull requests without this label will be ignored. | ||
runs: | ||
using: node12 | ||
main: dist/index.js | ||
branding: | ||
icon: refresh-cw | ||
color: blue |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
{ | ||
"name": "auto-update", | ||
"version": "1.0.0", | ||
"license": "MIT", | ||
"files": [ | ||
"action.yml", | ||
"dist" | ||
], | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"build": "ncc build src/index.ts --minify --v8-cache", | ||
"check-prettier": "yarn run prettier --check", | ||
"eslint": "eslint --ignore-path .gitignore --max-warnings 0 \"./**/*.{js,ts}\"", | ||
"format-prettier": "yarn run prettier --write", | ||
"prettier": "prettier --ignore-path .gitignore \"./**/*.{js,json,md,ts,yml}\"" | ||
}, | ||
"devDependencies": { | ||
"@actions/core": "^1.2.6", | ||
"@actions/github": "^4.0.0", | ||
"@octokit/webhooks": "^7.21.0", | ||
"@types/node": "^14.14.14", | ||
"@typescript-eslint/eslint-plugin": "^4.11.0", | ||
"@typescript-eslint/parser": "^4.11.0", | ||
"@vercel/ncc": "^0.26.1", | ||
"eslint": "^7.16.0", | ||
"eslint-config-prettier": "^7.1.0", | ||
"eslint-config-xo": "^0.33.1", | ||
"eslint-config-xo-typescript": "^0.37.0", | ||
"eslint-import-resolver-typescript": "^2.3.0", | ||
"eslint-plugin-import": "^2.22.1", | ||
"eslint-plugin-sort-destructure-keys": "^1.3.5", | ||
"eslint-plugin-typescript-sort-keys": "^1.5.0", | ||
"eslint-plugin-unicorn": "^24.0.0", | ||
"prettier": "^2.2.1", | ||
"typescript": "^4.1.3" | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
"use strict"; | ||
|
||
module.exports = { | ||
trailingComma: "all", | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import { | ||
error as logError, | ||
getInput, | ||
info, | ||
group, | ||
setFailed, | ||
warning, | ||
} from "@actions/core"; | ||
import { context, getOctokit } from "@actions/github"; | ||
import type { EventPayloads } from "@octokit/webhooks"; | ||
|
||
const handleError = ( | ||
error: unknown, | ||
{ | ||
handle = logError, | ||
}: Readonly<{ handle?: (error: string | Error) => void }> = {}, | ||
) => { | ||
if (typeof error !== "string" && !(error instanceof Error)) { | ||
throw new TypeError(`Caught error of unexpected type: ${typeof error}`); | ||
} | ||
|
||
handle(error); | ||
}; | ||
|
||
const run = async () => { | ||
try { | ||
const label = getInput("label") || undefined; | ||
const token = getInput("github_token", { required: true }); | ||
const octokit = getOctokit(token); | ||
|
||
if (context.eventName !== "push") { | ||
throw new Error( | ||
`Expected to be triggered by a "push" event but received a "${context.eventName}" event`, | ||
); | ||
} | ||
|
||
const payload = context.payload as EventPayloads.WebhookPayloadPush; | ||
// See https://docs.github.com/en/free-pro-team@latest/developers/webhooks-and-events/webhook-events-and-payloads#webhook-payload-object-34. | ||
const base = payload.ref.slice("/refs/heads/".length); | ||
|
||
info(`Fetching pull requests based on "${base}"`); | ||
|
||
const pullRequests = await octokit.paginate( | ||
"GET /repos/{owner}/{repo}/pulls", | ||
{ | ||
...context.repo, | ||
base, | ||
state: "open", | ||
}, | ||
); | ||
|
||
info( | ||
`Fetched pull requests: ${JSON.stringify( | ||
pullRequests.map((pullRequest) => pullRequest.number), | ||
)}`, | ||
); | ||
|
||
await Promise.all( | ||
pullRequests | ||
.filter((pullRequest) => { | ||
if ( | ||
label !== undefined && | ||
!pullRequest.labels.some(({ name }) => name === label) | ||
) { | ||
info( | ||
`Pull request #${pullRequest.number} does not have the "${label}" label`, | ||
); | ||
return false; | ||
} | ||
|
||
if (pullRequest.draft) { | ||
info(`Pull request #${pullRequest.number} is still a draft`); | ||
return false; | ||
} | ||
|
||
if (pullRequest.base.sha === payload.after) { | ||
info(`Pull request #${pullRequest.number} is already up to date`); | ||
return false; | ||
} | ||
|
||
return true; | ||
}) | ||
.map(async (pullRequest) => { | ||
await group( | ||
`Attempting to update pull request #${pullRequest.number}`, | ||
async () => { | ||
try { | ||
await octokit.request( | ||
"PUT /repos/{owner}/{repo}/pulls/{pull_number}/update-branch", | ||
{ | ||
...context.repo, | ||
// See https://docs.github.com/en/free-pro-team@latest/rest/reference/pulls#update-a-pull-request-branch-preview-notices. | ||
mediaType: { | ||
previews: ["lydian"], | ||
}, | ||
pull_number: pullRequest.number, | ||
}, | ||
); | ||
info("Updated!"); | ||
} catch (error: unknown) { | ||
handleError(error, { handle: warning }); | ||
} | ||
}, | ||
); | ||
}), | ||
); | ||
} catch (error: unknown) { | ||
handleError(error, { handle: setFailed }); | ||
} | ||
}; | ||
|
||
void run(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"compilerOptions": { | ||
"allowSyntheticDefaultImports": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"module": "esnext", | ||
"moduleResolution": "node", | ||
"noEmitOnError": true, | ||
"strict": true, | ||
"target": "esnext", | ||
"types": ["node"] | ||
}, | ||
"include": [".*.js", "*.js", "src"] | ||
} |
Oops, something went wrong.