Skip to content

Commit

Permalink
feat: add GitHub service (#35)
Browse files Browse the repository at this point in the history
* add github service

* add github service to gitFactory

* update commitShaPart

* update documentation
  • Loading branch information
weikangchia authored Aug 11, 2021
1 parent 434a2f9 commit 90af4b9
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 12 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Git Changelog Generator

**gitcg** is a customizable changelog generator for GitLab using milestone.
**gitcg** is a customizable changelog generator for GitLab and GitHub using milestone.

## Getting Started

Expand All @@ -16,7 +16,12 @@

- Get a personal token (scopes: `read_api`)
- Store it as a environment variable
```export GITLAB_TOKEN=<your personal gitlab token>```
```export GITLAB_TOKEN=<your personal GitLab token>```

### GitHub
- Get a personal token (scopes: `repo`)
- Store it as a environment variable
```export GITHUB_TOKEN=<your personal GitHub token>```

### Usage
```sh-session
Expand Down Expand Up @@ -65,7 +70,7 @@ Can be overridden with XDG_CONFIG_HOME
| ------------- |-------------|
| type | string |
| mandatory | true |
| supportedValues | gitlab |
| supportedValues | gitlab, github |


Example
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "gitcg",
"version": "0.0.9",
"description": "Customizable Changelog Generator for GitLab using Milestone",
"description": "Customizable Changelog Generator for GitLab and GitHub using Milestone",
"author": "Wei Kang @weikangchia",
"bin": {
"gitcg": "./bin/run"
Expand Down Expand Up @@ -41,6 +41,7 @@
"homepage": "https://github.com/weikangchia/gitcg",
"keywords": [
"gitlab",
"github",
"typescript",
"tools",
"developer-tools",
Expand Down
6 changes: 3 additions & 3 deletions src/changelogGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,9 @@ class ChangelogGenerator {
let title = mergeRequest.title;

if (this.#config.enableCommitSha) {
title = title.concat(
` (${this.#gitService.getCommitUrl(mergeRequest.commitSha ?? '', this.#projectPath)})`,
);
const commitUrl = this.#gitService.getCommitUrl(mergeRequest.commitSha ?? '', this.#projectPath);

title = title.concat(` ${this.#gitService.getCommitPart(mergeRequest.commitSha ?? '', commitUrl)}`);
}

mergeRequestSections[section.title].push(title);
Expand Down
3 changes: 3 additions & 0 deletions src/gitFactory.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import GitHubService = require('./github/githubService');
import GitLabService = require('./gitlab/gitlabService');
import GitService = require('./gitService');
import Config = require('./interfaces/config');
Expand All @@ -12,6 +13,8 @@ class GitFactory {
create(): GitService {
if (this.#config.service === 'gitlab') {
return new GitLabService(this.#config);
} else if (this.#config.service === 'github') {
return new GitHubService(this.#config);
}

throw new Error('unsupported git service');
Expand Down
5 changes: 5 additions & 0 deletions src/gitService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ abstract class GitService {
abstract getMergeRequests(milestone: string, projectPath: string): Promise<Array<MergeRequest>>;

abstract getCommitUrl(commitSha: string, projectPath: string): string;

getCommitPart(commitSha: string, commitUrl: string): string {
const first7CharOfSha = commitSha.substring(0, Math.min(7, commitSha.length));
return `[${first7CharOfSha}](${commitUrl.toString()})`;
}
}

export = GitService;
96 changes: 96 additions & 0 deletions src/github/githubService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import Url = require('url');
import Path = require('path');

import GitService = require('../gitService');
import Config = require('../interfaces/config');
import MergeRequest = require('../interfaces/milestone');

import { gql, GraphQLClient } from 'graphql-request';

class GitHubService extends GitService {
#client: GraphQLClient;

constructor(config: Config) {
super(config);

this.#client = new GraphQLClient('https://api.github.com/graphql', {
headers: {
authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
},
});
}

async getMergeRequests(milestone: string, projectPath: string): Promise<Array<MergeRequest>> {
const projectPathSplit = projectPath.split('/');
const query = gql`
{
repository(owner: "${projectPathSplit[0]}", name: "${projectPathSplit[1]}") {
milestones(query: "${milestone}", first: 1) {
nodes {
title
pullRequests(states: MERGED, first: 100) {
nodes {
title
labels(first: 100) {
nodes {
name
}
}
participants(first: 100) {
nodes {
login
}
}
mergeCommit {
oid
}
}
}
}
}
}
}
`;

let data;

try {
data = await this.#client.request(query);
} catch (ex) {
console.error(
'Unable to retrieve data from your GitHub, please check that your GitHub token, projectPath and your serviceUrl are correct.',
);
process.exit(1);
}

if (data.repository === null) {
console.error(
'Unable to retrieve data from your GitHub, please check that your GitHub token, projectPath and your serviceUrl are correct.',
);
process.exit(1);
}

const pullRequests = data.repository.milestones.nodes[0].pullRequests.nodes;

return pullRequests.map((pullRequest: any) => {
const labels = pullRequest.labels.nodes.map((label: any) => label.name);
const participants = pullRequest.participants.nodes.map((participant: any) => participant.login);

return {
title: pullRequest.title,
labels,
participants,
commitSha: pullRequest.mergeCommit.oid,
};
});
}

getCommitUrl(commitSha: string, projectPath: string): string {
const commitUrl = new Url.URL(this.config.serviceUrl);
commitUrl.pathname = Path.join(projectPath, 'commit', commitSha);

return commitUrl.toString();
}
}

export = GitHubService;
9 changes: 5 additions & 4 deletions src/gitlab/gitlabService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import Config = require('../interfaces/config');
import MergeRequest = require('../interfaces/milestone');

import { gql, GraphQLClient } from 'graphql-request';
import { exit } from 'process';

class GitLabService extends GitService {
#client: GraphQLClient;
Expand Down Expand Up @@ -46,9 +45,11 @@ class GitLabService extends GitService {
`;

const data = await this.#client.request(query);

if(data.project === null) {
console.error('Unable to retrieve data from your GitLab, please check that your GitLab token, projectPath and your serviceUrl are correct.');

if (data.project === null) {
console.error(
'Unable to retrieve data from your GitLab, please check that your GitLab token, projectPath and your serviceUrl are correct.',
);
process.exit(1);
}

Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import GitFactory = require('./gitFactory');
import path = require('path');

class GitCG extends Command {
static description = 'GitLab Changelog Generator';
static description = 'Git Changelog Generator';

static flags = {
milestone: flags.string({ char: 'm', description: 'title of milestone' }),
Expand Down

0 comments on commit 90af4b9

Please sign in to comment.