Skip to content

Commit

Permalink
fix: handle repository rename with search API
Browse files Browse the repository at this point in the history
  • Loading branch information
pvdlg committed Oct 26, 2018
1 parent 83444bf commit d51858e
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 25 deletions.
4 changes: 3 additions & 1 deletion lib/fail.js
Expand Up @@ -21,8 +21,10 @@ module.exports = async (pluginConfig, context) => {
if (failComment === false || failTitle === false) {
logger.log('Skip issue creation.');
} else {
const {name: repo, owner} = parseGithubUrl(repositoryUrl);
const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
let {name: repo, owner} = parseGithubUrl(repositoryUrl);
// In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
[owner, repo] = (await github.repos.get({repo, owner})).data.full_name.split('/');
const body = failComment ? template(failComment)({branch, errors}) : getFailComment(branch, errors);
const [srIssue] = await findSRIssues(github, failTitle, owner, repo);

Expand Down
6 changes: 5 additions & 1 deletion lib/success.js
Expand Up @@ -29,8 +29,12 @@ module.exports = async (pluginConfig, context) => {
failTitle,
releasedLabels,
} = resolveConfig(pluginConfig, context);
const {name: repo, owner} = parseGithubUrl(repositoryUrl);

const github = getClient({githubToken, githubUrl, githubApiPathPrefix, proxy});
let {name: repo, owner} = parseGithubUrl(repositoryUrl);
// In case the repo changed name, get the new `repo`/`owner` as the search API will not follow redirects
[owner, repo] = (await github.repos.get({repo, owner})).data.full_name.split('/');

const errors = [];

if (successComment === false) {
Expand Down
22 changes: 18 additions & 4 deletions test/fail.test.js
Expand Up @@ -29,6 +29,8 @@ test.afterEach.always(() => {
test.serial('Open a new issue with the list of errors', async t => {
const owner = 'test_user';
const repo = 'test_repo';
const redirectedOwner = 'test_user_2';
const redirectedRepo = 'test_repo_2';
const env = {GITHUB_TOKEN: 'github_token'};
const failTitle = 'The automated release is failing 🚨';
const pluginConfig = {failTitle};
Expand All @@ -39,13 +41,15 @@ test.serial('Open a new issue with the list of errors', async t => {
new SemanticReleaseError('Error message 3', 'ERR3', 'Error 3 details'),
];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${redirectedOwner}/${redirectedRepo}`})
.get(
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
'state:open'
)}+${escape(failTitle)}`
`/search/issues?q=${escape('in:title')}+${escape(`repo:${redirectedOwner}/${redirectedRepo}`)}+${escape(
'type:issue'
)}+${escape('state:open')}+${escape(failTitle)}`
)
.reply(200, {items: []})
.post(`/repos/${owner}/${repo}/issues`, {
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues`, {
title: failTitle,
body: /---\n\n### Error message 1\n\nError 1 details\n\n---\n\n### Error message 2\n\nError 2 details\n\n---\n\n### Error message 3\n\nError 3 details\n\n---/,
labels: ['semantic-release'],
Expand All @@ -71,6 +75,8 @@ test.serial('Open a new issue with the list of errors, retrying 4 times', async
new SemanticReleaseError('Error message 3', 'ERR3', 'Error 3 details'),
];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
'state:open'
Expand Down Expand Up @@ -118,6 +124,8 @@ test.serial('Open a new issue with the list of errors and custom title and comme
new SemanticReleaseError('Error message 3', 'ERR3', 'Error 3 details'),
];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
'state:open'
Expand Down Expand Up @@ -150,6 +158,8 @@ test.serial('Open a new issue with assignees and the list of errors', async t =>
new SemanticReleaseError('Error message 2', 'ERR2', 'Error 2 details'),
];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
'state:open'
Expand Down Expand Up @@ -183,6 +193,8 @@ test.serial('Open a new issue without labels and the list of errors', async t =>
new SemanticReleaseError('Error message 2', 'ERR2', 'Error 2 details'),
];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
'state:open'
Expand Down Expand Up @@ -220,6 +232,8 @@ test.serial('Update the first existing issue with the list of errors', async t =
{number: 3, body: `Issue 3 body\n\n${ISSUE_ID}`, title: failTitle},
];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
'state:open'
Expand Down
8 changes: 8 additions & 0 deletions test/integration.test.js
Expand Up @@ -190,6 +190,8 @@ test.serial('Comment and add labels on PR included in the releases', async t =>
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}})
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -231,6 +233,8 @@ test.serial('Open a new issue with the list of errors', async t => {
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}})
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
'state:open'
Expand Down Expand Up @@ -281,6 +285,8 @@ test.serial('Verify, release and notify success', async t => {
body: nextRelease.notes,
})
.reply(200, {upload_url: uploadUrl, html_url: releaseUrl})
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -342,6 +348,8 @@ test.serial('Verify and notify failure', async t => {
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {permissions: {push: true}})
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
'state:open'
Expand Down
72 changes: 53 additions & 19 deletions test/success.test.js
Expand Up @@ -31,6 +31,8 @@ test.serial(
async t => {
const owner = 'test_user';
const repo = 'test_repo';
const redirectedOwner = 'test_user_2';
const redirectedRepo = 'test_repo_2';
const env = {GITHUB_TOKEN: 'github_token'};
const failTitle = 'The automated release is failing 🚨';
const pluginConfig = {failTitle};
Expand All @@ -44,47 +46,49 @@ test.serial(
{hash: '456', message: 'Commit 2 message', tree: {long: 'ccc'}},
{
hash: '789',
message: `Commit 3 message Closes https://github.com/${owner}/${repo}/issues/4`,
message: `Commit 3 message Closes https://github.com/${redirectedOwner}/${redirectedRepo}/issues/4`,
tree: {long: 'ccc'},
},
];
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${redirectedOwner}/${redirectedRepo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
.join('+')}`
`/search/issues?q=${escape(`repo:${redirectedOwner}/${redirectedRepo}`)}+${escape('type:pr')}+${escape(
'is:merged'
)}+${commits.map(commit => commit.hash).join('+')}`
)
.reply(200, {items: prs})
.get(`/repos/${owner}/${repo}/pulls/1/commits`)
.get(`/repos/${redirectedOwner}/${redirectedRepo}/pulls/1/commits`)
.reply(200, [{sha: commits[0].hash}])
.get(`/repos/${owner}/${repo}/pulls/2/commits`)
.get(`/repos/${redirectedOwner}/${redirectedRepo}/pulls/2/commits`)
.reply(200, [{sha: commits[1].hash}])
.post(`/repos/${owner}/${repo}/issues/1/comments`, {body: /This PR is included/})
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/1/comments`, {body: /This PR is included/})
.reply(200, {html_url: 'https://github.com/successcomment-1'})
.post(`/repos/${owner}/${repo}/issues/1/labels`, '["released"]')
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/1/labels`, '["released"]')
.reply(200, {})
.post(`/repos/${owner}/${repo}/issues/2/comments`, {body: /This PR is included/})
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/2/comments`, {body: /This PR is included/})
.reply(200, {html_url: 'https://github.com/successcomment-2'})
.post(`/repos/${owner}/${repo}/issues/2/labels`, '["released"]')
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/2/labels`, '["released"]')
.reply(200, {})
.get(`/repos/${owner}/${repo}/issues/3`)
.get(`/repos/${redirectedOwner}/${redirectedRepo}/issues/3`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/3/comments`, {body: /This issue has been resolved/})
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/3/comments`, {body: /This issue has been resolved/})
.reply(200, {html_url: 'https://github.com/successcomment-3'})
.post(`/repos/${owner}/${repo}/issues/3/labels`, '["released"]')
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/3/labels`, '["released"]')
.reply(200, {})
.get(`/repos/${owner}/${repo}/issues/4`)
.get(`/repos/${redirectedOwner}/${redirectedRepo}/issues/4`)
.reply(200, {state: 'closed'})
.post(`/repos/${owner}/${repo}/issues/4/comments`, {body: /This issue has been resolved/})
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/4/comments`, {body: /This issue has been resolved/})
.reply(200, {html_url: 'https://github.com/successcomment-4'})
.post(`/repos/${owner}/${repo}/issues/4/labels`, '["released"]')
.post(`/repos/${redirectedOwner}/${redirectedRepo}/issues/4/labels`, '["released"]')
.reply(200, {})
.get(
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
'state:open'
)}+${escape(failTitle)}`
`/search/issues?q=${escape('in:title')}+${escape(`repo:${redirectedOwner}/${redirectedRepo}`)}+${escape(
'type:issue'
)}+${escape('state:open')}+${escape(failTitle)}`
)
.reply(200, {items: []});

Expand Down Expand Up @@ -123,6 +127,8 @@ test.serial(
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://custom-url.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -201,6 +207,8 @@ test.serial('Make multiple search queries if necessary', async t => {
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${
commits[0].hash
Expand Down Expand Up @@ -287,6 +295,8 @@ test.serial(
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -332,6 +342,8 @@ test.serial('Do not add comment and labels to open issues/PRs', async t => {
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -372,6 +384,8 @@ test.serial('Do not add comment and labels if no PR is associated with release c
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -405,6 +419,8 @@ test.serial('Do not add comment and labels to PR/issues from other repo', async
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -446,6 +462,8 @@ test.serial('Ignore missing issues/PRs', async t => {
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -502,6 +520,8 @@ test.serial('Add custom comment', async t => {
const nextRelease = {version: '2.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -543,6 +563,8 @@ test.serial('Add custom label', async t => {
const nextRelease = {version: '2.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -582,6 +604,8 @@ test.serial('Comment on issue/PR without ading a label', async t => {
const nextRelease = {version: '2.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -622,6 +646,8 @@ test.serial('Ignore errors when adding comments and closing issues', async t =>
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -677,6 +703,8 @@ test.serial('Close open issues when a release is successful', async t => {
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down Expand Up @@ -712,6 +740,8 @@ test.serial('Skip commention on issues/PR if "successComment" is "false"', async
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape('in:title')}+${escape(`repo:${owner}/${repo}`)}+${escape('type:issue')}+${escape(
'state:open'
Expand All @@ -735,6 +765,8 @@ test.serial('Skip closing issues if "failComment" is "false"', async t => {
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand All @@ -757,6 +789,8 @@ test.serial('Skip closing issues if "failTitle" is "false"', async t => {
const nextRelease = {version: '1.0.0'};
const releases = [{name: 'GitHub release', url: 'https://github.com/release'}];
const github = authenticate(env)
.get(`/repos/${owner}/${repo}`)
.reply(200, {full_name: `${owner}/${repo}`})
.get(
`/search/issues?q=${escape(`repo:${owner}/${repo}`)}+${escape('type:pr')}+${escape('is:merged')}+${commits
.map(commit => commit.hash)
Expand Down

0 comments on commit d51858e

Please sign in to comment.