Skip to content

Code Scanning API support #1787

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

Draft
wants to merge 11 commits into
base: main
Choose a base branch
from
Next Next commit
(feat) Add data models for code scanning alert and related structures
  • Loading branch information
akashRindhe authored and wwong committed Apr 1, 2024
commit 7912ab064678e3d7bf95edff058a760eb6701f00
249 changes: 249 additions & 0 deletions src/main/java/org/kohsuke/github/GHCodeScanningAlert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,249 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JsonIgnore;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.io.IOException;
import java.net.URL;
import java.util.Date;

/**
* Code scanning alert for a repository
*
* <a href="https://docs.github.com/en/rest/reference/code-scanning"></a>
*/
@SuppressFBWarnings(value = { "UUF_UNUSED_FIELD" }, justification = "JSON API")
public class GHCodeScanningAlert extends GHObject {
@JsonIgnore
private GHRepository owner;
private long number;
private String html_url;
private GHCodeScanningAlertState state;
private GHUser dismissed_by;
private String dismissed_at;
private String dismissed_reason;
private Tool tool;
private Rule rule;
private GHCodeScanningAlertInstance most_recent_instance;
private String instances_url;

GHCodeScanningAlert wrap(GHRepository owner) {
this.owner = owner;
return wrap(owner.root);
}

GHCodeScanningAlert wrap(GitHub root) {
this.root = root;
if (owner != null) {
owner.wrap(root);
}
return this;
}

/**
* Id/number of the alert.
*
* @return the id/number
* @see #getId()
*/
public long getNumber() {
return number;
}

/**
* Id/number of the alert.
*
* @return the id/number
* @see #getNumber()
*/
@Override
public long getId() {
return getNumber();
}

/**
* State of alert
*
* @return the state
*/
public GHCodeScanningAlertState getState() {
return state;
}

/**
* User that has dismissed the alert. Non-null when {@link #getState()} is <i>Dismissed</i>
*
* <p>
* Note: User object returned by code scanning GitHub API does not contain all fields. Use with caution
* </p>
*
* @return the user
*/
public GHUser getDismissedBy() {
return dismissed_by;
}

/**
* Time when alert was dismissed. Non-null when {@link #getState()} is <i>Dismissed</i>
*
* @return the time
*/
public Date getDismissedAt() {
return GitHubClient.parseDate(dismissed_at);
}

/**
* Reason provided for dismissing the alert.
*
* @return the reason
*/
public String getDismissedReason() {
return dismissed_reason;
}

/**
* Code scanning tool that created this alert
*
* @return the tool
*/
public Tool getTool() {
return tool;
}

/**
* Code scanning rule that was violated, causing the alert
*
* @return the rule
*/
public Rule getRule() {
return rule;
}

/**
* Most recent instance of the alert
*
* @return most recent instance
*/
public GHCodeScanningAlertInstance getMostRecentInstance() {
return most_recent_instance;
}

@Override
public URL getHtmlUrl() throws IOException {
return GitHubClient.parseURL(html_url);
}

/**
* Code scanning rule
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
static class Rule {
private String id;
private String severity;
private String description;
private String name;
private String full_description;
private String[] tags;
private String help;

/**
* Id of rule
*
* @return the id
*/
public String getId() {
return id;
}

/**
* Severity of rule
*
* @return the severity
*/
public String getSeverity() {
return severity;
}

/**
* Description of rule
*
* @return the description
*/
public String getDescription() {
return description;
}

/**
* Name of rule
*
* @return the name
*/
public String getName() {
return name;
}

/**
* Full description of rule
*
* @return the full description
*/
public String getFullDescription() {
return full_description;
}

/**
* Tags associated with the rule
*
* @return the tags
*/
public String[] getTags() {
return tags;
}

/**
* Help text for the rule
*
* @return the help text
*/
public String getHelp() {
return help;
}
}

/**
* Code scanning tool
*/
@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
static class Tool {
private String name;
private String guid;
private String version;

/**
* Name of code scanning tool
*
* @return the name
*/
public String getName() {
return name;
}

/**
* GUID of code scanning tool
*
* @return the GUID
*/
public String getGuid() {
return guid;
}

/**
* Version of code scanning tool
*
* @return the version
*/
public String getVersion() {
return version;
}
}
}
94 changes: 94 additions & 0 deletions src/main/java/org/kohsuke/github/GHCodeScanningAlertInstance.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package org.kohsuke.github;

import com.fasterxml.jackson.annotation.JsonIgnore;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

public class GHCodeScanningAlertInstance {

@JsonIgnore
private GHCodeScanningAlert owner;

private String ref;
private String analysis_key;
private String environment;
private GHCodeScanningAlertState state;
private String commit_sha;
private String[] classifications;
private Message message;
private Location location;

public String getRef() {
return ref;
}

public String getAnalysisKey() {
return analysis_key;
}

public String getEnvironment() {
return environment;
}

public GHCodeScanningAlertState getState() {
return state;
}

public String getCommitSha() {
return commit_sha;
}

public List<String> getClassifications() {
return Collections.unmodifiableList(Arrays.asList(classifications));
}

public Message getMessage() {
return message;
}

public Location getLocation() {
return location;
}

@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
static class Message {

private String text;

public String getText() {
return text;
}
}

@SuppressFBWarnings(value = { "UWF_UNWRITTEN_FIELD" }, justification = "JSON API")
static class Location {
private String path;
private long start_line;
private long end_line;
private long start_column;
private long end_column;

public String getPath() {
return path;
}

public long getStartLine() {
return start_line;
}

public long getEndLine() {
return end_line;
}

public long getStartColumn() {
return start_column;
}

public long getEndColumn() {
return end_column;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.kohsuke.github;

public enum GHCodeScanningAlertState {
OPEN, FIXED, DISMISSED
}