Skip to content

Commit

Permalink
fix(app): distinguish sender of a webhook and a repository owner
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Oct 31, 2022
1 parent ce6f4a2 commit 0d3a1d0
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions app/webhooks.ts
Expand Up @@ -103,24 +103,18 @@ const isDenoProject = async (github: GitHubClient) => {
// create .github/workflows/denopendabot.yml in a repository
const createWorkflow = async (
octokit: Octokit,
owner: string,
repo: string,
repository: string,
) => {
const deploy = await deployment();
const repository = `${owner}/${repo}`;
const testing = repository === env.APP_REPO;

if (deploy === "production" && testing) return;
if (deploy === "staging" && !testing) return;

console.info(`🚀 ${owner} installed Denopendabot to ${owner}/${repo}`);

const github = new GitHubClient({ octokit, repository });

if (!(await isDenoProject(github))) {
console.info("...but it does not seem to be a Deno project");
return;
}
if (!(await isDenoProject(github))) return;

const base = testing ? "test-install" : await github.defaultBranch();
const head = testing ? base + "-" + Date.now() : "denopendabot-setup";

Expand All @@ -144,21 +138,23 @@ const createWorkflow = async (
app.webhooks.on("installation.created", async ({ octokit, payload }) => {
console.debug(payload);
if (!payload.repositories) return;
const owner = payload.sender.login;
const sender = payload.sender.login;

for (const { name: repo } of payload.repositories) {
await createWorkflow(octokit, owner, repo);
for (const { full_name } of payload.repositories) {
console.info(`🚀 ${sender} installed Denopendabot to ${full_name}`);
await createWorkflow(octokit, full_name);
}
});

app.webhooks.on(
"installation_repositories.added",
async ({ octokit, payload }) => {
console.debug(payload);
const owner = payload.sender.login;
const sender = payload.sender.login;

for (const { name: repo } of payload.repositories_added) {
await createWorkflow(octokit, owner, repo);
for (const { full_name } of payload.repositories_added) {
console.info(`🚀 ${sender} installed Denopendabot to ${full_name}`);
await createWorkflow(octokit, full_name);
}
},
);
Expand Down

0 comments on commit 0d3a1d0

Please sign in to comment.