Skip to content

Commit

Permalink
feat: remove label for all prs after signing cla (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
gitcommitshow authored Sep 18, 2024
1 parent 2851b1d commit e085a5e
Showing 1 changed file with 84 additions and 22 deletions.
106 changes: 84 additions & 22 deletions src/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,40 +97,102 @@ export async function getOctokitForRepo(app, owner, repo) {
throw new Error(`Installation not found for repository ${owner}/${repo}`);
}

/**
* This function is used to remove the "Pending CLA" label from the PRs of the user who has signed the CLA.
* @param {Object} app - The Octokit app instance.
* @param {Object} claSignatureInfo - The information about the CLA signature.
* @example
* const claSignatureInfo = {
* referrer: "https://website.com/cla?org=rudderlabs&repo=rudder-server&prNumber=1234&username=githubUsername", // The URL of the page where the CLA was signed
* username: "githubUsername" // The username of the user who signed the CLA,
* }
* await afterCLA(app, claSignatureInfo);
*/
export async function afterCLA(app, claSignatureInfo) {
if (!claSignatureInfo || !claSignatureInfo.referrer) return;
const { org, repo, prNumber } = parseUrlQueryParams(
claSignatureInfo.referrer,
);
console.log(
`PR related to the CLA - owner: ${org}, repo: ${repo}, prNumber: ${prNumber}`,
);
if (!org || !repo || !prNumber) {
console.log("Not enough info to find the related PR.");
const { org, username } = parseUrlQueryParams(claSignatureInfo?.referrer) || {};
const githubUsername = claSignatureInfo.username || username;

if (!org || !githubUsername) {
console.log("Not enough info to find the related PRs.");
return;
}

console.log(`Processing CLA for ${githubUsername ? `user: ${githubUsername}` : 'unknown user'} in org/account: ${org}`);

try {
for await (const { installation } of app.eachInstallation.iterator()) {
for await (const { octokit, repository } of app.eachRepository.iterator({
installationId: installation.id,
})) {
console.log(`Processing repository: ${repository.name}`);

// Check if the current repository belongs to the org
if (repository?.owner?.login?.toLowerCase() !== org?.toLowerCase()) {
console.log(`Skipping ${repository.name} as it doesn't belong to ${org}`);
continue;
}

// Get all open PRs for the user in this repository
try {
const prs = await octokit.paginate(octokit.rest.pulls.list, {
owner: org,
repo: repository.name,
state: 'open',
creator: githubUsername
});
console.log(`Found ${prs.length} open PRs for ${githubUsername} in ${repository.name}:`, prs.map(pr => pr.number).join(', '));
for (const pr of prs) {
await removePendingCLALabel(octokit, org, repository.name, pr.number);
}
} catch (error) {
if (error.status === 404) {
console.log(`Repository ${org}/${repository.name} not found or no access. Skipping.`);
} else {
console.error(`Error processing ${org}/${repository.name}:`, error.message);
}
}
}
}
} catch (error) {
console.error("Error in afterCLA:", error);
} finally {
console.log("Completed post CLA verification tasks");
}
}

async function removePendingCLALabel(octokit, owner, repo, issue_number) {
try {
let octokit = await getOctokitForRepo(app, org, repo);
console.log(`Removing label 'Pending CLA' from PR #${issue_number} in ${owner}/${repo}`);
await octokit.rest.issues.removeLabel({
owner: org,
repo: repo,
issue_number: prNumber,
owner,
repo,
issue_number,
name: "Pending CLA",
});
console.log("Label 'Pending CLA' removed successfully.");
} catch (error) {
if (error.response) {
console.error(
`Error! Status: ${error.response.status}. Message: ${error.response.data.message}`,
);
console.log(`Label 'Pending CLA' removed successfully from PR #${issue_number}.`);
} catch (labelError) {
if (labelError.status === 404) {
console.log(`Label 'Pending CLA' not found on PR #${issue_number}. Skipping.`);
} else {
console.error(error);
console.error(`Error removing label from PR #${issue_number}:`, labelError.message);
}
} finally {
console.log("Completed post CLA verification tasks");
}
}

/**
* This function is used to get the message string based on the name of the message template and the context.
* @param {string} name - The name of the message template.
* @param {Object} context - The context object containing variables for the message template.
* @returns {string} - The message string.
* @example
* const context = {
* org: "rudderlabs",
* repo: "rudder-server",
* pr_number: 1234,
* username: "githubUsername"
* }
* const message = getMessage("ask-to-sign-cla", context);
*/
export function getMessage(name, context) {
let message = "";
switch (name) {
Expand Down

0 comments on commit e085a5e

Please sign in to comment.