Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically resolve repo default branch #10

Merged
merged 1 commit into from
Aug 3, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion docs/module-spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ containers | map? | A docker-compose [`services`](https://docs.docker.com/compos
Field | Type | Description
-|-|-
url | string | The github repo identifier of a remote module in the form `<owner>/<repo>`. Currently this is the only supported value but in future this will be expanded to support additional remote sources.
ref | string? | A git ref that the remote module should be resolved against. Defaults to `master`
ref | string? | A git ref that the remote module should be resolved against. Defaults to the default branch of the repo or`master`
sha | string? | An exact sha that should be used instead of dynamically resolving it from a ref. Exclusive with ref.
subdir | string? | A directory path within the remote repository that the `module` config file should be resolved from. Defaults to `.kl`

Expand Down
20 changes: 13 additions & 7 deletions src/k16/kl/api/resolver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,23 @@

(:sha data)))

(defn- get-default-branch [identifier]
(let [res (api.github/request {:path (str "/repos/" identifier)})
data (json/read-value (:body res) json/keyword-keys-object-mapper)]

(when (not= 200 (:status res))
(log/info (str "Failed to resolve default branch for " identifier))
(cli.util/exit! (:message data) 1))

(or (:default_branch data)
"master")))

(defn- resolve-module-ref [{:keys [url sha ref subdir]}]
(when-not sha
(log/debug (str "Resolving " url (if subdir (str "/" subdir) ""))))

(let [sha (if sha sha (get-commit-for-ref url ref))]
(let [ref (if ref ref (get-default-branch url))
sha (if sha sha (get-commit-for-ref url ref))]
(cond-> {:url url :sha sha :ref ref}
subdir (assoc :subdir subdir))))

Expand All @@ -45,12 +57,6 @@
(let [lock-entry (get lock submodule-name)
current-reference (:ref lock-entry)

ref (when (and (not (:sha partial-ref))
(not (:ref partial-ref)))
"master")
partial-ref (cond-> partial-ref
ref (assoc :ref ref))

should-resolve?
(or (not (:sha current-reference))

Expand Down
Loading