-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #158 from wallabag/feature/weblate-label
Add Weblate automatic label
- Loading branch information
Showing
8 changed files
with
682 additions
and
425 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
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,60 @@ | ||
import { client } from 'octonode' | ||
import { validateWebhook } from './utils/github' | ||
|
||
export async function weblate(event, context, callback) { | ||
const githubClient = client(process.env.GITHUB_TOKEN) | ||
|
||
const body = JSON.parse(event.body) | ||
|
||
// when creating the webhook | ||
if (body && ('hook' in body)) { | ||
let response | ||
|
||
try { | ||
const message = validateWebhook(body) | ||
|
||
console.log(message) | ||
|
||
response = { | ||
statusCode: 200, | ||
body: message, | ||
} | ||
} catch (e) { | ||
console.log(e.message) | ||
|
||
response = { | ||
statusCode: 500, | ||
body: e.message, | ||
} | ||
} | ||
|
||
return callback(null, response) | ||
} | ||
|
||
if (!(body && ('pull_request' in body))) { | ||
return callback(null, { | ||
statusCode: 500, | ||
body: 'Event is not a Pull Request', | ||
}) | ||
} | ||
|
||
console.log(`Working on repo ${body.repository.full_name} for PR #${body.pull_request.number}`) | ||
|
||
if (body.pull_request.user.login !== 'weblate' || body.sender.login !== 'weblate') { | ||
return callback(null, { | ||
statusCode: 204, | ||
body: 'PR is not from Weblate', | ||
}) | ||
} | ||
|
||
await githubClient | ||
.issue(body.repository.full_name, body.pull_request.number) | ||
.addLabelsAsync(['Translations']) | ||
|
||
console.log('Labelled!') | ||
|
||
return callback(null, { | ||
statusCode: 204, | ||
body: 'Process finished', | ||
}) | ||
} |
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 |
---|---|---|
@@ -1,5 +1,7 @@ | ||
import { checkExtension } from './functions/extension' | ||
import { weblate } from './functions/weblate' | ||
|
||
export { | ||
checkExtension, | ||
weblate, | ||
} |
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
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,181 @@ | ||
import { client } from 'octonode' | ||
import { weblate } from '../functions/weblate' | ||
|
||
jest.mock('octonode') | ||
|
||
describe('Validating GitHub event', () => { | ||
test('bad event body', async () => { | ||
const callback = jest.fn() | ||
|
||
await weblate({ body: '{}' }, {}, callback) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(callback).toHaveBeenCalledWith(null, { | ||
body: 'Event is not a Pull Request', | ||
statusCode: 500, | ||
}) | ||
}) | ||
|
||
test('hook event does not include PR', async () => { | ||
const callback = jest.fn() | ||
const githubEvent = { | ||
zen: 'Speak like a human.', | ||
hook_id: 1, | ||
hook: { | ||
events: [ | ||
'issue', | ||
'push', | ||
], | ||
}, | ||
repository: { | ||
full_name: '20minutes/serverless-github-check', | ||
}, | ||
sender: { | ||
login: 'diego', | ||
}, | ||
} | ||
|
||
await weblate({ body: JSON.stringify(githubEvent) }, {}, callback) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(callback).toHaveBeenCalledWith(null, { | ||
body: 'This webhook needs the "pull_request" event. Please tick it.', | ||
statusCode: 500, | ||
}) | ||
}) | ||
|
||
test('hook event is ok', async () => { | ||
const callback = jest.fn() | ||
const githubEvent = { | ||
zen: 'Speak like a human.', | ||
hook_id: 1, | ||
hook: { | ||
events: [ | ||
'pull_request', | ||
'push', | ||
], | ||
}, | ||
repository: { | ||
full_name: '20minutes/serverless-github-check', | ||
}, | ||
sender: { | ||
login: 'diego', | ||
}, | ||
} | ||
|
||
await weblate({ body: JSON.stringify(githubEvent) }, {}, callback) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(callback).toHaveBeenCalledWith(null, { | ||
body: 'Hello diego, the webhook is now enabled for 20minutes/serverless-github-check, enjoy!', | ||
statusCode: 200, | ||
}) | ||
}) | ||
|
||
test('hook event for an organization is ok', async () => { | ||
const callback = jest.fn() | ||
const githubEvent = { | ||
zen: 'Speak like a human.', | ||
hook_id: 1, | ||
hook: { | ||
events: [ | ||
'pull_request', | ||
'push', | ||
], | ||
}, | ||
organization: { | ||
login: '20minutes', | ||
}, | ||
sender: { | ||
login: 'diego', | ||
}, | ||
} | ||
|
||
await weblate({ body: JSON.stringify(githubEvent) }, {}, callback) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(callback).toHaveBeenCalledWith(null, { | ||
body: 'Hello diego, the webhook is now enabled for the organization 20minutes, enjoy!', | ||
statusCode: 200, | ||
}) | ||
}) | ||
}) | ||
|
||
describe('Apply label', () => { | ||
test('PR is NOT ok', async () => { | ||
client.mockReturnValue({ | ||
issue: jest.fn((fullName, number) => { | ||
expect(fullName).toBe('foo/bar') | ||
expect(number).toBe(42) | ||
|
||
return { | ||
addLabelsAsync: jest.fn((labels) => { | ||
expect(labels[0]).toBe('Translations') | ||
}), | ||
} | ||
}), | ||
}) | ||
|
||
const callback = jest.fn() | ||
const githubEvent = { | ||
pull_request: { | ||
user: { | ||
login: 'j0k3r', | ||
}, | ||
number: 42, | ||
}, | ||
repository: { | ||
full_name: 'foo/bar', | ||
}, | ||
sender: { | ||
login: 'j0k3r', | ||
}, | ||
} | ||
|
||
await weblate({ body: JSON.stringify(githubEvent) }, {}, callback) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(callback).toHaveBeenCalledWith(null, { | ||
body: 'PR is not from Weblate', | ||
statusCode: 204, | ||
}) | ||
}) | ||
test('PR is ok', async () => { | ||
client.mockReturnValue({ | ||
issue: jest.fn((fullName, number) => { | ||
expect(fullName).toBe('foo/bar') | ||
expect(number).toBe(42) | ||
|
||
return { | ||
addLabelsAsync: jest.fn((labels) => { | ||
expect(labels[0]).toBe('Translations') | ||
}), | ||
} | ||
}), | ||
}) | ||
|
||
const callback = jest.fn() | ||
const githubEvent = { | ||
pull_request: { | ||
user: { | ||
login: 'weblate', | ||
}, | ||
number: 42, | ||
}, | ||
repository: { | ||
full_name: 'foo/bar', | ||
}, | ||
sender: { | ||
login: 'weblate', | ||
}, | ||
} | ||
|
||
await weblate({ body: JSON.stringify(githubEvent) }, {}, callback) | ||
|
||
expect(callback).toHaveBeenCalledTimes(1) | ||
expect(callback).toHaveBeenCalledWith(null, { | ||
body: 'Process finished', | ||
statusCode: 204, | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.