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

Unable to fetch pipeline history #103

Closed
jg-synth opened this issue Jan 31, 2020 · 5 comments
Closed

Unable to fetch pipeline history #103

jg-synth opened this issue Jan 31, 2020 · 5 comments

Comments

@jg-synth
Copy link

Summary

Using plugin 1.4.0 and GoCD 20.1.0, I am unable to get pipeline history from the API.

I believe this is due to a missing header in the request. In GoCD 20.1.0, the pipeline history endpoint has been upgraded to version 1 and so it is necessary to include the Accept header:

GET /go/api/pipelines/ee-deploy-dev/history HTTP/1.1                                                                                      
Accept: application/vnd.go.cd.v1+json                                                                                                     
Accept-Encoding: gzip, deflate                                                                                                            
Authorization: Basic ***                                                                     
Connection: keep-alive                                                                                                                    
Host: localhost:8153                                                                                                                      
User-Agent: HTTPie/1.0.3                                                                                                                  

HTTP/1.1 200 OK                                                                                                                           
...

Configuration

GoCD Version: 20.1.0 (11114-f2c1771e333ea92dae82cd78d2efa1310c1086f0).
Plugin Version: 1.4.0

Error

Unable to fetch build details from API.

Screenshot from 2020-01-31 10-56-04

Logs

2020-01-31 09:50:11,889 INFO  [171@MessageListener for PluginNotificationMessageListener] Server:72 - Fetching http://localhost:8153/go/ap
i/pipelines/ee-deploy-dev/history
2020-01-31 09:50:11,940 WARN  [171@MessageListener for PluginNotificationMessageListener] SlackPipelineListener:102 - Couldn't fetch build
 details
java.io.FileNotFoundException: http://localhost:8153/go/api/pipelines/ee-deploy-dev/history
        at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream0(Unknown Source)
        at java.base/sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
        at java.base/java.net.URLConnection.getContent(Unknown Source)
        at in.ashwanthkumar.gocd.slack.jsonapi.Server.getUrl(Server.java:66)
        at in.ashwanthkumar.gocd.slack.jsonapi.Server.getPipelineHistory(Server.java:76)
        at in.ashwanthkumar.gocd.slack.GoNotificationMessage.fetchRecentPipelineHistory(GoNotificationMessage.java:139)
        at in.ashwanthkumar.gocd.slack.GoNotificationMessage.fetchDetailsForBuild(GoNotificationMessage.java:147)
        at in.ashwanthkumar.gocd.slack.GoNotificationMessage.fetchDetails(GoNotificationMessage.java:213)
        at in.ashwanthkumar.gocd.slack.SlackPipelineListener.slackAttachment(SlackPipelineListener.java:91)
        at in.ashwanthkumar.gocd.slack.SlackPipelineListener.onPassed(SlackPipelineListener.java:51)
        at in.ashwanthkumar.gocd.slack.ruleset.PipelineStatus$2.handle(PipelineStatus.java:22)
        at in.ashwanthkumar.gocd.slack.PipelineListener.handlePipelineStatus(PipelineListener.java:37)
        at in.ashwanthkumar.gocd.slack.PipelineListener.notify(PipelineListener.java:26)
        at in.ashwanthkumar.gocd.slack.GoNotificationPlugin.handleStageNotification(GoNotificationPlugin.java:153)
        at in.ashwanthkumar.gocd.slack.GoNotificationPlugin.handle(GoNotificationPlugin.java:93)
        at com.thoughtworks.go.plugin.infra.DefaultPluginManager.lambda$submitTo$0(DefaultPluginManager.java:134)
        at com.thoughtworks.go.plugin.infra.FelixGoPluginOSGiFramework.executeActionOnTheService(FelixGoPluginOSGiFramework.java:208)
        at com.thoughtworks.go.plugin.infra.FelixGoPluginOSGiFramework.doOn(FelixGoPluginOSGiFramework.java:164)
        at com.thoughtworks.go.plugin.infra.DefaultPluginManager.submitTo(DefaultPluginManager.java:131)
        at com.thoughtworks.go.plugin.access.PluginRequestHelper.submitRequest(PluginRequestHelper.java:49)
        at com.thoughtworks.go.plugin.access.notification.NotificationExtension.notify(NotificationExtension.java:83)
        at com.thoughtworks.go.server.messaging.notifications.PluginNotificationMessageListener.onMessage(PluginNotificationMessageListene
r.java:44)
        at com.thoughtworks.go.server.messaging.notifications.PluginNotificationMessageListener.onMessage(PluginNotificationMessageListene
r.java:29)
        at com.thoughtworks.go.server.messaging.activemq.JMSMessageListenerAdapter.runImpl(JMSMessageListenerAdapter.java:83)
        at com.thoughtworks.go.server.messaging.activemq.JMSMessageListenerAdapter.run(JMSMessageListenerAdapter.java:63)
        at java.base/java.lang.Thread.run(Unknown Source)

Context

gocd.slack {
  # gocd fields
  login = "***"
  password = "***"
  server-host = "http://localhost:8153/"
  api-server-host = "http://localhost:8153/"

  # slack fields
  slackDisplayName = "GoCD"
  slackUserIconURL = "https://www.singhajit.com/wp-content/uploads/2016/10/gocd.png"
  webhookUrl = "https://hooks.slack.com/services/***/***/***"

  # pipelines
  pipelines = [
    {
      name = ".*"
      stage = ".*"
      state = "failed|passed"
    }
  ]
}
@nicolaasuni
Copy link
Contributor

+1

@idontwanttosignin
Copy link

+1 to this being an issue

As @jg-synth mentioned this is due to missing headers.

I was able to work around this by passing in the headers during the getUrl function in src/main/java/in/ashwanthkumar/gocd/slack/jsonapi/Server.java. I'll put the code below, but don't plan on making an MR since I'm sure there are much better ways to do this, but I'm not well versed in java and can't spend the time finding it.

We were also not getting fixed notifications after a pipeline broke and then succeeded, this also restored that.

    JsonElement getUrl(URL url)
            throws IOException {
        URL normalizedUrl;
        try {
            normalizedUrl = url.toURI().normalize().toURL();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
        LOG.info("Fetching " + normalizedUrl.toString());

        HttpURLConnection request = httpConnectionUtil.getConnection(normalizedUrl);

        // Add in our HTTP authorization credentials if we have them. Favor the API Token
        // over username/password
        if (isNotEmpty(mRules.getGoAPIToken())) {
            String bearerToken = "Bearer "
                    + DatatypeConverter.printBase64Binary(mRules.getGoAPIToken().getBytes());
            request.setRequestProperty("Authorization", bearerToken);
            request.setRequestProperty("Accept", "application/vnd.go.cd.v1+json");
        } else if (isNotEmpty(mRules.getGoLogin()) && isNotEmpty(mRules.getGoPassword())) {
            String userpass = mRules.getGoLogin() + ":" + mRules.getGoPassword();
            String basicAuth = "Basic "
                    + DatatypeConverter.printBase64Binary(userpass.getBytes());
            request.setRequestProperty("Authorization", basicAuth);
            request.setRequestProperty("Accept", "application/vnd.go.cd.v1+json");
        }
        request.connect();

        return httpConnectionUtil.responseToJson(request.getContent());
    }

@nicolaasuni
Copy link
Contributor

nicolaasuni commented Feb 10, 2020

PR to fix this:
#104

@ashwanthkumar
Copy link
Owner

Fixed as part of #104

Thanks @nicolaasuni

@jefftrimm
Copy link

Many Thanks!

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

No branches or pull requests

5 participants