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

[infra] Move first batch of workflows to the public repo #186

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
52 changes: 0 additions & 52 deletions .github/workflows/issue-cleanup.yml

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
permissions:
issues: write
steps:
- name: Check out public repo
uses: actions/checkout@v3
- name: Check out mui-public repo
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
# Check this repository out, otherwise the script won't be available,
# as it otherwise checks out the repository where the workflow caller is located
Expand All @@ -25,5 +25,5 @@ jobs:
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const script = require('./.github/workflows/scripts/addClosingMessage.js')
const script = require('./.github/workflows/scripts/issues/addClosingMessage.js')
await script({core, github, context})
36 changes: 36 additions & 0 deletions .github/workflows/issues_body-cleanup.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
name: Issue cleanup & Order ID validation

on:
issues:
types:
- opened
workflow_call:
outputs:
orderId:
description: 'If a order id is found in the issue body, it will be outputted here'
value: ${{ jobs.issue_cleanup.outputs.orderId }}

permissions: {}

jobs:
issue_cleanup:
name: Clean issue body
runs-on: ubuntu-latest
permissions:
issues: write
outputs:
orderId: ${{ steps.cleanup.outputs.ORDER_ID }}
steps:
- name: Check out mui-public repo
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
# Check this repository out, otherwise the script won't be available,
# as it otherwise checks out the repository where the workflow caller is located
repository: mui/mui-public
- name: Clean issue body
id: cleanup
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const script = require('./.github/workflows/scripts/issues/bodyCleanup.js')
await script({core, github, context})
35 changes: 35 additions & 0 deletions .github/workflows/issues_order-id-validation.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
name: Order ID validation

on:
issues:
types:
- opened
workflow_call:
inputs:
orderId:
required: true
type: string

permissions: {}

jobs:
order_validation:
name: Validate order ID
runs-on: ubuntu-latest
permissions:
issues: write
steps:
- name: Check out mui-public repo
uses: actions/checkout@692973e3d937129bcbf40652eb9f2f61becf3332 # v4.1.7
with:
# Check this repository out, otherwise the script won't be available,
# as it otherwise checks out the repository where the workflow caller is located
repository: mui/mui-public
- name: Validate order ID
uses: actions/github-script@60a0d83039c74a4aee543508d2ffcb1c3799cdea # v7.0.1
with:
script: |
const script = require('./.github/workflows/scripts/issues/orderIdValidation.js')
await script({core, github, context})
env:
ORDER_ID: ${{ inputs.orderId }}
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,12 @@ module.exports = async ({ core, context, github }) => {
issue_number: issueNumber,
name: labelName,
});
} catch (e) {
core.error(`>>> Failed to remove label: ${e.message}`);
} catch (error) {
// intentionally not failing this job, since the label might not exist
core.error(`>>> Failed to remove label: ${error.message}`);
}
} catch (error) {
core.error(`>>> Workflow failed with: ${error.message}`);
core.setFailed(error.message);
}
};
90 changes: 90 additions & 0 deletions .github/workflows/scripts/issues/bodyCleanup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// @ts-check

function extractInputSection(lines, title) {
const index = lines.findIndex((line) => line.startsWith('###') && line.includes(title));
if (index === -1) {
return '';
}
return lines.splice(index, 4)[2].trim();
}

const productMap = {
'Data Grid': 'data grid',
'Date and Time Pickers': 'pickers',
Charts: 'charts',
'Tree View': 'tree view',
};

/**
* @param {Object} params
* @param {import("@actions/core")} params.core
* @param {ReturnType<import("@actions/github").getOctokit>} params.github
* @param {import("@actions/github").context} params.context
*/
module.exports = async ({ core, context, github }) => {
try {
const owner = context.repo.owner;
const repo = context.repo.repo;
const issueNumber = context.issue.number;

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

const lines = issue.data.body.split('\n');

// this is here to remove this section from the issue body
extractInputSection(lines, 'Latest version');

const searchKeywords = extractInputSection(lines, 'Search keywords');
const products = extractInputSection(lines, 'Affected products');

// get the order id and set it as an output for the support label step
let orderID = extractInputSection(lines, 'Order ID or Support key');
if (orderID === '_No response_') {
orderID = '';
}

// set the order id as an output (to be consumed by following workflows)
core.setOutput('ORDER_ID', orderID);

// log all the values
core.info(`>>> Search Keywords: ${searchKeywords}`);
core.info(`>>> Order ID: ${orderID}`);
core.info(`>>> Affected Products: ${products}`);

lines.push('');
lines.push(`**Search keywords**: ${searchKeywords}`);
if (orderID !== '') {
lines.push(`**Order ID**: ${orderID}`);
}

const body = lines.join('\n');
core.info(`>>> Cleansed issue body: ${body}`);

const labels = issue.data.labels.map((label) => label.name);

if (products !== '') {
products.split(',').forEach((product) => {
if (productMap[product.trim()]) {
labels.push(`component: ${productMap[product.trim()]}`);
}
});
}

core.info(`>>> Labels: ${labels.join(',')}`);

await github.rest.issues.update({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body,
labels,
});
} catch (error) {
core.error(`>>> Workflow failed with: ${error.message}`);
core.setFailed(error.message);
}
};
72 changes: 72 additions & 0 deletions .github/workflows/scripts/issues/orderIdValidation.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// @ts-check
/**
* @param {Object} params
* @param {import("@actions/core")} params.core
* @param {ReturnType<import("@actions/github").getOctokit>} params.github
* @param {import("@actions/github").context} params.context
*/
module.exports = async ({ core, context, github }) => {
try {
const owner = context.repo.owner;
const repo = context.repo.repo;
const issueNumber = context.issue.number;

const orderId = process.env.ORDER_ID;
const orderApiToken = process.env.ORDER_API_TOKEN;

const orderApi = 'https://store-wp.mui.com/wp-json/wc/v3/orders/';

core.info(`>>> Order ID: ${orderId}`);

if (!orderId) {
core.info('No Order ID');
} else {
const order = await fetch(`${orderApi}${orderId}`, {
headers: {
Authorization: `Basic ${orderApiToken}`,
'User-Agent': 'MUI-Tools-Private/X-Orders-Inspector v1',
},
});

if (!order.ok) {
core.info(`Request to ${orderApi} failed. Response status code: ${order.status}.`);
}

const orderDetails = await order.json();

core.info(`>>> Order Items: ${orderDetails.line_items?.join(',')}`);

const plan =
orderDetails.line_items?.filter((item) => /\b(pro|premium)\b/i.test(item.name))[0].name ||
'';

if (!plan) {
core.info('No Pro or Premium plan found in order');
return;
}

const planName = plan.match(/\b(pro|premium)\b/i)[0].toLowerCase();

if (planName !== 'pro' && planName !== 'premium') {
core.info(`>>> planName: ${planName}`);
core.info('planName could not be extracted');
return;
}

const labelName = `support: ${planName} standard`;

core.info(`>>> planName: ${planName}`);
core.info(`>>> labelName: ${labelName}`);

await github.rest.issues.addLabels({
owner,
repo,
issue_number: issueNumber,
labels: [labelName],
});
}
} catch (error) {
core.error(`>>> Workflow failed with: ${error.message}`);
core.setFailed(error.message);
}
};
Loading