Skip to content

Commit bfedfa6

Browse files
author
Tom Osowski
authored
Merge pull request #1 from github/master
Pull in latest
2 parents 36154d6 + b278780 commit bfedfa6

23 files changed

+888
-41
lines changed

LICENSE renamed to LICENSE.txt

File renamed without changes.

api/PrintRepoAccess.groovy

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
#!/usr/bin/env groovy
2+
3+
/**
4+
* groovy script to show all users that can access a given repository in a GitHub Enterprise instance
5+
*
6+
* Run 'groovy PrintRepoAccess.groovy' to see the list of command line options
7+
*
8+
* Example on how to list access rights for repos foo/bar and bar/foo on GitHub Enterprise instance https://foobar.com:
9+
*
10+
* groovy PrintRepoAccess.groovy -u https://foobar.com -t <access token> foo/bar bar/foo
11+
*
12+
* Example on how to list access rights for repos stored in <org-name>/<repo-name.git> in directory local:
13+
* groovy PrintRepoAccess.groovy -u https://foobar.com -t <access token> -l local
14+
*
15+
* Example that combines the two examples above but uses environmental variables instead of explicit parameters:
16+
*
17+
* export GITHUB_TOKEN="<personal access token>"
18+
* export GITHUB_URL="https://foobar.com"
19+
* groovy PrintRepoAccess.groovy -l local foo/bar bar/foo
20+
*
21+
* Apart from Groovy (and Java), you do not need to install any libraries on your system as the script will download them when you first start it
22+
* The first run may take some time as required dependencies have to get downloaded, then it should be quite fast
23+
*
24+
* If you do not have groovy yet, run 'brew install groovy' on a Mac, for Windows and Linux follow the instructions here:
25+
* http://groovy-lang.org/install.html
26+
*
27+
*/
28+
29+
@Grab(group='org.kohsuke', module='github-api', version='1.75')
30+
import org.kohsuke.github.GitHub
31+
32+
// parsing command line args
33+
cli = new CliBuilder(usage: 'groovy PrintRepoAccess.groovy [options] [repos]\nPrint out users that can access the repos specified, ALL if public repo')
34+
cli.t(longOpt: 'token', 'personal access token of a GitHub Enterprise site admin with repo scope (or use GITHUB_TOKEN env variable)', required: false , args: 1 )
35+
cli.u(longOpt: 'url', 'GitHub Enterprise URL (or use GITHUB_URL env variable), e.g. https://myghe.com', required: false , args: 1 )
36+
cli.l(longOpt: 'localDirectory', 'Directory with org/repo directory structure (show access for all contained repos)', required: false, args: 1)
37+
cli.h(longOpt: 'help', 'Print this usage info', required: false , args: 0 )
38+
39+
OptionAccessor opt = cli.parse(args)
40+
41+
token = opt.t?opt.t:System.getenv("GITHUB_TOKEN")
42+
url = opt.u?opt.u:System.getenv("GITHUB_URL")
43+
44+
// bail out if help parameter was supplied or not sufficient input to proceed
45+
if (opt.h || !token || !url ) {
46+
cli.usage()
47+
return
48+
}
49+
50+
// chop potential trailing slash from GitHub Enterprise URL
51+
url = url.replaceAll('/\$', "")
52+
53+
// connect to GitHub Enterprise
54+
client=GitHub.connectToEnterprise("${url}/api/v3", token)
55+
56+
// printing CSV header
57+
println "REPOSITORY,USER_WITH_ACCESS"
58+
59+
// iterate over all supplied repos
60+
printAccessRightsForCommandLineRepos(opt)
61+
62+
if (opt.l) {
63+
localRepoStore = new File(opt.l)
64+
if (!localRepoStore.isDirectory()) {
65+
printErr "${localRepoStore.canonicalPath} is not a directory"
66+
return
67+
}
68+
printAccessRightsForStoredRepos(localRepoStore)
69+
}
70+
71+
// END OF MAIN
72+
73+
def printAccessRightsForRepo(org, repo) {
74+
if (repo.endsWith(".git")) {
75+
repo=repo.take(repo.length()-4)
76+
}
77+
78+
try {
79+
ghRepo=client.getRepository("${org}/${repo}")
80+
isPublic=!ghRepo.isPrivate()
81+
if (isPublic) {
82+
println "${org}/${repo},ALL"
83+
} else {
84+
ghRepo.getCollaboratorNames().each {
85+
println "${org}/${repo},${it}"
86+
}
87+
}
88+
} catch (Exception e) {
89+
printErr "Could not access repo ${org}/${repo}, skipping ..."
90+
printErr "Reason: ${e.message}"
91+
return
92+
}
93+
}
94+
95+
def printAccessRightsForCommandLineRepos(opt) {
96+
opt.arguments().each {
97+
parsed=it.tokenize("/")
98+
if (parsed.size!=2 || parsed[1] == 0) {
99+
printErr "Could not parse new repo ${it}, please use org/repo format, skipping ..."
100+
return
101+
}
102+
printAccessRightsForRepo(parsed[0], parsed[1])
103+
}
104+
}
105+
106+
def printAccessRightsForStoredRepos(localRepoStore) {
107+
localRepoStore.eachDir { org ->
108+
org.eachDir { repo ->
109+
printAccessRightsForRepo(org.name,repo.name)
110+
}
111+
}
112+
}
113+
114+
def printErr (msg) {
115+
System.err.println "ERROR: ${msg}"
116+
}

