-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests to cover basic functionality (#21)
* add test workflow * update/add deps * update vitest config * remove boilerplate * add test helpers * add tests for basic PR functionality * add tests for installation functionality * change root branch name
- Loading branch information
Showing
13 changed files
with
985 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ name: Build | |
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
pull_request: | ||
branches: '*' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,7 +3,7 @@ name: Linter | |
on: | ||
push: | ||
branches: | ||
- master | ||
- main | ||
pull_request: | ||
branches: '*' | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
name: Test | ||
|
||
on: | ||
push: | ||
branches: | ||
- main | ||
pull_request: | ||
branches: '*' | ||
|
||
jobs: | ||
test: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- name: Install node | ||
uses: actions/setup-node@v4 | ||
with: | ||
node-version: 18 | ||
- name: Cache | ||
uses: actions/cache@v4 | ||
id: cache | ||
with: | ||
path: '**/node_modules' | ||
key: yarn-v1-${{ hashFiles('**/yarn.lock') }} | ||
- name: Install | ||
run: yarn --immutable | ||
if: steps.cache.outputs.cache-hit != 'true' | ||
- name: Test | ||
run: yarn test:ci |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{ | ||
"action": "created", | ||
"installation": { | ||
"id": 2, | ||
"account": { | ||
"login": "github", | ||
"id": 1 | ||
}, | ||
"repository_selection": "selected", | ||
"access_tokens_url": "https://api.github.com/app/installations/2/access_tokens", | ||
"repositories_url": "https://api.github.com/installation/repositories", | ||
"html_url": "https://github.com/settings/installations/2" | ||
}, | ||
"repositories": [ | ||
{ | ||
"id": 1296269, | ||
"name": "Hello-World", | ||
"full_name": "octocat/Hello-World" | ||
} | ||
], | ||
"sender": { | ||
"login": "octocat", | ||
"id": 1 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"pull_request": { | ||
"title": "Test Pull Request", | ||
"number": 1, | ||
"state": "open", | ||
"draft": false, | ||
"head": { | ||
"sha": "abc123" | ||
}, | ||
"base": { | ||
"ref": "master" | ||
}, | ||
"labels": [] | ||
}, | ||
"repository": { | ||
"name": "your-repo-name", | ||
"owner": { | ||
"login": "your-repo" | ||
} | ||
}, | ||
"installation": { | ||
"id": 2 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
import { describe, beforeEach, afterEach, test, expect } from 'vitest'; | ||
import fs from 'fs'; | ||
import nock from 'nock'; | ||
import path from 'path'; | ||
|
||
import { setupProbot, teardownProbot } from '../testHelpers'; | ||
import { labels } from '../../src/labels.js'; | ||
|
||
const installationPayload = JSON.parse( | ||
fs.readFileSync( | ||
path.join(__dirname, '../fixtures', 'installation.created.json'), | ||
'utf-8', | ||
), | ||
); | ||
|
||
const reposResponse = { | ||
repositories: [ | ||
{ owner: { login: 'owner1' }, name: 'repo1' }, | ||
{ owner: { login: 'owner2' }, name: 'repo2' }, | ||
], | ||
}; | ||
|
||
describe('Probot installation Handlers', () => { | ||
let probot: any; | ||
|
||
beforeEach(() => { | ||
probot = setupProbot(); | ||
}); | ||
|
||
afterEach(() => { | ||
teardownProbot(); | ||
}); | ||
|
||
test('make sure labels exist', async () => { | ||
const mock = nock('https://api.github.com') | ||
.post('/app/installations/2/access_tokens') | ||
.reply(200, { | ||
token: 'test', | ||
permissions: { | ||
issues: 'write', | ||
}, | ||
}) | ||
.get('/installation/repositories') | ||
.query({ installation_id: 2 }) | ||
.reply(200, reposResponse); | ||
|
||
for (const repo of reposResponse.repositories) { | ||
mock.get(`/repos/${repo.owner.login}/${repo.name}/labels`).reply(200, []); | ||
|
||
for (const label of Object.values(labels)) { | ||
const color = label.color.substring(1).toUpperCase(); | ||
|
||
mock | ||
.post( | ||
`/repos/${repo.owner.login}/${repo.name}/labels`, | ||
(body: any) => { | ||
expect(body).toMatchObject({ | ||
name: label.name, | ||
color: color, | ||
}); | ||
return true; | ||
}, | ||
) | ||
.reply(201); | ||
} | ||
} | ||
|
||
await probot.receive({ | ||
name: 'installation', | ||
payload: installationPayload, | ||
}); | ||
|
||
expect(mock.pendingMocks()).toStrictEqual([]); | ||
}); | ||
|
||
test('updates label color if it does not match', async () => { | ||
const mock = nock('https://api.github.com') | ||
.post('/app/installations/2/access_tokens') | ||
.reply(200, { | ||
token: 'test', | ||
permissions: { | ||
issues: 'write', | ||
}, | ||
}) | ||
.get('/installation/repositories') | ||
.query({ installation_id: 2 }) | ||
.reply(200, reposResponse); | ||
|
||
for (const repo of reposResponse.repositories) { | ||
const existingLabels = Object.values(labels).map(label => ({ | ||
name: label.name, | ||
color: '000000', | ||
})); | ||
|
||
mock | ||
.get(`/repos/${repo.owner.login}/${repo.name}/labels`) | ||
.reply(200, existingLabels); | ||
|
||
for (const label of Object.values(labels)) { | ||
const color = label.color.substring(1).toUpperCase(); | ||
|
||
mock | ||
.patch( | ||
`/repos/${repo.owner.login}/${repo.name}/labels/${encodeURIComponent(label.name)}`, | ||
(body: any) => { | ||
expect(body).toMatchObject({ | ||
color, | ||
}); | ||
return true; | ||
}, | ||
) | ||
.reply(200); | ||
} | ||
} | ||
|
||
await probot.receive({ | ||
name: 'installation', | ||
payload: installationPayload, | ||
}); | ||
|
||
expect(mock.pendingMocks()).toStrictEqual([]); | ||
}); | ||
}); |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.