|
| 1 | +import { LogTester } from '../tests/LogTester'; |
| 2 | +import { jiraClientStub } from '../tests/JiraClientStub'; |
| 3 | +import { createOctokitRestStub } from '../tests/OctokitRestStub'; |
| 4 | +import { OctokitAction } from './OctokitAction'; |
| 5 | +import { fail } from 'assert'; |
| 6 | + |
| 7 | +class TestOctokitAction extends OctokitAction { |
| 8 | + |
| 9 | + constructor() { |
| 10 | + super(); |
| 11 | + (this as any).jira = jiraClientStub; |
| 12 | + (this as any).rest = createOctokitRestStub('PR title'); |
| 13 | + } |
| 14 | + |
| 15 | + async execute(): Promise<void> { |
| 16 | + this.log('Invoked execute()'); |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +describe('OctokitAction', () => { |
| 21 | + const originalKeys = Object.keys(process.env); |
| 22 | + let logTester: LogTester; |
| 23 | + let sut: any; // TestOctokitAction, but with access to protected methods |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + logTester = new LogTester(); |
| 27 | + const token = process.env["GITHUB_TOKEN"]; |
| 28 | + if (token) { |
| 29 | + for (const key of Object.keys(process.env)) { |
| 30 | + if (!originalKeys.includes(key)) { |
| 31 | + // Otherwise, changes form previous UT are propagated to the next one |
| 32 | + delete process.env[key]; |
| 33 | + } |
| 34 | + } |
| 35 | + process.env['GITHUB_REPOSITORY'] = 'test-owner/test-repo'; |
| 36 | + process.env['INPUT_GITHUB-TOKEN'] = token; |
| 37 | + sut = new TestOctokitAction(); |
| 38 | + } else { |
| 39 | + fail("OctokitAction tests require GITHUB_TOKEN environment variables to be set."); |
| 40 | + } |
| 41 | + }); |
| 42 | + |
| 43 | + afterEach(() => { |
| 44 | + logTester.afterEach(); |
| 45 | + }); |
| 46 | + |
| 47 | + it('isEngXpSquad is true', async () => { |
| 48 | + process.env['INPUT_IS-ENG-XP-SQUAD'] = 'true'; |
| 49 | + const action = new TestOctokitAction(); |
| 50 | + expect((action as any).isEngXpSquad).toBe(true); |
| 51 | + }); |
| 52 | + |
| 53 | + it('isEngXpSquad is false', async () => { |
| 54 | + const action = new TestOctokitAction(); |
| 55 | + expect((action as any).isEngXpSquad).toBe(false); |
| 56 | + }); |
| 57 | + |
| 58 | + it('sendGraphQL', async () => { |
| 59 | + const result = await sut.sendGraphQL(` |
| 60 | + query { |
| 61 | + repository (owner: "SonarSource", name: "gh-action-lt-backlog") { |
| 62 | + id |
| 63 | + name |
| 64 | + } |
| 65 | + }`); |
| 66 | + expect(result).toEqual({ |
| 67 | + repository: { |
| 68 | + id: "R_kgDOGZ6k-Q", |
| 69 | + name: "gh-action-lt-backlog" |
| 70 | + } |
| 71 | + }) |
| 72 | + }); |
| 73 | + |
| 74 | + it('inputString', async () => { |
| 75 | + process.env['INPUT_KEY'] = 'Value'; // KEY must be upper case, otherwise Ubuntu in CI doesn't work |
| 76 | + expect(sut.inputString('key')).toBe('Value'); |
| 77 | + expect(sut.inputString('missing')).toBe(''); |
| 78 | + }); |
| 79 | + |
| 80 | + it('inputNumber', async () => { |
| 81 | + process.env['INPUT_KEY'] = '42'; |
| 82 | + process.env['INPUT_STRING'] = 'Lorem Ipsum'; |
| 83 | + expect(sut.inputNumber('key')).toBe(42); |
| 84 | + expect(() => sut.inputNumber('string')).toThrow("Value of input 'string' is not a number: Lorem Ipsum") |
| 85 | + }); |
| 86 | + |
| 87 | + it('inputBoolean true', async () => { |
| 88 | + for (const value of ['true', 'True', 'TRUE', 'tRuE']) { |
| 89 | + process.env['INPUT_KEY'] = value; |
| 90 | + expect(sut.inputBoolean('key')).toBe(true); |
| 91 | + } |
| 92 | + }); |
| 93 | + |
| 94 | + it('inputBoolean false', async () => { |
| 95 | + for (const value of ['', 'false', 'False', '0', 'no']) { |
| 96 | + process.env['INPUT_KEY'] = value; |
| 97 | + expect(sut.inputBoolean('key')).toBe(false); |
| 98 | + } |
| 99 | + }); |
| 100 | + |
| 101 | + it('loadPullRequest', async () => { |
| 102 | + const pr = await sut.loadPullRequest(42); |
| 103 | + expect(pr).toMatchObject({ number: 42, title: "PR title" }); // Plus the remainig properties from scaffolding |
| 104 | + expect(logTester.logsParams).toStrictEqual(["Loading PR #42"]); |
| 105 | + }); |
| 106 | + |
| 107 | + it('loadIssue', async () => { |
| 108 | + const issue = await sut.loadIssue(24); |
| 109 | + expect(issue).toMatchObject({ number: 24, title: "Issue title" }); // Plus the remainig properties from scaffolding |
| 110 | + expect(logTester.logsParams).toStrictEqual(["Loading issue #24"]); |
| 111 | + }); |
| 112 | + |
| 113 | + it.skip('findFixedIssues no body', async () => { |
| 114 | + |
| 115 | + }); |
| 116 | + |
| 117 | + it.skip('findFixedIssues no issue', async () => { |
| 118 | + |
| 119 | + }); |
| 120 | + |
| 121 | + it.skip('findFixedIssues with issues in body', async () => { |
| 122 | + |
| 123 | + }); |
| 124 | + |
| 125 | + it.skip('findFixedIssues renovate no issue', async () => { |
| 126 | + |
| 127 | + }); |
| 128 | + |
| 129 | + it.skip('findFixedIssues renovate with issue', async () => { |
| 130 | + |
| 131 | + }); |
| 132 | + |
| 133 | + it.skip('addComment', async () => { |
| 134 | + |
| 135 | + }); |
| 136 | + |
| 137 | + it.skip('listComments', async () => { |
| 138 | + |
| 139 | + }); |
| 140 | + |
| 141 | + it.skip('updateIssueTitle', async () => { |
| 142 | + |
| 143 | + }); |
| 144 | + |
| 145 | + it.skip('updatePullRequestTitle', async () => { |
| 146 | + |
| 147 | + }); |
| 148 | + |
| 149 | + it.skip('updatePullRequestDescription', async () => { |
| 150 | + |
| 151 | + }); |
| 152 | + |
| 153 | + it.skip('findEmail returns first', async () => { |
| 154 | + |
| 155 | + }); |
| 156 | + |
| 157 | + it.skip('findEmail no results', async () => { |
| 158 | + |
| 159 | + }); |
| 160 | + |
| 161 | + it.skip('findEmail bad token', async () => { |
| 162 | + |
| 163 | + }); |
| 164 | + |
| 165 | + it.skip('sendSlackMessage', async () => { |
| 166 | + |
| 167 | + }); |
| 168 | + |
| 169 | + it.skip('processRequestReview', async () => { |
| 170 | + |
| 171 | + }); |
| 172 | + |
| 173 | + it.skip('addJiraComponent success', async () => { |
| 174 | + |
| 175 | + }); |
| 176 | + |
| 177 | + it.skip('addJiraComponent fails to create component', async () => { |
| 178 | + |
| 179 | + }); |
| 180 | + |
| 181 | + it.skip('addJiraComponent fails to assign component', async () => { |
| 182 | + |
| 183 | + }); |
| 184 | +}); |
0 commit comments