Skip to content

Commit

Permalink
ci(actions): move javascript code to separate files (#7548)
Browse files Browse the repository at this point in the history
## Summary

Moves javascript that was inlined into yaml to separate files so it can
be formatted/linted/highlighted properly.
  • Loading branch information
benelan authored Sep 1, 2023
1 parent 9066e58 commit 7b2377a
Show file tree
Hide file tree
Showing 27 changed files with 542 additions and 504 deletions.
13 changes: 13 additions & 0 deletions .github/scripts/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"extends": ["eslint:recommended", "plugin:prettier/recommended"],
"env": {
"node": true,
"es6": true
},
"parserOptions": {
"ecmaVersion": 2021
},
"rules": {
"comma-dangle": "off"
}
}
44 changes: 44 additions & 0 deletions .github/scripts/addCalcitePackageLabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
module.exports = async ({ github, context }) => {
const {
repo: { owner, repo },
payload: {
issue: { body, number: issue_number },
},
} = context;

if (!body) {
console.log("could not determine the issue body");
return;
}

// NOTE: assumes all packages will be in the @esri NPM scope
const packageRegex = /(?<=\[X\]\s@esri\/)[\w-]*$/gm;
const packages = body.match(packageRegex) || [];

for (const package of packages) {
/** Creates a label if it does not exist */
try {
await github.rest.issues.getLabel({
owner,
repo,
name: package,
});
} catch (e) {
await github.rest.issues.createLabel({
owner,
repo,
name: package,
color: "BFBEAF",
description: `Issues specific to the @esri/${package} package.`,
});
}

/** add new package label */
await github.rest.issues.addLabels({
issue_number,
owner,
repo,
labels: [package],
});
}
};
55 changes: 55 additions & 0 deletions .github/scripts/addEsriProductLabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module.exports = async ({ github, context }) => {
const {
repo: { owner, repo },
payload: {
action,
issue: { body, number: issue_number },
},
} = context;

if (!body) {
console.log("could not determine the issue body");
return;
}

const productRegex = new RegExp(
action === "edited"
? // the way GitHub parses the issue body into plaintext
// requires this exact format for edits
"(?<=### Esri team\r\n\r\n).+"
: // otherwise it depends on the submitter's OS
"(?<=### Esri team[\r\n|\r|\n]{2}).+$",
"m"
);

const productRegexMatch = body.match(productRegex);

const product = (productRegexMatch && productRegexMatch[0] ? productRegexMatch[0] : "").trim();

if (product && product !== "N/A") {
/** Creates a label if it does not exist */
try {
await github.rest.issues.getLabel({
owner,
repo,
name: product,
});
} catch (e) {
await github.rest.issues.createLabel({
owner,
repo,
name: product,
color: "006B75",
description: `Issues logged by ${product} team members.`,
});
}

/** add new product label */
await github.rest.issues.addLabels({
issue_number,
owner,
repo,
labels: [product],
});
}
};
55 changes: 55 additions & 0 deletions .github/scripts/addPriorityLabel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
module.exports = async ({ github, context }) => {
const {
repo: { owner, repo },
payload: {
action,
issue: { body, number: issue_number },
},
} = context;

if (!body) {
console.log("could not determine the issue body");
return;
}

const addPriorityRegex = new RegExp(
action === "edited"
? // the way GitHub parses the issue body into plaintext
// requires this exact format for edits
"(?<=### Priority impact\r\n\r\n).+"
: // otherwise it depends on the submitter's OS
"(?<=### Priority impact[\r\n|\r|\n]{2}).+$",
"m"
);

const addPriorityRegexMatch = body.match(addPriorityRegex);

const addPriorityLabel = (addPriorityRegexMatch && addPriorityRegexMatch[0] ? addPriorityRegexMatch[0] : "").trim();

if (addPriorityLabel && addPriorityLabel !== "N/A") {
/** Creates a label if it does not exist */
try {
await github.rest.issues.getLabel({
owner,
repo,
name: addPriorityLabel,
});
} catch (error) {
await github.rest.issues.createLabel({
owner,
repo,
name: addPriorityLabel,
color: "bb7fe0",
description: `User set priority status of ${addPriorityLabel}`,
});
}

/** add new priority label */
await github.rest.issues.addLabels({
issue_number,
owner,
repo,
labels: [addPriorityLabel],
});
}
};
32 changes: 32 additions & 0 deletions .github/scripts/assignForVerification.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = async ({ github, context }) => {
const { ISSUE_VERIFIERS, CALCITE_DESIGNERS } = process.env;
const { label } = context.payload;
if (label && label.name === "3 - installed") {
const assignees = ISSUE_VERIFIERS.split(",").map((v) => v.trim());

const { data: issue } = await github.rest.issues.get({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
});

// assign designers if figma updates are required
if (issue.labels.map((label) => label.name).includes("figma changes")) {
assignees.push(...CALCITE_DESIGNERS.split(",").map((v) => v.trim()));
}

await github.rest.issues.update({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
assignees,
});

await github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: "Installed and assigned for verification.",
});
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ module.exports = async ({ github, context }) => {
} = context.payload.pull_request;

