Skip to content

Commit

Permalink
[FIXED JENKINS-60017] Fetch collaborators if the user of the credenti…
Browse files Browse the repository at this point in the history
…als has appropriate permission (#24)

Co-authored-by: Steven <61625851+justusbunsi@users.noreply.github.com>
  • Loading branch information
mecseid and justusbunsi authored Nov 1, 2021
1 parent 8c9922c commit 42157be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 9 deletions.
34 changes: 25 additions & 9 deletions src/main/java/org/jenkinsci/plugin/gitea/GiteaSCMSource.java
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@
import org.jenkinsci.plugin.gitea.client.api.GiteaPullRequest;
import org.jenkinsci.plugin.gitea.client.api.GiteaRepository;
import org.jenkinsci.plugin.gitea.client.api.GiteaTag;
import org.jenkinsci.plugin.gitea.client.api.GiteaUser;
import org.jenkinsci.plugin.gitea.client.api.GiteaVersion;
import org.jenkinsci.plugin.gitea.servers.GiteaServer;
import org.jenkinsci.plugin.gitea.servers.GiteaServers;
Expand All @@ -114,8 +113,10 @@
import org.kohsuke.stapler.QueryParameter;

public class GiteaSCMSource extends AbstractGitSCMSource {
private static final Logger LOGGER = Logger.getLogger(GiteaSCMSource.class.getName());
public static final VersionNumber TAG_SUPPORT_MINIMUM_VERSION = new VersionNumber("1.9.0");
public static final VersionNumber READ_ACCESS_COLLABORATOR_LISTING_SUPPORT_MINIMUM_VERSION =
new VersionNumber("1.12.0");
private static final Logger LOGGER = Logger.getLogger(GiteaSCMSource.class.getName());
private final String serverUrl;
private final String repoOwner;
private final String repository;
Expand Down Expand Up @@ -182,6 +183,9 @@ public void setTraits(List<SCMSourceTrait> traits) {
protected SCMRevision retrieve(@NonNull SCMHead head, @NonNull TaskListener listener)
throws IOException, InterruptedException {
try (GiteaConnection c = gitea().open()) {
listener.getLogger().format("Looking up repository %s/%s%n", repoOwner, repository);
giteaRepository = c.fetchRepository(repoOwner, repository);

if (head instanceof BranchSCMHead) {
listener.getLogger().format("Querying the current revision of branch %s...%n", head.getName());
String revision = c.fetchBranch(repoOwner, repository, head.getName()).getCommit().getId();
Expand Down Expand Up @@ -291,8 +295,7 @@ protected void retrieve(SCMSourceCriteria criteria, @NonNull SCMHeadObserver obs
}
if (request.isFetchTags()) {
final GiteaVersion version = c.fetchVersion();
int index = version.getVersion().indexOf('+');
VersionNumber v = new VersionNumber(index == -1 ? version.getVersion() : version.getVersion().substring(0, index));
VersionNumber v = version.getVersionNumber();
if (v.isOlderThan(TAG_SUPPORT_MINIMUM_VERSION)) {
listener.getLogger()
.format("%n Ignoring tags as Gitea server is version %s and version %s is the "
Expand Down Expand Up @@ -564,11 +567,24 @@ public SCMRevision getTrustedRevision(@NonNull SCMRevision revision, @NonNull Ta
.withTraits(getTraits())
.newRequest(this, listener)) {
request.setConnection(c);
Set<String> names = new HashSet<>();
for (GiteaUser u : c.fetchCollaborators(giteaRepository)) {
names.add(u.getUsername());

final GiteaVersion giteaVersion = c.fetchVersion();
final VersionNumber versionNumber = giteaVersion.getVersionNumber();

if (!versionNumber.isOlderThan(READ_ACCESS_COLLABORATOR_LISTING_SUPPORT_MINIMUM_VERSION) ||
giteaRepository.getPermissions().isAdmin()) {
request.setCollaboratorNames(
c.fetchCollaborators(giteaRepository).stream().map(GiteaOwner::getUsername)
.collect(Collectors.toSet()));
} else {
listener.getLogger()
.format("%n[Gitea] Ignore collaborator fetching because Gitea server version is %s " +
"and it's requires admin privileges. From %s, " +
"collaborator fetching requires only read permission to repository.%n",
giteaVersion.getVersion(),
READ_ACCESS_COLLABORATOR_LISTING_SUPPORT_MINIMUM_VERSION.toString());
}
request.setCollaboratorNames(names);

if (request.isTrusted(head)) {
return revision;
}
Expand Down Expand Up @@ -951,7 +967,7 @@ public String getIconClassName() {
@NonNull
@Override
protected SCMHeadCategory[] createCategories() {
return new SCMHeadCategory[] {
return new SCMHeadCategory[]{
new UncategorizedSCMHeadCategory(Messages._GiteaSCMSource_UncategorizedCategory()),
new ChangeRequestSCMHeadCategory(Messages._GiteaSCMSource_ChangeRequestCategory()),
new TagSCMHeadCategory(Messages._GiteaSCMSource_TagCategory())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import hudson.util.VersionNumber;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;

Expand Down Expand Up @@ -55,6 +56,11 @@ public void set_Version(String version) {
setVersion(version);
}

public VersionNumber getVersionNumber() {
int index = version.indexOf('+');
return new VersionNumber(index == -1 ? version : version.substring(0, index));
}

/**
* {@inheritDoc}
*/
Expand Down

0 comments on commit 42157be

Please sign in to comment.