Skip to content

Commit

Permalink
Release 1.0.0 (#3)
Browse files Browse the repository at this point in the history
* Add basic README file (#1)

* Add javascript action for pull requests (#2)

---------

Co-authored-by: Kai Mayer <[email protected]>
  • Loading branch information
github-actions[bot] and Kazuto authored Jul 13, 2023
1 parent 5eb2a0d commit 516806e
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 0 deletions.
36 changes: 36 additions & 0 deletions .github/workflows/create-pull-request.yml
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,
})
85 changes: 85 additions & 0 deletions .github/workflows/scripts/create-pull-request.js
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)
}
14 changes: 14 additions & 0 deletions README.md
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.

0 comments on commit 516806e

Please sign in to comment.