Skip to content

Commit 34be247

Browse files
authored
Merge pull request #36173 from github/repo-sync
Repo sync
2 parents 20deffb + a8a29c8 commit 34be247

File tree

19 files changed

+602
-50
lines changed

19 files changed

+602
-50
lines changed

.github/workflows/alert-changed-branch-protections.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
branch_protection_rule:
55
workflow_dispatch:
66
schedule:
7-
- cron: '20 16 * * 3' # Run every Wednesday at 16:30 UTC / 8:30 PST
7+
- cron: '20 16 * * 3' # Run every Wednesday at 16:20 UTC / 8:20 PST
88

99
permissions:
1010
contents: read
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Update Moda allowed IPs
2+
3+
# **What it does**: Make sure that the allowed IPs in Moda are up to date.
4+
# **Why we have it**: The IP ranges from Fastly can change.
5+
# **Who does it impact**: Docs engineering.
6+
7+
on:
8+
schedule:
9+
- cron: '20 16 * * 4' # Run every Thursday at 16:20 UTC / 8:20 PST
10+
workflow_dispatch:
11+
12+
permissions:
13+
contents: write
14+
pull-requests: write
15+
16+
jobs:
17+
update-moda-allowed-ips:
18+
if: github.repository == 'github/docs-internal'
19+
runs-on: ubuntu-latest
20+
steps:
21+
- name: Check out the repository
22+
uses: actions/checkout@v4
23+
24+
- name: Update list of allowed IPs
25+
run: |
26+
echo "Getting a list of Fastly IP addresses...."
27+
ips=$( \
28+
curl -s https://api.fastly.com/public-ip-list \
29+
| jq -r '.addresses | join(",")' \
30+
)
31+
echo "Got a list of Fastly IP addresses: $ips"
32+
33+
echo "Updating the list of allowed IPs in Moda config..."
34+
yq -i ".metadata.annotations[\"moda.github.net/allowed-ips\"] = \"$ips\"" \
35+
config/kubernetes/production/services/webapp.yaml
36+
echo "Updated the list of allowed IPs in Moda config"
37+
38+
echo "Checking if there is a change to make..."
39+
if git diff --quiet; then
40+
echo "No changes to the allowed IPs"
41+
exit 0
42+
fi
43+
44+
echo "Change found; making a pull request..."
45+
branchname=update-allowed-ips-$(date +%s)
46+
git checkout -b $branchname
47+
git commit -am "Update list of allowed IPs"
48+
git push
49+
gh pr create \
50+
--title "Update list of allowed IPs" \
51+
--body 'This PR updates the list of allowed IPs in Moda. It is automatically generated.' \
52+
--head=$branchname
53+
echo "Pull request created"
54+
55+
- uses: ./.github/actions/slack-alert
56+
if: ${{ failure() }}
57+
with:
58+
slack_channel_id: ${{ secrets.DOCS_ALERTS_SLACK_CHANNEL_ID }}
59+
slack_token: ${{ secrets.SLACK_DOCS_BOT_TOKEN }}

config/kubernetes/production/services/webapp.yaml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ metadata:
77
annotations:
88
moda.github.net/domain-name: 'docs-internal.github.com'
99
moda.github.net/dns-registration-enabled: 'false'
10-
moda.github.net/load-balancer-type:
11-
public-external-http
12-
# moda.github.net/allowed-ips: '23.235.32.0/20,43.249.72.0/22,103.244.50.0/24,103.245.222.0/23,103.245.224.0/24,104.156.80.0/20,140.248.64.0/18,140.248.128.0/17,146.75.0.0/17,151.101.0.0/16,157.52.64.0/18,167.82.0.0/17,167.82.128.0/20,167.82.160.0/20,167.82.224.0/20,172.111.64.0/18,185.31.16.0/22,199.27.72.0/21,199.232.0.0/1'
13-
# ipv6 addresses not included
14-
# curl -i "https://api.fastly.com/public-ip-list"
10+
moda.github.net/load-balancer-type: public-external-http
11+
moda.github.net/allowed-ips: 23.235.32.0/20,43.249.72.0/22,103.244.50.0/24,103.245.222.0/23,103.245.224.0/24,104.156.80.0/20,140.248.64.0/18,140.248.128.0/17,146.75.0.0/17,151.101.0.0/16,157.52.64.0/18,167.82.0.0/17,167.82.128.0/20,167.82.160.0/20,167.82.224.0/20,172.111.64.0/18,185.31.16.0/22,199.27.72.0/21,199.232.0.0/16
1512
spec:
1613
ports:
1714
- name: http

src/events/components/events.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import Cookies from 'src/frame/components/lib/cookies'
33
import { parseUserAgent } from './user-agent'
44
import { Router } from 'next/router'
55
import { isLoggedIn } from 'src/frame/components/hooks/useHasAccount'
6+
import { getExperimentVariationForContext } from './experiments/experiment'
67
import { EventType, EventPropsByType } from '../types'
78

89
const COOKIE_NAME = '_docs-events'
@@ -110,6 +111,8 @@ export function sendEvent<T extends EventType>({
110111
color_mode_preference: getColorModePreference(),
111112
os_preference: Cookies.get('osPreferred'),
112113
code_display_preference: Cookies.get('annotate-mode'),
114+
115+
experiment_variation: getExperimentVariationForContext(getMetaContent('path-language')),
113116
},
114117

115118
...props,

src/events/components/experiment.ts

Lines changed: 0 additions & 34 deletions
This file was deleted.
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
# Experiments
2+
3+
There are times when we want make a change, but aren't sure if it will provide a better user experience.
4+
5+
In these scenarios we can run experiments.
6+
7+
Experiments are A/B tests where A is some version of our site and B is another. When the user requests our site they are randomly served either site A or B.
8+
9+
After the experiment is live, we gather data via [events](../../README.md) that help determine which version of the site we want to stick with.
10+
11+
## TOC
12+
13+
- [Experiments as feature flags](#experiments-as-feature-flags)
14+
- [Experiment variations](#experiment-variations)
15+
- [Adding an experiment](#adding-an-experiment)
16+
- [Implementing an experiment](#implementing-an-experiment)
17+
- [Toggling an experiment on or off](#toggling-an-experiment-on-or-off)
18+
- [Tracking results on an experiment](#tracking-results-on-an-experiment)
19+
- [Via regular events](#via-regular-events)
20+
- [Via the `experiment` event](#via-the-experiment-event)
21+
22+
23+
## Experiments as feature flags
24+
25+
An additional benefit of this pattern is that it lets you turn on/off a feature in the UI and toggle it from the developer console. This is useful if you want to ship UI changes in parts, or test something in production before turning it on.
26+
27+
## Experiment variations
28+
29+
To clarify terminology, if a user is shown site A which is the original site _without_ the experiment they will have an `experiment_variation` value of `"control"` to indicate that they are in the control group.
30+
31+
If the user is shown the experiment (site B), they will have an `experiment_variation` value of `"treatment"`
32+
33+
## Adding an experiment
34+
35+
1. Create a `key` for you experiment, e.g. `ai_search_experiment`
36+
2. Determine how many users will see the experiment. The default is 50% and makes sense for _most_ use cases.
37+
3. Add your experiment to [experiments.ts](./experiments.ts)
38+
39+
Example,
40+
41+
```typescript
42+
// Add new experiment key to this list
43+
export type ExperimentNames = 'example_experiment' | 'ai_search_experiment'
44+
45+
export const EXPERIMENTS = {
46+
example_experiment: { ... }
47+
ai_search_experiment: {
48+
key: 'ai_search_experiment',
49+
isActive: true, // Set to false when the experiment is over
50+
percentOfUsersToGetExperiment: 10, // Only 10% of users will get the experiment
51+
limitToLanguages: ['en'], // Only users with the `en` language will be included in the experiment
52+
includeVariationInContext: true, // See note below
53+
},
54+
}
55+
```
56+
57+
When `includeVariationInContext` is true **all** analytics events sent will include `experiment_variation` in their context. `experiment_variation` will be `"treatment"` or `"control"` depending on which the user was randomly assigned.
58+
59+
> [!IMPORTANT]
60+
> Since the `experiment_variation` is a single key in the context, **only one experiment** can include their variations in the context e.g. only one value of `includeVariationInContext` can be `true`.
61+
62+
## Implementing an experiment
63+
64+
For example, let's say you are conducting an experiment that changes the search bar.
65+
66+
In the code that displays the search bar, you can use the `shouldShowExperiment` function to determine which version of the code to show the user.
67+
68+
Example:
69+
70+
```typescript
71+
import { useRouter } from 'next/router'
72+
import { useShouldShowExperiment } from '@/events/components/experiments/useShouldShowExperiment'
73+
import { EXPERIMENTS } from '@/events/components/experiments/experiments'
74+
import { ClassicSearchBar } from "@/search/components/ClassicSearchBar.tsx"
75+
import { NewSearchBar } from "@/search/components/NewSearchBar.tsx"
76+
77+
export function SearchBar() {
78+
const router = useRouter()
79+
// Users who were randomly placed in the `treatment` group will be shown the experiment
80+
const { shouldShow: shouldShowNewSearch } = useShouldShowExperiment(
81+
EXPERIMENTS.ai_search_experiment,
82+
router.locale
83+
)
84+
85+
if (shouldShowNewSearch) {
86+
return (
87+
<NewSearchBar />
88+
)
89+
}
90+
return <ClassicSearchBar />
91+
}
92+
```
93+
94+
> [!NOTE]
95+
> If a user is placed in the `"treatment"` group e.g. they are shown the experiment and will continue to see the treatment across all sessions from the same browser. This is because we use a hash of user's session ID cookie to deterministically set the control group. The session cookie lasts for 365 days, otherwise they might see something different on each reload.
96+
97+
## Toggling an experiment on or off
98+
99+
In development every session is placed into the `"treatment"` control group so that the experiment can be developed on.
100+
101+
However, you can change which experiment to show by calling the following function in the `Console` tab in Chrome dev tools,
102+
103+
```javascript
104+
window.overrideControlGroup("<experiment_key>", "treatment" | "control");
105+
106+
// Example to see original search experience
107+
window.overrideControlGroup("ai_search_experiment", "control");
108+
```
109+
110+
For events, you can verify that your `experiment_variation` values are being included in the event context from the `Network` tab in Chrome dev tools.
111+
112+
## Tracking results on an experiment
113+
114+
### Via regular events
115+
116+
If your experiment object in [experiments.ts](./experiments.ts) included the `includeVariationInContext: true` key (and is the ONLY object to include that key) then the `experiment_variation` of your experiment will be sent in the context of an event.
117+
118+
This means that you can send other events, like
119+
120+
```typescript
121+
sendEvent({
122+
type: EventType.search,
123+
search_query: "How do I open pdf?",
124+
search_context: "general-search",
125+
});
126+
```
127+
128+
And the `context` on that event will include the `experiment_variation` key and value of your experiment,
129+
130+
e.g.
131+
132+
```javascript
133+
{
134+
search_query: "How do I open pdf?",
135+
search_context: "general-search",
136+
context: {
137+
...
138+
experiment_variation: "treatment" // Could also be "control" depending on the random outcome
139+
}
140+
}
141+
```
142+
143+
### Via the `experiment` event
144+
145+
If your experiment is specific, meaning it can be tracked with a boolean event, e.g.
146+
147+
```javascript
148+
{
149+
experiment_name: <string>, // e.g. `new_button_experiment` for did user click new button?
150+
experiment_variation: 'treatment' | 'control',
151+
experiment_success: <boolean>, // e.g. true the user is using the new button!
152+
}
153+
```
154+
155+
Then you should omit the `includeVariationInContext` key from your experiment object and use the `sendExperimentSuccess` function to track events.
156+
157+
Example:
158+
159+
```typescript
160+
import { sendExperimentSuccess } from '@/events/components/experiments/experiment-event'
161+
import { EXPERIMENTS } from '@/events/components/experiments/experiments'
162+
163+
export function MyNewComponent() {
164+
return (
165+
<button onClick={() => {
166+
console.log("The user did the thing!")
167+
sendExperimentSuccess(EXPERIMENTS.new_button_experiment)
168+
}}>
169+
)
170+
}
171+
172+
```
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { EventType } from '@/events/types'
2+
import { sendEvent } from '../events'
3+
import { getExperimentControlGroupFromSession } from './experiment'
4+
import { ExperimentNames } from './experiments'
5+
6+
export function sendExperimentSuccess(experimentKey: ExperimentNames, success = true) {
7+
return sendEvent({
8+
type: EventType.experiment,
9+
experiment_name: experimentKey,
10+
experiment_variation: getExperimentControlGroupFromSession(experimentKey).toLowerCase(),
11+
experiment_success: success,
12+
})
13+
}

0 commit comments

Comments
 (0)