Commit 6aa16da9 authored by José Henrique's avatar José Henrique
Browse files

Merge branch 'staging' into 'master'

Rework everything

See merge request !1
parents 2995885a c574e701
image: docker:stable
before_script:
- apk add bash coreutils git
build:
stage: build
services:
- docker:dind
script:
- |-
if [ -n "$GIT_PR_CLOSED" ]; then
exit 0
fi
echo "Cloning repository..."
rm -rf .git
git clone https://github.com/PixelExperience/official_devices
if [ -n "$GIT_PR_NUMBER" ]; then
cd official_devices
git fetch origin "pull/$GIT_PR_NUMBER/head"
git checkout FETCH_HEAD
cd ..
fi
docker build --no-cache -t official_devices_ci:latest ./
docker run -e RUBYOPT='-W0' -e TELEGRAM_TOKEN=${TELEGRAM_TOKEN} -e GH_PERSONAL_TOKEN=${GH_PERSONAL_TOKEN} -e GIT_PR_NUMBER=${GIT_PR_NUMBER} -e BUILD_URL=${CI_JOB_URL} official_devices_ci:latest
only:
- triggers
- web
- api
FROM pixelexperience/official_devices_ci:latest
COPY . /app
RUN rm -rf infra_resources
COPY infra_resources/ci-linter/ /app
COPY .gitignore /app
WORKDIR /app
RUN cargo build --release
CMD ["bash", "runner.sh"]
FROM pixelexperience/official_devices_ci:latest
RUN npm install xmlhttprequest
RUN npm install moment
COPY . /app
WORKDIR /app
CMD ["bash", "maintainers.sh"]
#!/usr/bin/env bash
# Copyright © 2020 PixelExperience Project
#
# SPDX-License-Identifier: GPL-3.0
#
### Script to test and format our jsons
ADMINS="@Hlcpereira @baalajimaestro @Shreejoy\_Dash @Rk585 @itsjoeoui @Hasaber8"
BUILD_START=$(date +"%s")
BUILD_END=$(date +"%s")
DIFF=$((BUILD_END - BUILD_START))
COMMIT_MESSAGE="$(git log -1 --pretty=%B)"
COMMIT_SMALL_HASH="$(git rev-parse --short HEAD)"
COMMIT_HASH="$(git rev-parse --verify HEAD)"
git remote rm origin
git remote add origin https://baalajimaestro:"${GH_PERSONAL_TOKEN}"@github.com/PixelExperience/official_devices.git
function sendAdmins() {
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendmessage" --data "text=${*}&chat_id=-1001463677498&disable_web_page_preview=true&parse_mode=Markdown" > /dev/null
}
function sendAdmins_HTML() {
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendmessage" --data "text=${*}&chat_id=-1001463677498&disable_web_page_preview=true&parse_mode=html" > /dev/null
}
function sendMaintainers() {
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendmessage" --data "text=${*}&chat_id=-1001287849567&disable_web_page_preview=true&parse_mode=Markdown" > /dev/null
}
function closePR() {
curl -s -X POST -d '{"state": "closed"}' -H "Authorization: token $GH_PERSONAL_TOKEN" https://api.github.com/repos/PixelExperience/official_devices/pulls/$SEMAPHORE_GIT_PR_NUMBER >/dev/null
curl -s -X POST -d '{"body": "This is Pixel CI Automation Service! This PR has been closed as it has a derped devices.json and/or you are PR-ing some other file. Please recheck your PR."}' -H "Authorization: token $GH_PERSONAL_TOKEN" https://api.github.com/repos/PixelExperience/official_devices/issues/$SEMAPHORE_GIT_PR_NUMBER/comments >/dev/null
exit 1
}
function approvePR() {
curl -s -X POST -d "{\"commit_id\": \"${COMMIT_HASH}\", \"event\": \"APPROVE\"}" -H "Authorization: token $GH_PERSONAL_TOKEN" https://api.github.com/repos/PixelExperience/official_devices/pulls/$SEMAPHORE_GIT_PR_NUMBER/reviews >/dev/null
}
function checkPullReq() {
printf "\n\n***Pixel Experience CI***\n\n"
if [ -z "$SEMAPHORE_GIT_PR_NUMBER" ]; then
git checkout master >/dev/null
git pull origin master >/dev/null
else
git fetch origin master >/dev/null
export CHANGED_FILES="$(git --no-pager diff --name-only HEAD $(git merge-base HEAD origin/master))"
fi
}
function checkLint() {
if [[ "$COMMIT_MESSAGE" =~ "[PIXEL-CI]" ]]; then
if [[ -n "$SEMAPHORE_GIT_PR_NUMBER" ]]; then
sendMaintainers "\`Official-Devices CI%0A%0APR $SEMAPHORE_GIT_PR_NUMBER has CI-Skip mechanism. It has been closed.\`"
sendAdmins "\`Official-Devices CI%0A%0AI have closed PR $SEMAPHORE_GIT_PR_NUMBER for using CI-Skip mechanism. \`%0A%0A[PR Link](https://github.com/PixelExperience/official_devices/pull/$SEMAPHORE_GIT_PR_NUMBER)"
curl -s -X POST -d '{"state": "closed"}' -H "Authorization: token $GH_PERSONAL_TOKEN" https://api.github.com/repos/PixelExperience/official_devices/pulls/$SEMAPHORE_GIT_PR_NUMBER >/dev/null
curl -s -X POST -d '{"body": "This is Pixel CI Automation Service! You attempted to skip CI on a PR, its not permitted. Reopen PR after you fix the commit message."}' -H "Authorization: token $GH_PERSONAL_TOKEN" https://api.github.com/repos/PixelExperience/official_devices/issues/$SEMAPHORE_GIT_PR_NUMBER/comments >/dev/null
exit 1
else
printf "\n\n***Commit Already Linted***\n\n"
exit 0
fi
fi
}
function checkJsons() {
cargo run --release
RESULT=$?
export ERROR_CODE="$(cat /tmp/error_code)"
export FAILED_FILE="$(cat /tmp/failed_file)"
if [ -n "$SEMAPHORE_GIT_PR_NUMBER" ]; then
if [[ ! "$CHANGED_FILES" =~ "devices.json" ]] && [[ ! "$CHANGED_FILES" =~ "team/" ]] && [[ ! "$CHANGED_FILES" =~ "images/*.png" ]]; then
sendMaintainers "\`Official-Devices CI%0A%0APR $SEMAPHORE_GIT_PR_NUMBER has changed some other file.\`"
sendAdmins "\`Official-Devices CI%0A%0AI have closed PR $SEMAPHORE_GIT_PR_NUMBER due to ERROR: Mismatched Files.\`%0A%0A[PR Link](https://github.com/PixelExperience/official_devices/pull/$SEMAPHORE_GIT_PR_NUMBER)"
closePR
elif [ "$RESULT" -ne 0 ]; then
sendAdmins "\`Official-Devices CI%0A%0APR $SEMAPHORE_GIT_PR_NUMBER is failing JSON checks. Please don't merge\`%0A%0A\`Error Code: ${ERROR_CODE}\`%0A%0A\`Failed File: ${FAILED_FILE}\`"
sendMaintainers "\`Official-Devices CI%0A%0APR $SEMAPHORE_GIT_PR_NUMBER is failing JSON checks.%0A%0AError Code: ${ERROR_CODE}%0A%0AFailed File: ${FAILED_FILE}%0A%0AMaintainer is requested to check it\` %0A%0A[PR Link](https://github.com/PixelExperience/official_devices/pull/$SEMAPHORE_GIT_PR_NUMBER)"
closePR
else
if [[ "$CHANGED_FILES" =~ "devices.json" ]] && [[ ! "$CHANGED_FILES" =~ "team/" ]] && [[ ! "$CHANGED_FILES" =~ "images/*.png" ]]; then
ALTERED_DEVICE="$(git --no-pager diff HEAD "$(git merge-base HEAD origin/master)" | grep "codename")"
if [[ -z "$ALTERED_DEVICE" ]]; then
echo "Yay! My works took $((DIFF / 60)) minute(s) and $((DIFF % 60)) seconds.~"
sendAdmins "\`Official-Devices CI%0A%0APR $SEMAPHORE_GIT_PR_NUMBER can be merged.\` %0A%0A${ADMINS} %0A%0A[PR Link](https://github.com/PixelExperience/official_devices/pull/$SEMAPHORE_GIT_PR_NUMBER) %0A%0A\`CI couldn't figure out which device was PR-ed\`"
sendMaintainers "\`Official-Devices CI%0A%0APR $SEMAPHORE_GIT_PR_NUMBER has passed all checks. Please wait for the merge. CI can't figure out which device was PR-ed\`"
else
echo "Yay! My works took $((DIFF / 60)) minute(s) and $((DIFF % 60)) seconds.~"
curl -s -X GET -H "Authorization: token $GH_PERSONAL_TOKEN" -H "Accept: application/vnd.github.v3.diff" https://api.github.com/repos/PixelExperience/official_devices/pulls/$SEMAPHORE_GIT_PR_NUMBER > git-diff
sendAdmins "\`Official-Devices CI%0A%0APR $SEMAPHORE_GIT_PR_NUMBER can be merged.\`%0A%0A${ADMINS}%0A%0A\`Diff sent below\`%0A%0A[PR Link](https://github.com/PixelExperience/official_devices/pull/$SEMAPHORE_GIT_PR_NUMBER)"
sendAdmins_HTML "<code>$(cat git-diff)</code>"
rm -rf git-diff
sendMaintainers "\`Official-Devices CI%0A%0APR $SEMAPHORE_GIT_PR_NUMBER has passed all checks. Please wait for merge.%0A%0ADevice PR-ed for: ${ALTERED_DEVICE}\`"
exit 0
fi
fi
fi
elif [ "$RESULT" -ne 0 ]; then
sendAdmins "\`Official-Devices CI%0A%0ASomeone has merged a failing file. Please look in ASAP.\`%0A%0A\`Error Code: ${ERROR_CODE}\`%0A%0A\`Failed File: ${FAILED_FILE}\`%0A%0A${ADMINS}"
echo "My works took $((DIFF / 60)) minute(s) and $((DIFF % 60)) seconds. But its an error!"
exit 1
else
sendAdmins "\`Official-Devices CI\`%0A%0A**I am building master branch job.** %0A**Commit Point:** [${COMMIT_SMALL_HASH}](https://github.com/PixelExperience/official_devices/commit/${COMMIT_HASH})"
fi
}
function pushToGit() {
if [ -z "$SEMAPHORE_GIT_PR_NUMBER" ] && [ -n "$(git status -s)" ]; then
git add .
git commit --amend -m "[PIXEL-CI]: ${COMMIT_MESSAGE}"
git push -f origin master
sendAdmins "\`Official-Devices CI\`%0A%0AJSON Linted and Force Pushed!"
echo "Yay! My works took $((DIFF / 60)) minute(s) and $((DIFF % 60)) seconds.~"
fi
}
checkPullReq
checkLint
checkJsons
pushToGit
......@@ -40,22 +40,21 @@ fn test_integrity_devices(json_iter: json::JsonValue) -> String {
fn test_integrity_team(json_iter: json::JsonValue) -> String {
let mut return_value = String::new();
// Sometimes the file can contain just one entry
if(!json_iter["name"].is_empty()) {
if(json_iter["country"].is_empty()) {
// Sometimes the file can contain just one entry
if !json_iter["name"].is_empty() {
if json_iter["country"].is_empty() {
return_value.push_str("I cant find maintainer country!");
}
if(json_iter["github_username"].is_empty()) {
if json_iter["github_username"].is_empty() {
return_value.push_str("I cant find maintainer github username!");
}
let devices =json_iter["devices"].clone();
let devices = json_iter["devices"].clone();
for device in 0..devices.len() {
if (devices[device]["codename"].is_empty()) {
if devices[device]["codename"].is_empty() {
return_value.push_str("I cant find maintainer github username!");
}
......@@ -66,91 +65,96 @@ fn test_integrity_team(json_iter: json::JsonValue) -> String {
&& !(&versions[version] == "ten_plus")
&& !(&versions[version] == "pie")
&& !(&versions[version] == "pie_plus")
{
return_value.push_str("Unsupported Version has been pushed!");
}
{
return_value.push_str("Unsupported Version has been pushed!");
}
}
}
// An array of entries. Lets iterate on them!
else {
for json_value in 0..json_iter.len() {
if(json_iter[json_value]["country"].is_empty()) {
return_value.push_str("I cant find maintainer country!");
}
if(json_iter[json_value]["github_username"].is_empty()) {
return_value.push_str("I cant find maintainer github username!");
}
let devices = json_iter[json_value]["devices"].clone();
} else {
for json_value in 0..json_iter.len() {
if json_iter[json_value]["country"].is_empty() {
return_value.push_str("I cant find maintainer country!");
}
for device in 0..devices.len() {
if (devices[device]["codename"].is_empty()) {
if json_iter[json_value]["github_username"].is_empty() {
return_value.push_str("I cant find maintainer github username!");
}
let versions = devices[device]["versions"].clone();
let devices = json_iter[json_value]["devices"].clone();
for version in 0..versions.len() {
if !(&versions[version] == "ten")
&& !(&versions[version] == "ten_plus")
&& !(&versions[version] == "pie")
&& !(&versions[version] == "pie_plus")
{
for device in 0..devices.len() {
if devices[device]["codename"].is_empty() {
return_value.push_str("I cant find maintainer github username!");
}
let versions = devices[device]["versions"].clone();
for version in 0..versions.len() {
if !(&versions[version] == "ten")
&& !(&versions[version] == "ten_plus")
&& !(&versions[version] == "pie")
&& !(&versions[version] == "pie_plus")
{
return_value.push_str("Unsupported Version has been pushed!");
}
}
}
}
}
}
return return_value;
}
return return_value;
}
fn main() {
let data = fs::read_to_string("devices.json").expect("Unable to read file");
let data = fs::read_to_string("official_devices/devices.json").expect("Unable to read file");
println!("Running Integrity Tests on devices.json.....");
let parsed = json::parse(&data).unwrap();
fs::write("/tmp/current_file", "devices.json").expect("Unable to write file");
let parsed = json::parse(&data).unwrap_or_else(|error| {
println!("\tError parsing JSON: {}", error);
fs::write("/tmp/error_message", error.to_string()).expect("Unable to write file");
panic!("Invalid JSON");
});
let resp = test_integrity_devices(parsed.clone());
if !resp.is_empty() {
println!("Integrity Tests Failed!");
fs::write("/tmp/error_code", resp.to_string()).expect("Unable to write file");
fs::write("/tmp/failed_file","devices.json").expect("Unable to write file");
fs::write("/tmp/error_message", resp.to_string()).expect("Unable to write file");
panic!("{}", resp);
}
else {
} else {
let stringifed = json::stringify_pretty(parsed, 3);
fs::write("devices.json", stringifed).expect("Unable to write file");
fs::write("official_devices/devices.json", stringifed).expect("Unable to write file");
}
let entries = fs::read_dir("team/").unwrap();
let entries = fs::read_dir("official_devices/team/").unwrap();
for entry in entries {
let path = entry.unwrap().path();
println!("Running Integrity Tests on {:?}", path);
let current_entry = path.clone();
let current_file = current_entry
.clone()
.into_os_string()
.into_string()
.unwrap();
let data2 = fs::read_to_string(path).expect("Unable to read file");
let parsed_ = json::parse(&data2).unwrap();
fs::write("/tmp/current_file", current_file).expect("Unable to write file");
let parsed_ = json::parse(&data2).unwrap_or_else(|error| {
println!("\tError parsing JSON: {}", error);
fs::write("/tmp/error_message", error.to_string()).expect("Unable to write file");
panic!("Invalid JSON");
});
let resp2 = test_integrity_team(parsed_.clone());
if !resp2.is_empty() {
println!("Integrity Tests Failed!");
fs::write("/tmp/error_code", resp.to_string()).expect("Unable to write file");
fs::write("/tmp/failed_file",current_entry.into_os_string().into_string().unwrap()).expect("Unable to write file");
fs::write("/tmp/error_message", resp.to_string()).expect("Unable to write file");
panic!("{}", resp2);
}
else {
} else {
let stringifed = json::stringify_pretty(parsed_, 3);
fs::write(current_entry, stringifed).expect("Unable to write file");
}
}
fs::remove_file("/tmp/current_file").unwrap();
println!("Success!");
}
#!/usr/bin/env bash
# Copyright © 2020 PixelExperience Project
#
# SPDX-License-Identifier: GPL-3.0
#
### Script to test and format our jsons
ADMINS="@Hlcpereira @baalajimaestro @Shreejoy\_Dash @Rk585 @itsjoeoui @Hasaber8"
GIT_DIR="official_devices"
GIT_CMD="git -C ${GIT_DIR}"
COMMIT_MESSAGE="$(${GIT_CMD} log -1 --pretty=%B)"
COMMIT_SMALL_HASH="$(${GIT_CMD} rev-parse --short HEAD)"
COMMIT_HASH="$(${GIT_CMD} rev-parse --verify HEAD)"
function sendAdmins() {
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendmessage" --data "text=${*}&chat_id=-1001463677498&disable_web_page_preview=true&parse_mode=Markdown" > /dev/null
}
function commentPR() {
message="$1"
curl -s -X POST -d "{\"body\": \"$message\"}" -H "Authorization: token $GH_PERSONAL_TOKEN" "https://api.github.com/repos/PixelExperience/official_devices/issues/$GIT_PR_NUMBER/comments" >/dev/null
}
function closePR() {
curl -s -X POST -d '{"state": "closed"}' -H "Authorization: token $GH_PERSONAL_TOKEN" "https://api.github.com/repos/PixelExperience/official_devices/pulls/$GIT_PR_NUMBER" >/dev/null
}
function approvePR() {
curl -s -X POST -d "{\"commit_id\": \"${COMMIT_HASH}\", \"event\": \"APPROVE\"}" -H "Authorization: token $GH_PERSONAL_TOKEN" "https://api.github.com/repos/PixelExperience/official_devices/pulls/$GIT_PR_NUMBER/reviews" >/dev/null
}
function checkLint() {
if [[ "$COMMIT_MESSAGE" =~ "[PIXEL-CI]" ]]; then
if [[ -n "$GIT_PR_NUMBER" ]]; then
commentPR "This is Pixel CI Automation Service! You attempted to skip CI on a PR, its not permitted. Reopen PR after you fix the commit message."
closePR
exit 0
else
echo "Commit already linted, nothing to do."
exit 0
fi
fi
}
function checkJsons() {
cargo run --release
RESULT=$?
if [ "$RESULT" -ne 0 ]; then
ERROR_MESSAGE="$(cat /tmp/error_message)"
FAILED_FILE="$(cat /tmp/current_file)"
fi
if [ -n "$GIT_PR_NUMBER" ]; then
if [ "$RESULT" -ne 0 ]; then
message="JSON validation failed\nFile: ${FAILED_FILE}\nError message: ${ERROR_MESSAGE}"
commentPR "$message"
echo -e "$message"
else
message="JSON successfully validated"
commentPR "$message"
echo -e "$message"
fi
elif [ "$RESULT" -ne 0 ]; then
sendAdmins "\`Official-Devices CI%0A%0ASomeone has merged a failing file. Please look in ASAP.\`%0A%0A\`Error message: ${ERROR_MESSAGE}\`%0A%0A\`File: ${FAILED_FILE}\`%0A%0A${ADMINS}"
else
sendAdmins "\`Official-Devices CI\`%0A%0A**I am building master branch job.** %0A**Commit Point:** [${COMMIT_SMALL_HASH}](https://github.com/PixelExperience/official_devices/commit/${COMMIT_HASH})"
pushToGit
fi
}
function pushToGit() {
if [ -z "$GIT_PR_NUMBER" ] && [ -n "$(${GIT_CMD} status -s)" ]; then
${GIT_CMD} add --all
${GIT_CMD} commit --amend -m "[PIXEL-CI]: ${COMMIT_MESSAGE}"
${GIT_CMD} remote rm origin
${GIT_CMD} remote add origin "https://pixelexperiencebot:${GH_PERSONAL_TOKEN}@github.com/PixelExperience/official_devices.git"
${GIT_CMD} push -f origin master
sendAdmins "\`Official-Devices CI\`%0A%0AJSON Linted and Force Pushed!"
fi
}
checkLint
checkJsons
FROM alpine:edge
RUN apk update && apk add --no-cache bash curl
COPY runner.sh /tmp/runner.sh
CMD ["bash", "runner.sh"]
\ No newline at end of file
#!/usr/bin/env bash
# Copyright © 2020 PixelExperience Project
#
# SPDX-License-Identifier: GPL-3.0
#
### Script to notify me of PRs
ADMINS_XIAOMI="@Hlcpereira @baalajimaestro @Shreejoy\_Dash @Dyneteve @jhenrique09"
ADMINS_MOTO="@JorgeLucas @Hasaber8"
function sendMaintainers() {
curl -s "https://api.telegram.org/bot${TELEGRAM_TOKEN}/sendmessage" --data "text=${*}&chat_id=-1001287849567&disable_web_page_preview=true&parse_mode=Markdown" > /dev/null
}
if [[ -n "$SEMAPHORE_GIT_PR_NUMBER" ]]; then
if [[ "$SEMAPORE_GIT_REPO_SLUG" == "PixelExperience-Devices/vendor_xiaomi" ]]; then
MESSAGE="\`Xiaomi Vendor PR Notifier%0A%0AI have recieved PR $SEMAPHORE_GIT_PR_NUMBER.\`%0A%0A$ADMINS_XIAOMI \`Please merge the PR\`%0A%0A[PR Link](https://github.com/PixelExperience-Devices/vendor_xiaomi/pull/$SEMAPHORE_GIT_PR_NUMBER)"
elif [[ "$SEMAPORE_GIT_REPO_SLUG" == "PixelExperience-Devices/vendor_motorola" ]]; then
MESSAGE="\`Moto Vendor PR Notifier%0A%0AI have recieved PR $SEMAPHORE_GIT_PR_NUMBER.\`%0A%0A$ADMINS_MOTO \`Please merge the PR\`%0A%0A[PR Link](https://github.com/PixelExperience-Devices/vendor_motorola/pull/$SEMAPHORE_GIT_PR_NUMBER)"
else
MESSAGE="ERR"
fi
sendMaintainers $MESSAGE
fi
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment