Skip to content

Commit 36fe0a7

Browse files
[infra] Moved first batch of workflows to the public repo (#186)
1 parent 7af1d2d commit 36fe0a7

7 files changed

+240
-57
lines changed

.github/workflows/issue-cleanup.yml

-52
This file was deleted.

.github/workflows/add-closing-message-to-issue.yml .github/workflows/issues_add-closing-message.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ jobs:
1515
permissions:
1616
issues: write
1717
steps:
18-
- name: Check out public repo
19-
uses: actions/checkout@v3
18+
- name: Check out mui-public repo
19+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
2020
with:
2121
# Check this repository out, otherwise the script won't be available,
2222
# as it otherwise checks out the repository where the workflow caller is located
@@ -25,5 +25,5 @@ jobs:
2525
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
2626
with:
2727
script: |
28-
const script = require('./.github/workflows/scripts/addClosingMessage.js')
28+
const script = require('./.github/workflows/scripts/issues/addClosingMessage.js')
2929
await script({core, github, context})
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: Issue cleanup & Order ID validation
2+
3+
on:
4+
issues:
5+
types:
6+
- opened
7+
workflow_call:
8+
outputs:
9+
orderId:
10+
description: 'If a order id is found in the issue body, it will be outputted here'
11+
value: ${{ jobs.issue_cleanup.outputs.orderId }}
12+
13+
permissions: {}
14+
15+
jobs:
16+
issue_cleanup:
17+
name: Clean issue body
18+
runs-on: ubuntu-latest
19+
permissions:
20+
issues: write
21+
outputs:
22+
orderId: ${{ steps.cleanup.outputs.ORDER_ID }}
23+
steps:
24+
- name: Check out mui-public repo
25+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
26+
with:
27+
# Check this repository out, otherwise the script won't be available,
28+
# as it otherwise checks out the repository where the workflow caller is located
29+
repository: mui/mui-public
30+
- name: Clean issue body
31+
id: cleanup
32+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
33+
with:
34+
script: |
35+
const script = require('./.github/workflows/scripts/issues/bodyCleanup.js')
36+
await script({core, github, context})
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: Order ID validation
2+
3+
on:
4+
issues:
5+
types:
6+
- opened
7+
workflow_call:
8+
inputs:
9+
orderId:
10+
required: true
11+
type: string
12+
13+
permissions: {}
14+
15+
jobs:
16+
order_validation:
17+
name: Validate order ID
18+
runs-on: ubuntu-latest
19+
permissions:
20+
issues: write
21+
steps:
22+
- name: Check out mui-public repo
23+
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
24+
with:
25+
# Check this repository out, otherwise the script won't be available,
26+
# as it otherwise checks out the repository where the workflow caller is located
27+
repository: mui/mui-public
28+
- name: Validate order ID
29+
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
30+
with:
31+
script: |
32+
const script = require('./.github/workflows/scripts/issues/orderIdValidation.js')
33+
await script({core, github, context})
34+
env:
35+
ORDER_ID: ${{ inputs.orderId }}

.github/workflows/scripts/addClosingMessage.js .github/workflows/scripts/issues/addClosingMessage.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,12 @@ module.exports = async ({ core, context, github }) => {
7171
issue_number: issueNumber,
7272
name: labelName,
7373
});
74-
} catch (e) {
75-
core.error(`>>> Failed to remove label: ${e.message}`);
74+
} catch (error) {
75+
// intentionally not failing this job, since the label might not exist
76+
core.error(`>>> Failed to remove label: ${error.message}`);
7677
}
7778
} catch (error) {
79+
core.error(`>>> Workflow failed with: ${error.message}`);
7880
core.setFailed(error.message);
7981
}
8082
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
// @ts-check
2+
3+
function extractInputSection(lines, title) {
4+
const index = lines.findIndex((line) => line.startsWith('###') && line.includes(title));
5+
if (index === -1) {
6+
return '';
7+
}
8+
return lines.splice(index, 4)[2].trim();
9+
}
10+
11+
const productMap = {
12+
'Data Grid': 'data grid',
13+
'Date and Time Pickers': 'pickers',
14+
Charts: 'charts',
15+
'Tree View': 'tree view',
16+
};
17+
18+
/**
19+
* @param {Object} params
20+
* @param {import("@actions/core")} params.core
21+
* @param {ReturnType<import("@actions/github").getOctokit>} params.github
22+
* @param {import("@actions/github").context} params.context
23+
*/
24+
module.exports = async ({ core, context, github }) => {
25+
try {
26+
const owner = context.repo.owner;
27+
const repo = context.repo.repo;
28+
const issueNumber = context.issue.number;
29+
30+
const issue = await github.rest.issues.get({
31+
owner,
32+
repo,
33+
issue_number: issueNumber,
34+
});
35+
36+
const lines = issue.data.body.split('\n');
37+
38+
// this is here to remove this section from the issue body
39+
extractInputSection(lines, 'Latest version');
40+
41+
const searchKeywords = extractInputSection(lines, 'Search keywords');
42+
const products = extractInputSection(lines, 'Affected products');
43+
44+
// get the order id and set it as an output for the support label step
45+
let orderID = extractInputSection(lines, 'Order ID or Support key');
46+
if (orderID === '_No response_') {
47+
orderID = '';
48+
}
49+
50+
// set the order id as an output (to be consumed by following workflows)
51+
core.setOutput('ORDER_ID', orderID);
52+
53+
// log all the values
54+
core.info(`>>> Search Keywords: ${searchKeywords}`);
55+
core.info(`>>> Order ID: ${orderID}`);
56+
core.info(`>>> Affected Products: ${products}`);
57+
58+
lines.push('');
59+
lines.push(`**Search keywords**: ${searchKeywords}`);
60+
if (orderID !== '') {
61+
lines.push(`**Order ID**: ${orderID}`);
62+
}
63+
64+
const body = lines.join('\n');
65+
core.info(`>>> Cleansed issue body: ${body}`);
66+
67+
const labels = issue.data.labels.map((label) => label.name);
68+
69+
if (products !== '') {
70+
products.split(',').forEach((product) => {
71+
if (productMap[product.trim()]) {
72+
labels.push(`component: ${productMap[product.trim()]}`);
73+
}
74+
});
75+
}
76+
77+
core.info(`>>> Labels: ${labels.join(',')}`);
78+
79+
await github.rest.issues.update({
80+
owner: context.repo.owner,
81+
repo: context.repo.repo,
82+
issue_number: context.issue.number,
83+
body,
84+
labels,
85+
});
86+
} catch (error) {
87+
core.error(`>>> Workflow failed with: ${error.message}`);
88+
core.setFailed(error.message);
89+
}
90+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// @ts-check
2+
/**
3+
* @param {Object} params
4+
* @param {import("@actions/core")} params.core
5+
* @param {ReturnType<import("@actions/github").getOctokit>} params.github
6+
* @param {import("@actions/github").context} params.context
7+
*/
8+
module.exports = async ({ core, context, github }) => {
9+
try {
10+
const owner = context.repo.owner;
11+
const repo = context.repo.repo;
12+
const issueNumber = context.issue.number;
13+
14+
const orderId = process.env.ORDER_ID;
15+
const orderApiToken = process.env.ORDER_API_TOKEN;
16+
17+
const orderApi = 'https://store-wp.mui.com/wp-json/wc/v3/orders/';
18+
19+
core.info(`>>> Order ID: ${orderId}`);
20+
21+
if (!orderId) {
22+
core.info('No Order ID');
23+
} else {
24+
const order = await fetch(`${orderApi}${orderId}`, {
25+
headers: {
26+
Authorization: `Basic ${orderApiToken}`,
27+
'User-Agent': 'MUI-Tools-Private/X-Orders-Inspector v1',
28+
},
29+
});
30+
31+
if (!order.ok) {
32+
core.info(`Request to ${orderApi} failed. Response status code: ${order.status}.`);
33+
}
34+
35+
const orderDetails = await order.json();
36+
37+
core.info(`>>> Order Items: ${orderDetails.line_items?.join(',')}`);
38+
39+
const plan =
40+
orderDetails.line_items?.filter((item) => /\b(pro|premium)\b/i.test(item.name))[0].name ||
41+
'';
42+
43+
if (!plan) {
44+
core.info('No Pro or Premium plan found in order');
45+
return;
46+
}
47+
48+
const planName = plan.match(/\b(pro|premium)\b/i)[0].toLowerCase();
49+
50+
if (planName !== 'pro' && planName !== 'premium') {
51+
core.info(`>>> planName: ${planName}`);
52+
core.info('planName could not be extracted');
53+
return;
54+
}
55+
56+
const labelName = `support: ${planName} standard`;
57+
58+
core.info(`>>> planName: ${planName}`);
59+
core.info(`>>> labelName: ${labelName}`);
60+
61+
await github.rest.issues.addLabels({
62+
owner,
63+
repo,
64+
issue_number: issueNumber,
65+
labels: [labelName],
66+
});
67+
}
68+
} catch (error) {
69+
core.error(`>>> Workflow failed with: ${error.message}`);
70+
core.setFailed(error.message);
71+
}
72+
};

0 commit comments

Comments
 (0)