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

Add label to pull request #12

Merged
merged 4 commits into from
Oct 30, 2017
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
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@ node_js:
- "6"
- "8"
cache: yarn
script:
- npm run lint
- npm test
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
{
"type": "node",
"request": "launch",
"name": "Launch Program",
"name": "Run file",
"program": "${file}"
},
{
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
{
"name": "backport",
"version": "1.0.1",
"version": "1.1.0",
"main": "./src/index.js",
"bin": {
"backport": "./src/index.js"
},
"license": "MIT",
"scripts": {
"test": "jest"
"test": "jest",
"lint": "eslint ./src"
},
"repository": {
"type": "git",
Expand Down
6 changes: 4 additions & 2 deletions readme.MD
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ You need to update the config file with your Github username and a Github Access
"repositories": [
{
"name": "elastic/x-pack-kibana",
"versions": ["6.x", "6.0", "5.6", "5.5", "5.4"]
"versions": ["6.x", "6.0", "5.6", "5.5", "5.4"],
"labels": ["backport"]
},
{
"name": "elastic/kibana",
"versions": ["6.x", "6.0", "5.6", "5.5", "5.4"]
"versions": ["6.x", "6.0", "5.6", "5.5", "5.4"],
"labels": ["backport"]
}
]
}
Expand Down
16 changes: 10 additions & 6 deletions src/cli.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
const github = require('./github');
const { ensureConfigAndFoldersExists, validateConfig } = require('./configs');
const {
ensureConfigAndFoldersExists,
validateConfig,
getRepoConfig
} = require('./configs');
const {
promptRepoInfo,
promptCommit,
Expand All @@ -11,7 +15,7 @@ const {
} = require('./cliService');

function init(config, options) {
let commit, versions, reference, owner, repoName;
let commit, versions, reference, owner, repoName, repoConfig;

return ensureConfigAndFoldersExists()
.then(() => validateConfig(config))
Expand All @@ -20,14 +24,13 @@ function init(config, options) {
.then(({ owner: _owner, repoName: _repoName }) => {
owner = _owner;
repoName = _repoName;
repoConfig = getRepoConfig(owner, repoName, config.repositories);
})
.then(() =>
promptCommit(owner, repoName, options.own ? config.username : null)
)
.then(c => (commit = c))
.then(() =>
promptVersions(owner, repoName, config.repositories, options.multiple)
)
.then(() => promptVersions(repoConfig.versions, options.multiple))
.then(v => (versions = v))
.then(() => getReference(owner, repoName, commit.sha))
.then(ref => (reference = ref))
Expand All @@ -39,7 +42,8 @@ function init(config, options) {
commit,
reference,
versions,
username: config.username
username: config.username,
labels: repoConfig.labels
})
)
.catch(handleErrors);
Expand Down
92 changes: 44 additions & 48 deletions src/cliService.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,15 @@ const {
setupRepo
} = require('./git');

function sequentially(items, handler) {
return items.reduce(
(p, item) => p.then(() => handler(item)),
Promise.resolve()
);
}

const service = {};
service.doBackportVersions = ({
owner,
repoName,
commit,
reference,
versions,
username
username,
labels
}) => {
return sequentially(versions, version => {
return service
Expand All @@ -39,7 +33,8 @@ service.doBackportVersions = ({
commit,
reference,
version,
username
username,
labels
})
.then(res => console.log(`View pull request: ${res.data.html_url}\n`))
.catch(service.handleErrors);
Expand All @@ -52,13 +47,12 @@ service.doBackportVersion = ({
commit,
reference,
version,
username
username,
labels = []
}) => {
const backportBranchName = getBackportBranchName(version, reference);

console.log(
`Backporting ${service.getReferenceValue(reference)} to ${version}`
);
console.log(`Backporting ${getReferenceLong(reference)} to ${version}`);

return withSpinner(
resetAndPullMaster(owner, repoName).then(() =>
Expand All @@ -81,7 +75,14 @@ service.doBackportVersion = ({
username
);
return withSpinner(
github.createPullRequest(owner, repoName, payload),
github.createPullRequest(owner, repoName, payload).then(res => {
if (labels.length > 0) {
return github
.addLabels(owner, repoName, res.data.number, labels)
.then(() => res);
}
return res;
}),
'Creating pull request'
);
});
Expand All @@ -108,9 +109,7 @@ service.promptRepoInfo = (repositories, cwd) => {
console.log(`Repository: ${currentFullRepoName}`);
return currentFullRepoName;
}
return prompts
.listFullRepoName(fullRepoNames)
.then(({ fullRepoName }) => fullRepoName);
return prompts.listFullRepoName(fullRepoNames);
})
.then(fullRepoName => {
const [owner, repoName] = fullRepoName.split('/');
Expand Down Expand Up @@ -140,22 +139,13 @@ service.promptCommit = (owner, repoName, username) => {
.then(commits => {
spinner.stop();
return prompts.listCommits(commits);
})
.then(({ commit }) => commit);
});
};

service.promptVersions = (
owner,
repoName,
repositories,
multipleChoice = false
) => {
const versions = getVersions(owner, repoName, repositories);
if (multipleChoice) {
return prompts.checkboxVersions(versions).then(({ versions }) => versions);
}

return prompts.listVersions(versions).then(({ version }) => [version]);
service.promptVersions = (versions, multipleChoice = false) => {
return multipleChoice
? prompts.checkboxVersions(versions)
: prompts.listVersions(versions);
};

service.handleErrors = e => {
Expand Down Expand Up @@ -207,15 +197,27 @@ service.handleErrors = e => {
}
};

service.getReferenceValue = reference => {
return reference.type === 'pullRequest'
? `pull request #${reference.value}`
: `commit ${reference.value}`;
};
function sequentially(items, handler) {
return items.reduce(
(p, item) => p.then(() => handler(item)),
Promise.resolve()
);
}

function getVersions(owner, repoName, repositories) {
return repositories.find(repo => repo.name === `${owner}/${repoName}`)
.versions;
function getReferenceValue({ type, value }, { short }) {
if (type === 'pullRequest') {
return short ? `pr-${value}` : `pull request #${value}`;
}

return short ? `commit-${value}` : `commit ${value}`;
}

function getReferenceLong(reference) {
return getReferenceValue(reference, { short: false });
}

function getReferenceShort(reference) {
return getReferenceValue(reference, { short: true });
}

function isCherrypickConflict(e) {
Expand All @@ -235,7 +237,7 @@ function cherrypickAndPrompt(owner, repoName, sha) {
throw e;
}

return prompts.confirmConflictResolved().then(({ isConflictResolved }) => {
return prompts.confirmConflictResolved().then(isConflictResolved => {
if (!isConflictResolved) {
const error = new Error(constants.CHERRYPICK_CONFLICT_NOT_HANDLED);
error.details = e.message;
Expand All @@ -246,24 +248,18 @@ function cherrypickAndPrompt(owner, repoName, sha) {
}

function getBackportBranchName(version, reference) {
const refValue = getReferenceValueShort(reference);
const refValue = getReferenceShort(reference);
return `backport/${version}/${refValue}`;
}

function getReferenceValueShort(reference) {
return reference.type === 'pullRequest'
? `pr-${reference.value}`
: `commit-${reference.value}`;
}

function getCurrentFullRepoName(fullRepoNames, cwd) {
const currentDir = path.basename(cwd);
return fullRepoNames.find(name => name.endsWith(`/${currentDir}`));
}

function getPullRequestPayload(commitMessage, version, reference, username) {
const backportBranchName = getBackportBranchName(version, reference);
const refValue = service.getReferenceValue(reference);
const refValue = getReferenceLong(reference);

return {
title: `[${version}] ${commitMessage}`,
Expand Down
6 changes: 4 additions & 2 deletions src/configTemplate.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
"repositories": [
{
"name": "elastic/x-pack-kibana",
"versions": ["6.x", "6.0", "5.6", "5.5", "5.4"]
"versions": ["6.x", "6.0", "5.6", "5.5", "5.4"],
"labels": ["backport"]
},
{
"name": "elastic/kibana",
"versions": ["6.x", "6.0", "5.6", "5.5", "5.4"]
"versions": ["6.x", "6.0", "5.6", "5.5", "5.4"],
"labels": ["backport"]
}
]
}
5 changes: 5 additions & 0 deletions src/configs.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ function ensureConfigAndFoldersExists() {
});
}

function getRepoConfig(owner, repoName, repositories) {
return repositories.find(repo => repo.name === `${owner}/${repoName}`);
}

function getConfigTemplate() {
return utils.readFile(path.join(__dirname, 'configTemplate.json'), 'utf8');
}
Expand Down Expand Up @@ -63,5 +67,6 @@ function getConfig() {
module.exports = {
ensureConfigAndFoldersExists,
getConfig,
getRepoConfig,
validateConfig
};
5 changes: 0 additions & 5 deletions src/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,11 @@ function resetAndPullMaster(owner, repoName) {
);
}

function getCommit(repo, sha) {
return repo.getCommit(sha);
}

module.exports = {
resetAndPullMaster,
cherrypick,
cloneRepo,
createAndCheckoutBranch,
getCommit,
repoExists,
setupRepo,
push
Expand Down
10 changes: 10 additions & 0 deletions src/github.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ function createPullRequest(owner, repoName, payload) {
.catch(throwGithubError);
}

function addLabels(owner, repoName, pullNumber, labels) {
return axios
.post(
`https://api.github.com/repos/${owner}/${repoName}/issues/${pullNumber}/labels?access_token=${accessToken}`,
labels
)
.catch(throwGithubError);
}

function getPullRequestByCommit(owner, repoName, commitSha) {
return axios(
`https://api.github.com/search/issues?q=repo:${owner}/${repoName}+${commitSha}&access_token=${accessToken}`
Expand All @@ -69,6 +78,7 @@ function setAccessToken(_accessToken) {

module.exports = {
setAccessToken,
addLabels,
createPullRequest,
getCommits,
getPullRequestByCommit
Expand Down
Loading