UNPKG

@dxdeveloperexperience/hygie

Version:

Hygie is an easy-to-use Open-Source REST API allowing you to interact with GIT events. This NestJS API expose a set of customizable rules to automate your project's life cycle.

1,183 lines (1,156 loc) 251 kB
import * as utils from './utils.enum'; import { CommitStatusEnum, GitTypeEnum, GitEventEnum } from './utils.enum'; import { IssuePRStateEnum } from '../git/gitIssueInfos'; import { GitlabService } from '../gitlab/gitlab.service'; import { GithubService } from '../github/github.service'; import { HttpService } from '@nestjs/common'; import { Webhook } from './webhook'; // INIT const gitlabService: GitlabService = new GitlabService(new HttpService()); const githubService: GithubService = new GithubService(new HttpService()); let webhook = new Webhook(gitlabService, githubService); describe('Utils Enum', () => { describe('convertCommitStatus', () => { it('should equal "failure"', () => { const res: string = utils.convertCommitStatus( utils.GitTypeEnum.Github, CommitStatusEnum.Failure, ); expect(res).toBe('failure'); }); it('should equal "failed"', () => { const res: string = utils.convertCommitStatus( utils.GitTypeEnum.Gitlab, CommitStatusEnum.Failure, ); expect(res).toBe('failed'); }); it('should equal "success"', () => { const res: string = utils.convertCommitStatus( utils.GitTypeEnum.Gitlab, CommitStatusEnum.Success, ); expect(res).toBe('success'); }); it('should equal "success"', () => { const res: string = utils.convertCommitStatus( utils.GitTypeEnum.Gitlab, CommitStatusEnum.Success, ); expect(res).toBe('success'); }); }); describe('convertIssueState', () => { it('should equal "open"', () => { const res: string = utils.convertIssueState( utils.GitTypeEnum.Github, IssuePRStateEnum.Open, ); expect(res).toBe('open'); }); it('should equal "closed"', () => { const res: string = utils.convertIssueState( utils.GitTypeEnum.Github, IssuePRStateEnum.Close, ); expect(res).toBe('closed'); }); it('should equal "reopen"', () => { const res: string = utils.convertIssueState( utils.GitTypeEnum.Gitlab, IssuePRStateEnum.Open, ); expect(res).toBe('reopen'); }); it('should equal "close"', () => { const res: string = utils.convertIssueState( utils.GitTypeEnum.Gitlab, IssuePRStateEnum.Close, ); expect(res).toBe('close'); }); }); describe('isPushEvent', () => { const gitlabPushEvent = { object_kind: 'push', event_name: 'push', before: '518f9aad2cac09a78157198fcc3f88cf0cc79be3', after: 'a3b2abdc7815a4cfa8998e2b91aa74ff1736ce97', ref: 'refs/heads/master', checkout_sha: 'a3b2abdc7815a4cfa8998e2b91aa74ff1736ce97', message: null, user_id: 3360534, user_name: 'Bastien Terrier', user_username: 'bastien.terrier', user_email: '', user_avatar: 'https://secure.gravatar.com/avatar/a17bda7df8612108bc4442ce57cc6dc3?s=80&d=identicon', project_id: 10607595, project: { id: 10607595, name: 'test_webhook', description: '', web_url: 'https://gitlab.com/bastien.terrier/test_webhook', avatar_url: null, git_ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.git', git_http_url: 'https://gitlab.com/bastien.terrier/test_webhook.git', namespace: 'bastien.terrier', visibility_level: 0, path_with_namespace: 'bastien.terrier/test_webhook', default_branch: 'master', ci_config_path: null, homepage: 'https://gitlab.com/bastien.terrier/test_webhook', url: 'git@gitlab.com:bastien.terrier/test_webhook.git', ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.git', http_url: 'https://gitlab.com/bastien.terrier/test_webhook.git', }, commits: [ { id: 'a3b2abdc7815a4cfa8998e2b91aa74ff1736ce97', message: 'Update README.md', timestamp: '2019-03-14T08:27:14Z', url: 'https://gitlab.com/bastien.terrier/test_webhook/commit/a3b2abdc7815a4cfa8998e2b91aa74ff1736ce97', author: { name: 'Bastien Terrier', email: 'bastien.terrier@gmail.com', }, added: [], modified: ['README.md'], removed: [], }, ], total_commits_count: 1, push_options: [], repository: { name: 'test_webhook', url: 'git@gitlab.com:bastien.terrier/test_webhook.git', description: '', homepage: 'https://gitlab.com/bastien.terrier/test_webhook', git_http_url: 'https://gitlab.com/bastien.terrier/test_webhook.git', git_ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.git', visibility_level: 0, }, }; const githubPushEvent = { ref: 'refs/heads/test4', before: 'a302f5fcc3f67ff85b1bfd31a703df04b9090ff4', after: 'f1e7b7b0eff0ac9b582a248e42534bffea0e3885', created: false, deleted: false, forced: false, base_ref: null, compare: 'https://github.com/bastienterrier/test-webhook/compare/a302f5fcc3f6...f1e7b7b0eff0', commits: [ { id: 'f1e7b7b0eff0ac9b582a248e42534bffea0e3885', tree_id: 'a753c249b5f65116cd17f98aee9bea5ac2661844', distinct: true, message: 'Update README.md', timestamp: '2019-03-14T09:37:07+01:00', url: 'https://github.com/bastienterrier/test-webhook/commit/f1e7b7b0eff0ac9b582a248e42534bffea0e3885', author: { name: 'Bastien TERRIER', email: 'bastien.terrier@gmail.com', username: 'bastienterrier', }, committer: { name: 'GitHub', email: 'noreply@github.com', username: 'web-flow', }, added: [], removed: [], modified: ['README.md'], }, ], head_commit: { id: 'f1e7b7b0eff0ac9b582a248e42534bffea0e3885', tree_id: 'a753c249b5f65116cd17f98aee9bea5ac2661844', distinct: true, message: 'Update README.md', timestamp: '2019-03-14T09:37:07+01:00', url: 'https://github.com/bastienterrier/test-webhook/commit/f1e7b7b0eff0ac9b582a248e42534bffea0e3885', author: { name: 'Bastien TERRIER', email: 'bastien.terrier@gmail.com', username: 'bastienterrier', }, committer: { name: 'GitHub', email: 'noreply@github.com', username: 'web-flow', }, added: [], removed: [], modified: ['README.md'], }, repository: { id: 169373600, node_id: 'MDEwOlJlcG9zaXRvcnkxNjkzNzM2MDA=', name: 'test-webhook', full_name: 'bastienterrier/test-webhook', private: false, owner: { name: 'bastienterrier', email: 'bastien.terrier@gmail.com', login: 'bastienterrier', id: 25296454, node_id: 'MDQ6VXNlcjI1Mjk2NDU0', avatar_url: 'https://avatars0.githubusercontent.com/u/25296454?v=4', gravatar_id: '', url: 'https://api.github.com/users/bastienterrier', html_url: 'https://github.com/bastienterrier', followers_url: 'https://api.github.com/users/bastienterrier/followers', following_url: 'https://api.github.com/users/bastienterrier/following{/other_user}', gists_url: 'https://api.github.com/users/bastienterrier/gists{/gist_id}', starred_url: 'https://api.github.com/users/bastienterrier/starred{/owner}{/repo}', subscriptions_url: 'https://api.github.com/users/bastienterrier/subscriptions', organizations_url: 'https://api.github.com/users/bastienterrier/orgs', repos_url: 'https://api.github.com/users/bastienterrier/repos', events_url: 'https://api.github.com/users/bastienterrier/events{/privacy}', received_events_url: 'https://api.github.com/users/bastienterrier/received_events', type: 'User', site_admin: false, }, html_url: 'https://github.com/bastienterrier/test-webhook', description: null, fork: false, url: 'https://github.com/bastienterrier/test-webhook', forks_url: 'https://api.github.com/repos/bastienterrier/test-webhook/forks', keys_url: 'https://api.github.com/repos/bastienterrier/test-webhook/keys{/key_id}', collaborators_url: 'https://api.github.com/repos/bastienterrier/test-webhook/collaborators{/collaborator}', teams_url: 'https://api.github.com/repos/bastienterrier/test-webhook/teams', hooks_url: 'https://api.github.com/repos/bastienterrier/test-webhook/hooks', issue_events_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/events{/number}', events_url: 'https://api.github.com/repos/bastienterrier/test-webhook/events', assignees_url: 'https://api.github.com/repos/bastienterrier/test-webhook/assignees{/user}', branches_url: 'https://api.github.com/repos/bastienterrier/test-webhook/branches{/branch}', tags_url: 'https://api.github.com/repos/bastienterrier/test-webhook/tags', blobs_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/blobs{/sha}', git_tags_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/tags{/sha}', git_refs_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/refs{/sha}', trees_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/trees{/sha}', statuses_url: 'https://api.github.com/repos/bastienterrier/test-webhook/statuses/{sha}', languages_url: 'https://api.github.com/repos/bastienterrier/test-webhook/languages', stargazers_url: 'https://api.github.com/repos/bastienterrier/test-webhook/stargazers', contributors_url: 'https://api.github.com/repos/bastienterrier/test-webhook/contributors', subscribers_url: 'https://api.github.com/repos/bastienterrier/test-webhook/subscribers', subscription_url: 'https://api.github.com/repos/bastienterrier/test-webhook/subscription', commits_url: 'https://api.github.com/repos/bastienterrier/test-webhook/commits{/sha}', git_commits_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/commits{/sha}', comments_url: 'https://api.github.com/repos/bastienterrier/test-webhook/comments{/number}', issue_comment_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/comments{/number}', contents_url: 'https://api.github.com/repos/bastienterrier/test-webhook/contents/{+path}', compare_url: 'https://api.github.com/repos/bastienterrier/test-webhook/compare/{base}...{head}', merges_url: 'https://api.github.com/repos/bastienterrier/test-webhook/merges', archive_url: 'https://api.github.com/repos/bastienterrier/test-webhook/{archive_format}{/ref}', downloads_url: 'https://api.github.com/repos/bastienterrier/test-webhook/downloads', issues_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues{/number}', pulls_url: 'https://api.github.com/repos/bastienterrier/test-webhook/pulls{/number}', milestones_url: 'https://api.github.com/repos/bastienterrier/test-webhook/milestones{/number}', notifications_url: 'https://api.github.com/repos/bastienterrier/test-webhook/notifications{?since,all,participating}', labels_url: 'https://api.github.com/repos/bastienterrier/test-webhook/labels{/name}', releases_url: 'https://api.github.com/repos/bastienterrier/test-webhook/releases{/id}', deployments_url: 'https://api.github.com/repos/bastienterrier/test-webhook/deployments', created_at: 1549440848, updated_at: '2019-02-25T13:02:50Z', pushed_at: 1552552627, git_url: 'git://github.com/bastienterrier/test-webhook.git', ssh_url: 'git@github.com:bastienterrier/test-webhook.git', clone_url: 'https://github.com/bastienterrier/test-webhook.git', svn_url: 'https://github.com/bastienterrier/test-webhook', homepage: null, size: 25427, stargazers_count: 0, watchers_count: 0, language: 'Shell', has_issues: true, has_projects: true, has_downloads: true, has_wiki: true, has_pages: true, forks_count: 0, mirror_url: null, archived: false, open_issues_count: 8, license: null, forks: 0, open_issues: 8, watchers: 0, default_branch: 'master', stargazers: 0, master_branch: 'master', }, pusher: { name: 'bastienterrier', email: 'bastien.terrier@gmail.com', }, sender: { login: 'bastienterrier', id: 25296454, node_id: 'MDQ6VXNlcjI1Mjk2NDU0', avatar_url: 'https://avatars0.githubusercontent.com/u/25296454?v=4', gravatar_id: '', url: 'https://api.github.com/users/bastienterrier', html_url: 'https://github.com/bastienterrier', followers_url: 'https://api.github.com/users/bastienterrier/followers', following_url: 'https://api.github.com/users/bastienterrier/following{/other_user}', gists_url: 'https://api.github.com/users/bastienterrier/gists{/gist_id}', starred_url: 'https://api.github.com/users/bastienterrier/starred{/owner}{/repo}', subscriptions_url: 'https://api.github.com/users/bastienterrier/subscriptions', organizations_url: 'https://api.github.com/users/bastienterrier/orgs', repos_url: 'https://api.github.com/users/bastienterrier/repos', events_url: 'https://api.github.com/users/bastienterrier/events{/privacy}', received_events_url: 'https://api.github.com/users/bastienterrier/received_events', type: 'User', site_admin: false, }, }; it('isGitlabPushEvent should equal "true"', () => { expect(utils.isGitlabPushEvent(gitlabPushEvent)).toBe(true); }); it('isGitlabPushEvent should equal "false"', () => { expect(utils.isGitlabPushEvent(githubPushEvent)).toBe(false); }); it('isGithubPushEvent should equal "true"', () => { expect(utils.isGithubPushEvent(githubPushEvent)).toBe(true); }); it('isGithubPushEvent should equal "false"', () => { expect(utils.isGithubPushEvent(gitlabPushEvent)).toBe(false); }); it('should create a Webhook object according to the Gitlab Push Hook', () => { webhook = new Webhook(gitlabService, githubService); webhook.gitToWebhook(gitlabPushEvent); expect(webhook.gitType).toBe(GitTypeEnum.Gitlab); expect(webhook.gitEvent).toBe(GitEventEnum.Push); expect(webhook.projectId).toBe(10607595); expect(webhook.gitService).toBe(gitlabService); expect(webhook.commits).toEqual([ { added: [], message: 'Update README.md', modified: ['README.md'], removed: [], sha: 'a3b2abdc7815a4cfa8998e2b91aa74ff1736ce97', }, ]); expect(webhook.branchName).toBe('master'); expect(webhook.repository.cloneURL).toBe( 'https://gitlab.com/bastien.terrier/test_webhook.git', ); expect(webhook.user.login).toBe('bastien.terrier'); }); it('should create a Webhook object according to the Github Push Hook', () => { webhook = new Webhook(gitlabService, githubService); webhook.gitToWebhook(githubPushEvent); expect(webhook.gitType).toBe(GitTypeEnum.Github); expect(webhook.gitEvent).toBe(GitEventEnum.Push); expect(webhook.repository.fullName).toBe('bastienterrier/test-webhook'); expect(webhook.gitService).toBe(githubService); expect(webhook.commits).toEqual([ { sha: 'f1e7b7b0eff0ac9b582a248e42534bffea0e3885', message: 'Update README.md', added: [], modified: ['README.md'], removed: [], }, ]); expect(webhook.branchName).toBe('test4'); expect(webhook.repository.cloneURL).toBe( 'https://github.com/bastienterrier/test-webhook.git', ); expect(webhook.user.login).toBe('bastienterrier'); }); }); describe('isNewBranchEvent', () => { const gitlabNewBranchEvent = { object_kind: 'push', event_name: 'push', before: '0000000000000000000000000000000000000000', after: 'a3b2abdc7815a4cfa8998e2b91aa74ff1736ce97', ref: 'refs/heads/feature/tdd', checkout_sha: 'a3b2abdc7815a4cfa8998e2b91aa74ff1736ce97', message: null, user_id: 3360534, user_name: 'Bastien Terrier', user_username: 'bastien.terrier', user_email: '', user_avatar: 'https://secure.gravatar.com/avatar/a17bda7df8612108bc4442ce57cc6dc3?s=80&d=identicon', project_id: 10607595, project: { id: 10607595, name: 'test_webhook', description: '', web_url: 'https://gitlab.com/bastien.terrier/test_webhook', avatar_url: null, git_ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.git', git_http_url: 'https://gitlab.com/bastien.terrier/test_webhook.git', namespace: 'bastien.terrier', visibility_level: 0, path_with_namespace: 'bastien.terrier/test_webhook', default_branch: 'master', ci_config_path: null, homepage: 'https://gitlab.com/bastien.terrier/test_webhook', url: 'git@gitlab.com:bastien.terrier/test_webhook.git', ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.git', http_url: 'https://gitlab.com/bastien.terrier/test_webhook.git', }, commits: [], total_commits_count: 0, push_options: [], repository: { name: 'test_webhook', url: 'git@gitlab.com:bastien.terrier/test_webhook.git', description: '', homepage: 'https://gitlab.com/bastien.terrier/test_webhook', git_http_url: 'https://gitlab.com/bastien.terrier/test_webhook.git', git_ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.git', visibility_level: 0, }, }; const githubNewBranchEvent = { ref: 'feat/tdd', ref_type: 'branch', master_branch: 'master', description: null, pusher_type: 'user', repository: { id: 169373600, node_id: 'MDEwOlJlcG9zaXRvcnkxNjkzNzM2MDA=', name: 'test-webhook', full_name: 'bastienterrier/test-webhook', private: false, owner: { login: 'bastienterrier', id: 25296454, node_id: 'MDQ6VXNlcjI1Mjk2NDU0', avatar_url: 'https://avatars0.githubusercontent.com/u/25296454?v=4', gravatar_id: '', url: 'https://api.github.com/users/bastienterrier', html_url: 'https://github.com/bastienterrier', followers_url: 'https://api.github.com/users/bastienterrier/followers', following_url: 'https://api.github.com/users/bastienterrier/following{/other_user}', gists_url: 'https://api.github.com/users/bastienterrier/gists{/gist_id}', starred_url: 'https://api.github.com/users/bastienterrier/starred{/owner}{/repo}', subscriptions_url: 'https://api.github.com/users/bastienterrier/subscriptions', organizations_url: 'https://api.github.com/users/bastienterrier/orgs', repos_url: 'https://api.github.com/users/bastienterrier/repos', events_url: 'https://api.github.com/users/bastienterrier/events{/privacy}', received_events_url: 'https://api.github.com/users/bastienterrier/received_events', type: 'User', site_admin: false, }, html_url: 'https://github.com/bastienterrier/test-webhook', description: null, fork: false, url: 'https://api.github.com/repos/bastienterrier/test-webhook', forks_url: 'https://api.github.com/repos/bastienterrier/test-webhook/forks', keys_url: 'https://api.github.com/repos/bastienterrier/test-webhook/keys{/key_id}', collaborators_url: 'https://api.github.com/repos/bastienterrier/test-webhook/collaborators{/collaborator}', teams_url: 'https://api.github.com/repos/bastienterrier/test-webhook/teams', hooks_url: 'https://api.github.com/repos/bastienterrier/test-webhook/hooks', issue_events_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/events{/number}', events_url: 'https://api.github.com/repos/bastienterrier/test-webhook/events', assignees_url: 'https://api.github.com/repos/bastienterrier/test-webhook/assignees{/user}', branches_url: 'https://api.github.com/repos/bastienterrier/test-webhook/branches{/branch}', tags_url: 'https://api.github.com/repos/bastienterrier/test-webhook/tags', blobs_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/blobs{/sha}', git_tags_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/tags{/sha}', git_refs_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/refs{/sha}', trees_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/trees{/sha}', statuses_url: 'https://api.github.com/repos/bastienterrier/test-webhook/statuses/{sha}', languages_url: 'https://api.github.com/repos/bastienterrier/test-webhook/languages', stargazers_url: 'https://api.github.com/repos/bastienterrier/test-webhook/stargazers', contributors_url: 'https://api.github.com/repos/bastienterrier/test-webhook/contributors', subscribers_url: 'https://api.github.com/repos/bastienterrier/test-webhook/subscribers', subscription_url: 'https://api.github.com/repos/bastienterrier/test-webhook/subscription', commits_url: 'https://api.github.com/repos/bastienterrier/test-webhook/commits{/sha}', git_commits_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/commits{/sha}', comments_url: 'https://api.github.com/repos/bastienterrier/test-webhook/comments{/number}', issue_comment_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/comments{/number}', contents_url: 'https://api.github.com/repos/bastienterrier/test-webhook/contents/{+path}', compare_url: 'https://api.github.com/repos/bastienterrier/test-webhook/compare/{base}...{head}', merges_url: 'https://api.github.com/repos/bastienterrier/test-webhook/merges', archive_url: 'https://api.github.com/repos/bastienterrier/test-webhook/{archive_format}{/ref}', downloads_url: 'https://api.github.com/repos/bastienterrier/test-webhook/downloads', issues_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues{/number}', pulls_url: 'https://api.github.com/repos/bastienterrier/test-webhook/pulls{/number}', milestones_url: 'https://api.github.com/repos/bastienterrier/test-webhook/milestones{/number}', notifications_url: 'https://api.github.com/repos/bastienterrier/test-webhook/notifications{?since,all,participating}', labels_url: 'https://api.github.com/repos/bastienterrier/test-webhook/labels{/name}', releases_url: 'https://api.github.com/repos/bastienterrier/test-webhook/releases{/id}', deployments_url: 'https://api.github.com/repos/bastienterrier/test-webhook/deployments', created_at: '2019-02-06T08:14:08Z', updated_at: '2019-02-25T13:02:50Z', pushed_at: '2019-03-14T08:40:08Z', git_url: 'git://github.com/bastienterrier/test-webhook.git', ssh_url: 'git@github.com:bastienterrier/test-webhook.git', clone_url: 'https://github.com/bastienterrier/test-webhook.git', svn_url: 'https://github.com/bastienterrier/test-webhook', homepage: null, size: 25427, stargazers_count: 0, watchers_count: 0, language: 'Shell', has_issues: true, has_projects: true, has_downloads: true, has_wiki: true, has_pages: true, forks_count: 0, mirror_url: null, archived: false, open_issues_count: 8, license: null, forks: 0, open_issues: 8, watchers: 0, default_branch: 'master', }, sender: { login: 'bastienterrier', id: 25296454, node_id: 'MDQ6VXNlcjI1Mjk2NDU0', avatar_url: 'https://avatars0.githubusercontent.com/u/25296454?v=4', gravatar_id: '', url: 'https://api.github.com/users/bastienterrier', html_url: 'https://github.com/bastienterrier', followers_url: 'https://api.github.com/users/bastienterrier/followers', following_url: 'https://api.github.com/users/bastienterrier/following{/other_user}', gists_url: 'https://api.github.com/users/bastienterrier/gists{/gist_id}', starred_url: 'https://api.github.com/users/bastienterrier/starred{/owner}{/repo}', subscriptions_url: 'https://api.github.com/users/bastienterrier/subscriptions', organizations_url: 'https://api.github.com/users/bastienterrier/orgs', repos_url: 'https://api.github.com/users/bastienterrier/repos', events_url: 'https://api.github.com/users/bastienterrier/events{/privacy}', received_events_url: 'https://api.github.com/users/bastienterrier/received_events', type: 'User', site_admin: false, }, }; it('isGitlabBranchEvent should equal "true"', () => { expect(utils.isGitlabNewBranchEvent(gitlabNewBranchEvent)).toBe(true); }); it('isGitlabBranchEvent should equal "false"', () => { expect(utils.isGitlabNewBranchEvent(githubNewBranchEvent)).toBe(false); }); it('isGithubNewBranchEvent should equal "true"', () => { expect(utils.isGithubNewBranchEvent(githubNewBranchEvent)).toBe(true); }); it('isGithubNewBranchEvent should equal "false"', () => { expect(utils.isGithubNewBranchEvent(gitlabNewBranchEvent)).toBe(false); }); it('should create a Webhook object according to the Gitlab Branch Hook', () => { webhook = new Webhook(gitlabService, githubService); webhook.gitToWebhook(gitlabNewBranchEvent); expect(webhook.gitType).toBe(GitTypeEnum.Gitlab); expect(webhook.gitEvent).toBe(GitEventEnum.NewBranch); expect(webhook.projectId).toBe(10607595); expect(webhook.gitService).toBe(gitlabService); expect(webhook.branchName).toBe('feature/tdd'); expect(webhook.repository.cloneURL).toBe( 'https://gitlab.com/bastien.terrier/test_webhook.git', ); expect(webhook.user.login).toBe('bastien.terrier'); }); it('should create a Webhook object according to the Github Branch Hook', () => { webhook = new Webhook(gitlabService, githubService); webhook.gitToWebhook(githubNewBranchEvent); expect(webhook.gitType).toBe(GitTypeEnum.Github); expect(webhook.gitEvent).toBe(GitEventEnum.NewBranch); expect(webhook.gitService).toBe(githubService); expect(webhook.branchName).toBe('feat/tdd'); expect(webhook.repository.fullName).toBe('bastienterrier/test-webhook'); expect(webhook.repository.cloneURL).toBe( 'https://github.com/bastienterrier/test-webhook.git', ); expect(webhook.user.login).toBe('bastienterrier'); }); }); describe('isDeletedBranchEvent', () => { const githubDeletedBranchEvent = { ref: 'feature/titi', ref_type: 'branch', pusher_type: 'user', repository: { id: 169373600, node_id: 'MDEwOlJlcG9zaXRvcnkxNjkzNzM2MDA=', name: 'test-webhook', full_name: 'bastienterrier/test-webhook', private: false, owner: { login: 'bastienterrier', id: 25296454, node_id: 'MDQ6VXNlcjI1Mjk2NDU0', avatar_url: 'https://avatars0.githubusercontent.com/u/25296454?v=4', gravatar_id: '', url: 'https://api.github.com/users/bastienterrier', html_url: 'https://github.com/bastienterrier', followers_url: 'https://api.github.com/users/bastienterrier/followers', following_url: 'https://api.github.com/users/bastienterrier/following{/other_user}', gists_url: 'https://api.github.com/users/bastienterrier/gists{/gist_id}', starred_url: 'https://api.github.com/users/bastienterrier/starred{/owner}{/repo}', subscriptions_url: 'https://api.github.com/users/bastienterrier/subscriptions', organizations_url: 'https://api.github.com/users/bastienterrier/orgs', repos_url: 'https://api.github.com/users/bastienterrier/repos', events_url: 'https://api.github.com/users/bastienterrier/events{/privacy}', received_events_url: 'https://api.github.com/users/bastienterrier/received_events', type: 'User', site_admin: false, }, html_url: 'https://github.com/bastienterrier/test-webhook', description: null, fork: false, url: 'https://api.github.com/repos/bastienterrier/test-webhook', forks_url: 'https://api.github.com/repos/bastienterrier/test-webhook/forks', keys_url: 'https://api.github.com/repos/bastienterrier/test-webhook/keys{/key_id}', collaborators_url: 'https://api.github.com/repos/bastienterrier/test-webhook/collaborators{/collaborator}', teams_url: 'https://api.github.com/repos/bastienterrier/test-webhook/teams', hooks_url: 'https://api.github.com/repos/bastienterrier/test-webhook/hooks', issue_events_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/events{/number}', events_url: 'https://api.github.com/repos/bastienterrier/test-webhook/events', assignees_url: 'https://api.github.com/repos/bastienterrier/test-webhook/assignees{/user}', branches_url: 'https://api.github.com/repos/bastienterrier/test-webhook/branches{/branch}', tags_url: 'https://api.github.com/repos/bastienterrier/test-webhook/tags', blobs_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/blobs{/sha}', git_tags_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/tags{/sha}', git_refs_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/refs{/sha}', trees_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/trees{/sha}', statuses_url: 'https://api.github.com/repos/bastienterrier/test-webhook/statuses/{sha}', languages_url: 'https://api.github.com/repos/bastienterrier/test-webhook/languages', stargazers_url: 'https://api.github.com/repos/bastienterrier/test-webhook/stargazers', contributors_url: 'https://api.github.com/repos/bastienterrier/test-webhook/contributors', subscribers_url: 'https://api.github.com/repos/bastienterrier/test-webhook/subscribers', subscription_url: 'https://api.github.com/repos/bastienterrier/test-webhook/subscription', commits_url: 'https://api.github.com/repos/bastienterrier/test-webhook/commits{/sha}', git_commits_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/commits{/sha}', comments_url: 'https://api.github.com/repos/bastienterrier/test-webhook/comments{/number}', issue_comment_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/comments{/number}', contents_url: 'https://api.github.com/repos/bastienterrier/test-webhook/contents/{+path}', compare_url: 'https://api.github.com/repos/bastienterrier/test-webhook/compare/{base}...{head}', merges_url: 'https://api.github.com/repos/bastienterrier/test-webhook/merges', archive_url: 'https://api.github.com/repos/bastienterrier/test-webhook/{archive_format}{/ref}', downloads_url: 'https://api.github.com/repos/bastienterrier/test-webhook/downloads', issues_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues{/number}', pulls_url: 'https://api.github.com/repos/bastienterrier/test-webhook/pulls{/number}', milestones_url: 'https://api.github.com/repos/bastienterrier/test-webhook/milestones{/number}', notifications_url: 'https://api.github.com/repos/bastienterrier/test-webhook/notifications{?since,all,participating}', labels_url: 'https://api.github.com/repos/bastienterrier/test-webhook/labels{/name}', releases_url: 'https://api.github.com/repos/bastienterrier/test-webhook/releases{/id}', deployments_url: 'https://api.github.com/repos/bastienterrier/test-webhook/deployments', created_at: '2019-02-06T08:14:08Z', updated_at: '2019-06-18T09:06:29Z', pushed_at: '2019-06-18T10:01:13Z', git_url: 'git://github.com/bastienterrier/test-webhook.git', ssh_url: 'git@github.com:bastienterrier/test-webhook.git', clone_url: 'https://github.com/bastienterrier/test-webhook.git', svn_url: 'https://github.com/bastienterrier/test-webhook', homepage: null, size: 25625, stargazers_count: 0, watchers_count: 0, language: 'Shell', has_issues: true, has_projects: true, has_downloads: true, has_wiki: true, has_pages: true, forks_count: 0, mirror_url: null, archived: false, disabled: false, open_issues_count: 53, license: null, forks: 0, open_issues: 53, watchers: 0, default_branch: 'master', }, sender: { login: 'bastienterrier', id: 25296454, node_id: 'MDQ6VXNlcjI1Mjk2NDU0', avatar_url: 'https://avatars0.githubusercontent.com/u/25296454?v=4', gravatar_id: '', url: 'https://api.github.com/users/bastienterrier', html_url: 'https://github.com/bastienterrier', followers_url: 'https://api.github.com/users/bastienterrier/followers', following_url: 'https://api.github.com/users/bastienterrier/following{/other_user}', gists_url: 'https://api.github.com/users/bastienterrier/gists{/gist_id}', starred_url: 'https://api.github.com/users/bastienterrier/starred{/owner}{/repo}', subscriptions_url: 'https://api.github.com/users/bastienterrier/subscriptions', organizations_url: 'https://api.github.com/users/bastienterrier/orgs', repos_url: 'https://api.github.com/users/bastienterrier/repos', events_url: 'https://api.github.com/users/bastienterrier/events{/privacy}', received_events_url: 'https://api.github.com/users/bastienterrier/received_events', type: 'User', site_admin: false, }, }; const gitlabDeletedBranchEvent = { object_kind: 'push', event_name: 'push', before: '14f8a81413fc6021fef076c86297c0bc9e4e1041', after: '0000000000000000000000000000000000000000', ref: 'refs/heads/some/feature', checkout_sha: null, message: null, user_id: 3360534, user_name: 'Bastien Terrier', user_username: 'bastien.terrier', user_email: '', user_avatar: 'https://secure.gravatar.com/avatar/a17bda7df8612108bc4442ce57cc6dc3?s=80&d=identicon', project_id: 10607595, project: { id: 10607595, name: 'test_webhook', description: '', web_url: 'https://gitlab.com/bastien.terrier/test_webhook', avatar_url: null, git_ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.git', git_http_url: 'https://gitlab.com/bastien.terrier/test_webhook.git', namespace: 'bastien.terrier', visibility_level: 20, path_with_namespace: 'bastien.terrier/test_webhook', default_branch: 'master', ci_config_path: null, homepage: 'https://gitlab.com/bastien.terrier/test_webhook', url: 'git@gitlab.com:bastien.terrier/test_webhook.git', ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.git', http_url: 'https://gitlab.com/bastien.terrier/test_webhook.git', }, commits: [], total_commits_count: 0, push_options: {}, repository: { name: 'test_webhook', url: 'git@gitlab.com:bastien.terrier/test_webhook.git', description: '', homepage: 'https://gitlab.com/bastien.terrier/test_webhook', git_http_url: 'https://gitlab.com/bastien.terrier/test_webhook.git', git_ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.git', visibility_level: 20, }, }; it('isDeletedBranchEvent should equal "true"', () => { expect(utils.isGithubDeletedBranchEvent(githubDeletedBranchEvent)).toBe( true, ); }); it('isDeletedBranchEvent should equal "false"', () => { expect(utils.isGithubDeletedBranchEvent(gitlabDeletedBranchEvent)).toBe( false, ); }); it('isGitlabDeletedBranchEvent should equal "true"', () => { expect(utils.isGitlabDeletedBranchEvent(gitlabDeletedBranchEvent)).toBe( true, ); }); it('isGitlabDeletedBranchEvent should equal "false"', () => { expect(utils.isGitlabDeletedBranchEvent(githubDeletedBranchEvent)).toBe( false, ); }); }); describe('isIssueEvent', () => { const githubIssueEvent = { action: 'opened', issue: { url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/89', repository_url: 'https://api.github.com/repos/bastienterrier/test-webhook', labels_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/89/labels{/name}', comments_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/89/comments', events_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/89/events', html_url: 'https://github.com/bastienterrier/test-webhook/issues/89', id: 420902360, node_id: 'MDU6SXNzdWU0MjA5MDIzNjA=', number: 89, title: 'new issue', user: { login: 'bastienterrier', id: 25296454, node_id: 'MDQ6VXNlcjI1Mjk2NDU0', avatar_url: 'https://avatars0.githubusercontent.com/u/25296454?v=4', gravatar_id: '', url: 'https://api.github.com/users/bastienterrier', html_url: 'https://github.com/bastienterrier', followers_url: 'https://api.github.com/users/bastienterrier/followers', following_url: 'https://api.github.com/users/bastienterrier/following{/other_user}', gists_url: 'https://api.github.com/users/bastienterrier/gists{/gist_id}', starred_url: 'https://api.github.com/users/bastienterrier/starred{/owner}{/repo}', subscriptions_url: 'https://api.github.com/users/bastienterrier/subscriptions', organizations_url: 'https://api.github.com/users/bastienterrier/orgs', repos_url: 'https://api.github.com/users/bastienterrier/repos', events_url: 'https://api.github.com/users/bastienterrier/events{/privacy}', received_events_url: 'https://api.github.com/users/bastienterrier/received_events', type: 'User', site_admin: false, }, labels: [], state: 'open', locked: false, assignee: null, assignees: [], milestone: null, comments: 0, created_at: '2019-03-14T09:07:46Z', updated_at: '2019-03-14T09:07:46Z', closed_at: null, author_association: 'OWNER', body: '', }, repository: { id: 169373600, node_id: 'MDEwOlJlcG9zaXRvcnkxNjkzNzM2MDA=', name: 'test-webhook', full_name: 'bastienterrier/test-webhook', private: false, owner: { login: 'bastienterrier', id: 25296454, node_id: 'MDQ6VXNlcjI1Mjk2NDU0', avatar_url: 'https://avatars0.githubusercontent.com/u/25296454?v=4', gravatar_id: '', url: 'https://api.github.com/users/bastienterrier', html_url: 'https://github.com/bastienterrier', followers_url: 'https://api.github.com/users/bastienterrier/followers', following_url: 'https://api.github.com/users/bastienterrier/following{/other_user}', gists_url: 'https://api.github.com/users/bastienterrier/gists{/gist_id}', starred_url: 'https://api.github.com/users/bastienterrier/starred{/owner}{/repo}', subscriptions_url: 'https://api.github.com/users/bastienterrier/subscriptions', organizations_url: 'https://api.github.com/users/bastienterrier/orgs', repos_url: 'https://api.github.com/users/bastienterrier/repos', events_url: 'https://api.github.com/users/bastienterrier/events{/privacy}', received_events_url: 'https://api.github.com/users/bastienterrier/received_events', type: 'User', site_admin: false, }, html_url: 'https://github.com/bastienterrier/test-webhook', description: null, fork: false, url: 'https://api.github.com/repos/bastienterrier/test-webhook', forks_url: 'https://api.github.com/repos/bastienterrier/test-webhook/forks', keys_url: 'https://api.github.com/repos/bastienterrier/test-webhook/keys{/key_id}', collaborators_url: 'https://api.github.com/repos/bastienterrier/test-webhook/collaborators{/collaborator}', teams_url: 'https://api.github.com/repos/bastienterrier/test-webhook/teams', hooks_url: 'https://api.github.com/repos/bastienterrier/test-webhook/hooks', issue_events_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/events{/number}', events_url: 'https://api.github.com/repos/bastienterrier/test-webhook/events', assignees_url: 'https://api.github.com/repos/bastienterrier/test-webhook/assignees{/user}', branches_url: 'https://api.github.com/repos/bastienterrier/test-webhook/branches{/branch}', tags_url: 'https://api.github.com/repos/bastienterrier/test-webhook/tags', blobs_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/blobs{/sha}', git_tags_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/tags{/sha}', git_refs_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/refs{/sha}', trees_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/trees{/sha}', statuses_url: 'https://api.github.com/repos/bastienterrier/test-webhook/statuses/{sha}', languages_url: 'https://api.github.com/repos/bastienterrier/test-webhook/languages', stargazers_url: 'https://api.github.com/repos/bastienterrier/test-webhook/stargazers', contributors_url: 'https://api.github.com/repos/bastienterrier/test-webhook/contributors', subscribers_url: 'https://api.github.com/repos/bastienterrier/test-webhook/subscribers', subscription_url: 'https://api.github.com/repos/bastienterrier/test-webhook/subscription', commits_url: 'https://api.github.com/repos/bastienterrier/test-webhook/commits{/sha}', git_commits_url: 'https://api.github.com/repos/bastienterrier/test-webhook/git/commits{/sha}', comments_url: 'https://api.github.com/repos/bastienterrier/test-webhook/comments{/number}', issue_comment_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues/comments{/number}', contents_url: 'https://api.github.com/repos/bastienterrier/test-webhook/contents/{+path}', compare_url: 'https://api.github.com/repos/bastienterrier/test-webhook/compare/{base}...{head}', merges_url: 'https://api.github.com/repos/bastienterrier/test-webhook/merges', archive_url: 'https://api.github.com/repos/bastienterrier/test-webhook/{archive_format}{/ref}', downloads_url: 'https://api.github.com/repos/bastienterrier/test-webhook/downloads', issues_url: 'https://api.github.com/repos/bastienterrier/test-webhook/issues{/number}', pulls_url: 'https://api.github.com/repos/bastienterrier/test-webhook/pulls{/number}', milestones_url: 'https://api.github.com/repos/bastienterrier/test-webhook/milestones{/number}', notifications_url: 'https://api.github.com/repos/bastienterrier/test-webhook/notifications{?since,all,participating}', labels_url: 'https://api.github.com/repos/bastienterrier/test-webhook/labels{/name}', releases_url: 'https://api.github.com/repos/bastienterrier/test-webhook/releases{/id}', deployments_url: 'https://api.github.com/repos/bastienterrier/test-webhook/deployments', created_at: '2019-02-06T08:14:08Z', updated_at: '2019-02-25T13:02:50Z', pushed_at: '2019-03-14T08:53:55Z', git_url: 'git://github.com/bastienterrier/test-webhook.git', ssh_url: 'git@github.com:bastienterrier/test-webhook.git', clone_url: 'https://github.com/bastienterrier/test-webhook.git', svn_url: 'https://github.com/bastienterrier/test-webhook', homepage: null, size: 25427, stargazers_count: 0, watchers_count: 0, language: 'Shell', has_issues: true, has_projects: true, has_downloads: true, has_wiki: true, has_pages: true, forks_count: 0, mirror_url: null, archived: false, open_issues_count: 9, license: null, forks: 0, open_issues: 9, watchers: 0, default_branch: 'master', }, sender: { login: 'bastienterrier', id: 25296454, node_id: 'MDQ6VXNlcjI1Mjk2NDU0', avatar_url: 'https://avatars0.githubusercontent.com/u/25296454?v=4', gravatar_id: '', url: 'https://api.github.com/users/bastienterrier', html_url: 'https://github.com/bastienterrier', followers_url: 'https://api.github.com/users/bastienterrier/followers', following_url: 'https://api.github.com/users/bastienterrier/following{/other_user}', gists_url: 'https://api.github.com/users/bastienterrier/gists{/gist_id}', starred_url: 'https://api.github.com/users/bastienterrier/starred{/owner}{/repo}', subscriptions_url: 'https://api.github.com/users/bastienterrier/subscriptions', organizations_url: 'https://api.github.com/users/bastienterrier/orgs', repos_url: 'https://api.github.com/users/bastienterrier/repos', events_url: 'https://api.github.com/users/bastienterrier/events{/privacy}', received_events_url: 'https://api.github.com/users/bastienterrier/received_events', type: 'User', site_admin: false, }, }; const gitlabIssueEvent = { object_kind: 'issue', event_type: 'issue', user: { name: 'Bastien Terrier', username: 'bastien.terrier', avatar_url: 'https://secure.gravatar.com/avatar/a17bda7df8612108bc4442ce57cc6dc3?s=80&d=identicon', }, project: { id: 10607595, name: 'test_webhook', description: '', web_url: 'https://gitlab.com/bastien.terrier/test_webhook', avatar_url: null, git_ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.git', git_http_url: 'https://gitlab.com/bastien.terrier/test_webhook.git', namespace: 'bastien.terrier', visibility_level: 0, path_with_namespace: 'bastien.terrier/test_webhook', default_branch: 'master', ci_config_path: null, homepage: 'https://gitlab.com/bastien.terrier/test_webhook', url: 'git@gitlab.com:bastien.terrier/test_webhook.git', ssh_url: 'git@gitlab.com:bastien.terrier/test_webhook.g