Skip to content

Commit 8f8b2e9

Browse files
author
Ian Walter
committed
Refactoring
1 parent 211e6e5 commit 8f8b2e9

File tree

8 files changed

+118
-166
lines changed

8 files changed

+118
-166
lines changed
Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1-
name: Commit
1+
name: PR
22
on:
33
workflow_dispatch:
44
inputs:
5-
branch:
6-
description: The git branch you want to commit to.
5+
title:
6+
description: The title of the pull request you want to create.
7+
required: true
8+
head:
9+
description: The branch for the pull request you want to create.
10+
required: false
11+
base:
12+
description: The branch you want to point the pull request to.
713
required: false
814
files:
915
description: The changed files you want included in the commit.
@@ -18,20 +24,19 @@ on:
1824
description: The GitHub token to use to push the commit to GitHub.
1925
required: false
2026
jobs:
21-
commit:
27+
run:
28+
name: Run
2229
runs-on: ubuntu-latest
2330
timeout-minutes: 5
2431
env:
25-
DEBUG: true
32+
DEBUG: pull-request-action
2633
steps:
2734
- name: Checkout
2835
uses: actions/checkout@v2
2936
with:
30-
fetch-depth: 0
31-
persist-credentials: false
37+
token: ${{ secrets.GH_PAT }}
3238
- name: Make Change
3339
run: echo "Racks 2 Skinny" > commits/$(date +%s).txt
34-
- name: Commit Changes
40+
- name: Create pull request
3541
uses: ./
36-
with:
37-
token: ${{ secrets.GH_PAT }}
42+

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# commit-action
1+
# pull-request-action
22

33
## 1.0.2
44

Dockerfile

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
FROM node:15-buster
22

3-
RUN mkdir -p /opt/commit-action
3+
RUN mkdir -p /opt/pull-request-action
44

5-
COPY yarn.lock /opt/commit-action
6-
COPY package.json /opt/commit-action
7-
RUN cd /opt/commit-action && yarn && cd $HOME
5+
COPY yarn.lock /opt/pull-request-action
6+
COPY package.json /opt/pull-request-action
7+
RUN cd /opt/pull-request-action && yarn && cd $HOME
88

9-
COPY index.js /opt/commit-action
9+
COPY index.js /opt/pull-request-action
1010

11-
CMD ["node", "/opt/commit-action"]
11+
CMD ["node", "/opt/pull-request-action"]

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
# Commit Action
2-
> Automate git commits with GitHub Actions
1+
# Pull Request Action
2+
> Automate pull requests with GitHub Actions
33
44
## About
55

