Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support adding labels to PRs (#173)
* feat: support adding labels to PRs * add new line at eof * fix lint errors * add fix changes * another new line at eof * fix license year * fix: no-op addLabels with empty labels * remove trailing s in comment * fix lint error
- Loading branch information
Showing
11 changed files
with
245 additions
and
0 deletions.
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 () => { | ||
// 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