Skip to content

Commit

Permalink
Merge pull request #1 from swve/dev
Browse files Browse the repository at this point in the history
1.1.0 + Auto Publish
  • Loading branch information
swve authored Mar 5, 2022
2 parents 4202197 + 1bcb35b commit 61b44c9
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 67 deletions.
Binary file added .github/img/gitstorykit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
33 changes: 33 additions & 0 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
# For more information see: https://help.github.com/actions/language-and-framework-guides/publishing-nodejs-packages

name: Node.js Package

on:
release:
types: [created]

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
- run: npm ci
- run: npm test

publish-npm:
needs: build
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16
registry-url: https://registry.npmjs.org/
- run: npm ci
- run: npm publish
env:
NODE_AUTH_TOKEN: ${{secrets.npm_token}}
81 changes: 81 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
<p align="center"><br>
<a href="#">
<img src=".github/img/gitstorykit.png" height="128">
</a>
</p>
<center>
<p align="center">

<img alt="GitHub" src="https://img.shields.io/github/license/swve/gitstorykit">
<img alt="GitHub contributors" src="https://img.shields.io/github/contributors/swve/gitstorykit">
<img alt="GitHub issues" src="https://img.shields.io/github/issues/swve/gitstorykit">
</p>
</center>



GitStoryKit is a little development kit that can be used to develop apps and software that enables git time travel, the library is in early development and is designed for simplicity.

GitStoryKit is heavily used in GitStory

The following Git clients are supported :
- GitHub ✅
- Gitlab ⏳ (on development)
- BitBucket ⏳ (on development)

## Usage

### Initialization

```js
import GitStory from "gitstorykit";

const gitstory = new GitStory("Github");

gitstory.init({ owner: "vercel", repo: "next.js", sha: "5.0" });
```

### Get first commit
```js
const firstcommit = await gitstory.getFirstCommit();
```

### Get first commit Date
```js
const firstcommitdate = await gitstory.getFirstCommitDate();
```

### Get a commit's Date
Dates should be in the **ISO 8601 format**
```js
const commit_date = await gitstory.getCommitDate(commit_sha);
```

### Get commit between dates

Dates should be in the **ISO 8601 format**
```js
const commit = await gitstory.getCommitsBetweenDates(startDate, endDate, per_page: number, page: number);
```

### Get commits until date

Dates should be in the **ISO 8601 format**
```js
const commit = await gitstory.getCommitsUntilDate(date, per_page: number, page: number);
```

### Get a repository active years
```js
const active_years = await gitstory.yearsActive();
```



## Contributing

Please see our [contributing.md](/CONTRIBUTING.md).

## Authors

