Skip to content

Commit 08dd6bb

Browse files
committed
Explain the GitGitGadget PR Handler in more detail
1 parent dc2f783 commit 08dd6bb

File tree

1 file changed

+55
-2
lines changed

1 file changed

+55
-2
lines changed

GitGitGadget's-Azure-Function-and-Azure-Pipelines.md

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,62 @@ Apart from validating that the payload really originated from GitHub, the Azure
88

99
The `push` event (and also the `pull_request` event) is not actually handled by the Azure Function. It is handled via the Azure Pipeline being configured as "Pull request validation". That also allows it to be shown in the Checks tab of the PR.
1010

11-
You can see the difference on https://dev.azure.com/gitgitgadget/git/_build?definitionId=3: the `push`-triggered builds are labeled as "Pull request build", and the `issue_comment` ones as "Manual build" (because they are triggered using a Personal Access Token, also known as "PAT").
11+
You can see the difference on [the summary page of the GitGitGadget PR Handler pipeline](https://dev.azure.com/gitgitgadget/git/_build?definitionId=3): the `push`-triggered builds are labeled as "Pull request build", and the `issue_comment` ones as "Manual build" (because they are triggered using a Personal Access Token, also known as "PAT").
12+
13+
Depending how the Azure Pipeline was triggered, it calls [`misc-helper.ts`](https://github.com/gitgitgadget/gitgitgadget/blob/master/script/misc-helper.ts) with the `handle-pr-comment` or with the `handle-pr-push` parameter, respectively. More precisely, the pipeline has 4 steps:
14+
15+
1. Use Node 10.x
16+
2. Pull GitGitGadget, run npm, essentially:
17+
```sh
18+
test -d .git || git init || exit 1
19+
20+
git rev-parse HEAD >.git/PRE_FETCH_HEAD &&
21+
git fetch https://github.com/gitgitgadget/gitgitgadget master ||
22+
exit 1
23+
24+
[... commented-out dirty tricks to use PRs' patches early ...]
25+
git reset --hard FETCH_HEAD ||
26+
exit 1
27+
28+
# If nothing was pulled, we already built it
29+
test "$(git rev-parse HEAD)" != "$(cat .git/PRE_FETCH_HEAD)" ||
30+
exit 0
31+
32+
npm install &&
33+
npm run build ||
34+
exit 1
35+
```
36+
3. Obtain GitHub Token:
37+
```sh
38+
GIT_CONFIG_PARAMETERS="$GIT_CONFIG_PARAMETERS $EXTRA_CONFIG"
39+
node build/script/misc-helper.js set-app-token &&
40+
git config gitgitgadget.publishRemote \
41+
https://x-access-token:$(git config gitgitgadget.githubToken)@github.com/gitgitgadget/git
42+
```
43+
(The `EXTRA_CONFIG` is necessary because it contains a value that is configured via a secret pipeline variable.)
44+
4. Handle PR (Comment or Push):
45+
```sh
46+
GIT_CONFIG_PARAMETERS="$GIT_CONFIG_PARAMETERS $EXTRA_CONFIG"
47+
set -x
48+
if test "$(pr.comment.id)" -lt 0
49+
then
50+
case "$BUILD_SOURCEBRANCH" in
51+
refs/pull/[1-9]*/head|refs/pull/[1-9]*/merge)
52+
branchname=${BUILD_SOURCEBRANCH#refs/pull/}
53+
prnumber=${branchname%/*}
54+
;;
55+
*) echo "Invalid source branch: $BUILD_SOURCEBRANCH">&2; exit 1;;
56+
esac
57+
node build/script/misc-helper.js handle-pr-push "$prnumber"
58+
else
59+
node build/script/misc-helper.js handle-pr-comment "$(pr.comment.id)"
60+
fi
61+
```
62+
(The `pr.comment.id` pipeline variable is set when queuing the build via the Azure Function that is registered as a webhook, that's how we can discern between this mode vs push.)
63+
64+
The pipeline variable `GIT_CONFIG_PARAMETERS` (which is pre-set as an environment variable in the scripts, interpreted by Git in the same way as `git -c <key>=<value>` would be) is defined as `'user.name=GitGitGadget' '[email protected]' 'gitgitgadget.workDir=$(Agent.HomeDirectory)/../git-worktree'`, i.e. it configures GitGitGadget as committer and it asks GitGitGadget to fetch the commits and notes to the directory`git-worktree/` that is located next to the Azure Pipeline build agent's own directory, which assumes that the agent is running in a suitable VM and its files are installed into the home directory of the account running the agent.
1265
13-
Depending how the Azure Pipeline was triggered, it calls [`misc-helper.ts`](https://github.com/gitgitgadget/gitgitgadget/blob/master/script/misc-helper.ts) with the `handle-pr-comment` or with the `handle-pr-push` parameter, respectively.
66+
Ideally, this definition (even if it is very small compared to other pipeline definitions I maintain) would be tracked in a Git repository, but since we want this to be a CI build (so that it neatly shows those Checks in the PR page), the pipeline is already associated with gitgitgadget/git (even if the pipeline is configured not to "sync the source", i.e. it does not check out the code pushed to the PR), and we cannot simply add GitGitGadget's Azure Pipelines' definitions to `master` because that is mirrored verbatim from git/git.
1467
1568
# Keeping https://github.com/gitgitgadget/git up to date
1669

0 commit comments

Comments
 (0)