Skip to content

Commit

Permalink
feat(store-gitea): add store support for gitea. fixes #100
Browse files Browse the repository at this point in the history
  • Loading branch information
paulrobertlloyd committed Oct 10, 2020
1 parent cac37c5 commit 255be67
Show file tree
Hide file tree
Showing 5 changed files with 555 additions and 0 deletions.
54 changes: 54 additions & 0 deletions packages/store-gitea/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# @indiekit/store-gitea

Store IndieWeb content on Gitea.

## Installation

`npm i @indiekit/store-gitea`

## Configuration

```js
const GiteaStore = require('@indiekit/store-gitea');

const gitea = new GiteaStore({
// config options here
});
```

## Options

### `branch`

The branch files will be saved to.

Type: `string`\
*Optional*, defaults to `master`

### `host`

Gitea instance URL.

Type: `string`\
*Optional*, defaults to `https://gitea.com`

### `repo`

The name of your Gitea repository.

Type: `string`\
*Required*

### `token`

A Gitea access token.

Type: `string`\
*Required*

### `user`

Your Gitea username.

Type: `string`\
*Required*
117 changes: 117 additions & 0 deletions packages/store-gitea/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import got from 'got';

const defaults = {
branch: 'master',
instance: 'https://gitea.com'
};

/**
* @typedef Response
* @property {object} response HTTP response
*/
export const GiteaStore = class {
constructor(options = {}) {
this.id = 'gitea';
this.name = 'Gitea';
this.options = {...defaults, ...options};
}

gitea() {
return got.extend({
prefixUrl: `${this.options.instance}/api/v1/repos/${this.options.user}/${this.options.repo}/contents`,
responseType: 'json',
headers: {
authorization: `token ${this.options.token}`
}
});
}

/**
* Create file in a repository
*
* @param {string} path Path to file
* @param {string} content File content
* @param {string} message Commit message
* @returns {Promise<Response>} HTTP response
* @see https://gitea.com/api/swagger#/repository/repoCreateFile
*/
async createFile(path, content, message) {
content = Buffer.from(content).toString('base64');
const response = await this.gitea().post(path, {
json: {
branch: this.options.branch,
content,
message
}
});
return response;
}

/**
* Read file in a repository
*
* @param {string} path Path to file
* @returns {Promise<Response>} A promise to the response
* @see https://gitea.com/api/swagger#/repository/repoGetContents
*/
async readFile(path) {
const request = await this.gitea().get(`${path}?ref=${this.options.branch}`, {
resolveBodyOnly: true
});
const content = Buffer.from(request.content, 'base64').toString('utf8');
return content;
}

/**
* Update file in a repository
*
* @param {string} path Path to file
* @param {string} content File content
* @param {string} message Commit message
* @returns {Promise<Response>} A promise to the response
* @see https://gitea.com/api/swagger#/repository/repoUpdateFile
*/
async updateFile(path, content, message) {
const request = await this.gitea().get(`${path}?ref=${this.options.branch}`, {
resolveBodyOnly: true
}).catch(() => {
return false;
});

content = Buffer.from(content).toString('base64');
const response = await this.gitea().put(path, {
json: {
branch: this.options.branch,
content,
message,
sha: (request) ? request.sha : false
}
});
return response;
}

/**
* Delete file in a repository
*
* @param {string} path Path to file
* @param {string} message Commit message
* @returns {Promise<Response>} A promise to the response
* @see https://gitea.com/api/swagger#/repository/repoDeleteFile
*/
async deleteFile(path, message) {
const contents = await this.gitea().get(`${path}?ref=${this.options.branch}`, {
resolveBodyOnly: true
}).catch(() => {
return false;
});

await this.gitea().delete(path, {
json: {
branch: this.options.branch,
message,
sha: (contents) ? contents.sha : false
}
});
return true;
}
};
226 changes: 226 additions & 0 deletions packages/store-gitea/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 255be67

Please sign in to comment.