-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathgitMeta.ts
More file actions
268 lines (233 loc) · 7.45 KB
/
gitMeta.ts
File metadata and controls
268 lines (233 loc) · 7.45 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import fs from "fs/promises";
import { join } from "path";
import ini from "ini";
import git from "git-last-commit";
import { x } from "tinyexec";
import { GitMeta } from "@trigger.dev/core/v3";
export async function createGitMeta(directory: string): Promise<GitMeta | undefined> {
if (isGitHubApp()) {
return getGitHubAppMeta();
}
if (isGitHubActions()) {
return getGitHubActionsMeta();
}
// Fall back to git commands for local development
const remoteUrl = await getOriginUrl(join(directory, ".git/config"));
const [commitResult, dirtyResult] = await Promise.allSettled([
getLastCommit(directory),
isDirty(directory),
]);
if (commitResult.status === "rejected") {
return;
}
if (dirtyResult.status === "rejected") {
return;
}
const dirty = dirtyResult.value;
const commit = commitResult.value;
return {
source: "local",
remoteUrl: remoteUrl ?? undefined,
commitAuthorName: commit.author.name,
commitMessage: commit.subject,
commitRef: commit.branch,
commitSha: commit.hash,
dirty,
};
}
function getLastCommit(directory: string): Promise<git.Commit> {
return new Promise((resolve, reject) => {
git.getLastCommit(
(err, commit) => {
if (err) {
return reject(err);
}
resolve(commit);
},
{ dst: directory }
);
});
}
async function isDirty(directory: string): Promise<boolean> {
try {
const result = await x("git", ["--no-optional-locks", "status", "-s"], {
nodeOptions: {
cwd: directory,
},
});
// Example output (when dirty):
// M ../fs-detectors/src/index.ts
return result.stdout.trim().length > 0;
} catch (error) {
throw error;
}
}
async function parseGitConfig(configPath: string) {
try {
return ini.parse(await fs.readFile(configPath, "utf8"));
} catch (err: unknown) {
return;
}
}
function pluckOriginUrl(gitConfig: { [key: string]: any }): string | undefined {
// Assuming "origin" is the remote url that the user would want to use
return gitConfig['remote "origin"']?.url;
}
async function getOriginUrl(configPath: string): Promise<string | null> {
const gitConfig = await parseGitConfig(configPath);
if (!gitConfig) {
return null;
}
const originUrl = pluckOriginUrl(gitConfig);
if (originUrl) {
return originUrl;
}
return null;
}
function errorToString(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
function isGitHubActions() {
// GH Actions CI sets these env variables
return (
process.env.GITHUB_ACTIONS === "true" &&
process.env.GITHUB_SHA !== undefined &&
process.env.GITHUB_REF !== undefined
);
}
async function getGitHubActionsMeta(): Promise<GitMeta> {
const remoteUrl =
process.env.GITHUB_SERVER_URL && process.env.GITHUB_REPOSITORY
? `${process.env.GITHUB_SERVER_URL}/${process.env.GITHUB_REPOSITORY}.git`
: undefined;
// GITHUB_SHA and GITHUB_REF are both set in GH Actions CI
const githubSha = process.env.GITHUB_SHA ?? "";
const githubRef = process.env.GITHUB_REF ?? "";
// For push events, GITHUB_REF is fully qualified, e.g., "refs/heads/main"
let commitRef = githubRef.replace(/^refs\/(heads|tags)\//, "");
let commitMessage: string | undefined;
let commitSha = githubSha;
let pullRequestNumber: number | undefined;
let pullRequestTitle: string | undefined;
let pullRequestState: "open" | "closed" | "merged" | undefined;
if (process.env.GITHUB_EVENT_PATH) {
try {
const eventData = JSON.parse(await fs.readFile(process.env.GITHUB_EVENT_PATH, "utf8"));
if (process.env.GITHUB_EVENT_NAME === "push") {
commitMessage = eventData.head_commit?.message;
} else if (process.env.GITHUB_EVENT_NAME === "pull_request") {
// For PRs, use the head commit info
pullRequestTitle = eventData.pull_request?.title;
commitRef = eventData.pull_request?.head?.ref;
pullRequestNumber = eventData.pull_request?.number;
commitSha = eventData.pull_request?.head?.sha;
pullRequestState = eventData.pull_request?.state as "open" | "closed";
// Check if PR was merged
if (pullRequestState === "closed" && eventData.pull_request?.merged === true) {
pullRequestState = "merged";
}
await x("git", ["status"], {
nodeOptions: {
cwd: process.cwd(),
},
}).then((result) => console.debug(result.stdout));
commitMessage = await getCommitMessage(process.cwd(), commitSha, pullRequestNumber);
}
} catch (error) {
console.debug("Failed to parse GitHub event payload:", errorToString(error));
}
} else {
console.debug("No GITHUB_EVENT_PATH found");
}
return {
provider: "github",
source: "github_actions",
remoteUrl,
commitSha,
commitRef,
// In CI, the workspace is always clean
dirty: false,
// These fields might not be available in all GitHub Actions contexts
commitAuthorName: process.env.GITHUB_ACTOR,
commitMessage,
pullRequestNumber:
pullRequestNumber ??
(process.env.GITHUB_PULL_REQUEST_NUMBER
? parseInt(process.env.GITHUB_PULL_REQUEST_NUMBER)
: undefined),
pullRequestTitle,
pullRequestState,
};
}
function isGitHubApp() {
// we set this env variable in our build server
return process.env.TRIGGER_GITHUB_APP === "true";
}
function getGitHubAppMeta(): GitMeta {
return {
provider: "github",
source: "trigger_github_app",
remoteUrl: process.env.GITHUB_REPOSITORY_URL,
commitSha: process.env.GITHUB_HEAD_COMMIT_SHA,
commitRef: process.env.GITHUB_REF?.replace(/^refs\/(heads|tags)\//, ""),
commitMessage: process.env.GITHUB_HEAD_COMMIT_MESSAGE,
commitAuthorName: process.env.GITHUB_HEAD_COMMIT_AUTHOR_NAME,
pullRequestNumber: process.env.GITHUB_PULL_REQUEST_NUMBER
? parseInt(process.env.GITHUB_PULL_REQUEST_NUMBER)
: undefined,
pullRequestTitle: process.env.GITHUB_PULL_REQUEST_TITLE,
pullRequestState: process.env.GITHUB_PULL_REQUEST_STATE as
| "open"
| "closed"
| "merged"
| undefined,
// the workspace is always clean as we clone the repo in the build server
dirty: false,
ghUsername: process.env.GITHUB_USERNAME,
ghUserAvatarUrl: process.env.GITHUB_USER_AVATAR_URL,
};
}
async function getCommitMessage(
directory: string,
sha: string,
prNumber?: number
): Promise<string | undefined> {
try {
// First try to fetch the specific commit
await x("git", ["fetch", "origin", sha], {
nodeOptions: {
cwd: directory,
},
});
// Try to get the commit message
const result = await x("git", ["log", "-1", "--format=%B", sha], {
nodeOptions: {
cwd: directory,
},
});
const message = result.stdout.trim();
if (!message && prNumber) {
// If that didn't work, try fetching the PR branch
const branchResult = await x(
"git",
["fetch", "origin", `pull/${prNumber}/head:pr-${prNumber}`],
{
nodeOptions: {
cwd: directory,
},
}
);
// Try again with the fetched branch
const branchCommitResult = await x("git", ["log", "-1", "--format=%B", `pr-${prNumber}`], {
nodeOptions: {
cwd: directory,
},
});
return branchCommitResult.stdout.trim();
}
return message;
} catch (error) {
console.debug("Error getting commit message:", errorToString(error));
return undefined;
}
}