dev: add peril to invite contributors

Invite a contributor to GolangCI
organization after a first pull request
get merged.
This commit is contained in:
Denis Isaev 2020-05-17 17:43:35 +03:00
parent f3380053af
commit 4c94e3400e
No known key found for this signature in database
GPG Key ID: E3AC3F0F5A16B88A
8 changed files with 4049 additions and 0 deletions

1
.github/peril/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
node_modules/

1
.github/peril/README.md vendored Normal file
View File

@ -0,0 +1 @@
Based on https://github.com/gatsbyjs/gatsby/tree/master/peril.

3857
.github/peril/package-lock.json generated vendored Normal file

File diff suppressed because it is too large Load Diff

24
.github/peril/package.json vendored Normal file
View File

@ -0,0 +1,24 @@
{
"dependencies": {
"jest": "^24.9.0"
},
"scripts": {
"test": "jest"
},
"jest": {
"rootDir": "../",
"roots": [
"<rootDir>/peril"
],
"transform": {
"^.+\\.tsx?$": "<rootDir>/node_modules/ts-jest/preprocessor.js"
},
"moduleFileExtensions": [
"js",
"jsx",
"ts",
"tsx",
"json"
]
}
}

View File

@ -0,0 +1,78 @@
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.orgs.getTeamMembership({
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 (_) {
// If the user hasnt been invited, the invite check throws an error.
}
try {
const invite = await api.orgs.addTeamMembership({
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();
};

14
.github/peril/settings.json vendored Normal file
View File

@ -0,0 +1,14 @@
{
"$schema": "https://raw.githubusercontent.com/danger/peril/master/peril-settings-json.schema",
"settings": {
"ignored_repos": [],
"env_vars": ["SLACK_WEBHOOK_URL", "GITHUB_ACCESS_TOKEN"]
},
"repos": {
"golangci/golangci-lint": {
"pull_request.closed (pull_request.merged == true)": [
".github/peril/rules/invite-collaborator.ts"
]
}
}
}

View File

@ -0,0 +1,65 @@
jest.mock("danger", () => jest.fn());
import * as danger from "danger";
const dm = danger as any;
import { inviteCollaborator } from "../rules/invite-collaborator";
beforeEach(() => {
dm.danger = {
github: {
thisPR: {
owner: "gatsbyjs",
repo: "peril-gatsbyjs",
number: 1,
},
pr: {
user: {
login: "someUser",
},
},
api: {
orgs: {
getTeamMembership: () => Promise.resolve({ meta: { status: "404" } }),
addTeamMembership: jest.fn(() =>
Promise.resolve({ data: { state: "pending" } })
),
},
issues: {
createComment: jest.fn(),
},
},
},
};
});
describe("a closed pull request", () => {
it("was merged and authored by a first-time contributor", async () => {
dm.danger.github.pr.merged = true;
await inviteCollaborator();
expect(dm.danger.github.api.issues.createComment).toBeCalled();
expect(dm.danger.github.api.orgs.addTeamMembership).toBeCalled();
});
it("was merged and authored by an existing collaborator", async () => {
dm.danger.github.pr.merged = true;
dm.danger.github.api.orgs.getTeamMembership = () =>
Promise.resolve({ headers: { status: "204 No Content" } });
await inviteCollaborator();
expect(dm.danger.github.api.issues.createComment).not.toBeCalled();
});
it("does not comment if invitation failed", async () => {
dm.danger.github.pr.merged = true;
dm.danger.github.api.orgs.addTeamMembership = () =>
Promise.reject({ headers: { status: "422 Unprocessable Entity" } });
await inviteCollaborator();
expect(dm.danger.github.api.issues.createComment).not.toBeCalled();
});
});

9
.github/peril/tsconfig.json vendored Normal file
View File

@ -0,0 +1,9 @@
{
"compilerOptions": {
"esModuleInterop": false,
"target": "es5",
"module": "commonjs",
"lib": ["es2017"],
"strict": true
}
}