Skip to content

Commit c9ab897

Browse files
authored
Explain how to develop and contribute (#10)
* Explain how to develop and contribute * Create github config * New workflow for testing github integration * Add octokit/rest and oktokit/auth (for JWT for app auth) * ignore private keys * Setup GitHub app * Sadly allow for esModuleInterop due to octokit * Initial implementatin of build trigger for unity-ci/docker
1 parent 9aaf924 commit c9ab897

File tree

9 files changed

+320
-3
lines changed

9 files changed

+320
-3
lines changed

Diff for: DEVELOPMENT.md

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Development
2+
3+
Some keys are set as environment variables;
4+
5+
6+
## Local Setup (optional)
7+
8+
#### Credentials
9+
10+
To be able to use functions that use the Firebase AdminSDK you need to set credentials.
11+
12+
1. Download the service account file from firebase
13+
2. Set the path to that file to an environment variable
14+
15+
__Linux / MacOS__
16+
17+
```bash
18+
export GOOGLE_APPLICATION_CREDENTIALS="/home/user/Downloads/service-account-file.json"
19+
```
20+
21+
__Windows (PowerShell)__
22+
23+
```ps
24+
$env:GOOGLE_APPLICATION_CREDENTIALS="C:\Users\username\Downloads\service-account-file.json"
25+
```
26+
27+
_(for more information, please see the [docs](https://firebase.google.com/docs/admin/setup))_
28+
29+
#### Integrations
30+
31+
To test specific functionality like the integrations you will also have to set the following environment variables
32+
33+
__Discord__
34+
35+
```bash
36+
export discord.token="my_discord_token"
37+
```
38+
39+
__Github__
40+
41+
```bash
42+
export github.client-secret="my_github_app_client_secret"`
43+
export github.private-key="my_github_app_private_key"
44+
```
45+
46+
## Local Commands
47+
48+
In order to run firebase locally simply use
49+
50+
```
51+
firebase serve
52+
```
53+
54+
To only run one component, like `hosting`, `functions` or `firestore` you may use the `--only` flag.
55+
56+
```
57+
firebase serve --only functions
58+
```
59+
60+
If everything works, finally deploy the changes
61+
62+
```
63+
firebase deploy
64+
```
65+
66+
## New project setup
67+
68+
_(only when migrating to new firebase project / environment)_
69+
70+
```
71+
firebase functions:config:set discord.token="my_discord_token"
72+
firebase functions:config:set github.client-secret="my_github_app_client_secret"
73+
firebase functions:config:set discord.private-key="my_github_app_private_key"
74+
```

Diff for: functions/.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@ node_modules/
1010

1111
# Service account
1212
service-account.json
13+
14+
# Private key
15+
*.pem

Diff for: functions/package-lock.json

+178
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: functions/package.json

+2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
},
1717
"main": "lib/index.js",
1818
"dependencies": {
19+
"@octokit/auth-app": "^2.6.0",
20+
"@octokit/rest": "^18.0.6",
1921
"eris": "^0.13.3",
2022
"firebase-admin": "^8.10.0",
2123
"firebase-functions": "^3.6.1",

Diff for: functions/src/api/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * as buildQueue from './buildQueue';
22
export { authorise } from './authorise';
33
export { unityVersions } from './unityVersions';
4+
export { triggerWorkflow } from './triggerWorkflow';

Diff for: functions/src/api/triggerWorkflow.ts

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { Request } from 'firebase-functions/lib/providers/https';
2+
import { Response } from 'express-serve-static-core';
3+
import { firebase, functions } from '../config/firebase';
4+
import { GitHub } from '../config/github';
5+
6+
export const triggerWorkflow = functions.https.onRequest(
7+
async (request: Request, response: Response) => {
8+
try {
9+
const gitHub = await GitHub.init();
10+
11+
// This works only in the "installation" auth scope.
12+
const result = await gitHub.repos.createDispatchEvent({
13+
owner: 'unity-ci',
14+
repo: 'docker',
15+
event_type: 'new_build_requested',
16+
client_payload: {
17+
test_var: 'test value',
18+
},
19+
});
20+
21+
response.status(200).send(result);
22+
} catch (err) {
23+
firebase.logger.error(err);
24+
response.status(500).send('Oops.');
25+
}
26+
},
27+
);

Diff for: functions/src/config/github.ts

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { createAppAuth } from '@octokit/auth-app';
2+
import { Octokit } from '@octokit/rest';
3+
import { settings } from './settings';
4+
import { firebase } from './firebase';
5+
6+
const { 'private-key': privateKey, 'client-secret': clientSecret } = firebase.config().github;
7+
8+
export class GitHub {
9+
static async init() {
10+
const appOctokit = new Octokit({
11+
authStrategy: createAppAuth,
12+
auth: {
13+
...settings.github.auth,
14+
privateKey,
15+
clientSecret,
16+
},
17+
});
18+
19+
const { data } = await appOctokit.request('/app');
20+
firebase.logger.debug('app parameters', data);
21+
22+
return appOctokit;
23+
}
24+
}

Diff for: functions/src/config/settings.ts

+9
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,13 @@ export const settings = {
77
alerts: '763544776649605151',
88
},
99
},
10+
github: {
11+
auth: {
12+
id: 84327,
13+
privateKey: '-----BEGIN PRIVATE KEY-----\n... (do not place the actual private key here)',
14+
installationId: 12321333,
15+
clientId: 'Iv1.fa93dce6a47c9357',
16+
clientSecret: '(do not place the real secret here)',
17+
},
18+
},
1019
};

0 commit comments

Comments
 (0)