golangci-lint/.github/peril/rules/invite-collaborator.ts
2020-05-17 18:14:43 +03:00

82 lines
2.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { danger } from "danger";
const comment = (username: string) => `
Hey, @${username} — we just merged your PR to \`golangci-lint\`! 🔥🚀
\`golangci-lint\` is built by awesome people like you. Let us say “thanks”: **we just invited you to join the [GolangCI](https://github.com/golangci) organization on GitHub.**
This will add you to our team of maintainers. Accept the invite by visiting [this link](https://github.com/orgs/golangci/invitation).
By joining the team, youll be able to label issues, review pull requests, and merge approved pull requests.
More information about contributing is [here](https://golangci-lint.run/contributing/quick-start/).
Thanks again!
`;
const teamId = `3840765`;
export const inviteCollaborator = async () => {
const gh = danger.github;
const api = gh.api;
// Details about the repo.
const owner = gh.thisPR.owner;
const repo = gh.thisPR.repo;
const number = gh.thisPR.number;
// Details about the collaborator.
const username = gh.pr.user.login;
// Check whether or not weve already invited this contributor.
try {
const inviteCheck = (await api.teams.getMembership({
team_id: teamId,
username,
} as any)) as any;
const isInvited = inviteCheck.headers.status !== "404";
// If weve already invited them, dont spam them with more messages.
if (isInvited) {
console.log(
`@${username} has already been invited to this org. Doing nothing.`
);
return;
}
} catch (err) {
console.info(
`Error checking membership of ${username} in team ${teamId}: ${err.stack}`
);
// If the user hasnt been invited, the invite check throws an error.
}
try {
const invite = await api.teams.addOrUpdateMembership({
team_id: teamId,
username,
} as any);
if (invite.data.state === "active") {
console.log(
`@${username} is already a ${invite.data.role} for this team.`
);
} else {
console.log(`Weve invited @${username} to join this team.`);
}
} catch (err) {
console.log("Something went wrong.");
console.log(err);
return;
}
// For new contributors, roll out the welcome wagon!
await api.issues.createComment({
owner,
repo,
number,
body: comment(username),
});
};
export default async () => {
await inviteCollaborator();
};