api/java/deployment/.gitignore

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
*.class
2+
.settings
3+
.project
4+
.classpath
5+
target/
6+
.idea
7+
*.iml
8+
9+
# Mobile Tools for Java (J2ME)
10+
.mtj.tmp/
11+
12+
# Package Files #
13+
*.jar
14+
*.war
15+
*.ear
16+
17+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
18+
hs_err_pid*

api/java/deployment/README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# DeployServer
2+
3+
A sample implementation for using GitHub Deployment API.
4+
5+
Ported [this](https://developer.github.com/guides/delivering-deployments/) by Java. Powered by [Spark](http://sparkjava.com/).
6+
7+
## Prerequisite
8+
- JDK8
9+
- Maven3
10+
- GitHub OAuth Token
11+
12+
## Getting Started
13+
First, you should set your OAuth token into an environment variable somewhere, like:
14+
```
15+
export GITHUB_OAUTH=xxxxxxx
16+
```
17+
18+
After that, you can:
19+
20+
- For development
21+
22+
```
23+
$ mvn compile exec:java
24+
```
25+
26+
If you aren't familiar with CLI, you can just run the main class via an execution button in your IDE as well.
27+
28+
- For deployment
29+
30+
```
31+
$ mvn clean package
32+
$ java -jar target/DeployServer-{version}.jar
33+
```
34+
35+
Then you can see it works on `http://localhost:4567`.
36+
37+
After you make sure this sever deployed a place where GitHub can reach out to, you can test how it interacts with GitHub via its Deployment API.
38+
39+
You can also place it on your local pc, then expose it by using ngrok. Please refer the direction described [here](https://developer.github.com/guides/delivering-deployments/).
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.github</groupId>
5+
<artifactId>DeployServer</artifactId>
6+
<name>DeployServer</name>
7+
<version>1.0-SNAPSHOT</version>
8+
<url>https://github.com/github/platform-samples</url>
9+
<build>
10+
<plugins>
11+
<plugin>
12+
<artifactId>maven-compiler-plugin</artifactId>
13+
<version>3.1</version>
14+
<configuration>
15+
<source>${java.version}</source>
16+
<target>${java.version}</target>
17+
</configuration>
18+
</plugin>
19+
<plugin>
20+
<artifactId>maven-shade-plugin</artifactId>
21+
<version>2.3</version>
22+
<executions>
23+
<execution>
24+
<phase>package</phase>
25+
<goals>
26+
<goal>shade</goal>
27+
</goals>
28+
<configuration>
29+
<transformers>
30+
<transformer>
31+
<mainClass>${main.class}</mainClass>
32+
</transformer>
33+
</transformers>
34+
</configuration>
35+
</execution>
36+
</executions>
37+
</plugin>
38+
<plugin>
39+
<groupId>org.codehaus.mojo</groupId>
40+
<artifactId>exec-maven-plugin</artifactId>
41+
<version>1.2.1</version>
42+
<executions>
43+
<execution>
44+
<goals>
45+
<goal>java</goal>
46+
</goals>
47+
</execution>
48+
</executions>
49+
<configuration>
50+
<mainClass>${main.class}</mainClass>
51+
<arguments />
52+
</configuration>
53+
</plugin>
54+
</plugins>
55+
</build>
56+
<dependencies>
57+
<dependency>
58+
<groupId>junit</groupId>
59+
<artifactId>junit</artifactId>
60+
<version>4.11</version>
61+
<scope>test</scope>
62+
<exclusions>
63+
<exclusion>
64+
<artifactId>hamcrest-core</artifactId>
65+
<groupId>org.hamcrest</groupId>
66+
</exclusion>
67+
</exclusions>
68+
</dependency>
69+
</dependencies>
70+
<properties>
71+
<main.class>com.github.DeployServer</main.class>
72+
<java.version>1.8</java.version>
73+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
74+
</properties>
75+
</project>
76+

api/java/deployment/pom.xml

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.github</groupId>
6+
<artifactId>DeployServer</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>DeployServer</name>
11+
<url>https://github.com/github/platform-samples</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
<java.version>1.8</java.version>
16+
<main.class>com.github.DeployServer</main.class>
17+
</properties>
18+
19+
<build>
20+
<plugins>
21+
<plugin>
22+
<groupId>org.apache.maven.plugins</groupId>
23+
<artifactId>maven-compiler-plugin</artifactId>
24+
<version>3.1</version>
25+
<configuration>
26+
<source>${java.version}</source>
27+
<target>${java.version}</target>
28+
</configuration>
29+
</plugin>
30+
<plugin>
31+
<groupId>org.apache.maven.plugins</groupId>
32+
<artifactId>maven-shade-plugin</artifactId>
33+
<version>2.3</version>
34+
<executions>
35+
<!-- Run shade goal on package phase -->
36+
<execution>
37+
<phase>package</phase>
38+
<goals>
39+
<goal>shade</goal>
40+
</goals>
41+
<configuration>
42+
<transformers>
43+
<!-- add Main-Class to manifest file -->
44+
<transformer
45+
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
46+
<mainClass>${main.class}</mainClass>
47+
</transformer>
48+
</transformers>
49+
</configuration>
50+
</execution>
51+
</executions>
52+
</plugin>
53+
<plugin>
54+
<groupId>org.codehaus.mojo</groupId>
55+
<artifactId>exec-maven-plugin</artifactId>
56+
<version>1.2.1</version>
57+
<executions>
58+
<execution>
59+
<goals>
60+
<goal>java</goal>
61+
</goals>
62+
</execution>
63+
</executions>
64+
<configuration>
65+
<mainClass>${main.class}</mainClass>
66+
<arguments></arguments>
67+
</configuration>
68+
</plugin>
69+
</plugins>
70+
</build>
71+
72+
<dependencies>
73+
<dependency>
74+
<groupId>junit</groupId>
75+
<artifactId>junit</artifactId>
76+
<version>4.11</version>
77+
<scope>test</scope>
78+
</dependency>
79+
<dependency>
80+
<groupId>com.sparkjava</groupId>
81+
<artifactId>spark-core</artifactId>
82+
<version>2.3</version>
83+
</dependency>
84+
<dependency>
85+
<groupId>com.google.code.gson</groupId>
86+
<artifactId>gson</artifactId>
87+
<version>2.3.1</version>
88+
</dependency>
89+
<dependency>
90+
<groupId>org.kohsuke</groupId>
91+
<artifactId>github-api</artifactId>
92+
<version>1.75</version>
93+
</dependency>
94+
</dependencies>
95+
</project>

0 commit comments

Comments
 (0)