From e3a800ddd17d8faa6b1ae970965bf9274a5cedff Mon Sep 17 00:00:00 2001 From: James Forbes Date: Sat, 2 Oct 2021 23:22:24 +1000 Subject: [PATCH 1/5] Scaffold feature-pr command --- .vscode/launch.json | 10 ++++++ README.md | 2 ++ lib/index.js | 77 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 89 insertions(+) diff --git a/.vscode/launch.json b/.vscode/launch.json index ac67872..02b1b24 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -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": [ + "/**" + ], + "args": ["featurePR"], + "program": "${workspaceFolder}/bin.js" + }, { "type": "pwa-node", "request": "launch", diff --git a/README.md b/README.md index ab3e8cf..c341100 100644 --- a/README.md +++ b/README.md @@ -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`: diff --git a/lib/index.js b/lib/index.js index 6daebbd..729ae6b 100644 --- a/lib/index.js +++ b/lib/index.js @@ -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 @@ -922,6 +929,74 @@ async function changelog(options){ } } +async function featurePR(options){ + // 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'; + + 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 { + // commitSubject = process.env.GITHUB_REF + // } + // } + + if(!existing) { + await octokit.rest.pulls.create({ + owner + ,repo + ,head: process.env.GITHUB_REF + ,base: source + ,draft: useDraft + }) + } + + return; +} + let help= chalk`{green pr-release} @@ -963,6 +1038,7 @@ let subcommands = { , 'actions-yml': actionsYML , 'extract-changelog': extractChangelog , changelog + , featurePR } let preflights = { @@ -970,6 +1046,7 @@ let preflights = { , merge , 'extract-changelog': extractChangelog , changelog + , featurePR } From a0d834e9a05c81e62fd6b73797a12a4091e7645a Mon Sep 17 00:00:00 2001 From: James Forbes Date: Sat, 2 Oct 2021 23:25:42 +1000 Subject: [PATCH 2/5] Add commit subject as title --- lib/index.js | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/index.js b/lib/index.js index 729ae6b..604fce7 100644 --- a/lib/index.js +++ b/lib/index.js @@ -975,19 +975,20 @@ async function featurePR(options){ }) useDraft; - // let commitSubject; { - // try { - // commitSubject = await $`git show -s --format=%s HEAD` - // .stdout.trim() - // } catch { - // commitSubject = process.env.GITHUB_REF - // } - // } + 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 From 93c454ae0fae3ce4cc2fdd7d67364c1e85d4c17d Mon Sep 17 00:00:00 2001 From: James Forbes Date: Sat, 2 Oct 2021 23:31:08 +1000 Subject: [PATCH 3/5] Skip push events for source or target --- lib/index.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/lib/index.js b/lib/index.js index 604fce7..822b3fe 100644 --- a/lib/index.js +++ b/lib/index.js @@ -929,7 +929,7 @@ async function changelog(options){ } } -async function featurePR(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 @@ -939,6 +939,10 @@ async function featurePR(options){ 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({ @@ -977,7 +981,7 @@ async function featurePR(options){ let commitSubject; { try { - commitSubject = await $`git show -s --format=%s HEAD` + commitSubject = (await $`git show -s --format=%s HEAD`) .stdout.trim() } catch (e) { commitSubject = process.env.GITHUB_REF @@ -1029,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._ From 6a5e906c8644ce387fa41fdfb4f0b6cac487c10d Mon Sep 17 00:00:00 2001 From: James Forbes Date: Sat, 2 Oct 2021 23:33:44 +1000 Subject: [PATCH 4/5] Feature PR yml --- .github/workflows/feature-branch.yml | 27 +++++++++++++++++++++++++++ templates/feature-branch.yml | 27 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) create mode 100644 .github/workflows/feature-branch.yml create mode 100644 templates/feature-branch.yml diff --git a/.github/workflows/feature-branch.yml b/.github/workflows/feature-branch.yml new file mode 100644 index 0000000..69c64c4 --- /dev/null +++ b/.github/workflows/feature-branch.yml @@ -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 main --source next + env: + GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} \ No newline at end of file diff --git a/templates/feature-branch.yml b/templates/feature-branch.yml new file mode 100644 index 0000000..b99e069 --- /dev/null +++ b/templates/feature-branch.yml @@ -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 }} \ No newline at end of file From efd7f8729391d7347129c70a081c2605703029dc Mon Sep 17 00:00:00 2001 From: James Forbes Date: Sat, 2 Oct 2021 23:36:03 +1000 Subject: [PATCH 5/5] Make feature-pr command accessible --- .github/workflows/{feature-branch.yml => feature-pr.yml} | 2 +- .vscode/launch.json | 2 +- lib/index.js | 4 ++-- templates/{feature-branch.yml => feature-pr.yml} | 0 4 files changed, 4 insertions(+), 4 deletions(-) rename .github/workflows/{feature-branch.yml => feature-pr.yml} (86%) rename templates/{feature-branch.yml => feature-pr.yml} (100%) diff --git a/.github/workflows/feature-branch.yml b/.github/workflows/feature-pr.yml similarity index 86% rename from .github/workflows/feature-branch.yml rename to .github/workflows/feature-pr.yml index 69c64c4..19fc951 100644 --- a/.github/workflows/feature-branch.yml +++ b/.github/workflows/feature-pr.yml @@ -22,6 +22,6 @@ jobs: cache: 'npm' - run: npm ci - run: npm run build --if-present - - run: node bin.js feature-branch --verbose --target main --source next + - run: node bin.js feature-pr --verbose --target main --source next env: GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 02b1b24..3a1bc8c 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -61,7 +61,7 @@ "skipFiles": [ "/**" ], - "args": ["featurePR"], + "args": ["feature-pr"], "program": "${workspaceFolder}/bin.js" }, { diff --git a/lib/index.js b/lib/index.js index 822b3fe..eddde65 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1050,7 +1050,7 @@ let subcommands = { , 'actions-yml': actionsYML , 'extract-changelog': extractChangelog , changelog - , featurePR + , 'feature-pr': featurePR } let preflights = { @@ -1058,7 +1058,7 @@ let preflights = { , merge , 'extract-changelog': extractChangelog , changelog - , featurePR + , 'feature-pr': featurePR } diff --git a/templates/feature-branch.yml b/templates/feature-pr.yml similarity index 100% rename from templates/feature-branch.yml rename to templates/feature-pr.yml