Skip to content

Commit

Permalink
Merge branch 'release/1.3.0' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
tktcorporation committed Aug 2, 2020
2 parents e163aeb + 832c958 commit b8ea248
Show file tree
Hide file tree
Showing 14 changed files with 139 additions and 27 deletions.
18 changes: 18 additions & 0 deletions .github/workflows/issue-link-repo.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: 'Issue Link Repository'
on:
pull_request:
types: [opened]

jobs:
issue-link:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Add issue links with repository option
uses: ./
with:
repo-token: '${{ secrets.GITHUB_TOKEN }}'
branch-prefix: 'issue-'
position: 'top'
resolve: 'false'
repository: 'tktcorporation/tktcorporation'
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Add Issue Link
# Add Issue Links

A GitHub Action for [Linking a pull request to an issue](https://help.github.com/en/enterprise/2.17/user/github/managing-your-work-on-github/linking-a-pull-request-to-an-issue).

## :arrow_forward: Usage

this action add texts like this into the head of your Pull Request description when it is opened.
This action add texts into a body of Pull Request like this when it is opened.

```md
# Related Issue
Expand All @@ -17,7 +17,7 @@ this action add texts like this into the head of your Pull Request description w

### Create a workflow

Add `.github/workflows/issue-reference.yml` with the following:
Add `.github/workflows/issue-link.yml` with the following:

```yml
name: 'Issue Link'
Expand All @@ -35,19 +35,21 @@ jobs:
branch-prefix: 'issue-' # required
position: 'top' # optional (default: "bottom")
resolve: 'true' # optional (default: "false")
repository: 'tkt-actions/add-issue-links' # optional
```
### Set up required parameters
Need to contain the required parameters on the workflow file.
- `repo-token` - A token of the repository. It can be passed with `{{ secrets.GITHUB_TOKEN }}`
- `branch-prefix` - A prefix of the branch name for finding a related issue (e.g. `issue-`).

### Set up optional parameters

- `branch-prefix` - A prefix of the branch name for finding a related issue. Default: `issue-`
- `position` - Changing position of link text section. ("top" or "bottom" allowed)
- `resolve` - Adding \"resolve\" prefix to close a related issue when the branch is merged. ("true" or "false" allowed)
- `repository` - Changing a base repository related to an issue. (e.g. `tkt-actions/issue-links`)

### Add a section contained a link of related issue to a pull request

Expand Down
32 changes: 19 additions & 13 deletions action.yml
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
name: "issue link"
description: "A GitHub Action to add a related issue link to a pull request."
author: "tktcorporation"
name: 'Add an issue link'
description: 'Use to add an issue link related to your pull requests.'
author: 'tktcorporation'
inputs:
branch-prefix:
description: "(Required) A token of the repository. It can be passed with `{{ secrets.GITHUB_TOKEN }}`"
required: true
repo-token:
description: "(Required) A prefix of the branch name for finding a related issue (e.g. `issue-`)."
description: 'A token of the repository. It can be passed with `{{ secrets.GITHUB_TOKEN }}`'
required: true
branch-prefix:
description: 'A prefix of the branch name for finding a related issue. Default: `issue-`'
required: false
dafault: 'issue-'
position:
description: "(Optional) For changing position of linking text section. (\"top\" or \"bottom\")"
description: '`top` or `bottom`. A position name for inserting issue link section. Default: `bottom`'
required: false
default: 'bottom'
resolve:
description: "(Optional) Adding \"resolve\" prefix to close a related issue when the branch is merged. (\"true\" or \"false\")"
description: '`true` to add "Resolve" prefix to an issue link. "Resolve" prefix close an related issue when the branch is merged. Default: `true`'
required: false
default: 'true'
repository:
description: 'Repository name for using an other repository (e.g. "tkt-actions/issue-links")'
required: false
runs:
using: "node12"
main: "dist/index.js"
using: 'node12'
main: 'dist/index.js'
branding:
icon: "link"
color: "gray-dark"
icon: 'link'
color: 'gray-dark'
2 changes: 1 addition & 1 deletion dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "add-issue-links",
"version": "1.2.0",
"version": "1.3.0",
"description": "A GitHub Action for adding an issue reference to a pull request.",
"main": "lib/main.js",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion src/application/repository/PullRequestRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { Octokit } from '../../types/Octokit';
export interface PullRequestRepository {
update(
pullRequest: PullRequest,
): Promise<Octokit.Response<Octokit.PullsUpdateResponse>>;
): Promise<Octokit.Response<Octokit.PullsUpdateResponse>>; // TODO: Refactor
get(number: number, owner: string, repo: string): Promise<PullRequest>;
}
4 changes: 3 additions & 1 deletion src/application/service/PullRequestRecordService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Position } from './../../domain/position/Position';
import { Resolve } from './../../domain/resolve/Resolve';
import { IssueLinkSection } from './../../domain/pullRequest/pullRequestBody/issueLinkSection/IssueLinkSection';
import { IssueLink } from './../../domain/pullRequest/pullRequestBody/issueLinkSection/issueLink/IssueLinkText';
import { Repository } from 'src/domain/repository/Repository';

export class PullRequestRecordService {
constructor(private readonly pullRequestRepository: PullRequestRepository) {}
Expand All @@ -12,6 +13,7 @@ export class PullRequestRecordService {
issueNumber: number,
position: Position,
resolve: Resolve,
repository: Repository | undefined,
context: Context,
) => {
const { repo, issue } = context;
Expand All @@ -21,7 +23,7 @@ export class PullRequestRecordService {
repo.repo,
);
pr.body.addRelatedIssueSection(
new IssueLinkSection([new IssueLink(issueNumber, resolve)]),
new IssueLinkSection([new IssueLink(issueNumber, resolve, repository)]),
position,
);
return await this.pullRequestRepository.update(pr);
Expand Down
7 changes: 5 additions & 2 deletions src/domain/pullRequest/PullRequest.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Position } from '../position/Position';
import { IssueLink } from './pullRequestBody/issueLinkSection/issueLink/IssueLinkText';
import { Resolve } from '../resolve/Resolve';
import { IssueLinkSection } from './pullRequestBody/issueLinkSection/IssueLinkSection';
import { Repository } from '../repository/Repository';

describe('PullRequest', () => {
it('into bottom', () => {
Expand All @@ -15,11 +16,13 @@ describe('PullRequest', () => {
'pr-action',
);
pr.body.addRelatedIssueSection(
new IssueLinkSection([new IssueLink(12, Resolve.true())]),
new IssueLinkSection([
new IssueLink(12, Resolve.true(), Repository.build('owner/sample')),
]),
Position.bottom(),
);
expect(pr.body.value).toBe(
`some description\n\n# Related Issue\n\n- Resolve #12`,
`some description\n\n# Related Issue\n\n- Resolve owner/sample#12`,
);
});
});
13 changes: 13 additions & 0 deletions src/domain/pullRequest/pullRequestBody/PullRequestBody.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { PullRequestBody } from './PullRequestBody';
import { Position } from '../../position/Position';
import { Resolve } from '../../resolve/Resolve';
import { Repository } from '../../../domain/repository/Repository';
import { IssueLinkSection } from './issueLinkSection/IssueLinkSection';
import { IssueLink } from './issueLinkSection/issueLink/IssueLinkText';

