Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a shared workflow to get changed module #3

Merged
merged 16 commits into from
Jan 27, 2022
79 changes: 79 additions & 0 deletions .github/workflows/get-changed-module.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
name: Get changed module

on:
workflow_call:
outputs:
changed_module_dir:
description: "The directory of the changed module. Empty if no module was changed."
value: ${{ jobs.main.outputs.changed_module_dir }}

jobs:
main:
runs-on: ubuntu-latest
outputs:
changed_module_dir: ${{ steps.get-changed-module.outputs.result }}
steps:
- name: Checkout repository
uses: actions/checkout@v2

- name: Get changed module
uses: actions/github-script@v5
id: get-changed-module
with:
result-encoding: string
script: |
let base;
let head;

switch (context.eventName) {
case "pull_request":
base = context.payload.pull_request.base.sha
head = context.payload.pull_request.head.sha
break
case "push":
base = context.payload.before
head = context.payload.after
break
default:
core.setFailed(`Not supported event: ${context.eventName}.`)
}

const { status, data } = await github.rest.repos.compareCommitsWithBasehead({
owner: context.repo.owner,
repo: context.repo.repo,
basehead: `${base}...${head}`
})

if (status !== 200) {
core.setFailed(`Expected github.rest.repos.compareCommitsWithBasehead to return 200, got ${status}.`)
}

if (data.status !== "ahead") {
core.setFailed("The head commit ${head} is not ahead of the base commit ${base}.")
}

const path = require("path")
const fs = require("fs")
const cyan = "\u001b[48;5;6m"

const moduleDirs = [...new Set(data.files
.filter(file => file.filename.startsWith("modules/"))
.map(file => path.dirname(file.filename)
.split("/")
.slice(0, 3)
.join("/"))
.filter(dir => fs.existsSync(dir)))]

switch (moduleDirs.length) {
case 0:
core.info("No changed module found.")
return ""
case 1:
core.info("Found 1 changed module:")
core.info(`- ${cyan}${moduleDirs[0]}`)
return moduleDirs[0]
default:
core.log("Found more than one changed modules:")
moduleDirs.forEach((dir) => console.log(`- ${cyan}${dir}`))
core.setFailed("Only one module can be added or updated at a time.")
}
45 changes: 6 additions & 39 deletions .github/workflows/validate-module.yml
Original file line number Diff line number Diff line change
@@ -1,48 +1,15 @@
name: Validate module

on:
pull_request:
branches: [main]
workflow_dispatch:

# pull_request:
# branches:
# - main

jobs:
prerequisite:
runs-on: ubuntu-latest
outputs:
changed_module_dir: ${{ steps.check-for-module-changes.outputs.result }}
steps:
- name: Check for module changes
uses: actions/github-script@v5
id: check-for-module-changes
with:
result-encoding: string
script: |
const { data: files } = await github.rest.pulls.listFiles({
owner: context.payload.repository.owner.login,
repo: context.payload.repository.name,
pull_number: context.payload.number,
})

const path = require("path")
const moduleDirs = [...new Set(files
.filter(x => x.filename.startsWith("modules/"))
.map(x => {
const dirs = path.dirname(x.filename)
const segments = dirs.split("/").slice(0, 3)
return segments.join("/")
}))]

switch (moduleDirs.length) {
case 0:
console.log("No module changes found.")
return ""
case 1:
console.log(`Found 1 changed module: ${moduleDirs[0]}`)
return moduleDirs[0]
default:
console.log("Found more than one changed modules:")
moduleDirs.forEach((moduleDir) => console.log(`- ${moduleDir}`))
core.setFailed("Only one module can be added or updated at a time.")
}
uses: ./.github/workflows/get-changed-module.yml

main:
runs-on: ubuntu-latest
Expand Down