Skip to content

Stage the aimlapi v0.0.9 marketplace package #1

Stage the aimlapi v0.0.9 marketplace package

Stage the aimlapi v0.0.9 marketplace package #1

name: PR Risk Label
on:
pull_request_target:
types: [opened, edited, reopened, synchronize, ready_for_review]
permissions:
pull-requests: write
issues: write
jobs:
apply-risk-label:
runs-on: ubuntu-latest
steps:
- name: Apply risk label
uses: actions/github-script@f28e40c7f34bde8b3046d885e986cb6290c5673b # v7
with:
script: |
const riskLabels = {
low: {
name: 'risk: low',
color: '0E8A16',
description: 'Low-risk Marketplace submission',
pattern: /^- \[[xX]\] Low risk\s*$/m,
},
medium: {
name: 'risk: medium',
color: 'FBCA04',
description: 'Medium-risk Marketplace submission',
pattern: /^- \[[xX]\] Medium risk\s*$/m,
},
high: {
name: 'risk: high',
color: 'D93F0B',
description: 'High-risk Marketplace submission',
pattern: /^- \[[xX]\] High risk\s*$/m,
},
missing: {
name: 'risk: missing',
color: 'BFDADC',
description: 'Missing or invalid Marketplace risk selection',
},
};
const marker = '<!-- dify-plugins-risk-label-bot -->';
const warningBody = `${marker}\nPlease select exactly one risk level in the PR template: Low risk, Medium risk, or High risk. This helps Marketplace reviewers route the submission correctly.`;
const { owner, repo } = context.repo;
const pr = context.payload.pull_request;
const issue_number = pr.number;
const body = pr.body || '';
const selected = Object.entries(riskLabels)
.filter(([key, config]) => key !== 'missing' && config.pattern.test(body))
.map(([, config]) => config.name);
const targetLabel = selected.length === 1 ? selected[0] : riskLabels.missing.name;
const allRiskLabelNames = Object.values(riskLabels).map((label) => label.name);
async function updateLabel(label) {
await github.rest.issues.updateLabel({
owner,
repo,
name: label.name,
color: label.color,
description: label.description,
});
}
for (const label of Object.values(riskLabels)) {
try {
const { data: existing } = await github.rest.issues.getLabel({ owner, repo, name: label.name });
if (
existing.color?.toUpperCase() !== label.color ||
(existing.description ?? '') !== (label.description ?? '')
) {
await updateLabel(label);
}
} catch (error) {
if (error.status !== 404) {
throw error;
}
try {
await github.rest.issues.createLabel({
owner,
repo,
name: label.name,
color: label.color,
description: label.description,
});
} catch (createError) {
if (createError.status !== 422) {
throw createError;
}
await updateLabel(label);
}
}
}
for (const label of allRiskLabelNames.filter((name) => name !== targetLabel)) {
try {
await github.rest.issues.removeLabel({ owner, repo, issue_number, name: label });
} catch (error) {
if (error.status !== 404) {
throw error;
}
}
}
await github.rest.issues.addLabels({
owner,
repo,
issue_number,
labels: [targetLabel],
});
const comments = await github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number,
per_page: 100,
});
const managedComments = comments.filter(
(comment) => comment.user?.login === 'github-actions[bot]' && comment.body?.includes(marker)
);
if (selected.length === 1) {
for (const comment of managedComments) {
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: comment.id,
});
}
} else if (managedComments.length > 0) {
const [firstComment, ...duplicateComments] = managedComments;
await github.rest.issues.updateComment({
owner,
repo,
comment_id: firstComment.id,
body: warningBody,
});
for (const comment of duplicateComments) {
await github.rest.issues.deleteComment({
owner,
repo,
comment_id: comment.id,
});
}
} else {
await github.rest.issues.createComment({
owner,
repo,
issue_number,
body: warningBody,
});
}