Fixes #1 Allow for multiple artifacts
This commit is contained in:
@@ -1,15 +1,16 @@
|
||||
import { Inputs } from "./Inputs";
|
||||
import { Releases } from "./Releases";
|
||||
import { basename } from "path";
|
||||
import { readFileSync } from "fs";
|
||||
import { ArtifactUploader } from "./ArtifactUploader";
|
||||
|
||||
export class Action {
|
||||
private inputs: Inputs
|
||||
private releases: Releases
|
||||
private uploader: ArtifactUploader
|
||||
|
||||
constructor(inputs: Inputs, releases: Releases) {
|
||||
constructor(inputs: Inputs, releases: Releases, uploader: ArtifactUploader) {
|
||||
this.inputs = inputs
|
||||
this.releases = releases
|
||||
this.uploader = uploader
|
||||
}
|
||||
|
||||
async perform() {
|
||||
@@ -21,14 +22,11 @@ export class Action {
|
||||
this.inputs.name
|
||||
)
|
||||
|
||||
if (this.inputs.artifact) {
|
||||
const artifactData = this.inputs.readArtifact()
|
||||
await this.releases.uploadArtifact(
|
||||
createResult.data.upload_url,
|
||||
this.inputs.artifactContentLength,
|
||||
this.inputs.artifactContentType,
|
||||
artifactData,
|
||||
this.inputs.artifactName
|
||||
const artifacts = this.inputs.artifacts
|
||||
if (artifacts.length > 0) {
|
||||
await this.uploader.uploadArtifacts(
|
||||
artifacts,
|
||||
createResult.data.upload_url
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
22
src/Artifact.ts
Normal file
22
src/Artifact.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { basename } from "path";
|
||||
import { readFileSync, statSync } from "fs";
|
||||
|
||||
export class Artifact {
|
||||
readonly contentType: string
|
||||
readonly name: string
|
||||
readonly path: string
|
||||
|
||||
constructor(path: string, contentType: string = "raw") {
|
||||
this.path = path
|
||||
this.name = basename(path)
|
||||
this.contentType = contentType;
|
||||
}
|
||||
|
||||
get contentLength(): number {
|
||||
return statSync(this.path).size
|
||||
}
|
||||
|
||||
readFile(): Buffer {
|
||||
return readFileSync(this.path)
|
||||
}
|
||||
}
|
||||
22
src/ArtifactGlobber.ts
Normal file
22
src/ArtifactGlobber.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { Globber, FileGlobber } from "./Globber";
|
||||
import { Artifact } from "./Artifact";
|
||||
|
||||
export interface ArtifactGlobber {
|
||||
globArtifactString(artifact: string, contentType: string): Artifact[]
|
||||
}
|
||||
|
||||
export class FileArtifactGlobber implements ArtifactGlobber {
|
||||
private globber: Globber
|
||||
|
||||
constructor(globber: Globber = new FileGlobber()) {
|
||||
this.globber = globber
|
||||
}
|
||||
|
||||
globArtifactString(artifact: string, contentType: string): Artifact[] {
|
||||
return artifact.split(',')
|
||||
.map((path) => this.globber.glob(path))
|
||||
.reduce((accumulated, current) => accumulated.concat(current))
|
||||
.map((path) => new Artifact(path, contentType))
|
||||
}
|
||||
}
|
||||
|
||||
24
src/ArtifactUploader.ts
Normal file
24
src/ArtifactUploader.ts
Normal file
@@ -0,0 +1,24 @@
|
||||
import { Artifact } from "./Artifact";
|
||||
import { Releases } from "./Releases";
|
||||
|
||||
export interface ArtifactUploader {
|
||||
uploadArtifacts(artifacts: Artifact[], uploadUrl: string): void
|
||||
}
|
||||
|
||||
export class GithubArtifactUploader implements ArtifactUploader {
|
||||
private releases: Releases
|
||||
|
||||
constructor(releases: Releases) {
|
||||
this.releases = releases
|
||||
}
|
||||
|
||||
async uploadArtifacts(artifacts: Artifact[], uploadUrl: string) {
|
||||
artifacts.forEach(async artifact => {
|
||||
await this.releases.uploadArtifact(uploadUrl,
|
||||
artifact.contentLength,
|
||||
artifact.contentType,
|
||||
artifact.readFile(),
|
||||
artifact.name)
|
||||
});
|
||||
}
|
||||
}
|
||||
11
src/Globber.ts
Normal file
11
src/Globber.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
import { GlobSync } from "glob";
|
||||
|
||||
export interface Globber {
|
||||
glob(pattern: string): string[]
|
||||
}
|
||||
|
||||
export class FileGlobber implements Globber {
|
||||
glob(pattern: string): string[] {
|
||||
return new GlobSync(pattern, { mark: true }).found
|
||||
}
|
||||
}
|
||||
@@ -1,49 +1,42 @@
|
||||
import * as core from '@actions/core';
|
||||
import { Context } from "@actions/github/lib/context";
|
||||
import { readFileSync, statSync } from 'fs';
|
||||
import { basename } from 'path';
|
||||
import { readFileSync } from 'fs';
|
||||
import { ArtifactGlobber } from './ArtifactGlobber';
|
||||
import { Artifact } from './Artifact';
|
||||
|
||||
export interface Inputs {
|
||||
readonly artifact: string
|
||||
readonly artifactName: string
|
||||
readonly artifactContentType: string
|
||||
readonly artifactContentLength: number
|
||||
readonly artifacts: Artifact[]
|
||||
readonly body: string
|
||||
readonly commit: string
|
||||
readonly draft: boolean
|
||||
readonly name: string
|
||||
readonly tag: string
|
||||
readonly token: string
|
||||
|
||||
readArtifact(): Buffer
|
||||
}
|
||||
|
||||
export class CoreInputs implements Inputs {
|
||||
private artifactGlobber: ArtifactGlobber
|
||||
private context: Context
|
||||
|
||||
constructor(context: Context) {
|
||||
constructor(artifactGlobber: ArtifactGlobber, context: Context) {
|
||||
this.artifactGlobber = artifactGlobber
|
||||
this.context = context
|
||||
}
|
||||
|
||||
get artifact(): string {
|
||||
return core.getInput('artifact')
|
||||
}
|
||||
|
||||
get artifactName(): string {
|
||||
return basename(this.artifact)
|
||||
}
|
||||
|
||||
get artifactContentType(): string {
|
||||
const type = core.getInput('artifactContentType')
|
||||
if(type) {
|
||||
return type;
|
||||
get artifacts(): Artifact[] {
|
||||
let artifacts = core.getInput('artifacts')
|
||||
if (!artifacts) {
|
||||
artifacts = core.getInput('artifacts')
|
||||
}
|
||||
|
||||
return 'raw'
|
||||
}
|
||||
|
||||
get artifactContentLength(): number {
|
||||
return statSync(this.artifact).size
|
||||
if (artifacts) {
|
||||
let contentType = core.getInput('artifactContentType')
|
||||
if (!contentType) {
|
||||
contentType = 'raw'
|
||||
}
|
||||
return this.artifactGlobber
|
||||
.globArtifactString(artifacts, contentType)
|
||||
}
|
||||
return []
|
||||
}
|
||||
|
||||
get body(): string {
|
||||
@@ -97,10 +90,6 @@ export class CoreInputs implements Inputs {
|
||||
return core.getInput('token', { required: true })
|
||||
}
|
||||
|
||||
readArtifact(): Buffer {
|
||||
return readFileSync(this.artifact)
|
||||
}
|
||||
|
||||
stringFromFile(path: string): string {
|
||||
return readFileSync(path, 'utf-8')
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@ import * as core from '@actions/core';
|
||||
import { CoreInputs } from './Inputs';
|
||||
import { GithubReleases } from './Releases';
|
||||
import { Action } from './Action';
|
||||
import { GithubArtifactUploader } from './ArtifactUploader';
|
||||
import { FileArtifactGlobber } from './ArtifactGlobber';
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
@@ -17,9 +19,12 @@ function createAction(): Action {
|
||||
const token = core.getInput('token')
|
||||
const context = github.context
|
||||
const git = new github.GitHub(token)
|
||||
const globber = new FileArtifactGlobber()
|
||||
|
||||
const inputs = new CoreInputs(globber, context)
|
||||
const releases = new GithubReleases(context, git)
|
||||
const inputs = new CoreInputs(context)
|
||||
return new Action(inputs, releases)
|
||||
const uploader = new GithubArtifactUploader(releases)
|
||||
return new Action(inputs, releases, uploader)
|
||||
}
|
||||
|
||||
run();
|
||||
|
||||
Reference in New Issue
Block a user