Skip to content

Commit b595f8a

Browse files
authored
Merge pull request #4 from ThingsO2/BET-3664-auto-close-merge-monom-manifests-p-rs
Bet 3664 auto close merge monom manifests p rs
2 parents 99b3b77 + d067507 commit b595f8a

18 files changed

+11511
-0
lines changed

.github/workflows/test.yaml

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: Test
2+
on:
3+
pull_request:
4+
branches:
5+
- main
6+
7+
jobs:
8+
test:
9+
name: Test
10+
runs-on: ubuntu-latest
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
15+
- name: Install nodejs
16+
uses: actions/setup-node@v2
17+
with:
18+
node-version: '16'
19+
20+
- name: Cache dependencies
21+
uses: actions/cache@v2
22+
with:
23+
path: 'node_modules'
24+
key: ${{ hashFiles('yarn.lock') }}
25+
26+
- name: Install
27+
run: yarn install --pure-lockfile
28+
29+
- name: Test
30+
run: yarn test --coverage
31+
env:
32+
GITHUB_TOKEN: ${{ secrets.KINGKONG_GITHUB_TOKEN }}
33+
34+
- name: Get Package Data
35+
id: read-package
36+
run: |
37+
echo ::set-output name=NAME::$(node -p "require('./package.json').name") | sed 's/@monom\///g'
38+
echo ::set-output name=VERSION::$(node -p "require('./package.json').version")
39+
40+
- name: SonarQube Scan
41+
uses: sonarsource/sonarqube-scan-action@master
42+
env:
43+
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
44+
SONAR_HOST_URL: ${{ secrets.SONARQUBE_HOST }}
45+
with:
46+
args: >
47+
-Dsonar.projectKey=front-${{ steps.read-package.outputs.NAME }}
48+
-Dsonar.projectName=${{ steps.read-package.outputs.NAME }}
49+
-Dsonar.projectVersion=${{ steps.read-package.outputs.VERSION }}
50+
-Dsonar.javascript.lcov.reportPaths=coverage/lcov.info
51+
-Dsonar.sources=src
52+
-Dsonar.tests=__tests__
53+
-Dsonar.exclusions=src/__mocks__/**.*
54+
55+
- name: SonarQube Quality Gate check
56+
uses: sonarsource/sonarqube-quality-gate-action@master
57+
env:
58+
SONAR_TOKEN: ${{ secrets.SONARQUBE_TOKEN }}
59+
with:
60+
scanMetadataReportFile: .scannerwork/report-task.txt

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
node_modules

__tests__/getLabels.test.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
jest.mock('@actions/core')
2+
import { Octokit } from "@octokit/core"
3+
import { RequestError } from "@octokit/request-error"
4+
import { getLabels } from '../src/getLabels'
5+
6+
const octokit = new Octokit({
7+
auth: process.env.GITHUB_TOKEN
8+
})
9+
10+
const nooctokit = new Octokit({
11+
auth: '1234567890'
12+
})
13+
14+
const prNumber = 15
15+
const repoName = 'monom-manifests'
16+
const owner = 'ThingsO2'
17+
18+
test('get labels', async () => {
19+
const labels = await getLabels(octokit, prNumber, repoName, owner)
20+
expect(labels).toEqual(['test'])
21+
})
22+
23+
test('get labels not found', async () => {
24+
try {
25+
await getLabels(octokit, 999, repoName, owner)
26+
} catch (error) {
27+
expect(error).toBeInstanceOf(RequestError)
28+
expect(error).toHaveProperty('status', 404)
29+
}
30+
})
31+
32+
test('get labels error', async () => {
33+
try {
34+
await getLabels(nooctokit, prNumber, repoName, owner)
35+
} catch (error) {
36+
expect(error).toBeInstanceOf(RequestError)
37+
expect(error).toHaveProperty('status', 401)
38+
}
39+
})

__tests__/main.test.ts

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
jest.mock('@actions/core')
2+
import * as core from '@actions/core'
3+
import { Octokit } from "@octokit/core"
4+
import { main } from '../src/main'
5+
6+
const octokit = new Octokit({
7+
auth: process.env.GITHUB_TOKEN
8+
})
9+
10+
const nooctokit = new Octokit({
11+
auth: '1234567890'
12+
})
13+
14+
const input = {
15+
prNumber: 135,
16+
repoName: 'monom-manifests',
17+
owner: 'ThingsO2'
18+
}
19+
20+
test('main', async () => {
21+
const mainResult = await main(octokit, input, false)
22+
expect(mainResult).toBe(core.ExitCode.Success)
23+
})
24+
25+
test('main failed', async () => {
26+
const mainResult = await main(nooctokit, input, false)
27+
expect(mainResult).toBe(core.ExitCode.Failure)
28+
})

__tests__/mergePR.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
jest.mock('@actions/core')
2+
import { Octokit } from "@octokit/core"
3+
import { RequestError } from "@octokit/request-error"
4+
import { mergePR } from '../src/mergePR'
5+
6+
const octokit = new Octokit({
7+
auth: process.env.GITHUB_TOKEN
8+
})
9+
10+
const nooctokit = new Octokit({
11+
auth: '1234567890'
12+
})
13+
14+
const prNumber = 8
15+
const repoName = 'github-action-close-code-pr'
16+
const owner = 'ThingsO2'
17+
18+
test('merge PR', async () => {
19+
const mergeResult = await mergePR(octokit, prNumber, repoName, owner)
20+
expect(mergeResult).toBeDefined()
21+
expect(mergeResult.merged).toBeTruthy()
22+
expect(mergeResult.message).toEqual('Pull Request successfully merged')
23+
})
24+
25+
test('merge PR already merged', async () => {
26+
try {
27+
await mergePR(octokit, 5, repoName, owner)
28+
} catch (error) {
29+
expect(error).toBeInstanceOf(RequestError)
30+
expect(error).toHaveProperty('status', 405)
31+
}
32+
})
33+
34+
test('merge PR no token', async () => {
35+
try {
36+
await mergePR(nooctokit, prNumber, repoName, owner)
37+
} catch (error) {
38+
expect(error).toBeInstanceOf(RequestError)
39+
expect(error).toHaveProperty('status', 401)
40+
}
41+
})

__tests__/searchCodePR.test.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
jest.mock('@actions/core')
2+
import { Octokit } from "@octokit/core"
3+
import { searchCodePR } from '../src/searchCodePR'
4+
5+
const octokit = new Octokit({
6+
auth: process.env.GITHUB_TOKEN
7+
})
8+
9+
const nooctokit = new Octokit({
10+
auth: '1234567890'
11+
})
12+
13+
const prNumber = 135
14+
const repoName = 'monom-manifests'
15+
const owner = 'ThingsO2'
16+
17+
test('search code PR', async () => {
18+
const res = await searchCodePR(octokit, prNumber, repoName, owner)
19+
expect(res).toBeDefined()
20+
expect(res?.number).toBe(36)
21+
expect(res?.head.repo.name).toBe('dp-monom-2-awm')
22+
})
23+
24+
test('search code PR not found', async () => {
25+
try {
26+
await searchCodePR(octokit, 15, repoName, owner)
27+
} catch (error) {
28+
expect(error).toHaveProperty('status', 404)
29+
}
30+
})
31+
32+
test('search code PR error', async () => {
33+
try {
34+
await searchCodePR(nooctokit, prNumber, repoName, owner)
35+
} catch (error) {
36+
expect(error).toHaveProperty('status', 401)
37+
}
38+
})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
jest.mock('@actions/core')
2+
import { Octokit } from "@octokit/core"
3+
import { searchPRwithLabels } from '../src/searchPRwithLabels'
4+
5+
const octokit = new Octokit({
6+
auth: process.env.GITHUB_TOKEN
7+
})
8+
9+
const nooctokit = new Octokit({
10+
auth: '1234567890'
11+
})
12+
13+
const repoName = 'monom-manifests'
14+
const owner = 'ThingsO2'
15+
16+
test('search PR with Labels', async () => {
17+
const issue = await searchPRwithLabels(octokit, repoName, owner, ['test'])
18+
expect(issue).toEqual([15])
19+
})
20+
21+
test('search PR with Labels no found', async () => {
22+
const issue = await searchPRwithLabels(octokit, repoName, owner, ['test-no-found'])
23+
expect(issue).toEqual([])
24+
})
25+
26+
test('search PR with Labels error', async () => {
27+
try {
28+
await searchPRwithLabels(nooctokit, repoName, owner, ['test'])
29+
} catch (error) {
30+
expect(error).toHaveProperty('status', 401)
31+
}
32+
})

action.yml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
name: 'Close the original code PR when the app is deployed'
2+
description: 'Close the original code PR when the app is deployed'
3+
inputs:
4+
pr_number:
5+
description: 'The PR number to close'
6+
required: true
7+
repo_name:
8+
description: 'The repo name'
9+
required: true
10+
owner:
11+
description: 'The repo owner'
12+
required: true
13+
runs:
14+
using: 'node16'
15+
main: 'dist/index.js'
16+
branding:
17+
icon: 'lock'
18+
color: 'gray-dark'

0 commit comments

Comments
 (0)