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

Fix gitea API call for triggering pipelines #50

Closed
wants to merge 8 commits into from

Conversation

qdeliens
Copy link

@qdeliens qdeliens commented Jan 6, 2020

@bradrydzewski
Solution to the problem from this issue.
All that remains is to change drone/service/commit/commit.go line 90 to commit, _, err := s.client.Git.FindCommitByRef(ctx, repo, ref) to actually implement it.
Let me know if I need to make any changes regarding code style etc.

@bradrydzewski
Copy link
Member

Thanks for taking the time to send a pull request.

Upon review I do not believe we need to add a new method for FindCommitByRef. Instead we should be able to modify the existing `FindCommit for Gitea only:

FindCommit(ctx context.Context, repo, ref string) (*Commit, *Response, error) {
+	if strings.HasPrefix(ref, "refs/") {
+		// new API call
+	}

	path := fmt.Sprintf("api/v1/repos/%s/git/commits/%s", repo, ref)
	out := new(commitInfo)
	res, err := s.client.do(ctx, "GET", path, nil, out)
	return convertCommitInfo(out), res, err
}

if err != nil {
return nil, nil, err
}
sha := out[0].Object.Sha
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this code is subject to a panic if the index is out of bounds, so we should probably check the length of the slice just to be certain.

@tboerger
Copy link
Contributor

tboerger commented Jan 6, 2020

@appleboy @technowlogik @lunny does this make sense?

@qdeliens
Copy link
Author

qdeliens commented Jan 7, 2020

Thanks for taking the time to send a pull request.

Upon review I do not believe we need to add a new method for FindCommitByRef. Instead we should be able to modify the existing `FindCommit for Gitea only:

FindCommit(ctx context.Context, repo, ref string) (*Commit, *Response, error) {
+	if strings.HasPrefix(ref, "refs/") {
+		// new API call
+	}

	path := fmt.Sprintf("api/v1/repos/%s/git/commits/%s", repo, ref)
	out := new(commitInfo)
	res, err := s.client.do(ctx, "GET", path, nil, out)
	return convertCommitInfo(out), res, err
}

This indeed makes more sense, I have pushed a new fix, also checking the slice length as you remarked.

@lunny
Copy link
Contributor

lunny commented Jan 8, 2020

I think gitea could handle both commit id and refs, this PR is unnecessary. See

https://github.com/go-gitea/gitea/blob/master/routers/api/v1/repo/commits.go#L56
https://github.com/go-gitea/gitea/blob/master/modules/git/repo_commit.go#L121

If you found any problem of gitea, please send an issue.

UPDATE: I'm wrong, we haven't implemented that. But I think it's better for gitea to implement it so that there is only one request/reponse.

@6543
Copy link

6543 commented Apr 8, 2020

go-gitea/gitea#10915 got merged ...

@CLAassistant
Copy link

CLA assistant check
All committers have signed the CLA.

@qdeliens
Copy link
Author

go-gitea/gitea#10915 got merged ...

I have updated the code accordingly!

@@ -24,7 +25,11 @@ func (s *gitService) FindBranch(ctx context.Context, repo, name string) (*scm.Re
}

func (s *gitService) FindCommit(ctx context.Context, repo, ref string) (*scm.Commit, *scm.Response, error) {
path := fmt.Sprintf("api/v1/repos/%s/git/commits/%s", repo, ref)
ref = strings.TrimPrefix(ref, "refs/")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed this code trims refs/. Is it correct that refs/heads/master should be sent to the Gitea API as heads/master? I just wanted to confirm ...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the latest gitea image it was necessary yes. This is the new API call go-gitea/gitea#10915. It would of course be nice if someone else could verify it also works for him/her.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @bradrydzewski , we spoke about this issue (failing cron builds on newer Gitea) in Drone's Gitter room last Friday, could I ask what is the current status? :)

Copy link
Contributor

@CirnoT CirnoT May 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refs/heads/master works fine, trimming is unnecessary. The new API call behaves just like previous one, it's just a cosmetic change.

@lafriks
Copy link

lafriks commented May 11, 2020

API in Gitea has been changed back to not be breaking, so it should work again as it was but now for both commit sha hash and ref

Copy link

@6543 6543 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

api change (back) !

@CirnoT
Copy link
Contributor

CirnoT commented May 11, 2020

The changes made by this PR are now unnecessary. As for the original issue this PR tried to resolve (triggering pipeline via branch), the issue was never with Gitea API - it allows (and always did) passing ref or SHA commit.

We have however noticed potential reason why the problem exists - the ref should be URI component encoded, as in refs/heads/master should be sent as refs%2Fheads%2Fmaster. I've opened new PR to handle that issue specifically #61.

@qdeliens
Copy link
Author

The changes made by this PR are now unnecessary. As for the original issue this PR tried to resolve (triggering pipeline via branch), the issue was never with Gitea API - it allows (and always did) passing ref or SHA commit.

We have however noticed potential reason why the problem exists - the ref should be URI component encoded, as in refs/heads/master should be sent as refs%2Fheads%2Fmaster. I've opened new PR to handle that issue specifically #61.

#61 with the latest gitea build fixed the issue for me, closing this PR now.

@qdeliens qdeliens closed this May 12, 2020
jstrachan pushed a commit to jstrachan/go-scm that referenced this pull request Dec 22, 2020
feat: add function to get installations for an organisation
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants