Remove duplicated code (#1137)

* Remove duplicated code on ghe config apply

* Add funciton to replace duplicated cod

* Fix comment

* Fix java test duplicated code

* Fix python test duplicated code

* Update javascript es test acording to change

* Fix linters bash

* Fix javascript standard linters on javascript es good
This commit is contained in:
Gabriel Diaz 2021-01-12 13:54:00 -05:00 committed by GitHub
parent 594be7e0b2
commit 5c31b85619
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 190 additions and 390 deletions

View file

@ -31,6 +31,22 @@ Header() {
info "-------------------------------------------------------"
}
################################################################################
#### Function CheckShellErrors #################################################
CheckShellErrors() {
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
error "$1"
fatal "$2"
fi
}
################################################################################
#### Function CleanTestFiles ###################################################
CleanTestFiles() {
info "-------------------------------------------------------"
@ -44,18 +60,7 @@ CleanTestFiles() {
find "${GITHUB_WORKSPACE}" -type f -name "*_bad_*" -o -path "*javascript_prettier*" -name "*javascript_good*" 2>&1
)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
error "ERROR! failed to get list of all files!"
fatal "ERROR:[${FIND_CMD[*]}]"
fi
CheckShellErrors "ERROR! failed to get list of all files!" "ERROR:[${FIND_CMD[*]}]"
############################################################
# Get the directory and validate it came from tests folder #
@ -78,18 +83,7 @@ CleanTestFiles() {
rm -f "$FILE" 2>&1
)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
error "ERROR! failed to remove file:[${FILE}]!"
fatal "ERROR:[${REMOVE_FILE_CMD[*]}]"
fi
CheckShellErrors "ERROR! failed to remove file:[${FILE}]!" "ERROR:[${REMOVE_FILE_CMD[*]}]"
fi
done
}
@ -107,18 +101,7 @@ CleanTestDockerFiles() {
find "${GITHUB_WORKSPACE}" -type f -name "*Dockerfile" 2>&1
)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
error "ERROR! failed to get list of all file for Docker!"
fatal "ERROR:[${FIND_CMD[*]}]"
fi
CheckShellErrors "ERROR! failed to get list of all file for Docker!" "ERROR:[${FIND_CMD[*]}]"
############################################################
# Get the directory and validate it came from tests folder #
@ -141,18 +124,7 @@ CleanTestDockerFiles() {
rm -f "$FILE" 2>&1
)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
error "ERROR! failed to remove file:[${FILE}]!"
fatal "ERROR:[${REMOVE_FILE_CMD[*]}]"
fi
CheckShellErrors "ERROR! failed to remove file:[${FILE}]!" "ERROR:[${REMOVE_FILE_CMD[*]}]"
fi
done
}
@ -170,19 +142,7 @@ CleanSHAFolder() {
sudo rm -rf "${GITHUB_SHA}" 2>&1
)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
# Error
error "ERROR! Failed to remove folder:[${GITHUB_SHA}]!"
fatal "ERROR:[${REMOVE_CMD}]"
fi
CheckShellErrors "ERROR! Failed to remove folder:[${GITHUB_SHA}]!" "ERROR:[${REMOVE_CMD}]"
}
################################################################################
#### Function RenameTestFolder #################################################
@ -198,18 +158,7 @@ RenameTestFolder() {
mv "${TEST_FOLDER}" "${CLEAN_FOLDER}" 2>&1
)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
error "ERROR! failed to move test folder!"
fatal "ERROR:[${RENAME_FOLDER_CMD[*]}]"
fi
CheckShellErrors "ERROR! failed to move test folder!" "ERROR:[${RENAME_FOLDER_CMD[*]}]"
}
################################################################################
#### Function CleanPowershell ##################################################
@ -228,18 +177,7 @@ CleanPowershell() {
find "${GITHUB_WORKSPACE}" -type f -name "*.psd1" 2>&1
)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
error "ERROR! failed to get list of all file for *.psd1!"
fatal "ERROR:[${FIND_CMD[*]}]"
fi
CheckShellErrors "ERROR! failed to get list of all file for *.psd1!" "ERROR:[${FIND_CMD[*]}]"
############################################################
# Get the directory and validate it came from tests folder #
@ -262,18 +200,7 @@ CleanPowershell() {
rm -f "$FILE" 2>&1
)
#######################
# Load the error code #
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ $ERROR_CODE -ne 0 ]; then
error "ERROR! failed to remove file:[${FILE}]!"
fatal "ERROR:[${REMOVE_FILE_CMD[*]}]"
fi
CheckShellErrors "ERROR! failed to remove file:[${FILE}]!" "ERROR:[${REMOVE_FILE_CMD[*]}]"
fi
done
}

