Bring submodule update pipeline changes from feature branch

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
diff --git a/.github/scripts/update_submodules.py b/.github/scripts/update_submodules.py
index 3080894..1d72f45 100644
--- a/.github/scripts/update_submodules.py
+++ b/.github/scripts/update_submodules.py
@@ -19,8 +19,7 @@
     subjects: list[str]
 
     def describe(self):
-        header = f"* [{self.name}]({self.compare_url}) ({self.commit_count} commits):"
-        return "\n".join([header] + [f"  > {subject}" for subject in self.subjects])
+        return f"* [{self.name}]({self.compare_url}) ({self.commit_count} commits)"
 
 
 def run(*args):
@@ -49,21 +48,35 @@
     return has_ci and checks_passed and statuses_passed
 
 
-def update_submodule(path, upstream_branch):
+def submodule_repo(path):
     url = run("git", "config", "-f", ".gitmodules", "--get", f"submodule.{path}.url")
-    repo = github_repo(url)
+    return github_repo(url)
+
+
+def update_submodule(path, upstream_branch):
+    repo = submodule_repo(path)
     old_sha = run("git", "rev-parse", f"HEAD:{path}")
     new_sha = gh_api(f"repos/{repo}/commits/{upstream_branch}")["sha"]
 
     if old_sha == new_sha:
         print(f"{path} is up to date at {old_sha}.")
-        return None
+        return False
     if not is_green(repo, new_sha):
         print(f"Skipping {path}: CI for {repo}@{new_sha} is not green.")
-        return None
+        return False
 
     run("git", "update-index", "--cacheinfo", f"160000,{new_sha},{path}")
-    comparison = gh_api(f"repos/{repo}/compare/{old_sha}...{new_sha}")
+    return True
+
+
+def describe_update(path, base_sha):
+    """Describes the change to `path` between `base_sha` and the index."""
+    old_sha = run("git", "rev-parse", f"{base_sha}:{path}")
+    new_sha = run("git", "rev-parse", f":{path}")
+    if old_sha == new_sha:
+        return None
+
+    comparison = gh_api(f"repos/{submodule_repo(path)}/compare/{old_sha}...{new_sha}")
     return SubmoduleUpdate(
         name=path.split("/")[-1],
         new_sha=new_sha,
@@ -107,24 +120,28 @@
 def main():
     args = parse_args()
     run("git", "fetch", "origin", args.base_branch)
+    base_sha = run("git", "rev-parse", "FETCH_HEAD")
+    if run("git", "ls-remote", "origin", f"refs/heads/{args.update_branch}"):
+        run("git", "fetch", "--depth=1", "origin", args.update_branch)
     run("git", "checkout", "-B", args.update_branch, "FETCH_HEAD")
 
+    bumped = [update_submodule(path, args.upstream_branch) for path in args.submodules]
+    if not any(bumped):
+        print("No new submodule updates to propose.")
+        return
+
     updates = [
         update
         for path in args.submodules
-        if (update := update_submodule(path, args.upstream_branch))
+        if (update := describe_update(path, base_sha))
     ]
-    if not updates:
-        print("No submodule updates to propose.")
-        return
-
     title = "Update submodules: " + ", ".join(
         f"{update.name} to {update.new_sha[:8]}" for update in updates
     )
     body = "\n".join(update.describe() for update in updates)
 
     run("git", "commit", "-m", title, "-m", body)
-    run("git", "push", "--force", "origin", f"HEAD:refs/heads/{args.update_branch}")
+    run("git", "push", "origin", f"HEAD:refs/heads/{args.update_branch}")
     open_or_refresh_pull_request(
         args.repo, args.base_branch, args.update_branch, title, body
     )
diff --git a/.github/workflows/update-submodules.yml b/.github/workflows/update-submodules.yml
index dc51223..6f20c4b 100644
--- a/.github/workflows/update-submodules.yml
+++ b/.github/workflows/update-submodules.yml
@@ -3,6 +3,8 @@
 on:
   push:
     branches: [test/update-submodules]
+  schedule:
+    - cron: "0 12 * * 1"
   workflow_dispatch:
 
 permissions:
@@ -13,19 +15,24 @@
 
 jobs:
   update-submodules:
+    if: github.repository == 'microsoft/DirectXShaderCompiler' || github.event_name == 'workflow_dispatch'
     runs-on: ubuntu-latest
     timeout-minutes: 30
     env:
       HAS_APP_CREDENTIALS: ${{ secrets.SUBMODULE_UPDATER_CLIENT_ID != '' && secrets.SUBMODULE_UPDATER_PRIVATE_KEY != '' }}
     steps:
+      - name: Check submodule updater app credentials
+        if: env.HAS_APP_CREDENTIALS != 'true'
+        run: |
+          echo "::error::SUBMODULE_UPDATER_CLIENT_ID and SUBMODULE_UPDATER_PRIVATE_KEY secrets must be set to update submodules."
+          exit 1
+
       - name: Fetch DirectXShaderCompiler sources
-        if: env.HAS_APP_CREDENTIALS == 'true'
         uses: actions/checkout@fbc6f3992d24b796d5a048ff273f7fcc4a7b6c09 # v5.1.0
         with:
           persist-credentials: false
 
       - name: Get submodule updater app token
-        if: env.HAS_APP_CREDENTIALS == 'true'
         id: app-token
         uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
         with:
@@ -35,7 +42,6 @@
           permission-pull-requests: write
 
       - name: Update submodules and open pull request
-        if: env.HAS_APP_CREDENTIALS == 'true'
         env:
           GH_TOKEN: ${{ steps.app-token.outputs.token }}
           APP_BOT: ${{ steps.app-token.outputs.app-slug }}[bot]