Skip to content

Commit

Permalink
Update issue_triage.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayank77maruti authored Dec 18, 2024
1 parent ca9181a commit 1fc3133
Showing 1 changed file with 48 additions and 85 deletions.
133 changes: 48 additions & 85 deletions .github/workflows/issue_triage.yml
Original file line number Diff line number Diff line change
@@ -1,121 +1,84 @@
name: Issue Triage and Management
name: Auto Assign Project

on:
# Trigger on issue events
# Trigger when an issue is opened or labeled
issues:
types: [opened, labeled, unlabeled, assigned]

# Trigger on project card events
project_card:
types: [moved_to, unassigned, created, deleted, edited]
types: [opened, labeled]

# Add manual trigger for testing
# Manual trigger for testing
workflow_dispatch:

permissions:
contents: read
issues: write
repository-projects: write # Required to manage projects

jobs:
verify-project-assignment:
assign-project:
runs-on: ubuntu-latest
if: github.event.sender.login != 'github-actions'
steps:
- name: Verify Project Assignment
- name: Assign Issue to Project
uses: actions/github-script@v6
with:
script: |
const issueNumber = context.issue.number;
console.log(`Processing issue #${issueNumber} in repository ${context.repo.owner}/${context.repo.repo}`);
const owner = context.repo.owner;
const repo = context.repo.repo;
const targetProjectTitle = "Your Project Name"; // Replace with the desired project name
console.log(`Processing issue #${issueNumber} in repository ${owner}/${repo}`);
// Updated GraphQL query to fetch ProjectV2 details
const query = `
query ($owner: String!, $repo: String!, $issueNumber: Int!) {
// GraphQL query to find the target project
const projectQuery = `
query ($owner: String!, $repo: String!) {
repository(owner: $owner, name: $repo) {
issue(number: $issueNumber) {
projectItems(first: 10) {
nodes {
id
project {
id
title # For ProjectV2, use 'title' instead of 'name'
}
}
totalCount
projectsV2(first: 10) {
nodes {
id
title
}
}
}
}
`;
const variables = {
owner: context.repo.owner,
repo: context.repo.repo,
issueNumber: issueNumber,
const projectVariables = {
owner,
repo,
};
console.log("Executing GraphQL query to fetch project assignment details...");
try {
const result = await github.graphql(query, variables);
console.log("Fetching list of projects...");
const projectResult = await github.graphql(projectQuery, projectVariables);
const projectCount = result.repository.issue.projectItems.totalCount;
console.log(`Project count for issue #${issueNumber}: ${projectCount}`);
const projects = projectResult.repository.projectsV2.nodes;
const targetProject = projects.find(project => project.title === targetProjectTitle);
if (projectCount === 0) {
console.log("No projects assigned to this issue.");
const currentComments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
});
console.log("Fetched existing comments on the issue.");
if (!targetProject) {
throw new Error(`Project "${targetProjectTitle}" not found in repository ${owner}/${repo}`);
}
const commentBody = currentComments.data.map(comment => comment.body).join("\n");
if (!commentBody.includes("0 project(s)")) {
console.log("Adding comment about missing project assignment...");
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: "Assigned to 0 project(s) - This issue must be assigned to exactly one project. Please assign it to a single project to continue.",
});
console.log("Comment added.");
await github.rest.issues.addLabels({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
labels: ["needs-triage"],
});
console.log("Label 'needs-triage' added to the issue.");
} else {
console.log("Comment about missing project assignment already exists. Skipping...");
}
} else if (projectCount > 1) {
console.log("Multiple projects assigned to this issue.");
const currentComments = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
});
console.log("Fetched existing comments on the issue.");
console.log(`Target project found: ${targetProject.title} (ID: ${targetProject.id})`);
const commentBody = currentComments.data.map(comment => comment.body).join("\n");
if (!commentBody.includes(`${projectCount} project(s)`)) {
console.log(`Adding comment about multiple project assignments (${projectCount} projects)...`);
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: issueNumber,
body: `Assigned to ${projectCount} project(s) - This issue must be assigned to exactly one project.`,
});
console.log("Comment added.");
} else {
console.log("Comment about multiple project assignments already exists. Skipping...");
// GraphQL mutation to add the issue to the project
const addToProjectMutation = `
mutation ($projectId: ID!, $contentId: ID!) {
addProjectV2ItemById(input: {projectId: $projectId, contentId: $contentId}) {
item {
id
}
}
} else {
console.log("Issue is assigned to exactly one project. No further action needed.");
}
`;
const addToProjectVariables = {
projectId: targetProject.id,
contentId: context.payload.issue.node_id,
};
console.log(`Assigning issue #${issueNumber} to project "${targetProjectTitle}"...`);
try {
const addResult = await github.graphql(addToProjectMutation, addToProjectVariables);
console.log(`Issue successfully assigned to project. Item ID: ${addResult.addProjectV2ItemById.item.id}`);
} catch (error) {
console.error("Error executing GraphQL query or processing results:", error);
console.error("Failed to assign the issue to the project:", error);
throw error;
}

0 comments on commit 1fc3133

Please sign in to comment.