@@ -23,8 +23,11 @@ jobs:
2323
fetch-depth: 0
2424
- name: Make Changes
2525
uses: some-action
26-
- name: Commit Changes
27-
uses: generates/commit-action@v1.0.0
26+
- name: Create Pull Requst
27+
uses: generates/pull-request-action@v0.0.1
28+
with:
29+
title: These are days of miracle and wonder
30+
message: A loose affiliation of millionaires and billionaires
2831
```
2932
3033
## License
@@ -35,4 +38,4 @@ Hippocratic License - See [LICENSE][licenseUrl]
3538
3639
Created by [Ian Walter](https://ianwalter.dev)
3740
38-
[licenseUrl]: https://github.com/generates/commit-action/blob/main/LICENSE
41+
[licenseUrl]: https://github.com/generates/pull-request-action/blob/main/LICENSE

action.yml

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
name: Commit Action
2-
description: Automate git commits with GitHub Actions
1+
name: Pull Request Action
2+
description: Automate pull requests with GitHub Actions
33
author: Ian Walter
44
branding:
55
icon: book
66
color: blue
77
inputs:
8-
branch:
9-
description: The git branch you want to commit to.
8+
title:
9+
description: The title of the pull request you want to create.
10+
required: true
11+
head:
12+
description: The branch for the pull request you want to create.
13+
required: false
14+
base:
15+
description: The branch you want to point the pull request to.
1016
required: false
1117
files:
1218
description: The changed files you want included in the commit.

index.js

Lines changed: 52 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,72 @@
11
#!/usr/bin/env node
22

33
const core = require('@actions/core')
4-
const github = require('@actions/github')
54
const { createLogger } = require('@generates/logger')
6-
const dot = require('@ianwalter/dot')
75
const execa = require('execa')
6+
const slugify = require('slugify')
7+
const octokit = require('@octokit/request')
88

9-
const logger = createLogger()
9+
const logger = createLogger({ level: 'info', namespace: 'pull-request-action' })
1010

1111
async function run () {
12-
// Commit to the branch specified in inputs.
13-
let branch = process.env.INPUT_BRANCH
12+
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
13+
const headers = { authorization: `token ${process.env.GITHUB_TOKEN}` }
14+
const request = octokit.request.defaults({ headers })
1415

15-
// If no branch was specified, try to set it to the PR branch.
16-
if (!branch) branch = dot.get(github.context, 'payload.pull_request.head.ref')
16+
// Determine the pull request title.
17+
const title = process.env.INPUT_TITLE
18+
if (!title) throw new Error('No pull request title specified')
1719

18-
// If this is not a PR, use the current branch.
19-
if (!branch) branch = process.env.GITHUB_REF
20+
// Determine the head branch.
21+
let head = process.env.INPUT_HEAD
22+
if (!head) head = slugify(title)
2023

21-
// Output the branch that will be used.
22-
logger.info('Using branch:', branch)
24+
// Determine the base branch.
25+
let base = process.env.INPUT_BASE
26+
if (!base) {
27+
// If the base branch isn't specified, use the repo's default branch.
28+
const { data } = request('GET /repos/{owner}/{repo}', { owner, repo })
29+
base = data.default_branch
30+
}
31+
32+
let pr
33+
try {
34+
// Try fetching the head branch from origin.
35+
await execa('git', ['fetch', 'origin', head])
2336

24-
// TODO: check if branch exists and create it if necessary.
37+
// Search for an existing pull request if the head branch exists.
38+
const q = `head:${head} type:pr is:open repo:${owner}/${repo}`
39+
const { data } = await request('GET /search/issues', { q })
40+
pr = data.items?.length && data.items[0]
2541

26-
// Check if there are local changes.
27-
const { stdout: hasChanges } = await execa('git', ['status', '--porcelain'])
28-
if (hasChanges) {
29-
// Add specified files or all changes.
30-
const files = process.env.INPUT_FILES
31-
? process.env.INPUT_FILES.split('\n')
32-
: '.'
33-
await execa('git', ['add', files])
42+
// Check out the head branch.
43+
await execa('git', ['checkout', head])
44+
} catch (err) {
45+
// If an error was thrown, the branch doesn't exist, so create it.
46+
logger.debug('Failed to fetch branch from origin', err)
47+
await execa('git', ['checkout', '-b', head])
48+
}
3449

35-
// Check if there are staged changes.
36-
const args = ['diff', '--staged', '--name-only']
37-
const { stdout: hasStaged } = await execa('git', args)
38-
if (hasStaged) {
39-
// Configure the git user.
40-
const author = 'github-actions[bot]'
41-
await execa('git', ['config', '--global', 'user.name', author])
42-
const email = 'github-actions[bot]@users.noreply.github.com'
43-
await execa('git', ['config', '--global', 'user.email', email])
50+
// Commit changed files if necessary.
51+
const commitParams = ['--yes', '@generates/commit-action']
52+
const { INPUT_COMMIT, INPUT_MESSAGE, INPUT_FILES } = process.env
53+
if (INPUT_COMMIT || INPUT_MESSAGE || INPUT_FILES) {
54+
await execa('npx', commitParams)
55+
}
4456

45-
// Commit the changes.
46-
const message = process.env.INPUT_MESSAGE || 'Automated commit'
47-
await execa('git', ['commit', '-m', message])
57+
// Push the branch to origin.
58+
await execa('git', ['push', 'origin', head])
4859

49-
// Push the changes back to the branch.
50-
const actor = process.env.INPUT_ACTOR || process.env.GITHUB_ACTOR
51-
const token = process.env.INPUT_TOKEN
52-
const repo = process.env.GITHUB_REPOSITORY
53-
const origin = token
54-
? `https://${actor}:${token}@github.com/${repo}.git`
55-
: 'origin'
56-
await execa('git', ['push', origin, `HEAD:${branch}`])
57-
} else {
58-
logger.info('No staged files', { hasStaged })
59-
}
60+
const body = process.env.INPUT_BODY
61+
const payload = { owner, repo, title, body, head, base }
62+
if (pr) {
63+
// Update the existing pull request and make sure it's open.
64+
payload.pull_number = pr.pull_number
65+
payload.state = 'open'
66+
await request('PUT /repos/{owner}/{repo}/pulls/{pull_number}', payload)
6067
} else {
61-
logger.info('No local changes', { hasChanges })
68+
// Create the pull request if it doesn't exist.
69+
await request('POST /repos/{owner}/{repo}/pulls', payload)
6270
}
6371
}
6472

package.json

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
2-
"name": "@generates/commit-action",
3-
"version": "1.0.2",
4-
"description": "Automate git commits with GitHub Actions",
2+
"name": "@generates/pull-request-action",
3+
"version": "0.0.0",
4+
"description": "Automate pull requests with GitHub Actions",
55
"main": "index.js",
66
"bin": {
7-
"commit": "index.js"
7+
"pr": "index.js"
88
},
99
"scripts": {
1010
"lint": "eslint ."
1111
},
1212
"repository": {
1313
"type": "git",
14-
"url": "git+https://github.com/generates/commit-action.git"
14+
"url": "git+https://github.com/generates/pull-request-action.git"
1515
},
1616
"license": "SEE LICENSE IN LICENSE",
1717
"bugs": {
18-
"url": "https://github.com/generates/commit-action/issues"
18+
"url": "https://github.com/generates/pull-request-action/issues"
1919
},
2020
"funding": {
2121
"type": "github",
@@ -24,13 +24,13 @@
2424
"publishConfig": {
2525
"access": "public"
2626
},
27-
"homepage": "https://github.com/generates/commit-action#readme",
27+
"homepage": "https://github.com/generates/pull-request-action#readme",
2828
"dependencies": {
2929
"@actions/core": "^1.2.6",
30-
"@actions/github": "^4.0.0",
3130
"@generates/logger": "0.1.2",
32-
"@ianwalter/dot": "^1.0.5",
33-
"execa": "^4.0.3"
31+
"@octokit/request": "^5.4.14",
32+
"execa": "^4.0.3",
33+
"slugify": "^1.4.7"
3434
},
3535
"devDependencies": {
3636
"@changesets/cli": "^2.14.1",

0 commit comments

Comments
 (0)