View file

@ -20,6 +20,33 @@ PROCESS_CHECK=0 # Count of times to check the process
########################### SUB ROUTINES BELOW #################################
################################################################################
################################################################################
#### Function CheckShellErrors #################################################
CheckShellErrors() {
COUNTER=$1
##############################
# Check the shell for errors #
##############################
if [ "${ERROR_CODE}" -ne 0 ]; then
error "Failed to sleep!"
error "[${SLEEP_CMD}]"
info "Will try to call apply as last effort..."
####################################
# Call config apply as last effort #
####################################
RunConfigApply
else
#####################
# Increment counter #
#####################
((COUNTER++))
##########################################
# Try to check for the pid/process again #
##########################################
$2
fi
return "$COUNTER"
}
################################################################################
#### Function CheckGHEPid ######################################################
CheckGHEPid() {
##################################
@ -48,27 +75,7 @@ CheckGHEPid() {
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
error "Failed to sleep!"
error "[${SLEEP_CMD}]"
info "Will try to call apply as last effort..."
####################################
# Call config apply as last effort #
####################################
RunConfigApply
else
#####################
# Increment counter #
#####################
((PID_CHECK++))
##################################
# Try to check for the pid again #
##################################
CheckGHEPid
fi
PID_CHECK=CheckShellErrors "PID_CHECK" "CheckGHEPid"
fi
fi
}
@ -111,27 +118,7 @@ CheckGHEProcess() {
#######################
ERROR_CODE=$?
##############################
# Check the shell for errors #
##############################
if [ ${ERROR_CODE} -ne 0 ]; then
error "Failed to sleep!"
error "[${SLEEP_CMD}]"
info "Will try to call apply as last effort..."
####################################
# Call config apply as last effort #
####################################
RunConfigApply
else
#####################
# Increment counter #
#####################
((PROCESS_CHECK++))
######################################
# Try to check for the process again #
######################################
CheckGHEProcess
fi
PROCESS_CHECK=CheckShellErrors "PROCESS_CHECK" "CheckGHEProcess"
fi
fi
}

View file

@ -1,7 +1,8 @@
var http = require('http')
var createHandler = require('github-webhook-handler')
var handler = createHandler( { path : /webhook, secret : (process.env.SECRET) })
var handler = createHandler({
path: /webhook, secret : (process.env.SECRET) })
var userArray = ['user1']
here is some garbage = that
@ -70,19 +71,6 @@ function getTeamID (org) {
'Content-Type': 'application/json'
}
}
let body = []
const req = https.request(options, (res) => {
res.on('data', (chunk) => {
body.push(chunk)
}).on('end', () => {
body = JSON.parse(Buffer.concat(body))
body.forEach(item => {
if (item.name === teamName) {
teamId = item.id
}
})
})
})
req.on('error, (error) => {
console.error(error)
@ -203,20 +191,6 @@ function getRepositories (org) {
'Content-Type': 'application/json'
}
}
let body = []
const req = https.request(options, (res) => {
res.on('data', (chunk) => {
body.push(chunk)
}).on('end', () => {
body = JSON.parse(Buffer.concat(body))
body.forEach(item => {
orgRepos.push(item.full_name)
console.log(item.full_name)
})
})
})
req.on('error', (error) => {
console.error(error)

View file

@ -1,17 +1,15 @@
var http = require('http')
var createHandler = require('github-webhook-handler')
var handler = createHandler({ path: '/webhook', secret: (process.env.SECRET) })
const http = require('http')
const createHandler = require('github-webhook-handler')
const handler = createHandler({ path: '/webhook', secret: (process.env.SECRET) })
var userArray = ['user1']
const userArray = ['user1']
var teamDescription = 'Team of Robots'
var teamPrivacy = 'closed' // closed (visible) / secret (hidden) are options here
const teamDescription = 'Team of Robots'
const teamPrivacy = 'closed' // closed (visible) / secret (hidden) are options here
var teamName = process.env.GHES_TEAM_NAME
var teamAccess = 'pull' // pull,push,admin options here
var teamId = ''
var orgRepos = []
const teamName = process.env.GHES_TEAM_NAME
const teamAccess = 'pull' // pull,push,admin options here
const teamId = ''
// var creator = ""
@ -31,8 +29,6 @@ handler.on('repository', function (event) {
if (event.payload.action === 'created') {
const repo = event.payload.repository.full_name
console.log(repo)
const org = event.payload.repository.owner.login
getTeamID(org)
setTimeout(checkTeamIDVariable, 1000)
}
})
@ -41,51 +37,13 @@ handler.on('team', function (event) {
// TODO user events such as being removed from team or org
if (event.payload.action === 'deleted') {
// const name = event.payload.team.name
const org = event.payload.organization.login
getRepositories(org)
setTimeout(checkReposVariable, 5000)
} else if (event.payload.action === 'removed_from_repository') {
const org = event.payload.organization.login
getTeamID(org)
// const repo = event.payload.repository.full_name
setTimeout(checkTeamIDVariable, 1000)
}
})
function getTeamID (org) {
const https = require('https')
const options = {
hostname: (process.env.GHE_HOST),
port: 443,
path: '/api/v3/orgs/' + org + '/teams',
method: 'GET',
headers: {
Authorization: 'token ' + (process.env.GHE_TOKEN),
'Content-Type': 'application/json'
}
}
let body = []
const req = https.request(options, (res) => {
res.on('data', (chunk) => {
body.push(chunk)
}).on('end', () => {
body = JSON.parse(Buffer.concat(body))
body.forEach(item => {
if (item.name === teamName) {
teamId = item.id
}
})
})
})
req.on('error', (error) => {
console.error(error)
})
req.end()
}
function checkTeamIDVariable (repo) {
if (typeof teamId !== 'undefined') {
addTeamToRepo(repo, teamId)
@ -143,8 +101,7 @@ function reCreateTeam (org) {
name: teamName,
description: teamDescription,
privacy: teamPrivacy,
maintainers: userArray,
repo_names: orgRepos
maintainers: userArray
})
const options = {
@ -178,38 +135,3 @@ function reCreateTeam (org) {
req.write(data)
req.end()
}
function getRepositories (org) {
orgRepos = []
const https = require('https')
const options = {
hostname: (process.env.GHE_HOST),
port: 443,
path: '/api/v3/orgs/' + org + '/repos',
method: 'GET',
headers: {
Authorization: 'token ' + (process.env.GHE_TOKEN),
'Content-Type': 'application/json'
}
}
let body = []
const req = https.request(options, (res) => {
res.on('data', (chunk) => {
body.push(chunk)
}).on('end', () => {
body = JSON.parse(Buffer.concat(body))
body.forEach(item => {
orgRepos.push(item.full_name)
console.log(item.full_name)
})
})
})
req.on('error', (error) => {
console.error(error)
})
req.end()
}

View file

@ -2,6 +2,6 @@ TAP version 13
1..2
not ok 1 - javascript_bad_1.js
---
message: \n/tmp/lint/.automation/test/javascript_es/javascript_bad_1.js\n 4 39 error Parsing error Unterminated regular expression literal\n\n✖ 1 problem (1 error, 0 warnings)\n
message: \n/tmp/lint/.automation/test/javascript_es/javascript_bad_1.js\n 5 9 error Parsing error Unterminated regular expression literal\n\n✖ 1 problem (1 error, 0 warnings)\n
...
ok 2 - javascript_good_1.js

View file

@ -1,7 +1,8 @@
var http = require('http')
var createHandler = require('github-webhook-handler')
var handler = createHandler( { path : /webhook, secret : (process.env.SECRET) })
var handler = createHandler({
path: /webhook, secret : (process.env.SECRET) })
var userArray = ['user1']
here is some garbage = that
@ -70,19 +71,6 @@ function getTeamID (org) {
'Content-Type': 'application/json'
}
}
let body = []
const req = https.request(options, (res) => {
res.on('data', (chunk) => {
body.push(chunk)
}).on('end', () => {
body = JSON.parse(Buffer.concat(body))
body.forEach(item => {
if (item.name === teamName) {
teamId = item.id
}
})
})
})
req.on('error, (error) => {
console.error(error)
@ -203,20 +191,6 @@ function getRepositories (org) {
'Content-Type': 'application/json'
}
}
let body = []
const req = https.request(options, (res) => {
res.on('data', (chunk) => {
body.push(chunk)
}).on('end', () => {
body = JSON.parse(Buffer.concat(body))
body.forEach(item => {
orgRepos.push(item.full_name)
console.log(item.full_name)
})
})
})
req.on('error', (error) => {
console.error(error)

View file

@ -26,6 +26,12 @@ headers = {
}
def make_request(query, query_variables):
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
return response
def create_label(repo_id, label):
"""
Create label in the supplied repo.
@ -51,8 +57,7 @@ def create_label(repo_id, label):
) as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers).json()
response = make_request(query, query_variables).json()
print("Created label {label}".format(label=label["name"]))
return response
@ -78,8 +83,7 @@ def get_labels(owner, repo):
) as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
response = make_request(query, query_variables)
status_code = response.status_code
result = response.json()

View file

@ -26,6 +26,12 @@ headers = {
}
def make_request(query, query_variables):
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
return response
def create_label(repo_id, label):
"""
Create label in the supplied repo.
@ -51,8 +57,7 @@ def create_label(repo_id, label):
) as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers).json()
response = make_request(query, query_variables).json()
print("Created label {label}".format(label=label["name"]))
return response
@ -78,8 +83,7 @@ def get_labels(owner, repo):
) as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
response = make_request(query, query_variables).json()
status_code = response.status_code
result = response.json()

View file

@ -26,6 +26,12 @@ headers = {
}
def make_request(query, query_variables):
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
return response
def create_label(repo_id, label):
"""
Create label in the supplied repo.
@ -51,8 +57,7 @@ def create_label(repo_id, label):
) as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers).json()
response = make_request(query, query_variables).json()
print("Created label {label}".format(label=label["name"]))
return response
@ -78,8 +83,7 @@ def get_labels(owner, repo):
) as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
response = make_request(query, query_variables).json()
status_code = response.status_code
result = response.json()

View file

@ -26,6 +26,12 @@ headers = {
}
def make_request(query, query_variables):
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
return response
def create_label(repo_id, label):
"""
Create label in the supplied repo.
@ -51,8 +57,7 @@ def create_label(repo_id, label):
) as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers).json()
response = make_request(query, query_variables).json()
print("Created label {label}".format(label=label["name"]))
return response
@ -78,8 +83,7 @@ def get_labels(owner, repo):
) as query_file:
query = "".join(query_file.readlines())
payload = {"query": query, "variables": query_variables}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
response = make_request(query, query_variables).json()
status_code = response.status_code
result = response.json()