Skip to content

Commit

Permalink
feat: only update desc (#19)
Browse files Browse the repository at this point in the history
* feat: only update desc

* 1

* 2

* 2

* 12

* 4

* e

* r

* r
  • Loading branch information
Timmatt-Lee authored Aug 25, 2020
1 parent 45c8155 commit fb2b7be
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 29 deletions.
24 changes: 24 additions & 0 deletions .github/workflows/desc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
on:
pull_request:
types: [opened, closed]
branches:
- master

name: Test Only Update Desc

jobs:
test:
name: Test Only Update Desc
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@master

- name: Integration
uses: ./
with:
host: ${{ secrets.JIRA_BASE_URL }}
githubToken: ${{ secrets.GITHUB_TOKEN }}
isOnlyAppendDesc: true
appendDescAfterRegex: "Jira Issue Link:"

12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ jobs:
githubToken: ${{ secrets.GITHUB_TOKEN }}
project: ${{ secrets.JIRA_PROJECT_NAME }}
transition: ${{ secrets.JIRA_PR_TRANSITION_NAME }}
type: ${{ secrets.JIRA_ISSUE_TYPE }} # optional, but required if you want to create issue
component: ${{ secrets.JIRA_COMPONENT_NAME }} # optional, created issue property
version: ${{ secrets.JIRA_VERSION_PREFIX }} # optional, created issue property
board: ${{ secrets.JIRA_BOARD_ID }} # optional, sprint detection for created issue
isCreateIssue: true # optional, if you want to auto create issue
type: ${{ secrets.JIRA_ISSUE_TYPE }} # optional, but required if you want to create issue
component: ${{ secrets.JIRA_COMPONENT_NAME }} # optional, created issue property
version: ${{ secrets.JIRA_VERSION_PREFIX }} # optional, created issue property
board: ${{ secrets.JIRA_BOARD_ID }} # optional, sprint detection for created issue
isCreateIssue: true # optional, if you want to auto create issue
isOnlyAppendDesc: true # optional, if you only need update PR description
appendDescAfterRegex: "Jira Issue Link:" # optional, insert link after regex in PR description
```

Create `.github/workflows/merge-jira.yml`
Expand Down
7 changes: 7 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ inputs:
description: If you want to re-assign the issue to reporter
required: false
default: false
isOnlyAppendDesc:
description: true if you only want to update PR description
required: false
default: false
appendDescAfterRegex:
description: insert jira issue link after this regex of PR description
required: false
runs:
using: 'node12'
main: 'dist/index.js'
Expand Down
47 changes: 35 additions & 12 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1809,6 +1809,8 @@ async function main() {
const isCreateIssue = core.getInput('isCreateIssue').toLowerCase() === 'true';
const otherAssignedTransition = core.getInput('otherAssignedTransition');
const isAssignToReporter = core.getInput('isAssignToReporter').toLowerCase() === 'true';
const isOnlyAppendDesc = core.getInput('isOnlyAppendDesc').toLowerCase() === 'true';
const appendDescAfterRegex = core.getInput('appendDescAfterRegex');

const gitService = new Github({ github, githubToken });

Expand All @@ -1829,23 +1831,44 @@ async function main() {
}

// `AB-1234` Jira issue key
let [key] = pr.title.match('\\w+-\\d+');
const foundInTitle = pr.title.match('\\w+-\\d+');
let key;
if (foundInTitle) [key] = foundInTitle;
// no key detected in title, find in branch name
if (!key) {
const foundInBranch = pr.head.ref.match('\\w+-\\d+');
if (foundInBranch)[key] = foundInBranch;
}

// project = key.substring(0, key.indexOf('-'));

// if isOnlyTransition or webhook required, but no key detection
if (!key && (isOnlyTransition || webhook)) {
core.info('No jira issue detected in PR title');
process.exit(0);
}

if (webhook) {
await request({ url: webhook, method: 'post', data: { issues: [key], pr } });
await gitService.updatePR({ body: `[${key}](${host}/browse/${key})\n${pr.body}` });
core.info('webhook complete');
process.exit(0);
}

if (isOnlyAppendDesc) {
let from = 0;
let length = 0;
if (appendDescAfterRegex) {
from = pr.body.search(appendDescAfterRegex);
if (from === -1) {
from = 0;
} else {
const [word] = pr.body.match(appendDescAfterRegex);
length = word.length;
}
}

const body = `${pr.body.slice(0, from + length)}${from === 0 ? '' : ' '}[${key}](${host}/browse/${key})${from === 0 ? '\n' : ''}${pr.body.slice(from + length)}`;

await gitService.updatePR({ body });
core.info('update PR description complete');
process.exit(0);
}

if (isCreateIssue) {
if (!project) throw new Error('Creating issue need project');
if (!type) throw new Error('Creating issue need type');
Expand All @@ -1860,13 +1883,11 @@ async function main() {
const { values: [{ id: activeSprintId }] } = await jira.getSprints('active');
await jira.postMoveIssuesToSprint([key], activeSprintId);
}
} else {
core.info('Nothing process');
process.exit(0);
}

if (!key) {
core.setFailed('Issue key parse error');
core.info('No jira issue detected in PR title/branch');
process.exit(0);
}

// transit issue
Expand All @@ -1875,7 +1896,9 @@ async function main() {
// if issue was assigned by other
if (!isMeCreatedIssue) transition = otherAssignedTransition;
}
await jira.postTransitIssue(key, transition);
if (transition) {
await jira.postTransitIssue(key, transition);
}

if (isOnlyTransition) {
core.info('transit completed');
Expand Down
47 changes: 35 additions & 12 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ async function main() {
const isCreateIssue = core.getInput('isCreateIssue').toLowerCase() === 'true';
const otherAssignedTransition = core.getInput('otherAssignedTransition');
const isAssignToReporter = core.getInput('isAssignToReporter').toLowerCase() === 'true';
const isOnlyAppendDesc = core.getInput('isOnlyAppendDesc').toLowerCase() === 'true';
const appendDescAfterRegex = core.getInput('appendDescAfterRegex');

const gitService = new Github({ github, githubToken });

Expand All @@ -42,23 +44,44 @@ async function main() {
}

// `AB-1234` Jira issue key
let [key] = pr.title.match('\\w+-\\d+');
const foundInTitle = pr.title.match('\\w+-\\d+');
let key;
if (foundInTitle) [key] = foundInTitle;
// no key detected in title, find in branch name
if (!key) {
const foundInBranch = pr.head.ref.match('\\w+-\\d+');
if (foundInBranch)[key] = foundInBranch;
}

// project = key.substring(0, key.indexOf('-'));

// if isOnlyTransition or webhook required, but no key detection
if (!key && (isOnlyTransition || webhook)) {
core.info('No jira issue detected in PR title');
process.exit(0);
}

if (webhook) {
await request({ url: webhook, method: 'post', data: { issues: [key], pr } });
await gitService.updatePR({ body: `[${key}](${host}/browse/${key})\n${pr.body}` });
core.info('webhook complete');
process.exit(0);
}

if (isOnlyAppendDesc) {
let from = 0;
let length = 0;
if (appendDescAfterRegex) {
from = pr.body.search(appendDescAfterRegex);
if (from === -1) {
from = 0;
} else {
const [word] = pr.body.match(appendDescAfterRegex);
length = word.length;
}
}

const body = `${pr.body.slice(0, from + length)}${from === 0 ? '' : ' '}[${key}](${host}/browse/${key})${from === 0 ? '\n' : ''}${pr.body.slice(from + length)}`;

await gitService.updatePR({ body });
core.info('update PR description complete');
process.exit(0);
}

if (isCreateIssue) {
if (!project) throw new Error('Creating issue need project');
if (!type) throw new Error('Creating issue need type');
Expand All @@ -73,13 +96,11 @@ async function main() {
const { values: [{ id: activeSprintId }] } = await jira.getSprints('active');
await jira.postMoveIssuesToSprint([key], activeSprintId);
}
} else {
core.info('Nothing process');
process.exit(0);
}

if (!key) {
core.setFailed('Issue key parse error');
core.info('No jira issue detected in PR title/branch');
process.exit(0);
}

// transit issue
Expand All @@ -88,7 +109,9 @@ async function main() {
// if issue was assigned by other
if (!isMeCreatedIssue) transition = otherAssignedTransition;
}
await jira.postTransitIssue(key, transition);
if (transition) {
await jira.postTransitIssue(key, transition);
}

if (isOnlyTransition) {
core.info('transit completed');
Expand Down

0 comments on commit fb2b7be

Please sign in to comment.