-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'production' into ranbel/infrastructure-access
merge in latest changes
- Loading branch information
Showing
41 changed files
with
1,056 additions
and
882 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,115 +1,113 @@ | ||
// NOTE: This is the source file! | ||
// ~> Run `npm run build` to produce `index.js` | ||
|
||
import * as github from '@actions/github'; | ||
import * as core from '@actions/core'; | ||
import * as github from "@actions/github"; | ||
import * as core from "@actions/core"; | ||
|
||
function getTopLevelFolder(path: string): string { | ||
const parts = path.split('/'); | ||
return parts[2]; | ||
const parts = path.split("/"); | ||
return parts[2]; | ||
} | ||
|
||
|
||
function getSubFolder(path: string): string { | ||
const parts = path.split('/'); | ||
return parts[3]; | ||
const parts = path.split("/"); | ||
return parts[3]; | ||
} | ||
|
||
function getChangedSubFolders(files: any[]): string[] { | ||
const changedFolders = new Set<string>(); | ||
const changedFolders = new Set<string>(); | ||
|
||
for (const file of files) { | ||
const path = file.filename; | ||
const topLevelFolder = getTopLevelFolder(path); | ||
for (const file of files) { | ||
const path = file.filename; | ||
const topLevelFolder = getTopLevelFolder(path); | ||
|
||
// Check if the file is within the top-level /content folder | ||
if (topLevelFolder === 'docs') { | ||
const subFolder = getSubFolder(path); | ||
changedFolders.add(subFolder); | ||
} | ||
} | ||
// Check if the file is within the top-level /content folder | ||
if (topLevelFolder === "docs") { | ||
const subFolder = getSubFolder(path); | ||
changedFolders.add(subFolder); | ||
} | ||
} | ||
|
||
return Array.from(changedFolders); | ||
return Array.from(changedFolders); | ||
} | ||
|
||
async function run(): Promise<void> { | ||
try { | ||
const ctx = github.context; | ||
const token = core.getInput('GITHUB_TOKEN', { required: true }); | ||
const octokit = github.getOctokit(token); | ||
const pr = ctx.payload.pull_request; | ||
const prNumber = pr.number; | ||
const files = await octokit.paginate(octokit.rest.pulls.listFiles, { | ||
...ctx.repo, | ||
pull_number: pr.number, | ||
per_page: 100, | ||
}); | ||
|
||
// Get the changed sub-folders within the top-level /content folder | ||
const changedFolders = getChangedSubFolders(files); | ||
|
||
// ... | ||
|
||
// Label the PR based on the changed sub-folders | ||
await labelPRSubFolders(octokit, ctx.repo, prNumber, changedFolders); | ||
|
||
// ... | ||
} catch (error) { | ||
console.error('An error occurred:', error); | ||
process.exit(1); | ||
} | ||
try { | ||
const ctx = github.context; | ||
const token = core.getInput("GITHUB_TOKEN", { required: true }); | ||
const octokit = github.getOctokit(token); | ||
const pr = ctx.payload.pull_request; | ||
const prNumber = pr.number; | ||
const files = await octokit.paginate(octokit.rest.pulls.listFiles, { | ||
...ctx.repo, | ||
pull_number: pr.number, | ||
per_page: 100, | ||
}); | ||
|
||
// Get the changed sub-folders within the top-level /content folder | ||
const changedFolders = getChangedSubFolders(files); | ||
|
||
// ... | ||
|
||
// Label the PR based on the changed sub-folders | ||
await labelPRSubFolders(octokit, ctx.repo, prNumber, changedFolders); | ||
|
||
// ... | ||
} catch (error) { | ||
console.error("An error occurred:", error); | ||
process.exit(1); | ||
} | ||
} | ||
|
||
async function labelPRSubFolders( | ||
octokit: any, | ||
repo: any, | ||
prNumber: number, | ||
changedFolders: string[] | ||
octokit: any, | ||
repo: any, | ||
prNumber: number, | ||
changedFolders: string[], | ||
): Promise<void> { | ||
const labelPrefix = 'product:'; | ||
const labelsToRemove: string[] = []; | ||
const labelsToAdd: string[] = []; | ||
|
||
for (const label of github.context.payload.pull_request.labels) { | ||
if (label.name.startsWith(labelPrefix)) { | ||
labelsToRemove.push(label.name); | ||
} | ||
} | ||
|
||
for (const folder of changedFolders) { | ||
const label = labelPrefix + folder; | ||
labelsToAdd.push(label); | ||
} | ||
|
||
const currentLabels = new Set( | ||
github.context.payload.pull_request.labels.map((label: any) => label.name) | ||
); | ||
|
||
for (const labelToRemove of labelsToRemove) { | ||
if (!labelsToAdd.includes(labelToRemove)) { | ||
await octokit.rest.issues.removeLabel({ | ||
...repo, | ||
issue_number: prNumber, | ||
name: labelToRemove, | ||
}); | ||
} | ||
} | ||
|
||
let newLabels: string[] = []; | ||
for (const labelToAdd of labelsToAdd) { | ||
if (!currentLabels.has(labelToAdd)) { | ||
newLabels.push(labelToAdd); | ||
} | ||
} | ||
|
||
if (newLabels.length > 0) { | ||
await octokit.rest.issues.addLabels({ | ||
...repo, | ||
issue_number: prNumber, | ||
labels: newLabels, | ||
}); | ||
} | ||
const labelPrefix = "product:"; | ||
const labelsToRemove: string[] = []; | ||
const labelsToAdd: string[] = []; | ||
|
||
for (const label of github.context.payload.pull_request.labels) { | ||
if (label.name.startsWith(labelPrefix)) { | ||
labelsToRemove.push(label.name); | ||
} | ||
} | ||
|
||
for (const folder of changedFolders) { | ||
const label = labelPrefix + folder; | ||
labelsToAdd.push(label); | ||
} | ||
|
||
const currentLabels = new Set( | ||
github.context.payload.pull_request.labels.map((label: any) => label.name), | ||
); | ||
|
||
for (const labelToRemove of labelsToRemove) { | ||
if (!labelsToAdd.includes(labelToRemove)) { | ||
await octokit.rest.issues.removeLabel({ | ||
...repo, | ||
issue_number: prNumber, | ||
name: labelToRemove, | ||
}); | ||
} | ||
} | ||
|
||
let newLabels: string[] = []; | ||
for (const labelToAdd of labelsToAdd) { | ||
if (!currentLabels.has(labelToAdd)) { | ||
newLabels.push(labelToAdd); | ||
} | ||
} | ||
|
||
if (newLabels.length > 0) { | ||
await octokit.rest.issues.addLabels({ | ||
...repo, | ||
issue_number: prNumber, | ||
labels: newLabels, | ||
}); | ||
} | ||
} | ||
|
||
run(); | ||
|
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 |
---|---|---|
@@ -1,67 +1,68 @@ | ||
import * as core from '@actions/core'; | ||
import * as github from '@actions/github'; | ||
import * as core from "@actions/core"; | ||
import * as github from "@actions/github"; | ||
|
||
async function run(): Promise<void> { | ||
try { | ||
const ctx = github.context; | ||
const token = core.getInput('GITHUB_TOKEN', { required: true }); | ||
const octokit = github.getOctokit(token); | ||
const pr = ctx.payload.pull_request; | ||
const files = await octokit.paginate(octokit.rest.pulls.listFiles, { | ||
...ctx.repo, | ||
pull_number: pr.number, | ||
per_page: 100 | ||
}); | ||
try { | ||
const ctx = github.context; | ||
const token = core.getInput("GITHUB_TOKEN", { required: true }); | ||
const octokit = github.getOctokit(token); | ||
const pr = ctx.payload.pull_request; | ||
const files = await octokit.paginate(octokit.rest.pulls.listFiles, { | ||
...ctx.repo, | ||
pull_number: pr.number, | ||
per_page: 100, | ||
}); | ||
|
||
const changes = files.reduce((total, file) => total + file.changes, 0); | ||
const changes = files.reduce((total, file) => total + file.changes, 0); | ||
|
||
let label: string | undefined; | ||
let label: string | undefined; | ||
|
||
switch (true) { | ||
case changes <= 10: | ||
label = 'size/xs'; | ||
break; | ||
case changes <= 100: | ||
label = 'size/s'; | ||
break; | ||
case changes <= 500: | ||
label = 'size/m'; | ||
break; | ||
case changes <= 1000: | ||
label = 'size/l'; | ||
break; | ||
default: | ||
label = 'size/xl'; | ||
break; | ||
} | ||
switch (true) { | ||
case changes <= 10: | ||
label = "size/xs"; | ||
break; | ||
case changes <= 100: | ||
label = "size/s"; | ||
break; | ||
case changes <= 500: | ||
label = "size/m"; | ||
break; | ||
case changes <= 1000: | ||
label = "size/l"; | ||
break; | ||
default: | ||
label = "size/xl"; | ||
break; | ||
} | ||
|
||
const currentLabels = pr.labels.map((label) => label.name); | ||
const sizeLabels = ['size/xs', 'size/s', 'size/m', 'size/l', 'size/xl']; | ||
const currentLabels = pr.labels.map((label) => label.name); | ||
const sizeLabels = ["size/xs", "size/s", "size/m", "size/l", "size/xl"]; | ||
|
||
// Remove previous size-related labels that are different from the current label | ||
const labelsToRemove = currentLabels.filter( | ||
(currentLabel) => sizeLabels.includes(currentLabel) && currentLabel !== label | ||
); | ||
// Remove previous size-related labels that are different from the current label | ||
const labelsToRemove = currentLabels.filter( | ||
(currentLabel) => | ||
sizeLabels.includes(currentLabel) && currentLabel !== label, | ||
); | ||
|
||
for (const labelToRemove of labelsToRemove) { | ||
await octokit.rest.issues.removeLabel({ | ||
...ctx.repo, | ||
issue_number: pr.number, | ||
name: labelToRemove | ||
}); | ||
} | ||
for (const labelToRemove of labelsToRemove) { | ||
await octokit.rest.issues.removeLabel({ | ||
...ctx.repo, | ||
issue_number: pr.number, | ||
name: labelToRemove, | ||
}); | ||
} | ||
|
||
// Add the current label | ||
if (!currentLabels.includes(label)) { | ||
await octokit.rest.issues.addLabels({ | ||
...ctx.repo, | ||
issue_number: pr.number, | ||
labels: [label] | ||
}); | ||
} | ||
} catch (error) { | ||
core.setFailed(error.message); | ||
} | ||
// Add the current label | ||
if (!currentLabels.includes(label)) { | ||
await octokit.rest.issues.addLabels({ | ||
...ctx.repo, | ||
issue_number: pr.number, | ||
labels: [label], | ||
}); | ||
} | ||
} catch (error) { | ||
core.setFailed(error.message); | ||
} | ||
} | ||
|
||
run(); |
Oops, something went wrong.