Skip to content

Commit 3da20ff

Browse files
committed
fix: empty organization
1 parent 83084c9 commit 3da20ff

File tree

5 files changed

+336
-3
lines changed

5 files changed

+336
-3
lines changed

src/bot.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export default (app: Probot) => {
3737
);
3838
return;
3939
}
40+
// Check if organization is valid
41+
if (!organization) {
42+
context.log.info(`Organization is empty`);
43+
return;
44+
}
4045

4146
const octokit = await app.auth(); // Not passing an id returns a JWT-authenticated client
4247
const jwt = (await octokit.auth({ type: 'app' })) as { token: string };
@@ -92,7 +97,7 @@ export default (app: Probot) => {
9297
app.on('issue_comment.created', async (context: Context<'issue_comment.created'>) => {
9398
const repo = context.payload.repository.name;
9499
const owner = context.payload.repository.owner.login;
95-
const organization = context.payload.repository.owner.login;
100+
const organization = context.payload.organization?.login ?? '';
96101
const sender = context.payload.sender.login;
97102
const comment = context.payload.comment.body;
98103
const issueNumber = context.payload.issue.number;
@@ -106,6 +111,11 @@ export default (app: Probot) => {
106111
context.log.info(`Sender didn't tag @gitpoap-bot explicitly in this comment`);
107112
return;
108113
}
114+
// Check if organization is valid
115+
if (!organization) {
116+
context.log.info(`Organization is empty`);
117+
return;
118+
}
109119

110120
// Fetch permission to check if commenter has appropriate permissions to use @gitpoap-bot
111121
const permissionRes = await context.octokit.rest.repos.getCollaboratorPermissionLevel({

test/bot.test.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import myProbotApp from '../src/bot';
77
// Requiring our fixtures
88
import issueCommentPayload from './fixtures/issue_comment.created_issue.json';
99
import issueCommentNoUsersPayload from './fixtures/issue_comment.created_issue_no_user.json';
10+
import issueCommentNoOrganizationPayload from './fixtures/issue_comment.created_issue_no_organization.json';
1011
import issueCommentInvalidUsersPayload from './fixtures/issue_comment.created_issue_invalid_users.json';
1112
import prCommentPayload from './fixtures/issue_comment.created_pr.json';
1213
import nonOwnerPayload from './fixtures/issue_comment.created_non_owner.json';
@@ -430,6 +431,70 @@ describe('gitpoap-bot', () => {
430431
expect(githubAPIMock.activeMocks()).toStrictEqual([]);
431432
expect(gitpoapAPIMock.activeMocks()).toStrictEqual([]);
432433
});
434+
435+
it('should not create claims if organization is empty', async () => {
436+
const githubAPIMock = nock('https://api.github.com')
437+
// Test that we correctly return a test token
438+
.post('/app/installations/29153052/access_tokens')
439+
.reply(200, {
440+
token: 'test',
441+
permissions: {
442+
issues: 'write',
443+
},
444+
})
445+
446+
// get github login ids
447+
.get('/users/test1')
448+
.reply(200, {
449+
id: 1,
450+
type: 'Organization',
451+
})
452+
.get('/users/test2')
453+
.reply(200, {
454+
id: 2,
455+
type: 'User',
456+
})
457+
.get('/users/test3')
458+
.reply(200, {
459+
id: 3,
460+
type: 'Organization',
461+
})
462+
463+
// get permissions
464+
.get('/repos/gitpoap/gitpoap-bot-test-repo/collaborators/gitpoap/permission')
465+
.reply(200, {
466+
user: {
467+
permissions: {
468+
admin: true,
469+
},
470+
},
471+
})
472+
473+
// Test that a comment is posted with the correct body
474+
.post(
475+
'/repos/gitpoap/gitpoap-bot-test-repo/issues/25/comments',
476+
issueCreatedBodyWithoutOrgs,
477+
)
478+
.reply(200);
479+
480+
// Test response from gitpoap api
481+
const gitpoapAPIMock = nock(`${process.env.API_URL}`)
482+
.post(`/claims/gitpoap-bot/create`)
483+
.reply(200, {
484+
newClaims: newClaimsWithoutOrgs,
485+
});
486+
487+
// Receive a webhook event
488+
await probot.receive({ name: 'issue_comment', payload: issueCommentNoOrganizationPayload });
489+
490+
expect(githubAPIMock.activeMocks()).toStrictEqual([
491+
'GET https://api.github.com:443/repos/gitpoap/gitpoap-bot-test-repo/collaborators/gitpoap/permission',
492+
'POST https://api.github.com:443/repos/gitpoap/gitpoap-bot-test-repo/issues/25/comments',
493+
]);
494+
expect(gitpoapAPIMock.activeMocks()).toStrictEqual([
495+
`POST ${process.env.API_URL}/claims/gitpoap-bot/create`,
496+
]);
497+
});
433498
});
434499

435500
describe('PR Close', () => {
Lines changed: 230 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,230 @@
1+
{
2+
"action": "created",
3+
"issue": {
4+
"url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/25",
5+
"repository_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo",
6+
"labels_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/25/labels{/name}",
7+
"comments_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/25/comments",
8+
"events_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/25/events",
9+
"html_url": "https: //github.com/gitpoap/gitpoap-bot-test-repo/issues/25",
10+
"id": 1375906907,
11+
"node_id": "I_kwDOHe30q85SAqxb",
12+
"number": 25,
13+
"title": "Issue 4",
14+
"user": {
15+
"login": "tyler415git",
16+
"id": 110413633,
17+
"node_id": "U_kgDOBpTHQQ",
18+
"avatar_url": "https: //avatars.githubusercontent.com/u/110413633?v=4",
19+
"gravatar_id": "",
20+
"url": "https: //api.github.com/users/tyler415git",
21+
"html_url": "https: //github.com/tyler415git",
22+
"followers_url": "https: //api.github.com/users/tyler415git/followers",
23+
"following_url": "https: //api.github.com/users/tyler415git/following{/other_user}",
24+
"gists_url": "https: //api.github.com/users/tyler415git/gists{/gist_id}",
25+
"starred_url": "https: //api.github.com/users/tyler415git/starred{/owner}{/repo}",
26+
"subscriptions_url": "https: //api.github.com/users/tyler415git/subscriptions",
27+
"organizations_url": "https: //api.github.com/users/tyler415git/orgs",
28+
"repos_url": "https: //api.github.com/users/tyler415git/repos",
29+
"events_url": "https: //api.github.com/users/tyler415git/events{/privacy}",
30+
"received_events_url": "https: //api.github.com/users/tyler415git/received_events",
31+
"type": "User",
32+
"site_admin": false
33+
},
34+
"labels": [],
35+
"state": "open",
36+
"locked": false,
37+
"assignee": null,
38+
"assignees": [],
39+
"milestone": null,
40+
"comments": 9,
41+
"created_at": "2022-09-16T12: 51: 10Z",
42+
"updated_at": "2022-09-18T12: 38: 01Z",
43+
"closed_at": null,
44+
"author_association": "CONTRIBUTOR",
45+
"active_lock_reason": null,
46+
"body": "issue 4",
47+
"reactions": {
48+
"url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/25/reactions",
49+
"total_count": 0,
50+
"+1": 0,
51+
"-1": 0,
52+
"laugh": 0,
53+
"hooray": 0,
54+
"confused": 0,
55+
"heart": 0,
56+
"rocket": 0,
57+
"eyes": 0
58+
},
59+
"timeline_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/25/timeline",
60+
"performed_via_github_app": null,
61+
"state_reason": null
62+
},
63+
"comment": {
64+
"url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/comments/1250261829",
65+
"html_url": "https: //github.com/gitpoap/gitpoap-bot-test-repo/issues/25#issuecomment-1250261829",
66+
"issue_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/25",
67+
"id": 1250261829,
68+
"node_id": "IC_kwDOHe30q85KhXtF",
69+
"user": {
70+
"login": "gitpoap",
71+
"id": 91488802,
72+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkxNDg4ODAy",
73+
"avatar_url": "https: //avatars.githubusercontent.com/u/91488802?v=4",
74+
"gravatar_id": "",
75+
"url": "https: //api.github.com/users/gitpoap",
76+
"html_url": "https: //github.com/gitpoap",
77+
"followers_url": "https: //api.github.com/users/gitpoap/followers",
78+
"following_url": "https: //api.github.com/users/gitpoap/following{/other_user}",
79+
"gists_url": "https: //api.github.com/users/gitpoap/gists{/gist_id}",
80+
"starred_url": "https: //api.github.com/users/gitpoap/starred{/owner}{/repo}",
81+
"subscriptions_url": "https: //api.github.com/users/gitpoap/subscriptions",
82+
"organizations_url": "https: //api.github.com/users/gitpoap/orgs",
83+
"repos_url": "https: //api.github.com/users/gitpoap/repos",
84+
"events_url": "https: //api.github.com/users/gitpoap/events{/privacy}",
85+
"received_events_url": "https: //api.github.com/users/gitpoap/received_events",
86+
"type": "Organization",
87+
"site_admin": false
88+
},
89+
"created_at": "2022-09-18T12: 38: 01Z",
90+
"updated_at": "2022-09-18T12: 38: 01Z",
91+
"author_association": "CONTRIBUTOR",
92+
"body": "@test1 @test2 @test3 good, @gitpoap-bot",
93+
"reactions": {
94+
"url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/comments/1250261829/reactions",
95+
"total_count": 0,
96+
"+1": 0,
97+
"-1": 0,
98+
"laugh": 0,
99+
"hooray": 0,
100+
"confused": 0,
101+
"heart": 0,
102+
"rocket": 0,
103+
"eyes": 0
104+
},
105+
"performed_via_github_app": null
106+
},
107+
"repository": {
108+
"id": 502133931,
109+
"node_id": "R_kgDOHe30qw",
110+
"name": "gitpoap-bot-test-repo",
111+
"full_name": "gitpoap/gitpoap-bot-test-repo",
112+
"private": false,
113+
"owner": {
114+
"login": "gitpoap",
115+
"id": 91488802,
116+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkxNDg4ODAy",
117+
"avatar_url": "https: //avatars.githubusercontent.com/u/91488802?v=4",
118+
"gravatar_id": "",
119+
"url": "https: //api.github.com/users/gitpoap",
120+
"html_url": "https: //github.com/gitpoap",
121+
"followers_url": "https: //api.github.com/users/gitpoap/followers",
122+
"following_url": "https: //api.github.com/users/gitpoap/following{/other_user}",
123+
"gists_url": "https: //api.github.com/users/gitpoap/gists{/gist_id}",
124+
"starred_url": "https: //api.github.com/users/gitpoap/starred{/owner}{/repo}",
125+
"subscriptions_url": "https: //api.github.com/users/gitpoap/subscriptions",
126+
"organizations_url": "https: //api.github.com/users/gitpoap/orgs",
127+
"repos_url": "https: //api.github.com/users/gitpoap/repos",
128+
"events_url": "https: //api.github.com/users/gitpoap/events{/privacy}",
129+
"received_events_url": "https: //api.github.com/users/gitpoap/received_events",
130+
"type": "Organization",
131+
"site_admin": false
132+
},
133+
"html_url": "https: //github.com/gitpoap/gitpoap-bot-test-repo",
134+
"description": "Repo to test whether gitpoap-bot is properly working",
135+
"fork": false,
136+
"url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo",
137+
"forks_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/forks",
138+
"keys_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/keys{/key_id}",
139+
"collaborators_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/collaborators{/collaborator}",
140+
"teams_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/teams",
141+
"hooks_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/hooks",
142+
"issue_events_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/events{/number}",
143+
"events_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/events",
144+
"assignees_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/assignees{/user}",
145+
"branches_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/branches{/branch}",
146+
"tags_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/tags",
147+
"blobs_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/git/blobs{/sha}",
148+
"git_tags_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/git/tags{/sha}",
149+
"git_refs_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/git/refs{/sha}",
150+
"trees_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/git/trees{/sha}",
151+
"statuses_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/statuses/{sha}",
152+
"languages_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/languages",
153+
"stargazers_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/stargazers",
154+
"contributors_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/contributors",
155+
"subscribers_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/subscribers",
156+
"subscription_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/subscription",
157+
"commits_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/commits{/sha}",
158+
"git_commits_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/git/commits{/sha}",
159+
"comments_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/comments{/number}",
160+
"issue_comment_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues/comments{/number}",
161+
"contents_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/contents/{+path}",
162+
"compare_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/compare/{base}...{head}",
163+
"merges_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/merges",
164+
"archive_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/{archive_format}{/ref}",
165+
"downloads_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/downloads",
166+
"issues_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/issues{/number}",
167+
"pulls_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/pulls{/number}",
168+
"milestones_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/milestones{/number}",
169+
"notifications_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/notifications{?since,all,participating}",
170+
"labels_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/labels{/name}",
171+
"releases_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/releases{/id}",
172+
"deployments_url": "https: //api.github.com/repos/gitpoap/gitpoap-bot-test-repo/deployments",
173+
"created_at": "2022-06-10T17: 52: 43Z",
174+
"updated_at": "2022-09-01T17: 37: 41Z",
175+
"pushed_at": "2022-09-16T11: 19: 21Z",
176+
"git_url": "git: //github.com/gitpoap/gitpoap-bot-test-repo.git",
177+
"ssh_url": "git@github.com:gitpoap/gitpoap-bot-test-repo.git",
178+
"clone_url": "https: //github.com/gitpoap/gitpoap-bot-test-repo.git",
179+
"svn_url": "https: //github.com/gitpoap/gitpoap-bot-test-repo",
180+
"homepage": null,
181+
"size": 3,
182+
"stargazers_count": 1,
183+
"watchers_count": 1,
184+
"language": null,
185+
"has_issues": true,
186+
"has_projects": true,
187+
"has_downloads": true,
188+
"has_wiki": true,
189+
"has_pages": false,
190+
"forks_count": 1,
191+
"mirror_url": null,
192+
"archived": false,
193+
"disabled": false,
194+
"open_issues_count": 3,
195+
"license": null,
196+
"allow_forking": true,
197+
"is_template": false,
198+
"web_commit_signoff_required": false,
199+
"topics": [],
200+
"visibility": "public",
201+
"forks": 1,
202+
"open_issues": 3,
203+
"watchers": 1,
204+
"default_branch": "main"
205+
},
206+
"sender": {
207+
"login": "gitpoap",
208+
"id": 91488802,
209+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkxNDg4ODAy",
210+
"avatar_url": "https: //avatars.githubusercontent.com/u/91488802?v=4",
211+
"gravatar_id": "",
212+
"url": "https: //api.github.com/users/gitpoap",
213+
"html_url": "https: //github.com/gitpoap",
214+
"followers_url": "https: //api.github.com/users/gitpoap/followers",
215+
"following_url": "https: //api.github.com/users/gitpoap/following{/other_user}",
216+
"gists_url": "https: //api.github.com/users/gitpoap/gists{/gist_id}",
217+
"starred_url": "https: //api.github.com/users/gitpoap/starred{/owner}{/repo}",
218+
"subscriptions_url": "https: //api.github.com/users/gitpoap/subscriptions",
219+
"organizations_url": "https: //api.github.com/users/gitpoap/orgs",
220+
"repos_url": "https: //api.github.com/users/gitpoap/repos",
221+
"events_url": "https: //api.github.com/users/gitpoap/events{/privacy}",
222+
"received_events_url": "https: //api.github.com/users/gitpoap/received_events",
223+
"type": "Organization",
224+
"site_admin": false
225+
},
226+
"installation": {
227+
"id": 29153052,
228+
"node_id": "MDIzOkludGVncmF0aW9uSW5zdGFsbGF0aW9uMjkxNTMwNTI="
229+
}
230+
}

test/fixtures/pull_request.closed.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,8 +451,22 @@
451451
"type": "User",
452452
"site_admin": false
453453
},
454+
"organization": {
455+
"login": "gitpoap",
456+
"id": 91488802,
457+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkxNDg4ODAy",
458+
"url": "https: //api.github.com/orgs/gitpoap",
459+
"repos_url": "https: //api.github.com/orgs/gitpoap/repos",
460+
"events_url": "https: //api.github.com/orgs/gitpoap/events",
461+
"hooks_url": "https: //api.github.com/orgs/gitpoap/hooks",
462+
"issues_url": "https: //api.github.com/orgs/gitpoap/issues",
463+
"members_url": "https: //api.github.com/orgs/gitpoap/members{/member}",
464+
"public_members_url": "https: //api.github.com/orgs/gitpoap/public_members{/member}",
465+
"avatar_url": "https: //avatars.githubusercontent.com/u/91488802?v=4",
466+
"description": ""
467+
},
454468
"installation": {
455469
"id": 29153052,
456470
"node_id": "MDIzOkludGVncmF0aW9uSW5zdGFsbGF0aW9uMjkxNTMwNTI="
457471
}
458-
}
472+
}

test/fixtures/pull_request.closed_non_merged.json

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,20 @@
337337
"deletions": 1,
338338
"changed_files": 1
339339
},
340+
"organization": {
341+
"login": "gitpoap",
342+
"id": 91488802,
343+
"node_id": "MDEyOk9yZ2FuaXphdGlvbjkxNDg4ODAy",
344+
"url": "https: //api.github.com/orgs/gitpoap",
345+
"repos_url": "https: //api.github.com/orgs/gitpoap/repos",
346+
"events_url": "https: //api.github.com/orgs/gitpoap/events",
347+
"hooks_url": "https: //api.github.com/orgs/gitpoap/hooks",
348+
"issues_url": "https: //api.github.com/orgs/gitpoap/issues",
349+
"members_url": "https: //api.github.com/orgs/gitpoap/members{/member}",
350+
"public_members_url": "https: //api.github.com/orgs/gitpoap/public_members{/member}",
351+
"avatar_url": "https: //avatars.githubusercontent.com/u/91488802?v=4",
352+
"description": ""
353+
},
340354
"repository": {
341355
"id": 186853002,
342356
"node_id": "MDEwOlJlcG9zaXRvcnkxODY4NTMwMDI=",
@@ -455,4 +469,4 @@
455469
"id": 29153052,
456470
"node_id": "MDIzOkludGVncmF0aW9uSW5zdGFsbGF0aW9uMjkxNTMwNTI="
457471
}
458-
}
472+
}

0 commit comments

Comments
 (0)