Skip to content

Commit

Permalink
add support for specifying push options
Browse files Browse the repository at this point in the history
Add `.spec.git.push.options` to allow specifying the push options to be
sent to the upstream Git server during a push operation. For more info,
see: https://git-scm.com/docs/git-push#Documentation/git-push.txt---push-optionltoptiongt

This is useful for platforms like GitLab when used with
image-automation-controller by enabling the creation of MRs automatically
upon a push operation (ref: https://docs.gitlab.com/ee/user/project/push_options.html#push-options-for-merge-requests)

Signed-off-by: Sanskar Jaiswal <[email protected]>
  • Loading branch information
aryan9600 committed Aug 23, 2023
1 parent 1e0fad1 commit 1dd0e63
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 6 deletions.
6 changes: 6 additions & 0 deletions api/v1beta1/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,10 @@ type PushSpec struct {
// https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
// +optional
Refspec string `json:"refspec,omitempty"`

// Options specifies the push options that are sent to the Git
// server when performing a push operation. For details, see:
// https://git-scm.com/docs/git-push#Documentation/git-push.txt---push-optionltoptiongt
// +optional
Options map[string]string `json:"options,omitempty"`
}
9 changes: 8 additions & 1 deletion api/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ spec:
to the branch named. The branch is created using `.spec.checkout.branch`
as the starting point, if it doesn't already exist.
type: string
options:
additionalProperties:
type: string
description: 'Options specifies the push options that are
sent to the Git server when performing a push operation.
For details, see: https://git-scm.com/docs/git-push#Documentation/git-push.txt---push-optionltoptiongt'
type: object
refspec:
description: 'Refspec specifies the Git Refspec to use for
a push operation. If both Branch and Refspec are provided,
Expand Down
14 changes: 14 additions & 0 deletions docs/api/v1beta1/image-automation.md
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,20 @@ For more details about Git Refspecs, see:
<a href="https://git-scm.com/book/en/v2/Git-Internals-The-Refspec">https://git-scm.com/book/en/v2/Git-Internals-The-Refspec</a></p>
</td>
</tr>
<tr>
<td>
<code>options</code><br>
<em>
map[string]string
</em>
</td>
<td>
<em>(Optional)</em>
<p>Options specifies the push options that are sent to the Git
server when performing a push operation. For details, see:
<a href="https://git-scm.com/docs/git-push#Documentation/git-push.txt---push-optionltoptiongt">https://git-scm.com/docs/git-push#Documentation/git-push.txt&mdash;push-optionltoptiongt</a></p>
</td>
</tr>
</tbody>
</table>
</div>
Expand Down
22 changes: 21 additions & 1 deletion docs/spec/v1beta1/imageupdateautomations.md
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,11 @@ type PushSpec struct {
// https://git-scm.com/book/en/v2/Git-Internals-The-Refspec
// +optional
Refspec string `json:"refspec,omitempty"`

// Options specifies the push options that are sent to the Git
// server when performing a push operation. For details, see:
// https://git-scm.com/docs/git-push#Documentation/git-push.txt---push-optionltoptiongt
Options map[string]string `json:"options,omitempty"`
}
```

Expand Down Expand Up @@ -477,8 +482,23 @@ spec:
refspec: refs/heads/main:refs/heads/auto
```

#### Gerrit
To specify the [push options](https://git-scm.com/docs/git-push#Documentation/git-push.txt---push-optionltoptiongt)
to be sent to the upstream Git server, use `.push.options`. These options can be
used to perform operations as a result of the push. For example, using the below
push options will open a GitLab Merge Request to the `release` branch
automatically with the commit the controller pushed to the `dev` branch:

```yaml
spec:
git:
push:
branch: dev
options:
merge_request.create: ""
merge_request.target: release
```

#### Gerrit

[Gerrit](https://www.gerritcodereview.com/) operates differently from a
standard Git server. Rather than sending individual commits to a branch,
Expand Down
9 changes: 5 additions & 4 deletions internal/controller/imageupdateautomation_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -416,11 +416,14 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
pushToBranch = true
}

var pushConfig repository.PushConfig
if gitSpec.Push != nil {
pushConfig.Options = gitSpec.Push.Options
}
if pushToBranch {
// If the force push feature flag is true and we are pushing to a
// different branch than the one we checked out to, then force push
// these changes.
var pushConfig repository.PushConfig
forcePush := r.features[features.GitForcePushBranch]
if forcePush && switchBranch {
pushConfig.Force = true
Expand All @@ -434,9 +437,7 @@ func (r *ImageUpdateAutomationReconciler) Reconcile(ctx context.Context, req ctr
}

if pushWithRefspec {
pushConfig := repository.PushConfig{
Refspecs: []string{gitSpec.Push.Refspec},
}
pushConfig.Refspecs = []string{gitSpec.Push.Refspec}
if err := gitClient.Push(pushCtx, pushConfig); err != nil {
return failWithError(err)
}
Expand Down

0 comments on commit 1dd0e63

Please sign in to comment.