Expand Down Expand Up @@ -49,4 +50,16 @@ describe('PullRequestBody', () => {
);
});
});
describe('Repository option', () => {
const pullRequestBody = new PullRequestBody('description');
pullRequestBody.addRelatedIssueSection(
new IssueLinkSection([
new IssueLink(12, Resolve.false(), Repository.build('owner/sample')),
]),
Position.bottom(),
);
expect(pullRequestBody.value).toBe(
`description\n\n# Related Issue\n\n- owner/sample#12`,
);
});
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IssueLink } from './IssueLinkText';
import { Resolve } from './../../../../resolve/Resolve';
import { Repository } from './../../../../repository/Repository';

describe('IssueLink', () => {
describe('resolve false', () => {
Expand All @@ -14,10 +15,14 @@ describe('IssueLink', () => {
describe('resolve true', () => {
let issueLink: IssueLink;
beforeAll(() => {
issueLink = new IssueLink(332, Resolve.true());
issueLink = new IssueLink(
332,
Resolve.true(),
Repository.build('sample/name'),
);
});
it('createText', () => {
expect(issueLink.createText()).toBe('Resolve #332');
expect(issueLink.createText()).toBe('Resolve sample/name#332');
});
});
});
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
import { Resolve } from './../../../../../domain/resolve/Resolve';
import { TextMapping } from '../text/Text';
import { Repository } from 'src/domain/repository/Repository';

export class IssueLink {
private static readonly resolveStr = 'Resolve';

constructor(
private readonly issueNumber: number,
private readonly resolve: Resolve,
private readonly repository?: Repository,
) {}

private createRepositoryText = () =>
this.repository ? this.repository.createText() : TextMapping.blank;
private createIssueLink = (): string => '#' + this.issueNumber;
private createResolvePrefix = (): string =>
this.resolve.isTrue
? IssueLink.resolveStr + TextMapping.whitespace
: TextMapping.blank;

createText = () => this.createResolvePrefix() + this.createIssueLink();
createText = () =>
this.createResolvePrefix() +
this.createRepositoryText() +
this.createIssueLink();
}
24 changes: 24 additions & 0 deletions src/domain/repository/Repository.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Repository } from './Repository';

describe('Repository', () => {
describe('new', () => {
let repository: Repository;
beforeAll(() => {
repository = new Repository('sample', 'name');
});
it('createText', () => {
expect(repository.createText()).toBe('sample/name');
});
});
describe('build', () => {
let repository: Repository | undefined;
beforeAll(() => {
repository = Repository.build('sample/name');
});
it('createText', () => {
expect(repository).toBeDefined();
if (repository === undefined) return;
expect(repository.createText()).toBe('sample/name');
});
});
});
29 changes: 29 additions & 0 deletions src/domain/repository/Repository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
export class Repository {
static build = (str: string) => {
const match = Repository.matchFields(str);
if (!match) return undefined;
const fields = Repository.extractFields(match);
if (!fields) return undefined;
return new Repository(fields?.username, fields?.repositoryName);
};
constructor(private username: string, private repositoryName: string) {}
createText = () => this.username + '/' + this.repositoryName;

private static readonly fieldsRegex = /^(.+)\/(.+)$/;
private static matchFields = (str: string) =>
str.match(Repository.fieldsRegex);
private static extractFields = (
match: RegExpMatchArray,
): {
username: string;
repositoryName: string;
} | null => {
const username = match[1];
const repositoryName = match[2];
if (!username || !repositoryName) return null;
return {
username,
repositoryName,
};
};
}
5 changes: 4 additions & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { PullRequestRecordService } from './application/service/PullRequestRecor
import { BranchQueryService } from './application/service/BranchQueryService';
import { Position } from './domain/position/Position';
import { Resolve } from './domain/resolve/Resolve';
import { Repository } from './domain/repository/Repository';

async function run(): Promise<void> {
try {
Expand All @@ -14,6 +15,7 @@ async function run(): Promise<void> {
branchPrefix: core.getInput('branch-prefix', { required: true }),
position: core.getInput('position', { required: false }),
resolve: core.getInput('resolve', { required: false }),
repository: core.getInput('repository', { required: false }),
};

const issueNumber = new BranchQueryService(github.context)
Expand All @@ -26,11 +28,12 @@ async function run(): Promise<void> {
issueNumber,
Position.build(withInput.position) ?? Position.bottom(),
Resolve.buildFromString(withInput.resolve) ?? Resolve.false(),
Repository.build(withInput.repository),
github.context,
);

core.info(
`Added issue #${issueNumber} reference to pull request #${prUpdateResult.data.number}.\n${prUpdateResult.data.html_url}`,
`Added issue #${issueNumber} reference to pull request ${withInput.repository}#${prUpdateResult.data.number}.\n${prUpdateResult.data.html_url}`,
);
} catch (error) {
if (error instanceof BranchIssueNumNotFound)
Expand Down

0 comments on commit b8ea248

Please sign in to comment.