Skip to content
Draft
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
404 changes: 0 additions & 404 deletions .github/scripts/bot-beginner-assign-on-comment.js

This file was deleted.

436 changes: 0 additions & 436 deletions .github/scripts/bot-gfi-assign-on-comment.js

This file was deleted.

243 changes: 0 additions & 243 deletions .github/scripts/bot-intermediate-assignment.js

This file was deleted.

18 changes: 18 additions & 0 deletions .github/scripts/bot-issue-assign-on-comment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// .github/scripts/bot-issue-assign-on-comment.js
//
// Assigns user to a issue by commenting /assign

const { runAssignmentFlow } = require('./shared');

module.exports = async ({ github, context }) => {
try {
await runAssignmentFlow({ github, context });
} catch (error) {
console.error('[assign-bot] Unexpected error:', {
message: error.message,
status: error.status,
issue: context.payload?.issue?.number,
comment: context.payload?.comment?.id,
});
}
};
115 changes: 115 additions & 0 deletions .github/scripts/shared/api/github-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// ---------------------------------------------------------------------------

const { CONFIG } = require('../config');
const { isSafeSearchToken } = require("../helpers/validation");

/**
* Counts closed issues historically assigned to a contributor at a given label,
Expand Down Expand Up @@ -64,7 +65,121 @@ async function fetchIssuesBatch(github, repoConfig) {
}
}

async function getOpenAssignments({ github, owner, repo, username }) {
const issues = await github.paginate(github.rest.issues.listForRepo, {
owner,
repo,
assignee: username,
state: 'open',
per_page: 100,
});
return issues.length;
}

/**
* Counts closed issues carrying `label` (in the given repo) assigned to
* `username`. Returns null (rather than throwing) on unsafe input or API
* error so callers can choose to fail open.
*/
async function countCompletedIssuesWithLabel({ github, owner, repo, username, label }) {
if (!isSafeSearchToken(owner) || !isSafeSearchToken(repo) || !isSafeSearchToken(username)) {
return null;
}

const searchQuery = [
`repo:${owner}/${repo}`,
`label:"${label}"`,
'is:issue',
'is:closed',
`assignee:${username}`,
].join(' ');

try {
const result = await github.graphql(
`
query ($searchQuery: String!) {
search(type: ISSUE, query: $searchQuery) {
issueCount
}
}
`,
{ searchQuery }
);
return result?.search?.issueCount ?? 0;
} catch (error) {
console.error('[github-api] countCompletedIssuesWithLabel failed:', {
owner,
repo,
username,
label,
message: error.message,
});
return null;
}
}

async function isRepoCollaborator({ github, owner, repo, username }) {
if (username === owner) {
console.log(`[github-api] @${username} is the repo owner — treated as collaborator.`);
return true;
}

try {
const response = await github.rest.repos.getCollaboratorPermissionLevel({ owner, repo, username });
const permission = response?.data?.permission;
const isTeamMember = ['admin', 'write', 'maintain', 'read'].includes(permission);
console.log('[github-api] isRepoCollaborator:', { username, permission, isTeamMember });
return isTeamMember;
} catch (error) {
if (error?.status === 401 || error?.status === 403 || error?.status === 404) {
console.log('[github-api] isRepoCollaborator: not a collaborator', { username, status: error.status });
return false;
}
console.error('[github-api] isRepoCollaborator: unexpected error', { username, message: error.message });
return false;
}
}

async function postComment({ github, owner, repo, issueNumber, body }, logLabel) {
try {
await github.rest.issues.createComment({ owner, repo, issue_number: issueNumber, body });
console.log(`[github-api] Posted comment: ${logLabel}`);
} catch (error) {
console.error(`[github-api] Failed to post comment (${logLabel}):`, { message: error.message });
}
}

async function fetchAllComments({ github, owner, repo, issueNumber }) {
return github.paginate(github.rest.issues.listComments, {
owner,
repo,
issue_number: issueNumber,
per_page: 100,
});
}

async function assignIssue({
github,
owner,
repo,
issueNumber,
username,
}) {
await github.rest.issues.addAssignees({
owner,
repo,
issue_number: issueNumber,
assignees: [username],
});
}

module.exports = {
fetchIssuesBatch,
countClosedIssuesByAssignee,
getOpenAssignments,
countCompletedIssuesWithLabel,
isRepoCollaborator,
postComment,
fetchAllComments,
assignIssue,
};
Loading
Loading