-
Notifications
You must be signed in to change notification settings - Fork 227
Expand file tree
/
Copy pathgithub.ts
More file actions
79 lines (70 loc) · 2.37 KB
/
github.ts
File metadata and controls
79 lines (70 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import { readFileSync } from 'node:fs';
import { create } from '@bufbuild/protobuf';
import { GitInfoSchema } from '@wundergraph/cosmo-connect/dist/platform/v1/platform_pb';
import type { GitInfo } from '@wundergraph/cosmo-connect/dist/platform/v1/platform_pb';
import envCi from 'env-ci';
import { Client } from './core/client/client.js';
import { getBaseHeaders } from './core/config.js';
// https://docs.github.com/en/actions/learn-github-actions/environment-variables#default-environment-variables
function getLatestPRCommit(): string | undefined {
try {
const event = process.env.GITHUB_EVENT_PATH
? JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'))
: undefined;
if (event && event.pull_request) {
return event.pull_request.head.sha;
}
} catch {
return undefined;
}
}
export function useGitHub() {
const isPr =
process.env.GITHUB_EVENT_NAME === 'pull_request' || process.env.GITHUB_EVENT_NAME === 'pull_request_target';
let commit = getLatestPRCommit();
if (!commit) {
const env = envCi();
commit = env.commit;
}
return {
isPr,
commit,
build: process.env.GITHUB_RUN_ID,
branch: process.env.GITHUB_HEAD_REF,
repository: process.env.GITHUB_REPOSITORY,
accountId: process.env.GITHUB_REPOSITORY_OWNER_ID,
repositoryId: process.env.GITHUB_REPOSITORY_ID,
root: process.env.GITHUB_WORKSPACE,
};
}
export const verifyGitHubIntegration = async (client: Client) => {
let gitInfo: GitInfo | undefined;
const { isPr, commit: commitSha, repository, accountId } = useGitHub();
if (isPr && commitSha && repository && accountId) {
const [ownerSlug, repositorySlug] = repository?.split('/');
gitInfo = create(GitInfoSchema, {
commitSha,
accountId,
ownerSlug,
repositorySlug,
});
}
let ignoreErrorsDueToGitHubIntegration = false;
if (gitInfo) {
const integrationCheckResponse = await client.platform.isGitHubAppInstalled(
{
gitInfo,
},
{
headers: getBaseHeaders(),
},
);
ignoreErrorsDueToGitHubIntegration = integrationCheckResponse.isInstalled;
if (ignoreErrorsDueToGitHubIntegration) {
console.log(
'GitHub integration detected. The command will succeed and any errors detected will be reflected on commit status instead.',
);
}
}
return { gitInfo, ignoreErrorsDueToGitHubIntegration };
};