-
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.
* Add basic README file (#1) * Add javascript action for pull requests (#2) --------- Co-authored-by: Kai Mayer <[email protected]>
- Loading branch information
1 parent
5eb2a0d
commit 516806e
Showing
3 changed files
with
135 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,36 @@ | ||
name: Create Pull Request | ||
|
||
on: | ||
pull_request: | ||
types: | ||
- closed | ||
branches: [ develop, master ] | ||
|
||
concurrency: ${{ github.workflow }}-${{ github.ref }} | ||
|
||
permissions: | ||
contents: write | ||
pull-requests: write | ||
|
||
env: | ||
REPO_HEAD: ${{ github.base_ref }} | ||
REPO_BASE: ${{ github.base_ref == 'develop' && 'master' || 'develop' }} | ||
|
||
jobs: | ||
create-pull-request: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout Code | ||
uses: actions/checkout@v3 | ||
|
||
- name: Create Pull Request | ||
uses: actions/github-script@v6 | ||
with: | ||
github-token: ${{ secrets.GITHUB_TOKEN }} | ||
script: | | ||
const script = require('./.github/workflows/scripts/create-pull-request.js') | ||
await script({ | ||
github, | ||
context, | ||
}) |
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,85 @@ | ||
const capitalize = (string) => { | ||
return string.charAt(0).toUpperCase() + string.slice(1) | ||
} | ||
|
||
const getQueryData = (context) => { | ||
const { REPO_HEAD: head, REPO_BASE: base } = process.env | ||
const { owner, repo } = context.repo | ||
|
||
return { owner, repo, head, base } | ||
} | ||
|
||
const getPullRequestTitle = (data) => { | ||
return `${capitalize(data.head)} -> ${capitalize(data.base)}` | ||
} | ||
|
||
const findPullRequest = async (github, context) => { | ||
const data = getQueryData(context) | ||
|
||
const response = await github.rest.pulls.list(data) | ||
|
||
if (response?.data === null || response.data.length === 0) { | ||
return null | ||
} | ||
|
||
return response.data.find((pr) => | ||
pr.title.includes(getPullRequestTitle(data)) | ||
) | ||
} | ||
|
||
const getBodyToAppend = (pullRequest) => { | ||
return `\n- #${pullRequest.number} by @${pullRequest.user.login}` | ||
} | ||
|
||
const createPullRequest = async (github, context) => { | ||
const data = getQueryData(context) | ||
|
||
const date = new Date().toLocaleDateString('us', { | ||
day: 'numeric', | ||
month: 'long', | ||
year: 'numeric', | ||
}) | ||
|
||
const title = `${getPullRequestTitle(data)} \`${date}\`` | ||
|
||
const body = | ||
'_This PR was automatically created by an action._\n' + | ||
'## Downtime \n' + | ||
'## Pull Requests' + | ||
getBodyToAppend(context.payload.pull_request) | ||
|
||
await github.rest.pulls.create({ | ||
...data, | ||
title, | ||
body, | ||
draft: true, | ||
}) | ||
} | ||
|
||
const updatePullRequest = async (github, context, pullRequest) => { | ||
const data = getQueryData(context) | ||
|
||
const body = pullRequest.body + getBodyToAppend(context.payload.pull_request) | ||
|
||
await github.rest.pulls.update({ | ||
...data, | ||
body, | ||
pull_number: pullRequest.number, | ||
}) | ||
} | ||
|
||
module.exports = async ({ github, context }) => { | ||
if (!context.payload.pull_request.merged) { | ||
return | ||
} | ||
|
||
const pullRequest = await findPullRequest(github, context) | ||
|
||
if (pullRequest === null) { | ||
await createPullRequest(github, context) | ||
|
||
return | ||
} | ||
|
||
await updatePullRequest(github, context, pullRequest) | ||
} |
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,14 @@ | ||
# Create Pull Request Action | ||
|
||
This action creates or updates a pull request in your repository. | ||
|
||
Example: | ||
Your development branch is `develop` and your release branch is `master`. | ||
|
||
On merging a pull request into `develop` the following pull request is either created or updated: | ||
`develop -> master` | ||
|
||
On merging a pull request into `master` the following pull request is either created or updated: | ||
`master -> develop` | ||
|
||
These pull request are updated with the latest pull requests merged to the target branch. |