mirror of
https://github.com/trympet/nextcloud-artifacts-action.git
synced 2025-07-12 21:25:38 +02:00
Compare commits
7 Commits
Author | SHA1 | Date | |
---|---|---|---|
314b7642ee | |||
1259b887e0 | |||
aae7f9ffbb | |||
0141d42d10 | |||
a6f0f86670 | |||
22aca2a29a | |||
b0bfe891cd |
3
.gitignore
vendored
3
.gitignore
vendored
@ -1,3 +1,5 @@
|
||||
lib
|
||||
|
||||
# Logs
|
||||
logs
|
||||
*.log
|
||||
@ -80,7 +82,6 @@ typings/
|
||||
|
||||
# Nuxt.js build / generate output
|
||||
.nuxt
|
||||
dist
|
||||
|
||||
# Gatsby files
|
||||
.cache/
|
||||
|
@ -11,6 +11,12 @@ inputs:
|
||||
nextcloud-url:
|
||||
description: 'The URL for the nextcloud server'
|
||||
required: true
|
||||
nextcloud-username:
|
||||
description: 'The username for the nextcloud user'
|
||||
required: true
|
||||
nextcloud-password:
|
||||
description: 'The password for the nextcloud user'
|
||||
required: true
|
||||
if-no-files-found:
|
||||
description: >
|
||||
The desired behavior if no files are found using the provided path.
|
||||
|
30706
dist/index.js
vendored
Normal file
30706
dist/index.js
vendored
Normal file
File diff suppressed because it is too large
Load Diff
14
package-lock.json
generated
14
package-lock.json
generated
@ -27,6 +27,15 @@
|
||||
"@types/glob": "*"
|
||||
}
|
||||
},
|
||||
"@types/btoa": {
|
||||
"version": "1.2.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/btoa/-/btoa-1.2.3.tgz",
|
||||
"integrity": "sha512-ANNCZICS/ofxhzUl8V1DniBJs+sFQ+Yg5am1ZwVEf/sxoKY/J2+h5Fuw3xUErlZ7eJLdgzukBjZwnsV6+/2Rmg==",
|
||||
"dev": true,
|
||||
"requires": {
|
||||
"@types/node": "*"
|
||||
}
|
||||
},
|
||||
"@types/glob": {
|
||||
"version": "7.1.3",
|
||||
"resolved": "https://registry.npmjs.org/@types/glob/-/glob-7.1.3.tgz",
|
||||
@ -146,6 +155,11 @@
|
||||
"concat-map": "0.0.1"
|
||||
}
|
||||
},
|
||||
"btoa": {
|
||||
"version": "1.2.1",
|
||||
"resolved": "https://registry.npmjs.org/btoa/-/btoa-1.2.1.tgz",
|
||||
"integrity": "sha512-SB4/MIGlsiVkMcHmT+pSmIPoNDoHg+7cMzmt3Uxt628MTz2487DKSqK/fuhFBrkuqrYv5UCEnACpF4dTFNKc/g=="
|
||||
},
|
||||
"buffer": {
|
||||
"version": "5.7.1",
|
||||
"resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz",
|
||||
|
@ -2,9 +2,10 @@
|
||||
"name": "nextcloud-artifacts-action",
|
||||
"version": "1.0.0",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"main": "lib/nextcloud-artifacts.js",
|
||||
"scripts": {
|
||||
"test": "echo \"Error: no test specified\" && exit 1"
|
||||
"test": "echo \"Error: no test specified\" && exit 1",
|
||||
"build": "npx tsc && npx @vercel/ncc build"
|
||||
},
|
||||
"repository": {
|
||||
"type": "git",
|
||||
@ -21,10 +22,12 @@
|
||||
"@actions/core": "^1.3.0",
|
||||
"@actions/glob": "^0.1.2",
|
||||
"archiver": "^5.3.0",
|
||||
"btoa": "^1.2.1",
|
||||
"node-fetch": "^2.6.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/archiver": "^5.1.0",
|
||||
"@types/btoa": "^1.2.3",
|
||||
"@types/node": "^15.6.2",
|
||||
"@types/node-fetch": "^2.5.10",
|
||||
"typescript": "^4.3.2"
|
||||
|
@ -18,6 +18,14 @@ export class Inputs {
|
||||
return core.getInput("nextcloud-url");
|
||||
}
|
||||
|
||||
static get Username(): string {
|
||||
return core.getInput("nextcloud-username");
|
||||
}
|
||||
|
||||
static get Password(): string {
|
||||
return core.getInput("nextcloud-password");
|
||||
}
|
||||
|
||||
static get NoFileBehvaior(): NoFileOption {
|
||||
const notFoundAction = core.getInput("if-no-files-found");
|
||||
const noFileBehavior: NoFileOption = NoFileOption[notFoundAction as keyof typeof NoFileOption];
|
||||
|
@ -1,12 +1,15 @@
|
||||
import * as fsSync from 'fs'
|
||||
import * as fs from 'fs/promises'
|
||||
import * as path from 'path'
|
||||
import core from '@actions/core';
|
||||
import * as os from 'os';
|
||||
import { randomUUID } from 'crypto';
|
||||
import * as archiver from 'archiver';
|
||||
import { URL } from 'url';
|
||||
import fetch from 'node-fetch';
|
||||
import fetch, { HeadersInit } from 'node-fetch';
|
||||
import { Inputs } from '../Inputs';
|
||||
import btoa from 'btoa';
|
||||
|
||||
const fs = fsSync.promises;
|
||||
|
||||
interface FileSpec {
|
||||
absolutePath: string,
|
||||
@ -14,15 +17,22 @@ interface FileSpec {
|
||||
}
|
||||
|
||||
export class NextcloudClient {
|
||||
private guid: string;
|
||||
private headers: HeadersInit;
|
||||
|
||||
public constructor(
|
||||
private endpoint: string,
|
||||
private artifact: string,
|
||||
private rootDirectory: string) { }
|
||||
private rootDirectory: string) {
|
||||
this.guid = randomUUID();
|
||||
this.headers = {'Authorization': 'Basic ' + btoa(`${Inputs.Username}:${Inputs.Password}`)};
|
||||
}
|
||||
|
||||
public async uploadFiles(files: string[]) {
|
||||
const spec = this.uploadSpec(files);
|
||||
var zip = await this.zipFiles(spec);
|
||||
await this.upload(zip);
|
||||
const path = await this.upload(zip);
|
||||
await this.shareFile(path);
|
||||
}
|
||||
|
||||
private uploadSpec(files: string[]): FileSpec[] {
|
||||
@ -93,7 +103,7 @@ export class NextcloudClient {
|
||||
|
||||
|
||||
private async zipFiles(specs: FileSpec[]): Promise<string> {
|
||||
const tempArtifactDir = path.join(os.tmpdir(), randomUUID());
|
||||
const tempArtifactDir = path.join(os.tmpdir(), this.guid);
|
||||
const artifactPath = path.join(tempArtifactDir, `artifact-${this.artifact}`);
|
||||
await fs.mkdir(artifactPath, { recursive: true });
|
||||
for (let spec of specs) {
|
||||
@ -118,15 +128,34 @@ export class NextcloudClient {
|
||||
}
|
||||
|
||||
private async upload(file: string) {
|
||||
const url = new URL(this.endpoint, '/remote.php/dav/files/user/path/to/file');
|
||||
const filePath = `/artifacts/${this.guid}/${this.artifact}`;
|
||||
const url = this.endpoint + `/remote.php/dav/files/${Inputs.Username}` + filePath;
|
||||
const stream = fsSync.createReadStream(file);
|
||||
await fetch(url.href, {
|
||||
const res = await fetch(url, {
|
||||
method: 'PUT',
|
||||
body: stream
|
||||
body: stream,
|
||||
headers: this.headers
|
||||
});
|
||||
core.debug(await res.json())
|
||||
|
||||
return filePath;
|
||||
}
|
||||
|
||||
private shareFile() {
|
||||
private async shareFile(nextcloudPath: string) {
|
||||
const url = this.endpoint + `/ocs/v2.php/apps/files_sharing/api/v1/shares`;
|
||||
const body = {
|
||||
path: nextcloudPath,
|
||||
shareType: 3,
|
||||
publicUpload: "false",
|
||||
permissions: 1,
|
||||
};
|
||||
|
||||
const res = await fetch(url, {
|
||||
method: 'PUT',
|
||||
headers: this.headers,
|
||||
body: JSON.stringify(body),
|
||||
});
|
||||
|
||||
core.debug(await res.json())
|
||||
}
|
||||
}
|
@ -7,7 +7,7 @@
|
||||
"skipLibCheck": true, /* Skip type checking of declaration files. */
|
||||
"forceConsistentCasingInFileNames": true, /* Disallow inconsistently-cased references to the same file. */
|
||||
"rootDir": "src",
|
||||
"outDir": "dist",
|
||||
"outDir": "lib",
|
||||
"sourceMap": true,
|
||||
"lib": ["es6"]
|
||||
}
|
||||
|
Reference in New Issue
Block a user