Badr B. ([@swve](https://github.com/swve))
71 changes: 52 additions & 19 deletions clients/github/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { ParamsInterface, TimeInterface } from "../../interfaces";
import { ParamsInterface } from "../../interfaces";
import { Octokit } from "@octokit/rest";
import * as parse from "parse-link-header";
import parse from "parse-link-header";

export class GithubClient {
octokit = new Octokit();
private octokit = new Octokit();
private config;

init(params: ParamsInterface) {
Expand Down Expand Up @@ -34,7 +34,6 @@ export class GithubClient {
return firstcommit;
}


/**
* Get first commit date
* Dates are in ISO 8601 format
Expand All @@ -46,30 +45,46 @@ export class GithubClient {
return date;
}

/**
* Get last commit date
* Dates are in ISO 8601 format
* @param params
*/
async getLastCommitDate() {
const commit = await this.octokit.rest.repos.listCommits({
per_page: 1,
...this.config,
});
const date = await commit.data[0].commit.author.date;
return date;
}

/**
* Get a commit date
* Dates are in ISO 8601 format
*/
async getCommitDate(commit_sha:string) {
*/
async getCommitDate(commit_sha: string) {
const commit = await this.octokit.rest.git.getCommit({
commit_sha: commit_sha,
...this.config
...this.config,
});
const date = await commit.data.author.date;
return date;
}

/**
* Get a particular day commits
* Get a particular day commits
* Dates are in ISO 8601 format
* @param params
* @param params
*/
async getCommitsBetween(startDate:string,endDate:string){
async getCommitsBetween(startDate: string, endDate: string, per_page: number, page: number) {
const res = await this.octokit.rest.repos.listCommits({
since:startDate,
until:endDate,
...this.config
})
since: startDate,
until: endDate,
per_page: per_page,
page: page,
...this.config,
});
return res;
}

Expand All @@ -78,13 +93,31 @@ export class GithubClient {
* Dates are in ISO 8601 format
* @param date
*/

async getCommitsUntil(date:string){
async getCommitsUntil(date: string, per_page: number, page: number) {
const res = await this.octokit.rest.repos.listCommits({
until:date,
...this.config
})
until: date,
per_page: per_page,
page: page,
...this.config,
});
return res;
}

/**
* Get a list of a repo active years
* Returns an array of years
* @param
*/
async yearsActive() {
const first = await this.getFirstCommitDate();
const last = await this.getLastCommitDate();
const firstYear = parseInt(first.substring(0, 4));
const lastYear = parseInt(last.substring(0, 4));

const years = [];
for (let i = firstYear; i <= lastYear; i++) {
years.push(i);
}
return years;
}
}
32 changes: 17 additions & 15 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,29 +6,30 @@ export default class GitStory {
private clientName: string;
private client;

constructor(client) {
this.clientName = client;
constructor(clientName: string) {
this.clientName = clientName;
}

/**
* Git client init
* @param params
/**
* Git client init
* @param params
*/

init(params: ParamsInterface) {
switch (this.clientName.toLowerCase()) {
case "github":
this.client = new GithubClient();
this.client.init(params);

break;

case "gitlab":
break;
}
}

/**
* Git Functions
/**
* Git Functions
* Available Features/endpoints
*/

Expand All @@ -40,18 +41,19 @@ export default class GitStory {
return this.client.getFirstCommitDate();
}

async getCommitDate(commit_sha){
async getCommitDate(commit_sha) {
return this.client.getCommitDate(commit_sha);
}

async getCommitsBetweenDates(startDate, endDate) {
return this.client.getCommitsBetween(startDate, endDate);
async getCommitsBetweenDates(startDate, endDate, per_page: number, page: number) {
return this.client.getCommitsBetween(startDate, endDate, per_page, page);
}

async getCommitsUntilDate(date) {
return this.client.getCommitsUntil(date);
async getCommitsUntilDate(date, per_page: number, page: number) {
return this.client.getCommitsUntil(date, per_page, page);
}

async yearsActive() {
return this.client.yearsActive();
}
}


9 changes: 0 additions & 9 deletions interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,3 @@ export interface ParamsInterface {
sha: string;
token?: string;
}

export interface TimeInterface {
date: {
years?: number;
weeks?: number;
months?: number;
};
commit?: string;
}
6 changes: 3 additions & 3 deletions package-lock.json

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

17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
{
"name": "gitstorykit",
"version": "1.0.0",
"version": "1.1.0",
"author": "swve",
"main": "index.js",
"main": "dist/index.js",
"module": "dist/index.js",
"files": [
"dist/"
],
"scripts": {
"test": "ts-node test.ts",
"prepublishOnly": "npm run compile",
"compile": "npm run clean && tsc -p .",
"watch": "tsc -w -p .",
"clean": "rm -rf dist"
"test": "ts-node tests/test.ts",
"tsc": "tsc -p tsconfig.json",
"prepublish": "npm run tsc"
},
"license": "GPL-3.0-or-later",
"dependencies": {
"@octokit/rest": "^18.5.3",
"@types/parse-link-header": "^1.0.0",
"dayjs": "^1.10.4",
"parse-link-header": "^1.0.1",
"ts-node": "^9.1.1",
"typescript": "^4.2.4"
Expand Down
14 changes: 11 additions & 3 deletions tests/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,15 @@ const test3 = "2018-01-13T00:00:00.000Z";


async function testCommitUntilDate(){
const test = await gitstory.getCommitsUntilDate(test3);

const test = await gitstory.getCommitsUntilDate(test3, 200, 1);
test.data.forEach((element) => {
console.log(element.commit.author.date);
});
}

async function testCommitBetween(){
const test = await gitstory.getCommitsBetweenDates(start, end);
const test = await gitstory.getCommitsBetweenDates(start, end, 200, 1);
test.data.forEach((element) => {
console.log(element.commit.author.date);
});
Expand All @@ -27,9 +28,16 @@ async function testGetCommitDate(){
console.log(test);
}

async function yearsActive() {
const test = await gitstory.yearsActive();
console.log(test);


}

(async function () {
testCommitUntilDate();
//testCommitUntilDate();
yearsActive();
//testGetCommitDate();
//testCommitBetween();
// → 🎉
Expand Down
Loading

0 comments on commit 61b44c9

Please sign in to comment.