Skip to content

Instantly share code, notes, and snippets.

@jrson83
Created February 11, 2023 22:28
Show Gist options
  • Save jrson83/db3e30952c086ed3ce54ff661aa755f8 to your computer and use it in GitHub Desktop.
Save jrson83/db3e30952c086ed3ce54ff661aa755f8 to your computer and use it in GitHub Desktop.
github-action-test
import { RunOptions, RunTarget } from 'github-action-ts-run-api'
import assert from 'node:assert'
import fs from 'node:fs'
import http from 'node:http'
async function runTest() {
const port = 8234
const server = http
.createServer((req, res) => {
if (req.url === '/repos/owner/repo') {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end('{"name":"x"}')
} else if (req.url === '/repos/owner/repo/pulls') {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end('{"body": "Please pull these awesome changes in!"}')
} else if (req.url === '/repos/owner/repo/pulls/123/files') {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end('{"filename": "packages/test-core/package.json","status":"added"}')
} else if (req.url === '/repos/owner/repo/issues/123/labels') {
res.writeHead(200, { 'Content-Type': 'application/json' })
res.end('{"id": 208045946,"name": "pkg: test-core"}')
} else {
res.writeHead(404)
res.end()
}
})
.listen(port)
try {
const target = RunTarget.mainJs('x2.action.yml')
const res = await target.run(
RunOptions.create()
.setGithubContext({ apiUrl: `http://localhost:${port}` })
.setGithubContext({ payload: { pull_request: { number: 123 } } })
.setWorkspaceDir('packages')
.setInputs({
GITHUB_TOKEN: 't',
workspace: 'packages',
prefix: 'pkg:',
})
.setEnv({ GITHUB_TOKEN: 't' })
)
console.log(res)
assert(typeof res.error === 'undefined')
assert(res.isSuccess === true)
assert(
JSON.stringify(res.commands.outputs) ===
JSON.stringify({
token: 't',
workspace: 'packages',
prefix: 'pkg:',
labels: '{"id":208045946,"name":"pkg: test-core"}',
})
)
} finally {
server.close()
}
}
runTest()
name: "test"
description: "test"
inputs:
GITHUB_TOKEN:
description: "The GITHUB_TOKEN secret"
required: true
workspace:
description: "What subfolder should the script run within"
required: true
prefix:
description: "The prefix to use for the labels"
required: false
runs:
using: "node16"
main: "x2.mjs"
import * as core from '@actions/core'
import * as github from '@actions/github'
import { Octokit } from '@octokit/rest'
import multimatch from 'multimatch'
export async function run() {
try {
const token = core.getInput('GITHUB_TOKEN', { required: true, trimWhitespace: true }) || process.env.GITHUB_TOKEN
if (!token) {
core.error('failed to provide a GitHub token for accessing the GitHub REST API.')
core.setFailed('failed to provide a GitHub token for accessing the GitHub REST API.')
return
}
core.setOutput('token', token)
const apiUrl = process.env.GITHUB_API_URL || 'https://api.github.com'
const workspace = core.getInput('workspace', { required: false }) || process.env.GITHUB_WORKSPACE || 'packages'
core.setOutput('workspace', workspace)
const prefix = core.getInput('prefix', { required: false }) || ''
core.setOutput('prefix', prefix)
const octokit = new Octokit({
auth: token,
baseUrl: apiUrl,
})
const pullRequest = github?.context?.payload?.pull_request?.number
if (!pullRequest) {
core.error('could not find pull request number')
core.setFailed('could not find pull request number')
return
}
let changedFiles = []
const iterator = await octokit.paginate(octokit.pulls.listFiles, {
owner: 'owner',
repo: 'repo',
pull_number: pullRequest,
})
for await (const data of iterator) {
changedFiles = [...changedFiles, ...[data].map((fileData) => fileData.filename)]
}
const subDirs = (workspace.match(/\//g) || []).length
const labels = multimatch(changedFiles, [`${workspace}/**/*`]).map(
(dir) => `${prefix}${dir.split('/')[1 + subDirs]}`
)
if (labels.length > 0) {
const { data } = await octokit.issues.addLabels({
owner: 'owner',
repo: 'repo',
issue_number: pullRequest,
labels: labels,
})
core.setOutput('labels', data)
} else {
core.debug('no labels to add')
}
} catch (error) {
if (error instanceof Error) {
console.error(`Error: ${error}`)
}
}
}
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment