-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(store-gitea): add store support for gitea. fixes #100
- Loading branch information
1 parent
cac37c5
commit 255be67
Showing
5 changed files
with
555 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.