Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make feature-pr command accessible #92

Merged
merged 5 commits into from
Oct 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions .github/workflows/feature-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: feature

on:
push:
workflow_dispatch:

jobs:
feature:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: node bin.js feature-pr --verbose --target main --source next
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}
10 changes: 10 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,16 @@
"args": ["merge", "--commit", "--target", "main", "--source", "next", "--verbose", "--refresh-clean"],
"program": "${workspaceFolder}/bin.js"
},
{
"type": "pwa-node",
"request": "launch",
"name": "featurePR",
"skipFiles": [
"<node_internals>/**"
],
"args": ["feature-pr"],
"program": "${workspaceFolder}/bin.js"
},
{
"type": "pwa-node",
"request": "launch",
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ If `GITHUB_REPOSITORY` is not provided, pr-release will exit. Even if you are r

If `GITHUB_SHA` is not specified, `pr-release` will make an API call to identify the relevant sha for the given command. If the relevant sha is not inferrable, `pr-release` will exit with a non zero code.

`GITHUB_REF` is used to identify if there is already a pull request for the current branch. This is especially useful for automatically generating feature branches on push.

### How do I do concurrent release channels?

Have a target branch for each channel e.g. `v1` and a release candidate branch like `v1-next`:
Expand Down
89 changes: 89 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,13 @@ async function preflight(){
} else {
if( !process.env.GITHUB_REPOSITORY ) throw new Error('GITHUB_REPOSITORY is required')
if( !process.env.GITHUB_TOKEN ) throw new Error('GITHUB_TOKEN is required')
if( !process.env.GITHUB_REF ) {
try {
process.env.GITHUB_REF = (await $`git branch --show-current`).stdout.trim()
} catch (e) {
throw new Error('GITHUB_REF is required')
}
}
octokit = new Octokit({
auth: process.env.GITHUB_TOKEN
,throttle: throttleOptions
Expand Down Expand Up @@ -922,6 +929,79 @@ async function changelog(options){
}
}

async function featurePR(){
// Ensure there is not a PR for the current branch
// Ensure the current branch is targeting $source
// If so, create a PR automatically
// If drafts are available, use them


let [owner,repo] = process.env.GITHUB_REPOSITORY.split('/')
// let owner = 'harth-systems', repo = 'odin';

if( [source, target].includes(process.env.GITHUB_REF) ){
return;
}

let useDraft; {

let repo2 = await octokit.rest.repos.get({
owner, repo
})
repo2 = repo2.data

let user = await octokit.rest.users.getByUsername({
username: owner
})
user = user.data

let org, plan; {
if(user.type == 'Organization'){
org = await octokit.rest.orgs.get({
org: owner
})
org = org.data
plan = org.plan
}
}

useDraft =
!repo2.private || plan.name == 'team'
}

let [existing] =
await octokit.paginate(octokit.rest.search.issuesAndPullRequests, {
q: `head:${process.env.GITHUB_REF} base:${source} is:pr is:open repo:${owner}/${repo}`
,sort: 'updated'
,order: 'desc'
,per_page: 1
,page: 1
})
useDraft;

let commitSubject; {
try {
commitSubject = (await $`git show -s --format=%s HEAD`)
.stdout.trim()
} catch (e) {
commitSubject = process.env.GITHUB_REF
}
}

if(!existing) {
await octokit.rest.pulls.create({
owner
,repo
,title: commitSubject
,head: process.env.GITHUB_REF
,base: source
,draft: useDraft
})
}

return;
}

let help=
chalk`{green pr-release}

Expand Down Expand Up @@ -953,6 +1033,13 @@ subcommands:
{magenta actions-yml}

Scaffold Github actions yml files

{magenta feature-pr}

Generate a feature-pr for the current branch that targets
the source branch if the push event is not for the target
or source branch.

`

let [subcommand] = argv._
Expand All @@ -963,13 +1050,15 @@ let subcommands = {
, 'actions-yml': actionsYML
, 'extract-changelog': extractChangelog
, changelog
, 'feature-pr': featurePR
}

let preflights = {
pr
, merge
, 'extract-changelog': extractChangelog
, changelog
, 'feature-pr': featurePR
}


Expand Down
27 changes: 27 additions & 0 deletions templates/feature-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: feature

on:
push:
workflow_dispatch:

jobs:
feature:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [16.x]

steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
cache: 'npm'
- run: npm ci
- run: npm run build --if-present
- run: node bin.js feature-branch --verbose --target $target --source $source
env:
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }}