Skip to content

Commit b07b5a8

Browse files
authored
Merge pull request #16600 from github/workflow-should-run-on-label-events
Workflow should run on label events
2 parents 1d2acd0 + a242eb4 commit b07b5a8

3 files changed

Lines changed: 45 additions & 6 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/usr/bin/env node
2+
3+
const fs = require('fs')
4+
const core = require('@actions/core')
5+
const eventPayload = JSON.parse(fs.readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8'))
6+
7+
// This workflow-run script does the following:
8+
// 1. Gets an array of labels on a PR.
9+
// 2. Finds one with the relevant Algolia text; if none found, exits early.
10+
// 3. Gets the version substring from the label string.
11+
12+
const labelText = 'sync-english-index-for-'
13+
const labelsArray = eventPayload.pull_request.labels
14+
15+
// Exit early if no labels are on this PR
16+
if (!(labelsArray && labelsArray.length)) {
17+
process.exit(0)
18+
}
19+
20+
// Find the relevant label
21+
const algoliaLabel = labelsArray
22+
.map(label => label.name)
23+
.find(label => label.startsWith(labelText))
24+
25+
// Exit early if no relevant label is found
26+
if (!algoliaLabel) {
27+
process.exit(0)
28+
}
29+
30+
// Given: sync-english-index-for-enterprise-server@3.0
31+
// Returns: enterprise-server@3.0
32+
const versionToSync = algoliaLabel.split(labelText)[1]
33+
34+
// Store the version so we can access it later in the workflow
35+
core.setOutput('versionToSync', versionToSync)
36+
process.exit(0)

.github/workflows/sync-single-english-algolia-index.yml

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ name: Algolia Sync Single English Index
33
on:
44
pull_request:
55
types:
6+
- labeled
7+
- unlabeled
68
- opened
79
- reopened
810
- synchronize
@@ -13,7 +15,7 @@ on:
1315
jobs:
1416
updateIndices:
1517
name: Update English index for single version based on a label's version
16-
if: github.repository == 'github/docs-internal' && startsWith(github.event.label.name, 'sync-english-index-for-')
18+
if: github.repository == 'github/docs-internal'
1719
runs-on: ubuntu-latest
1820
steps:
1921
- name: checkout
@@ -30,13 +32,13 @@ jobs:
3032
${{ runner.os }}-node-
3133
- name: npm ci
3234
run: npm ci
33-
- name: Get version from label
35+
- name: Get version from Algolia label if present; only continue if the label is found.
3436
id: getVersion
35-
run: |
36-
echo "::set-output name=version::$(github.event.label.name.split('sync-english-index-for-')[1])"
37-
- name: Sync English index for single version
37+
run: $GITHUB_WORKSPACE/.github/actions-scripts/enterprise-algolia-label.js
38+
- if: ${{ steps.getVersion.outputs.versionToSync }}
39+
name: Sync English index for single version
3840
env:
39-
VERSION: ${{ steps.getVersion.outputs.version }}
41+
VERSION: ${{ steps.getVersion.outputs.versionToSync }}
4042
LANGUAGE: 'en'
4143
ALGOLIA_APPLICATION_ID: ${{ secrets.ALGOLIA_APPLICATION_ID }}
4244
ALGOLIA_API_KEY: ${{ secrets.ALGOLIA_API_KEY }}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"webpack-cli": "^3.3.12"
7878
},
7979
"devDependencies": {
80+
"@actions/core": "^1.2.6",
8081
"ajv": "^6.11.0",
8182
"async": "^3.2.0",
8283
"await-sleep": "0.0.1",

0 commit comments

Comments
 (0)