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

Added output #60

Merged
merged 21 commits into from
Jun 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix review points
  • Loading branch information
Andrey Lobanovich committed Jun 28, 2023
commit cf0978ed2e4568bd39cfb5d51e51866912677978
9 changes: 0 additions & 9 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,8 +207,6 @@ describe('run', () => {

await run();

expect(addLabelsMock).toHaveBeenCalledTimes(0);
expect(removeLabelMock).toHaveBeenCalledTimes(1);
expect(setOutputMock).toHaveBeenCalledTimes(2);
expect(setOutputMock).toHaveBeenNthCalledWith(1, "new-labels", "");
expect(setOutputMock).toHaveBeenNthCalledWith(
Expand Down Expand Up @@ -253,11 +251,4 @@ function mockGitHubResponseAddLabels(...labelStrings: string[]): void {
});
i++;
}

addLabelsMock.mockResolvedValue({
status: 200,
headers: {},
url: "https://github.com/foo",
data,
});
}
24 changes: 11 additions & 13 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,26 @@ function run() {
core.debug(`fetching changed files for pr #${prNumber}`);
const changedFiles = yield getChangedFiles(client, prNumber);
const labelGlobs = yield getLabelGlobs(client, configPath);
const labelsToAdd = new Set();
const preexistingLabels = pullRequest.labels
.map(l => l.name)
.filter((l) => !!l); // just to get the type to be string[] instead of (string|undefined)[]
.map(l => l.name);
const allLabels = new Set(preexistingLabels);
for (const [label, globs] of labelGlobs.entries()) {
core.debug(`processing ${label}`);
if (checkGlobs(changedFiles, globs, dot)) {
labelsToAdd.add(label);
allLabels.add(label);
}
else if (syncLabels) {
labelsToAdd.delete(label);
allLabels.delete(label);
}
}
const labels = [...labelsToAdd].slice(0, GITHUB_MAX_LABELS);
const excessLabels = [...labelsToAdd].slice(GITHUB_MAX_LABELS);
const labelsToAdd = [...allLabels].slice(0, GITHUB_MAX_LABELS);
const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS);
try {
if (!isListEqual(labels, preexistingLabels)) {
yield setLabels(client, prNumber, labels);
const newLabels = labels.filter((l) => !preexistingLabels.includes(l));
if (!isListEqual(labelsToAdd, preexistingLabels)) {
yield setLabels(client, prNumber, labelsToAdd);
const newLabels = labelsToAdd.filter((l) => !preexistingLabels.includes(l));
core.setOutput("new-labels", newLabels.join(","));
core.setOutput("all-labels", labels.join(","));
core.setOutput("all-labels", labelsToAdd.join(","));
}
if (excessLabels.length) {
core.warning(`Maximum of ${GITHUB_MAX_LABELS} labels allowed. Excess labels: ${excessLabels.join(', ')}`, { title: 'Label limit for a PR exceeded' });
Expand Down Expand Up @@ -250,13 +249,12 @@ function isListEqual(listA, listB) {
}
function setLabels(client, prNumber, labels) {
return __awaiter(this, void 0, void 0, function* () {
const addLabelResult = yield client.rest.issues.setLabels({
yield client.rest.issues.setLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
labels: labels
});
return addLabelResult.data.map((datum) => datum.name);
});
}

Expand Down
25 changes: 11 additions & 14 deletions src/labeler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,29 +52,28 @@ export async function run() {
configPath
);

const labelsToAdd: Set<string> = new Set<string>();
const preexistingLabels = pullRequest.labels
.map(l => l.name)
.filter((l): l is string => !!l); // just to get the type to be string[] instead of (string|undefined)[]
const allLabels: Set<string> = new Set<string>(preexistingLabels);

for (const [label, globs] of labelGlobs.entries()) {
core.debug(`processing ${label}`);
if (checkGlobs(changedFiles, globs, dot)) {
labelsToAdd.add(label);
allLabels.add(label);
} else if (syncLabels) {
labelsToAdd.delete(label);
allLabels.delete(label);
}
}

const labels = [...labelsToAdd].slice(0, GITHUB_MAX_LABELS);
const excessLabels = [...labelsToAdd].slice(GITHUB_MAX_LABELS);
const labelsToAdd = [...allLabels].slice(0, GITHUB_MAX_LABELS);
const excessLabels = [...allLabels].slice(GITHUB_MAX_LABELS);

try {
if (!isListEqual(labels, preexistingLabels)) {
await setLabels(client, prNumber, labels);
const newLabels = labels.filter((l) => !preexistingLabels.includes(l));
if (!isListEqual(labelsToAdd, preexistingLabels)) {
await setLabels(client, prNumber, labelsToAdd);
const newLabels = labelsToAdd.filter((l) => !preexistingLabels.includes(l));
core.setOutput("new-labels", newLabels.join(","));
core.setOutput("all-labels", labels.join(","));
core.setOutput("all-labels", labelsToAdd.join(","));
}

if (excessLabels.length) {
Expand Down Expand Up @@ -295,13 +294,11 @@ async function setLabels(
client: ClientType,
prNumber: number,
labels: string[]
): Promise<string[]> {
const addLabelResult = await client.rest.issues.setLabels({
) {
await client.rest.issues.setLabels({
owner: github.context.repo.owner,
repo: github.context.repo.repo,
issue_number: prNumber,
labels: labels
});

return addLabelResult.data.map((datum) => datum.name);
}