-
Notifications
You must be signed in to change notification settings - Fork 17
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
feat: support adding labels to PRs #173
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
526dde6
feat: support adding labels to PRs
noahdietz 08632bb
add new line at eof
noahdietz fc39769
fix lint errors
noahdietz d477b0b
add fix changes
noahdietz eaa70c2
another new line at eof
noahdietz 2bdf70b
fix license year
noahdietz 89c7c08
fix: no-op addLabels with empty labels
noahdietz 18fe79e
remove trailing s in comment
noahdietz c60d5f1
fix lint error
noahdietz 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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,52 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// https://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {BranchDomain, RepoDomain} from '../types'; | ||
import {Octokit} from '@octokit/rest'; | ||
import {logger} from '../logger'; | ||
|
||
/** | ||
* Create a GitHub PR on the upstream organization's repo | ||
* Throws an error if the GitHub API fails | ||
* @param {Octokit} octokit The authenticated octokit instance | ||
* @param {RepoDomain} upstream The upstream repository | ||
* @param {BranchDomain} origin The remote origin information that contains the origin branch | ||
* @param {number} issue_number The issue number to add labels to. Can also be a PR number | ||
* @param {string[]} labels The list of labels to apply to the issue/pull request. Default is []. the funciton will no-op. | ||
* @returns {Promise<string[]>} The list of resulting labels after the addition of the given labels | ||
*/ | ||
async function addLabels( | ||
octokit: Octokit, | ||
upstream: RepoDomain, | ||
origin: BranchDomain, | ||
issue_number: number, | ||
labels?: string[] | ||
): Promise<string[]> { | ||
if (!labels || labels.length === 0) { | ||
return []; | ||
} | ||
|
||
const labelsResponseData = ( | ||
await octokit.issues.addLabels({ | ||
owner: upstream.owner, | ||
repo: origin.repo, | ||
issue_number: issue_number, | ||
labels: labels, | ||
}) | ||
).data; | ||
logger.info(`Successfully added labels ${labels} to issue: ${issue_number}`); | ||
return labelsResponseData.map(l => l.name); | ||
} | ||
|
||
export {addLabels}; |
This file contains 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
This file contains 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
This file contains 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
This file contains 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,20 @@ | ||
[ | ||
{ | ||
"id": 208045946, | ||
"node_id": "MDU6TGFiZWwyMDgwNDU5NDY=", | ||
"url": "https://api.github.com/repos/octocat/Hello-World/labels/bug", | ||
"name": "bug", | ||
"description": "Something isn't working", | ||
"color": "f29513", | ||
"default": true | ||
}, | ||
{ | ||
"id": 208045947, | ||
"node_id": "MDU6TGFiZWwyMDgwNDU5NDc=", | ||
"url": "https://api.github.com/repos/octocat/Hello-World/labels/enhancement", | ||
"name": "enhancement", | ||
"description": "New feature or request", | ||
"color": "a2eeef", | ||
"default": false | ||
} | ||
] |
This file contains 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,110 @@ | ||
// Copyright 2021 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
import {expect} from 'chai'; | ||
import {describe, it, before, afterEach} from 'mocha'; | ||
import {octokit, setup} from './util'; | ||
import * as sinon from 'sinon'; | ||
import {addLabels} from '../src/github-handler/issue-handler'; | ||
|
||
before(() => { | ||
setup(); | ||
}); | ||
|
||
describe('Adding labels', async () => { | ||
const sandbox = sinon.createSandbox(); | ||
const upstream = {owner: 'upstream-owner', repo: 'upstream-repo'}; | ||
const origin = { | ||
owner: 'origin-owner', | ||
repo: 'origin-repo', | ||
branch: 'issues-test-branch', | ||
}; | ||
const issue_number = 1; | ||
const labels = ['enhancement']; | ||
afterEach(() => { | ||
sandbox.restore(); | ||
}); | ||
|
||
it('Invokes octokit issues add labels on an existing pull request', async () => { | ||
// setup | ||
const responseAddLabelsData = await import( | ||
'./fixtures/add-labels-response.json' | ||
); | ||
const addLabelsResponse = { | ||
headers: {}, | ||
status: 200, | ||
url: 'http://fake-url.com', | ||
data: responseAddLabelsData, | ||
}; | ||
const stub = sandbox | ||
.stub(octokit.issues, 'addLabels') | ||
.resolves(addLabelsResponse); | ||
// tests | ||
const resultingLabels = await addLabels( | ||
octokit, | ||
upstream, | ||
origin, | ||
issue_number, | ||
labels | ||
); | ||
sandbox.assert.calledOnceWithExactly(stub, { | ||
owner: upstream.owner, | ||
repo: origin.repo, | ||
issue_number: issue_number, | ||
labels: labels, | ||
}); | ||
expect(resultingLabels).to.deep.equal(['bug', 'enhancement']); | ||
}); | ||
|
||
it('No-op undefined labels', async () => { | ||
// setup | ||
const stub = sandbox.stub(octokit.issues, 'addLabels').resolves(); | ||
// tests | ||
const resultingLabels = await addLabels( | ||
octokit, | ||
upstream, | ||
origin, | ||
issue_number | ||
); | ||
sandbox.assert.neverCalledWith(stub, sinon.match.any); | ||
expect(resultingLabels).to.deep.equal([]); | ||
}); | ||
|
||
it('No-op with empty labels', async () => { | ||
Comment on lines
+70
to
+84
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. @bcoe added no-op tests. |
||
// setup | ||
const stub = sandbox.stub(octokit.issues, 'addLabels').resolves(); | ||
// tests | ||
const resultingLabels = await addLabels( | ||
octokit, | ||
upstream, | ||
origin, | ||
issue_number, | ||
[] | ||
); | ||
sandbox.assert.neverCalledWith(stub, sinon.match.any); | ||
expect(resultingLabels).to.deep.equal([]); | ||
}); | ||
|
||
it('Passes up the error message with a throw when octokit issues add labels fails', async () => { | ||
// setup | ||
const errorMsg = 'Error message'; | ||
sandbox.stub(octokit.issues, 'addLabels').rejects(Error(errorMsg)); | ||
try { | ||
await addLabels(octokit, upstream, origin, issue_number, labels); | ||
expect.fail(); | ||
} catch (err) { | ||
expect(err.message).to.equal(errorMsg); | ||
} | ||
}); | ||
}); |
This file contains 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 |
---|---|---|
|
@@ -39,6 +39,7 @@ describe('Make PR main function', () => { | |
const primary = 'custom-primary'; | ||
const originRepo = 'Hello-World'; | ||
const originOwner = 'octocat'; | ||
const labelsToAdd = ['automerge']; | ||
const options: CreatePullRequestUserOptions = { | ||
upstreamOwner, | ||
upstreamRepo, | ||
|
@@ -48,6 +49,7 @@ describe('Make PR main function', () => { | |
force, | ||
message, | ||
primary, | ||
labels: labelsToAdd, | ||
}; | ||
const oldHeadSha = '7fd1a60b01f91b314f59955a4e4d4e80d8edf11d'; | ||
const changes: Changes = new Map(); | ||
|
@@ -116,6 +118,20 @@ describe('Make PR main function', () => { | |
expect(testMaintainersCanModify).equals(maintainersCanModify); | ||
expect(testPrimary).equals(primary); | ||
}, | ||
addLabels: ( | ||
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. @bcoe Added stub helpers for what actually seems to be the main tests for |
||
octokit: Octokit, | ||
upstream: {owner: string; repo: string}, | ||
originBranch: {owner: string; repo: string; branch: string}, | ||
issue_number: number, | ||
labels: string[], | ||
) => { | ||
expect(originBranch.owner).equals(originOwner); | ||
expect(originBranch.repo).equals(originRepo); | ||
expect(originBranch.branch).equals(branch); | ||
expect(upstream.owner).equals(upstreamOwner); | ||
expect(upstream.repo).equals(upstreamRepo); | ||
expect(labels).equals(labelsToAdd); | ||
} | ||
}; | ||
const stubMakePr = proxyquire.noCallThru()('../src/', { | ||
'./github-handler': stubHelperHandlers, | ||
|
@@ -171,6 +187,20 @@ describe('Make PR main function', () => { | |
expect(testMaintainersCanModify).equals(maintainersCanModify); | ||
expect(testPrimary).equals(primary); | ||
}, | ||
addLabels: ( | ||
octokit: Octokit, | ||
upstream: {owner: string; repo: string}, | ||
originBranch: {owner: string; repo: string; branch: string}, | ||
issue_number: number, | ||
labels: string[], | ||
) => { | ||
expect(originBranch.owner).equals(upstreamOwner); | ||
expect(originBranch.repo).equals(upstreamRepo); | ||
expect(originBranch.branch).equals(branch); | ||
expect(upstream.owner).equals(upstreamOwner); | ||
expect(upstream.repo).equals(upstreamRepo); | ||
expect(labels).equals(labelsToAdd); | ||
} | ||
}; | ||
const stubMakePr = proxyquire.noCallThru()('../src/', { | ||
'./github-handler': stubHelperHandlers, | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bcoe moved the check on
labels
intoaddLabels
instead of around the call site.