Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 82 additions & 10 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ runs:
run: |
git checkout -b manifest_pr
git add west.yml
git commit -m "manifest: Update ${{ github.event.repository.name }} revision (auto-manifest PR)" -m "$BODY" --signoff
git commit -m "manifest: Update ${{ github.event.repository.name }} revision (auto-manifest PR)" -m "$BODY" -m "X-Manifest-PR: true" --signoff
git remote add fork "https://nordicbuilder:${{ inputs.token }}@github.com/${{ inputs.forked-repo }}"
git push -u fork manifest_pr:auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}
USERNAME=$(echo "${{ inputs.forked-repo }}" | cut -d'/' -f1)
Expand All @@ -170,33 +170,105 @@ runs:
if: ${{ github.event.action == 'synchronize' && steps.check.outputs.SKIP_STRING != 'true' }}
shell: bash
run: |
git checkout auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}
BRANCH="auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}"
git checkout "$BRANCH"
git remote add upstream https://github.com/${{ inputs.target-repo }}
git fetch upstream
git rebase upstream/${{ inputs.base-branch }} -X theirs

# Find the original manifest commit by its durable marker
MANIFEST_COMMIT=$(git log "upstream/${{ inputs.base-branch }}..HEAD" \
--grep='X-Manifest-PR: true' --format='%H' | tail -1)
# Fallback for branches created before the marker was introduced
if [ -z "$MANIFEST_COMMIT" ]; then
MANIFEST_COMMIT=$(git log "upstream/${{ inputs.base-branch }}..HEAD" \
--grep='auto-manifest PR' --format='%H' | tail -1)
fi
if [ -z "$MANIFEST_COMMIT" ]; then
echo "Error: could not locate the manifest commit in the branch." >&2
exit 1
fi

# Collect any commits stacked on top of the manifest commit
AFTER_COMMITS=$(git rev-list --reverse "${MANIFEST_COMMIT}..HEAD")

# Rewind to the parent of the manifest commit
git reset --hard "${MANIFEST_COMMIT}^"

# Re-apply the manifest commit then amend its committer date to get a new SHA,
# which is necessary to retrigger CI when there are no upstream changes.
git cherry-pick "$MANIFEST_COMMIT"
git commit --amend --no-edit
git push origin auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}:auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }} -f

# Re-apply any commits that were stacked above the manifest commit
for c in $AFTER_COMMITS; do
if ! git cherry-pick "$c"; then
echo "Error: cherry-pick of commit $c failed. Aborting." >&2
git cherry-pick --abort
exit 1
fi
done

git push origin "$BRANCH:$BRANCH" -f

# This is used for changing PR pointer to sha in west.yml (assumes manifest PR exists)
- name: Change commit sha and push it
if: ${{ github.event.pull_request.merged == true && steps.check.outputs.SKIP_STRING != 'true' }}
shell: bash
run: |
git checkout auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}
BRANCH="auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}"
git checkout "$BRANCH"
git remote add upstream https://github.com/${{ inputs.target-repo }}
git fetch upstream

# Find the original manifest commit by its durable marker
MANIFEST_COMMIT=$(git log "upstream/${{ inputs.base-branch }}..HEAD" \
--grep='X-Manifest-PR: true' --format='%H' | tail -1)
# Fallback for branches created before the marker was introduced
if [ -z "$MANIFEST_COMMIT" ]; then
MANIFEST_COMMIT=$(git log "upstream/${{ inputs.base-branch }}..HEAD" \
--grep='auto-manifest PR' --format='%H' | tail -1)
fi
if [ -z "$MANIFEST_COMMIT" ]; then
echo "Error: could not locate the manifest commit in the branch." >&2
exit 1
fi

# Collect any commits stacked on top of the manifest commit
AFTER_COMMITS=$(git rev-list --reverse "${MANIFEST_COMMIT}..HEAD")

# Save the manifest commit message to reuse it on the rewritten commit
git log -1 --format='%B' "$MANIFEST_COMMIT" > /tmp/manifest_commit_msg.txt

# Rewind to the parent of the manifest commit and restore west.yml at that revision
git reset --hard "${MANIFEST_COMMIT}^"
git checkout "$MANIFEST_COMMIT" -- west.yml

# Update west.yml revision from the PR pointer to the merge commit SHA
PROJECT_KEY=$(yq --exit-status '.manifest.projects[] | select(.repo-path == "${{ github.event.repository.name }}") | key' west.yml)
yq ".manifest.projects[$PROJECT_KEY].revision = \"${{ github.event.pull_request.merge_commit_sha }}\"" west.yml > tmp.yml
diff -B west.yml tmp.yml | patch west.yml - || true

# Commit the updated west.yml, preserving the original manifest commit message
git add west.yml
git commit --amend --no-edit
git remote add upstream https://github.com/${{ inputs.target-repo }}
git fetch upstream
git commit -F /tmp/manifest_commit_msg.txt

# Re-apply any commits that were stacked above the manifest commit
for c in $AFTER_COMMITS; do
if ! git cherry-pick "$c"; then
echo "Error: cherry-pick of commit $c failed. Aborting." >&2
git cherry-pick --abort
exit 1
fi
done

set +e
git merge-tree --write-tree --name-only upstream/${{ inputs.base-branch }} auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}
git merge-tree --write-tree --name-only upstream/${{ inputs.base-branch }} HEAD
has_conflict="$?"
echo "has_conflict is" $has_conflict
set -e
if (( $has_conflict == 1)) ; then git rebase upstream/${{ inputs.base-branch }} -X theirs ; fi
git push origin auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }}:auto-manifest-${{ github.event.repository.name }}-${{ github.event.pull_request.number }} -f
if (( $has_conflict == 1 )); then git rebase upstream/${{ inputs.base-branch }} -X theirs ; fi
git push origin "$BRANCH:$BRANCH" -f

- name: Close manifest-PR upon closing PR
if: ${{ github.event.action == 'closed' && github.event.pull_request.merged == false && steps.check.outputs.SKIP_STRING != 'true'}}
Expand Down