mirror of
https://git.mirrors.martin98.com/https://github.com/actions/toolkit
synced 2026-04-04 00:53:14 +08:00
Add github package (#32)
* Add github package * Docs * Feedback * Accidentally added extra * Allow octokit to be extended * Respond to feedback * Feedback
This commit is contained in:
36
packages/github/src/@types/@octokit/index.d.ts
vendored
Normal file
36
packages/github/src/@types/@octokit/index.d.ts
vendored
Normal file
@@ -0,0 +1,36 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
declare module '@octokit/graphql' {
|
||||
export interface GraphQlQueryResponse {
|
||||
data: {[key: string]: any} | null
|
||||
errors?: [
|
||||
{
|
||||
message: string
|
||||
path: [string]
|
||||
extensions: {[key: string]: any}
|
||||
locations: [
|
||||
{
|
||||
line: number
|
||||
column: number
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
export interface GraphQLError {
|
||||
message: string
|
||||
locations?: {line: number; column: number}[]
|
||||
path?: (string | number)[]
|
||||
extensions?: {
|
||||
[key: string]: any
|
||||
}
|
||||
}
|
||||
|
||||
export interface Variables {
|
||||
[key: string]: any
|
||||
}
|
||||
|
||||
export function defaults(
|
||||
options: any
|
||||
): (query: string, variables?: Variables) => Promise<GraphQlQueryResponse>
|
||||
}
|
||||
60
packages/github/src/context.ts
Normal file
60
packages/github/src/context.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
// Originally pulled from https://github.com/JasonEtco/actions-toolkit/blob/master/src/context.ts
|
||||
import {WebhookPayload} from './interfaces'
|
||||
|
||||
/* eslint-disable @typescript-eslint/no-require-imports */
|
||||
|
||||
export class Context {
|
||||
/**
|
||||
* Webhook payload object that triggered the workflow
|
||||
*/
|
||||
payload: WebhookPayload
|
||||
|
||||
eventName: string
|
||||
sha: string
|
||||
ref: string
|
||||
workflow: string
|
||||
action: string
|
||||
actor: string
|
||||
|
||||
/**
|
||||
* Hydrate the context from the environment
|
||||
*/
|
||||
constructor() {
|
||||
this.payload = process.env.GITHUB_EVENT_PATH
|
||||
? require(process.env.GITHUB_EVENT_PATH)
|
||||
: {}
|
||||
this.eventName = process.env.GITHUB_EVENT_NAME as string
|
||||
this.sha = process.env.GITHUB_SHA as string
|
||||
this.ref = process.env.GITHUB_REF as string
|
||||
this.workflow = process.env.GITHUB_WORKFLOW as string
|
||||
this.action = process.env.GITHUB_ACTION as string
|
||||
this.actor = process.env.GITHUB_ACTOR as string
|
||||
}
|
||||
|
||||
get issue(): {owner: string; repo: string; number: number} {
|
||||
const payload = this.payload
|
||||
|
||||
return {
|
||||
...this.repo,
|
||||
number: (payload.issue || payload.pullRequest || payload).number
|
||||
}
|
||||
}
|
||||
|
||||
get repo(): {owner: string; repo: string} {
|
||||
if (process.env.GITHUB_REPOSITORY) {
|
||||
const [owner, repo] = process.env.GITHUB_REPOSITORY.split('/')
|
||||
return {owner, repo}
|
||||
}
|
||||
|
||||
if (this.payload.repository) {
|
||||
return {
|
||||
owner: this.payload.repository.owner.login,
|
||||
repo: this.payload.repository.name
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(
|
||||
"context.repo requires a GITHUB_REPOSITORY environment variable like 'owner/repo'"
|
||||
)
|
||||
}
|
||||
}
|
||||
23
packages/github/src/github.ts
Normal file
23
packages/github/src/github.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
// Originally pulled from https://github.com/JasonEtco/actions-toolkit/blob/master/src/github.ts
|
||||
import {GraphQlQueryResponse, Variables, defaults} from '@octokit/graphql'
|
||||
import Octokit from '@octokit/rest'
|
||||
import * as Context from './context'
|
||||
|
||||
// We need this in order to extend Octokit
|
||||
Octokit.prototype = new Octokit()
|
||||
|
||||
module.exports.context = new Context.Context()
|
||||
|
||||
export class GitHub extends Octokit {
|
||||
graphql: (
|
||||
query: string,
|
||||
variables?: Variables
|
||||
) => Promise<GraphQlQueryResponse>
|
||||
|
||||
constructor(token: string) {
|
||||
super({auth: `token ${token}`})
|
||||
this.graphql = defaults({
|
||||
headers: {authorization: `token ${token}`}
|
||||
})
|
||||
}
|
||||
}
|
||||
39
packages/github/src/interfaces.ts
Normal file
39
packages/github/src/interfaces.ts
Normal file
@@ -0,0 +1,39 @@
|
||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||
|
||||
export interface PayloadRepository {
|
||||
[key: string]: any
|
||||
fullName?: string
|
||||
name: string
|
||||
owner: {
|
||||
[key: string]: any
|
||||
login: string
|
||||
name?: string
|
||||
}
|
||||
htmlUrl?: string
|
||||
}
|
||||
|
||||
export interface WebhookPayload {
|
||||
[key: string]: any
|
||||
repository?: PayloadRepository
|
||||
issue?: {
|
||||
[key: string]: any
|
||||
number: number
|
||||
html_url?: string
|
||||
body?: string
|
||||
}
|
||||
pullRequest?: {
|
||||
[key: string]: any
|
||||
number: number
|
||||
htmlUrl?: string
|
||||
body?: string
|
||||
}
|
||||
sender?: {
|
||||
[key: string]: any
|
||||
type: string
|
||||
}
|
||||
action?: string
|
||||
installation?: {
|
||||
id: number
|
||||
[key: string]: any
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user