-
Notifications
You must be signed in to change notification settings - Fork 1
PR landing tool #6
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
Open
JuliaGerasymenko
wants to merge
15
commits into
master
Choose a base branch
from
pr-landing-tool
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
1b17f61
feat: add PR-URL to the commit message
JuliaGerasymenko 5488751
fix: add changes
JuliaGerasymenko c298dfb
fix: support fixup! and squash! commits
JuliaGerasymenko 40d0156
fix: support squash-all, cherry-pick, rebase
JuliaGerasymenko f2ab289
fix: apply changes
JuliaGerasymenko a63c888
fix: add additional functionality
JuliaGerasymenko a416cea
fix: fixup
JuliaGerasymenko 17de50d
fix: fixup
JuliaGerasymenko 4ea1e80
fix: fix
JuliaGerasymenko 4a2c03f
fix: delete duplicate option functions
JuliaGerasymenko 4e01d77
fix: using clipboardy
JuliaGerasymenko a706a06
fix: fix
JuliaGerasymenko d2f865e
fix: fixup
JuliaGerasymenko d2a7412
fix: clipboardy option was added
JuliaGerasymenko 062d83a
fix: fix
JuliaGerasymenko File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,239 @@ | ||
| #!/usr/bin/env node | ||
|
|
||
| 'use strict'; | ||
|
|
||
| const childProcess = require('child_process'); | ||
| const https = require('https'); | ||
| const path = require('path'); | ||
| const util = require('util'); | ||
| const clipboardy = require('clipboardy'); | ||
|
|
||
| const args = process.argv.slice(2); | ||
| const exec = util.promisify(childProcess.exec); | ||
| const targetBranch = args[0] || 'master'; | ||
| const toolName = path.basename(process.argv[1]); | ||
| const toolVersion = require('../package.json').version; | ||
|
|
||
| const help = `\ | ||
| This is a Pull Request landing tool that will automatically pick up commits from | ||
| the branch adds metadata to them and merge them into the specified branch. | ||
lundibundi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| Usage: ${toolName} <target-branch> [OPTION] | ||
| ${toolName} --help | ||
| ${toolName} --version | ||
|
|
||
| Options: | ||
| --remote-name get name from remote repo | ||
| --rebase start interactive rebase of the source branch | ||
| --autosquash move commits that begin with squash!/fixup! during rebase | ||
| --cherry-pick apply commits from source branch onto <target-branch> | ||
| --clipboardy print 'Landed in (...commitsHash)' | ||
| --help print this help message and exit | ||
| --version print version and exit | ||
| `; | ||
|
|
||
| const runGit = async function(options) { | ||
| return new Promise((resolve, reject) => { | ||
lundibundi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| const git = childProcess.spawn('git', options, { | ||
| stdio: 'inherit', | ||
| windowsHide: true, | ||
| }); | ||
| git.on('close', code => { | ||
| if (code !== 0) { | ||
| reject(new SyntaxError('git exit with exit code !== 0')); | ||
| } | ||
| resolve(); | ||
| }); | ||
| }); | ||
| }; | ||
|
|
||
| async function commitsHash(count) { | ||
| let git; | ||
| try { | ||
| git = await exec(`git log -${count} --pretty=format:%h`); | ||
| } catch (error) { | ||
| console.error(error); | ||
| process.exit(1); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: I'd prefer to handle these errors (here and in differCommits, currentBranch, getRepoName) at the calling site to avoid duplicate code (i.e. |
||
| } | ||
| return git.stdout; | ||
| } | ||
|
|
||
| async function differCommits() { | ||
| let git; | ||
| try { | ||
| git = await exec(`git rev-list --count ${targetBranch}..HEAD`); | ||
| } catch (error) { | ||
| console.error(error); | ||
| process.exit(1); | ||
| } | ||
| return parseInt(git.stdout.trim()); | ||
| } | ||
|
|
||
| async function currentBranch() { | ||
| let git; | ||
| try { | ||
| git = await exec('git rev-parse --abbrev-ref HEAD'); | ||
| } catch (error) { | ||
| console.error(error); | ||
| process.exit(1); | ||
| } | ||
| return git.stdout.trim(); | ||
| } | ||
|
|
||
| async function httpsGet(sourceBranch, targetBranch) { | ||
| const options = { | ||
| path: `/search/issues?q=repo:${await getRepoName()}+is:pr+is:open+head:${sourceBranch}+base:${targetBranch}`, | ||
| headers: { | ||
| 'user-agent': 'metarhia-api-landing-tool', | ||
| }, | ||
| host: 'api.github.com', | ||
| }; | ||
|
|
||
| return new Promise((resolve, reject) => { | ||
| https | ||
| .get(options, res => { | ||
| let data = ''; | ||
| res.setEncoding('utf8'); | ||
|
|
||
| res.on('data', chunk => { | ||
| data += chunk; | ||
| }); | ||
|
|
||
| res.on('end', () => { | ||
| resolve(JSON.parse(data).items[0].html_url); | ||
| }); | ||
| }) | ||
| .on('error', err => { | ||
| reject(err.message); | ||
| }); | ||
| }); | ||
| } | ||
|
|
||
SemenchenkoVitaliy marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| async function getRepoName() { | ||
| let git, | ||
| name = 'origin'; | ||
| for (const value of args) { | ||
| if (value.startsWith('--remote-name=')) { | ||
| name = value.split('=')[1]; | ||
| } | ||
| } | ||
|
|
||
| try { | ||
| git = await exec(`git remote get-url ${name}`); | ||
| } catch (error) { | ||
| console.error(error); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const gitConfig = git.stdout.trim(); | ||
|
|
||
| return gitConfig.slice( | ||
| gitConfig.indexOf(':') + 1, | ||
| gitConfig.lastIndexOf('.git') | ||
| ); | ||
| } | ||
|
|
||
| async function gitLog() { | ||
| let git; | ||
| try { | ||
| git = await exec('git log -1 --pretty=format:%B'); | ||
| } catch (error) { | ||
| console.error(error); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| return git.stdout.trim(); | ||
| } | ||
|
|
||
| if (args.includes('--help')) { | ||
| console.log(help); | ||
| process.exit(0); | ||
| } else if (args.includes('--version')) { | ||
| console.log(toolVersion); | ||
| process.exit(0); | ||
| } | ||
|
|
||
| const onTargetBranch = () => | ||
| childProcess.exec(`git checkout ${targetBranch}`, err => { | ||
| if (err) { | ||
| console.error(err); | ||
| process.exit(1); | ||
| } | ||
| }); | ||
|
|
||
| async function lastCommitHash() { | ||
| let git; | ||
| try { | ||
| git = await exec('git log -1 --pretty=format:%h'); | ||
| } catch (error) { | ||
| console.error(error); | ||
| process.exit(1); | ||
| } | ||
| return git.stdout; | ||
| } | ||
|
|
||
| (async () => { | ||
| const sourceBranch = await currentBranch(); | ||
|
|
||
| if (args.includes('--rebase')) { | ||
| runGit(['rebase', `origin/${targetBranch}`]); | ||
| } | ||
|
|
||
| if (args.includes('--autosquash')) { | ||
| runGit(['rebase', '-i', '--autosquash', `HEAD~${await differCommits()}`]); | ||
| } | ||
|
|
||
| const commitsCount = await differCommits(); | ||
| const differCommitsHash = (await commitsHash(commitsCount)).split('\n'); | ||
| const commitsHashArr = new Array(); | ||
|
|
||
| let prUrl, | ||
| extendedCommit, | ||
| urlRequest = false; | ||
|
|
||
| for (const hash of differCommitsHash) { | ||
| childProcess.execSync(`git checkout ${hash}`); | ||
|
|
||
| const gitLogBody = await gitLog(); | ||
|
|
||
| if (!gitLogBody.includes('PR-URL:')) { | ||
| if (!urlRequest) { | ||
lundibundi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| prUrl = await httpsGet(sourceBranch, targetBranch); | ||
| urlRequest = true; | ||
| } | ||
|
|
||
| extendedCommit = `${gitLogBody}\n\nPR-URL: ${prUrl}`; | ||
|
|
||
| childProcess.execSync( | ||
| `git commit --amend --allow-empty --message='${extendedCommit}'` | ||
lundibundi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ); | ||
|
|
||
| const modifiedCommitsHash = await lastCommitHash(); | ||
|
|
||
| childProcess.execSync(`git checkout ${sourceBranch}`); | ||
|
|
||
| childProcess.execSync(`git replace -f ${hash} ${modifiedCommitsHash}`); | ||
| commitsHashArr.push(modifiedCommitsHash); | ||
| } | ||
| commitsHashArr.push(hash); | ||
| childProcess.execSync(`git checkout ${sourceBranch}`); | ||
| } | ||
|
|
||
| if (args.includes('--cherry-pick')) { | ||
| onTargetBranch(); | ||
| childProcess.exec( | ||
| `git cherry-pick ${sourceBranch}..${targetBranch}`, | ||
| err => { | ||
| if (err) { | ||
| console.error(err); | ||
| process.exit(1); | ||
| } | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| if (args.includes('--clipboardy')) { | ||
| clipboardy.write(`Landed in ${commitsHashArr.join(', ')}`); | ||
| clipboardy.read().then(console.log); | ||
| } | ||
| })(); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.