Skip to content

Commit

Permalink
Fix template artifact upload
Browse files Browse the repository at this point in the history
Add notify block in GHA with the PR link
  • Loading branch information
QU3B1M committed Jan 23, 2025
1 parent 00607bb commit 46b8775
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/generate-ecs-mappings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ jobs:
run: sudo apt-get install docker-compose

- name: Generate PR to wazuh-indexer-plugins
id: generate-pr
env:
GITHUB_TOKEN: ${{ secrets.ACTION_TOKEN }}
COMMITER_EMAIL: ${{ secrets.INDEXER_BOT_EMAIL }}
Expand All @@ -38,10 +39,13 @@ jobs:
run: |
bash ecs/scripts/generate-pr-to-plugins.sh \
-b ${{ steps.branch-name.outputs.branch }} \
-o ./ecs-templates
-o ecs-templates && echo "PR_URL=$PR_URL" >> $GITHUB_OUTPUT
- name: Show PR URL as a notation
run: echo "::notice title=Pull Request URL::${{ steps.generate-pr.outputs.PR_URL }}"

- name: Upload artifact
uses: actions/upload-artifact@v4
with:
name: ecs-templates
path: "./ecs-templates"
path: "ecs-templates"
36 changes: 22 additions & 14 deletions ecs/scripts/generate-pr-to-plugins.sh
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ validate_dependencies() {
done
}

# Check if the script is being executed in a GHA Workflow
check_running_on_gha() {
if [[ -n "${GITHUB_RUN_ID}" ]]; then
return 0
else
return 1
fi
}

# Detect modified ECS modules by comparing the current branch with the base branch.
detect_modified_modules() {
echo
Expand All @@ -44,7 +53,7 @@ detect_modified_modules() {
local modified_files
local updated_modules=()
modified_files=$(git diff --name-only origin/"$BASE_BRANCH")

for file in $modified_files; do
if [[ $file == ecs/* ]]; then
ecs_module=$(echo "$file" | cut -d'/' -f2)
Expand Down Expand Up @@ -111,7 +120,7 @@ clone_target_repo() {
cd "$PLUGINS_LOCAL_PATH" || exit

# Only for the GH Workflow
if [[ -z "${INDEXER_BOT_PRIVATE_SSH_KEY}" ]] && [[ -z "${INDEXER_BOT_PUBLIC_SSH_KEY}" ]]; then
if check_running_on_gha; then
echo "Configuring Git for ${COMMITTER_USERNAME}"
configure_git
fi
Expand Down Expand Up @@ -186,19 +195,18 @@ create_or_update_pr() {
echo "---> Creating or updating Pull Request..."

local existing_pr
local modules_title
local modules_body
local title
local body

existing_pr=$(gh pr list --head "$BRANCH_NAME" --json number --jq '.[].number')
# Format modules
modules_title=$(IFS=", "; echo "${relevant_modules[*]}")
modules_body=$(printf -- '- %s\n' "${relevant_modules[@]}")

# Create title and body with formatted modules list
title="Update ECS templates for modified modules: ${modules_title[*]}"
body="This PR updates the ECS templates for the following modules:${modules_body}"
title="[ECS Generator] Update index templates"
body="This PR updates the ECS templates for the following modules:
${modules_body}"

# Store the PAT in a file that can be accessed by the GitHub CLI.
echo "${GITHUB_TOKEN}" > token.txt
Expand All @@ -208,16 +216,16 @@ create_or_update_pr() {
gh auth login --with-token < token.txt

if [ -z "$existing_pr" ]; then
gh pr create \
--title "$title" \
--body "$body" \
--base master \
--head "$BRANCH_NAME"
output=$(gh pr create --title "$title" --body "$body" --base master --head "$BRANCH_NAME")
pr_url=$(echo "$output" | grep -oP 'https://github.com/\S+')
export PR_URL="$pr_url"
echo "New pull request created: $PR_URL"
else
echo "PR already exists: $existing_pr. Updating the PR..."
gh pr edit "$existing_pr" \
--title "$title" \
--body "$body"
gh pr edit "$existing_pr" --title "$title" --body "$body"
pr_url=$(gh pr view "$existing_pr" --json url -q '.url')
export PR_URL="$pr_url"
echo "Pull request updated: $PR_URL"
fi
}

Expand Down
2 changes: 0 additions & 2 deletions ecs/states-inventory-hotfixes/fields/subset.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ fields:
groups: {}
id: {}
name: {}
type: {}
version: {}
host:
fields: "*"
package:
Expand Down

0 comments on commit 46b8775

Please sign in to comment.