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

Can't set AllowMergeCommit to false on imported repo #297

Open
abhinav opened this issue Feb 9, 2023 · 6 comments
Open

Can't set AllowMergeCommit to false on imported repo #297

abhinav opened this issue Feb 9, 2023 · 6 comments
Labels
awaiting-upstream The issue cannot be resolved without action in another repository (may be owned by Pulumi). blocked The issue cannot be resolved without 3rd party action. kind/bug Some behavior is incorrect or out of spec

Comments

@abhinav
Copy link

abhinav commented Feb 9, 2023

What happened?

I imported a repository with pulumi import and tried to set its AllowMergeCommit option to false. It failed with the following error:

    error: 1 error occurred:
        * updating urn:pulumi:dev::repro::github:index/repository:Repository::test-repo: PATCH https://api.github.com/repos/abhinav/test-repo: 422 Validation Failed [{Resource:Repository Field:m
erge_commit_allowed Code:invalid Message:Sorry, you need to allow the merge commit strategy in order to set the default merge commit message title and message. (no_merge_strategy)}]

Important part:

Sorry, you need to allow the merge commit strategy in order to set the default merge commit message title and message.

Expected Behavior

It should set AllowMergeCommit to false.

Steps to reproduce

  1. Create a GitHub public or private repository called "test-repo".

  2. Go to Repository settings. Under Pull Requests > Allow merge commits, change Default Message to Default to pull request title and description.

  3. In a new github-go project, import this repository.

    pulumi import github:index/repository:Repository test-repo test-repo
    
  4. Copy the generated code into main.go and run pulumi up to make sure everything is in order.
    It should report no changes.

    Do you want to perform this update? details
      pulumi:pulumi:Stack: (same)
        [urn=urn:pulumi:dev::repro::pulumi:pulumi:Stack::repro-dev]
    
  5. Add AllowMergeCommit: pulumi.Bool(false), to the RepositoryArgs, and delete MergeCommitMessage, MergeCommitTitle since they're no longer relevant.

  6. Run pulumi up.
    If you run the update, it will fail with the error:

    error: 1 error occurred:
        * updating urn:pulumi:dev::repro::github:index/repository:Repository::test-repo: PATCH https://api.github.com/repos/abhinav/test-repo: 422 Validation Failed [{Resource:Repository Field:merge_commit_allowed Code:invalid Message:Sorry, you need to allow the merge commit strategy in order to set the default merge commit message title and message. (no_merge_strategy)}]
    

Full program:
Note that this only fails if the repository has already been imported with non-default merge settings first.

package main