const updatedAssignees =
assignees && assignees.length
? [...assignees.map((a) => a.login).filter((a) => a !== author), author]
: [author];
assignees && assignees.length ? [...assignees.map((a) => a.login).filter((a) => a !== author), author] : [author];

try {
await github.rest.issues.addAssignees({
Expand All @@ -18,9 +16,6 @@ module.exports = async ({ github, context }) => {
assignees: updatedAssignees,
});
} catch (e) {
console.error(
"Unable to assign the PR author, they likely do not have write permissions\n",
e,
);
console.error("Unable to assign the PR author, they likely do not have write permissions\n", e);
}
};
88 changes: 88 additions & 0 deletions .github/scripts/changeMilestone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
// Moves open, non-installed issues from the previous, past-due milestone to the current one.
// This action only retrieves up to 100 issues per issue lifecycle label because of the per_page max.
// The max per_page is not a problem for our 2 week sprints, but if we change to monthly
// we need to iterate through the issue list's pages using the `page` parameter.
// https://octokit.github.io/rest.js/v18#issues-list-for-repo
module.exports = async ({ github, context }) => {
const { data: milestones } = await github.rest.issues.listMilestones({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
sort: "due_on",
per_page: 100,
direction: "asc",
});

if (!milestones.length) {
console.log("There are no open milestones in this repo, ending run.");
return;
}

const currentDate = new Date(Date.now());
for (const [index, milestone] of milestones.entries()) {
if (!milestone.due_on || new Date(milestone.due_on) < currentDate) {
continue;
}

if (index < 1) {
console.log("There is no open, past due milestone to move issues from, ending run.");
return;
}

const { data: previousMilestoneIssuesNew } = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
milestone: milestones[index - 1].number,
per_page: 100,
labels: "0 - new",
});

const { data: previousMilestoneIssuesAssigned } = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
milestone: milestones[index - 1].number,
per_page: 100,
labels: "1 - assigned",
});

const { data: previousMilestoneIssuesInDev } = await github.rest.issues.listForRepo({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
milestone: milestones[index - 1].number,
per_page: 100,
labels: "2 - in development",
});

const moveableIssues = [
...previousMilestoneIssuesNew,
...previousMilestoneIssuesAssigned,
...previousMilestoneIssuesInDev,
];

if (!moveableIssues.length) {
console.log("There are no movable issues, ending run.");
return;
}

for (const issue of moveableIssues) {
const labels = [
...issue.labels.map((label) => label.name).filter((name) => name !== "milestone adjusted"),
"milestone adjusted",
];

await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issue.number,
milestone: milestone.number,
labels,
});
}

console.log("Moved", moveableIssues.length, "issues to the current milestone, ending run.");
return;
}
};
37 changes: 37 additions & 0 deletions .github/scripts/closeMilestone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module.exports = async ({ github, context }) => {
const {
repo: { repo, owner },
payload: { issue, pull_request },
} = context;

// no optional chaining support
// https://github.com/actions/github-script/pull/182
const milestone_number =
(issue && issue.milestone && issue.milestone.number) ||
(pull_request && pull_request.milestone && pull_request.milestone.number);

if (!milestone_number) {
console.log("No milestone found, ending run.");
return;
}

const { data: milestone } = await github.rest.issues.getMilestone({
owner,
repo,
milestone_number,
});

const currentDate = new Date(Date.now());
currentDate.setUTCHours(0, 0, 0, 0);

// close milestone if it is past due and there are no open issues/PRs
!milestone.open_issues &&
milestone.due_on &&
new Date(milestone.due_on) < currentDate &&
(await github.rest.issues.updateMilestone({
owner,
repo,
milestone_number,
state: "closed",
}));
};
36 changes: 36 additions & 0 deletions .github/scripts/createMilestone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module.exports = async ({ github, context }) => {
const DAYS_PER_SPRINT = 14;
const DAYS_BETWEEN_SPRINTS = 3;
const MILLISECONDS_PER_DAY = 1000 * 60 * 60 * 24;

const { data: milestones } = await github.rest.issues.listMilestones({
owner: context.repo.owner,
repo: context.repo.repo,
state: "open",
sort: "due_on",
per_page: 1,
direction: "desc",
});

if (!milestones.length || !milestones[0].due_on) {
console.log("There are no open milestones with a due date in this repo, ending run.");
return;
}

const lastDueDate = new Date(milestones[0].due_on);
const newMilestoneStartDate = new Date(lastDueDate.getTime() + MILLISECONDS_PER_DAY * DAYS_BETWEEN_SPRINTS);
const newMilestoneDueDate = new Date(lastDueDate.getTime() + MILLISECONDS_PER_DAY * DAYS_PER_SPRINT);

const title =
"Sprint " +
newMilestoneStartDate.toISOString().split("T")[0].replace(/-/g, "/") +
" - " +
newMilestoneDueDate.toISOString().split("T")[0].replace(/-/g, "/");

await github.rest.issues.createMilestone({
owner: context.repo.owner,
repo: context.repo.repo,
due_on: newMilestoneDueDate.toISOString(),
title,
});
};
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,3 @@ function getLabelName(type) {
return "chore";
}
}

Loading

0 comments on commit 7b2377a

Please sign in to comment.