import (
	"github.com/pulumi/pulumi-github/sdk/v5/go/github"
	"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)

func main() {
	pulumi.Run(func(ctx *pulumi.Context) error {
		_, err := github.NewRepository(ctx, "test-repo", &github.RepositoryArgs{
			DefaultBranch:       pulumi.String("main"),
			HasDownloads:        pulumi.Bool(true),
			HasIssues:           pulumi.Bool(true),
			HasProjects:         pulumi.Bool(true),
			HasWiki:             pulumi.Bool(true),
			AllowMergeCommit:    pulumi.Bool(false),
			Name:                pulumi.String("test-repo"),
			Visibility:          pulumi.String("private"),
			VulnerabilityAlerts: pulumi.Bool(true),
		}, pulumi.Protect(true))
		if err != nil {
			return err
		}
		return nil
	})
}

Output of pulumi about

CLI
Version      3.54.0
Go Version   go1.20
Go Compiler  gc

Plugins
NAME    VERSION
github  5.3.0
go      unknown

Host
OS       arch
Version
Arch     x86_64

This project is written in go: executable='/usr/bin/go' version='go version go1.20 linux/amd64'

Current Stack: [...]

TYPE                                URN
pulumi:pulumi:Stack                 urn:pulumi:dev::repro::pulumi:pulumi:Stack::repro-dev
pulumi:providers:github             urn:pulumi:dev::repro::pulumi:providers:github::default
github:index/repository:Repository  urn:pulumi:dev::repro::github:index/repository:Repository::test-repo


Found no pending operations associated with dev

Backend
Name           pulumi.com
URL            [...]
User           [...]
Organizations  [...]

Dependencies:
NAME                                    VERSION
github.com/pulumi/pulumi-github/sdk/v5  5.3.0
github.com/pulumi/pulumi/sdk/v3         3.51.0

Additional context

Details in pulumi up included:

Do you want to perform this update? details
  pulumi:pulumi:Stack: (same)
    [urn=urn:pulumi:dev::repro::pulumi:pulumi:Stack::repro-dev]
    ~ github:index/repository:Repository: (update) 🔒
        [id=test-repo]
        [urn=urn:pulumi:dev::repro::github:index/repository:Repository::test-repo]
      ~ allowMergeCommit  : true => false
      ~ mergeCommitMessage: "PR_BODY" => "PR_TITLE"
      ~ mergeCommitTitle  : "PR_TITLE" => "MERGE_MESSAGE"

Contributing

Vote on this issue by adding a 👍 reaction.
To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

@abhinav abhinav added kind/bug Some behavior is incorrect or out of spec needs-triage Needs attention from the triage team labels Feb 9, 2023
@abhinav
Copy link
Author

abhinav commented Feb 9, 2023

I was able to work around this issue with the following rough steps:

  1. Export the stack (pulumi stack export > stack.json`)
  2. In stack.json, set all mergeCommitMessage and mergeCommitTitle fields to null
  3. Import the result (pulumi stack import < stack.json)
  4. Temporarily set AllowMergeCommit: pulumi.Bool(true) in the program above and run.
  5. Go back to AllowMergeCommit: pulumi.Bool(false) and run. This should work now.

@guineveresaenger
Copy link
Contributor

It sounds to me that the real issue is this part:

~ mergeCommitMessage: "PR_BODY" => "PR_TITLE"
~ mergeCommitTitle  : "PR_TITLE" => "MERGE_MESSAGE"

At first glance it sounds to me that this is an order-of-operations behavior from the GitHub API, but it is odd that removing those fields would make them be replaced with something else.

I wonder if this is at least somewhat related to integrations/terraform-provider-github#1510?

@guineveresaenger
Copy link
Contributor

I tested this and could reproduce it. It does look like it's trying to set the message and title fields to a default after having already set AllowMergeCommit to false. I'm not sure if this is an upstream bug but will try to verify.

In the meantime, here is a better workaround that doesn't require stack surgery:

  1. Leave AllowMergeCommit as true and delete MergeCommitMessage and MergeCommitTitle from your program.
  2. Run pulumi up. In your repo settings you should see that the dropdown has been reset to "Default settings"
  3. Now you can set AllowMergeCommit to false and it should work as expected.

@guineveresaenger guineveresaenger removed the needs-triage Needs attention from the triage team label Feb 9, 2023
@abhinav
Copy link
Author

abhinav commented Feb 9, 2023

  • Leave AllowMergeCommit as true and delete MergeCommitMessage and MergeCommitTitle from your program.
  • Run pulumi up. In your repo settings you should see that the dropdown has been reset to "Default settings"
  • Now you can set AllowMergeCommit to false and it should work as expected.

I'm not completely sure, but I think I tried that too. IIRC, it did not consider setting to default (by deleting those fields) as enough to remove them from the stack state, so when I flipped AllowMergeCommit back to false, it was still trying to ensure that MergeCommitMessage and MergeCommitTitle were set to the default values.

@abhinav
Copy link
Author

abhinav commented Feb 9, 2023

Just tried it out.
@guineveresaenger, your suggested workaround works!
Thanks!

@guineveresaenger
Copy link
Contributor

I filed integrations/terraform-provider-github#1541 upstream and am marking this as blocked. Please use the workaround for now if you can.

@guineveresaenger guineveresaenger added the awaiting-upstream The issue cannot be resolved without action in another repository (may be owned by Pulumi). label Feb 9, 2023
@mjeffryes mjeffryes added the blocked The issue cannot be resolved without 3rd party action. label Sep 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-upstream The issue cannot be resolved without action in another repository (may be owned by Pulumi). blocked The issue cannot be resolved without 3rd party action. kind/bug Some behavior is incorrect or out of spec
Projects
None yet
Development

No branches or pull requests

4 participants